Home | History | Annotate | Download | only in applescript
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #import "chrome/browser/ui/cocoa/applescript/browsercrapplication+applescript.h"
      6 
      7 #include "base/logging.h"
      8 #import "base/mac/scoped_nsobject.h"
      9 #import "chrome/browser/app_controller_mac.h"
     10 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
     11 #include "chrome/browser/profiles/profile.h"
     12 #include "chrome/browser/ui/browser.h"
     13 #include "chrome/browser/ui/browser_finder.h"
     14 #include "chrome/browser/ui/browser_iterator.h"
     15 #import "chrome/browser/ui/cocoa/applescript/bookmark_folder_applescript.h"
     16 #import "chrome/browser/ui/cocoa/applescript/constants_applescript.h"
     17 #import "chrome/browser/ui/cocoa/applescript/error_applescript.h"
     18 #import "chrome/browser/ui/cocoa/applescript/window_applescript.h"
     19 #include "components/bookmarks/browser/bookmark_model.h"
     20 
     21 @implementation BrowserCrApplication (AppleScriptAdditions)
     22 
     23 - (NSArray*)appleScriptWindows {
     24   NSMutableArray* appleScriptWindows = [NSMutableArray
     25       arrayWithCapacity:chrome::GetTotalBrowserCount()];
     26   // Iterate through all browsers and check if it closing,
     27   // if not add it to list.
     28   for (chrome::BrowserIterator browserIterator; !browserIterator.done();
     29        browserIterator.Next()) {
     30     if ((*browserIterator)->IsAttemptingToCloseBrowser())
     31       continue;
     32 
     33     base::scoped_nsobject<WindowAppleScript> window(
     34         [[WindowAppleScript alloc] initWithBrowser:*browserIterator]);
     35     [window setContainer:NSApp
     36                 property:AppleScript::kWindowsProperty];
     37     [appleScriptWindows addObject:window];
     38   }
     39   // Windows sorted by their index value, which is obtained by calling
     40   // orderedIndex: on each window.
     41   [appleScriptWindows sortUsingSelector:@selector(windowComparator:)];
     42   return appleScriptWindows;
     43 }
     44 
     45 - (void)insertInAppleScriptWindows:(WindowAppleScript*)aWindow {
     46   // This method gets called when a new window is created so
     47   // the container and property are set here.
     48   [aWindow setContainer:self
     49                property:AppleScript::kWindowsProperty];
     50 }
     51 
     52 - (void)insertInAppleScriptWindows:(WindowAppleScript*)aWindow
     53                            atIndex:(int)index {
     54   // This method gets called when a new window is created so
     55   // the container and property are set here.
     56   [aWindow setContainer:self
     57                property:AppleScript::kWindowsProperty];
     58   // Note: AppleScript is 1-based.
     59   index--;
     60   [aWindow setOrderedIndex:[NSNumber numberWithInt:index]];
     61 }
     62 
     63 - (void)removeFromAppleScriptWindowsAtIndex:(int)index {
     64   [[[self appleScriptWindows] objectAtIndex:index]
     65       handlesCloseScriptCommand:nil];
     66 }
     67 
     68 - (NSScriptObjectSpecifier*)objectSpecifier {
     69   return nil;
     70 }
     71 
     72 - (BookmarkFolderAppleScript*)otherBookmarks {
     73   AppController* appDelegate = [NSApp delegate];
     74 
     75   Profile* lastProfile = [appDelegate lastProfile];
     76   if (!lastProfile) {
     77     AppleScript::SetError(AppleScript::errGetProfile);
     78     return nil;
     79   }
     80 
     81   BookmarkModel* model = BookmarkModelFactory::GetForProfile(lastProfile);
     82   if (!model->loaded()) {
     83     AppleScript::SetError(AppleScript::errBookmarkModelLoad);
     84     return nil;
     85   }
     86 
     87   BookmarkFolderAppleScript* otherBookmarks =
     88       [[[BookmarkFolderAppleScript alloc]
     89           initWithBookmarkNode:model->other_node()] autorelease];
     90   [otherBookmarks setContainer:self
     91                       property:AppleScript::kBookmarkFoldersProperty];
     92   return otherBookmarks;
     93 }
     94 
     95 - (BookmarkFolderAppleScript*)bookmarksBar {
     96   AppController* appDelegate = [NSApp delegate];
     97 
     98   Profile* lastProfile = [appDelegate lastProfile];
     99   if (!lastProfile) {
    100     AppleScript::SetError(AppleScript::errGetProfile);
    101     return nil;
    102   }
    103 
    104   BookmarkModel* model = BookmarkModelFactory::GetForProfile(lastProfile);
    105   if (!model->loaded()) {
    106     AppleScript::SetError(AppleScript::errBookmarkModelLoad);
    107     return NULL;
    108   }
    109 
    110   BookmarkFolderAppleScript* bookmarksBar =
    111       [[[BookmarkFolderAppleScript alloc]
    112           initWithBookmarkNode:model->bookmark_bar_node()] autorelease];
    113   [bookmarksBar setContainer:self
    114                     property:AppleScript::kBookmarkFoldersProperty];
    115   return bookmarksBar;
    116 }
    117 
    118 - (NSArray*)bookmarkFolders {
    119   BookmarkFolderAppleScript* otherBookmarks = [self otherBookmarks];
    120   BookmarkFolderAppleScript* bookmarksBar = [self bookmarksBar];
    121   NSArray* folderArray = [NSArray arrayWithObjects:otherBookmarks,
    122                                                    bookmarksBar,
    123                                                    nil];
    124   return folderArray;
    125 }
    126 
    127 - (void)insertInBookmarksFolders:(id)aBookmarkFolder {
    128   NOTIMPLEMENTED();
    129 }
    130 
    131 - (void)insertInBookmarksFolders:(id)aBookmarkFolder atIndex:(int)index {
    132   NOTIMPLEMENTED();
    133 }
    134 
    135 - (void)removeFromBookmarksFoldersAtIndex:(int)index {
    136   NOTIMPLEMENTED();
    137 }
    138 
    139 @end
    140