summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/applescript/browsercrapplication+applescript.mm
blob: 1ac41656fd8c22aa34e96e02b8bb5cc09e0f550f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "chrome/browser/cocoa/applescript/browsercrapplication+applescript.h"

#include "base/logging.h"
#import "base/scoped_nsobject.h"
#import "chrome/browser/app_controller_mac.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/browser_list.h"
#import "chrome/browser/cocoa/applescript/bookmark_folder_applescript.h"
#import "chrome/browser/cocoa/applescript/constants_applescript.h"
#import "chrome/browser/cocoa/applescript/error_applescript.h"
#import "chrome/browser/cocoa/applescript/window_applescript.h"
#include "chrome/browser/profile.h"

@implementation BrowserCrApplication (AppleScriptAdditions)

- (NSArray*)appleScriptWindows {
  NSMutableArray* appleScriptWindows = [NSMutableArray
      arrayWithCapacity:BrowserList::size()];
  // Iterate through all browsers and check if it closing,
  // if not add it to list.
  for (BrowserList::const_iterator browserIterator = BrowserList::begin();
       browserIterator != BrowserList::end(); ++browserIterator) {
    if ((*browserIterator)->IsAttemptingToCloseBrowser())
      continue;

    scoped_nsobject<WindowAppleScript> window(
        [[WindowAppleScript alloc] initWithBrowser:*browserIterator]);
    [window setContainer:NSApp
                property:AppleScript::kWindowsProperty];
    [appleScriptWindows addObject:window];
  }
  // Windows sorted by their index value, which is obtained by calling
  // orderedIndex: on each window.
  [appleScriptWindows sortUsingSelector:@selector(windowComparator:)];
  return appleScriptWindows;
}

- (void)insertInAppleScriptWindows:(WindowAppleScript*)aWindow {
  // This method gets called when a new window is created so
  // the container and property are set here.
  [aWindow setContainer:self
               property:AppleScript::kWindowsProperty];
}

- (void)insertInAppleScriptWindows:(WindowAppleScript*)aWindow
                           atIndex:(int)index {
  // This method gets called when a new window is created so
  // the container and property are set here.
  [aWindow setContainer:self
               property:AppleScript::kWindowsProperty];
  // Note: AppleScript is 1-based.
  index--;
  [aWindow setOrderedIndex:[NSNumber numberWithInt:index]];
}

- (void)removeFromAppleScriptWindowsAtIndex:(int)index {
  [[[self appleScriptWindows] objectAtIndex:index]
      handlesCloseScriptCommand:nil];
}

- (NSScriptObjectSpecifier*)objectSpecifier {
  return nil;
}

- (BookmarkFolderAppleScript*)otherBookmarks {
  AppController* appDelegate = [NSApp delegate];

  Profile* defaultProfile = [appDelegate defaultProfile];
  if (!defaultProfile) {
    AppleScript::SetError(AppleScript::errGetProfile);
    return nil;
  }

  BookmarkModel* model = defaultProfile->GetBookmarkModel();
  if (!model->IsLoaded()) {
    AppleScript::SetError(AppleScript::errBookmarkModelLoad);
    return nil;
  }

  BookmarkFolderAppleScript* otherBookmarks =
      [[[BookmarkFolderAppleScript alloc]
          initWithBookmarkNode:model->other_node()] autorelease];
  [otherBookmarks setContainer:self
                      property:AppleScript::kBookmarkFoldersProperty];
  return otherBookmarks;
}

- (BookmarkFolderAppleScript*)bookmarksBar {
  AppController* appDelegate = [NSApp delegate];

  Profile* defaultProfile = [appDelegate defaultProfile];
  if (!defaultProfile) {
    AppleScript::SetError(AppleScript::errGetProfile);
    return nil;
  }

  BookmarkModel* model = defaultProfile->GetBookmarkModel();
  if (!model->IsLoaded()) {
    AppleScript::SetError(AppleScript::errBookmarkModelLoad);
    return NULL;
  }

  BookmarkFolderAppleScript* bookmarksBar =
      [[[BookmarkFolderAppleScript alloc]
          initWithBookmarkNode:model->GetBookmarkBarNode()] autorelease];
  [bookmarksBar setContainer:self
                    property:AppleScript::kBookmarkFoldersProperty];
  return bookmarksBar;
}

- (NSArray*)bookmarkFolders {
  BookmarkFolderAppleScript* otherBookmarks = [self otherBookmarks];
  BookmarkFolderAppleScript* bookmarksBar = [self bookmarksBar];
  NSArray* folderArray = [NSArray arrayWithObjects:otherBookmarks,
                                                   bookmarksBar,
                                                   nil];
  return folderArray;
}

- (void)insertInBookmarksFolders:(id)aBookmarkFolder {
  NOTIMPLEMENTED();
}

- (void)insertInBookmarksFolders:(id)aBookmarkFolder atIndex:(int)index {
  NOTIMPLEMENTED();
}

- (void)removeFromBookmarksFoldersAtIndex:(int)index {
  NOTIMPLEMENTED();
}

@end