No longer register app window placement preference keys on
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / applescript / bookmark_folder_applescript.mm
blob9da6b0b3ebd5caf0c5fb96ceb05f6abeb65f9e69
1 // Copyright (c) 2011 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.
5 #import "chrome/browser/ui/cocoa/applescript/bookmark_folder_applescript.h"
7 #import "base/mac/scoped_nsobject.h"
8 #import "base/strings/string16.h"
9 #include "base/strings/sys_string_conversions.h"
10 #import "chrome/browser/ui/cocoa/applescript/bookmark_item_applescript.h"
11 #import "chrome/browser/ui/cocoa/applescript/constants_applescript.h"
12 #include "chrome/browser/ui/cocoa/applescript/error_applescript.h"
13 #include "components/bookmarks/browser/bookmark_model.h"
14 #include "url/gurl.h"
16 @implementation BookmarkFolderAppleScript
18 - (NSArray*)bookmarkFolders {
19   NSMutableArray* bookmarkFolders = [NSMutableArray
20       arrayWithCapacity:bookmarkNode_->child_count()];
22   for (int i = 0; i < bookmarkNode_->child_count(); ++i) {
23     const BookmarkNode* node = bookmarkNode_->GetChild(i);
25     if (!node->is_folder())
26       continue;
27     base::scoped_nsobject<BookmarkFolderAppleScript> bookmarkFolder(
28         [[BookmarkFolderAppleScript alloc] initWithBookmarkNode:node]);
29     [bookmarkFolder setContainer:self
30                         property:AppleScript::kBookmarkFoldersProperty];
31     [bookmarkFolders addObject:bookmarkFolder];
32   }
34   return bookmarkFolders;
37 - (void)insertInBookmarkFolders:(id)aBookmarkFolder {
38   // This method gets called when a new bookmark folder is created so
39   // the container and property are set here.
40   [aBookmarkFolder setContainer:self
41                        property:AppleScript::kBookmarkFoldersProperty];
42   BookmarkModel* model = [self bookmarkModel];
43   if (!model)
44     return;
46   const BookmarkNode* node = model->AddFolder(bookmarkNode_,
47                                               bookmarkNode_->child_count(),
48                                               base::string16());
49   if (!node) {
50     AppleScript::SetError(AppleScript::errCreateBookmarkFolder);
51     return;
52   }
54   [aBookmarkFolder setBookmarkNode:node];
57 - (void)insertInBookmarkFolders:(id)aBookmarkFolder atIndex:(int)index {
58   // This method gets called when a new bookmark folder is created so
59   // the container and property are set here.
60   [aBookmarkFolder setContainer:self
61                        property:AppleScript::kBookmarkFoldersProperty];
62   int position = [self calculatePositionOfBookmarkFolderAt:index];
64   BookmarkModel* model = [self bookmarkModel];
65   if (!model)
66     return;
68   const BookmarkNode* node = model->AddFolder(bookmarkNode_,
69                                               position,
70                                               base::string16());
71   if (!node) {
72     AppleScript::SetError(AppleScript::errCreateBookmarkFolder);
73     return;
74   }
76   [aBookmarkFolder setBookmarkNode:node];
79 - (void)removeFromBookmarkFoldersAtIndex:(int)index {
80   int position = [self calculatePositionOfBookmarkFolderAt:index];
82   BookmarkModel* model = [self bookmarkModel];
83   if (!model)
84     return;
86   model->Remove(bookmarkNode_, position);
89 - (NSArray*)bookmarkItems {
90   NSMutableArray* bookmarkItems = [NSMutableArray
91       arrayWithCapacity:bookmarkNode_->child_count()];
93   for (int i = 0; i < bookmarkNode_->child_count(); ++i) {
94     const BookmarkNode* node = bookmarkNode_->GetChild(i);
96     if (!node->is_url())
97       continue;
98     base::scoped_nsobject<BookmarkItemAppleScript> bookmarkItem(
99         [[BookmarkItemAppleScript alloc] initWithBookmarkNode:node]);
100     [bookmarkItem setContainer:self
101                       property:AppleScript::kBookmarkItemsProperty];
102     [bookmarkItems addObject:bookmarkItem];
103   }
105   return bookmarkItems;
108 - (void)insertInBookmarkItems:(BookmarkItemAppleScript*)aBookmarkItem {
109   // This method gets called when a new bookmark item is created so
110   // the container and property are set here.
111   [aBookmarkItem setContainer:self
112                      property:AppleScript::kBookmarkItemsProperty];
114   BookmarkModel* model = [self bookmarkModel];
115   if (!model)
116     return;
118   GURL url = GURL(base::SysNSStringToUTF8([aBookmarkItem URL]));
119   if (!url.is_valid()) {
120     AppleScript::SetError(AppleScript::errInvalidURL);
121     return;
122   }
124   const BookmarkNode* node = model->AddURL(bookmarkNode_,
125                                            bookmarkNode_->child_count(),
126                                            base::string16(),
127                                            url);
128   if (!node) {
129     AppleScript::SetError(AppleScript::errCreateBookmarkItem);
130     return;
131   }
133   [aBookmarkItem setBookmarkNode:node];
136 - (void)insertInBookmarkItems:(BookmarkItemAppleScript*)aBookmarkItem
137                       atIndex:(int)index {
138   // This method gets called when a new bookmark item is created so
139   // the container and property are set here.
140   [aBookmarkItem setContainer:self
141                      property:AppleScript::kBookmarkItemsProperty];
142   int position = [self calculatePositionOfBookmarkItemAt:index];
144   BookmarkModel* model = [self bookmarkModel];
145   if (!model)
146     return;
148   GURL url(base::SysNSStringToUTF8([aBookmarkItem URL]));
149   if (!url.is_valid()) {
150     AppleScript::SetError(AppleScript::errInvalidURL);
151     return;
152   }
154   const BookmarkNode* node = model->AddURL(bookmarkNode_,
155                                            position,
156                                            base::string16(),
157                                            url);
158   if (!node) {
159     AppleScript::SetError(AppleScript::errCreateBookmarkItem);
160     return;
161   }
163   [aBookmarkItem setBookmarkNode:node];
166 - (void)removeFromBookmarkItemsAtIndex:(int)index {
167   int position = [self calculatePositionOfBookmarkItemAt:index];
169   BookmarkModel* model = [self bookmarkModel];
170   if (!model)
171     return;
173   model->Remove(bookmarkNode_, position);
176 - (int)calculatePositionOfBookmarkFolderAt:(int)index {
177   // Traverse through all the child nodes till the required node is found and
178   // return its position.
179   // AppleScript is 1-based therefore index is incremented by 1.
180   ++index;
181   int count = -1;
182   while (index) {
183     if (bookmarkNode_->GetChild(++count)->is_folder())
184       --index;
185   }
186   return count;
189 - (int)calculatePositionOfBookmarkItemAt:(int)index {
190   // Traverse through all the child nodes till the required node is found and
191   // return its position.
192   // AppleScript is 1-based therefore index is incremented by 1.
193   ++index;
194   int count = -1;
195   while (index) {
196     if (bookmarkNode_->GetChild(++count)->is_url())
197       --index;
198   }
199   return count;
202 @end