Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / applescript / bookmark_folder_applescript.mm
blobcc2e8b7ad79a0a826d119627fdd6be0995161372
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 using bookmarks::BookmarkModel;
17 using bookmarks::BookmarkNode;
19 @implementation BookmarkFolderAppleScript
21 - (NSArray*)bookmarkFolders {
22   NSMutableArray* bookmarkFolders = [NSMutableArray
23       arrayWithCapacity:bookmarkNode_->child_count()];
25   for (int i = 0; i < bookmarkNode_->child_count(); ++i) {
26     const BookmarkNode* node = bookmarkNode_->GetChild(i);
28     if (!node->is_folder())
29       continue;
30     base::scoped_nsobject<BookmarkFolderAppleScript> bookmarkFolder(
31         [[BookmarkFolderAppleScript alloc] initWithBookmarkNode:node]);
32     [bookmarkFolder setContainer:self
33                         property:AppleScript::kBookmarkFoldersProperty];
34     [bookmarkFolders addObject:bookmarkFolder];
35   }
37   return bookmarkFolders;
40 - (void)insertInBookmarkFolders:(id)aBookmarkFolder {
41   // This method gets called when a new bookmark folder is created so
42   // the container and property are set here.
43   [aBookmarkFolder setContainer:self
44                        property:AppleScript::kBookmarkFoldersProperty];
45   BookmarkModel* model = [self bookmarkModel];
46   if (!model)
47     return;
49   const BookmarkNode* node = model->AddFolder(bookmarkNode_,
50                                               bookmarkNode_->child_count(),
51                                               base::string16());
52   if (!node) {
53     AppleScript::SetError(AppleScript::errCreateBookmarkFolder);
54     return;
55   }
57   [aBookmarkFolder setBookmarkNode:node];
60 - (void)insertInBookmarkFolders:(id)aBookmarkFolder atIndex:(int)index {
61   // This method gets called when a new bookmark folder is created so
62   // the container and property are set here.
63   [aBookmarkFolder setContainer:self
64                        property:AppleScript::kBookmarkFoldersProperty];
65   int position = [self calculatePositionOfBookmarkFolderAt:index];
67   BookmarkModel* model = [self bookmarkModel];
68   if (!model)
69     return;
71   const BookmarkNode* node = model->AddFolder(bookmarkNode_,
72                                               position,
73                                               base::string16());
74   if (!node) {
75     AppleScript::SetError(AppleScript::errCreateBookmarkFolder);
76     return;
77   }
79   [aBookmarkFolder setBookmarkNode:node];
82 - (void)removeFromBookmarkFoldersAtIndex:(int)index {
83   int position = [self calculatePositionOfBookmarkFolderAt:index];
85   BookmarkModel* model = [self bookmarkModel];
86   if (!model)
87     return;
89   model->Remove(bookmarkNode_->GetChild(position));
92 - (NSArray*)bookmarkItems {
93   NSMutableArray* bookmarkItems = [NSMutableArray
94       arrayWithCapacity:bookmarkNode_->child_count()];
96   for (int i = 0; i < bookmarkNode_->child_count(); ++i) {
97     const BookmarkNode* node = bookmarkNode_->GetChild(i);
99     if (!node->is_url())
100       continue;
101     base::scoped_nsobject<BookmarkItemAppleScript> bookmarkItem(
102         [[BookmarkItemAppleScript alloc] initWithBookmarkNode:node]);
103     [bookmarkItem setContainer:self
104                       property:AppleScript::kBookmarkItemsProperty];
105     [bookmarkItems addObject:bookmarkItem];
106   }
108   return bookmarkItems;
111 - (void)insertInBookmarkItems:(BookmarkItemAppleScript*)aBookmarkItem {
112   // This method gets called when a new bookmark item is created so
113   // the container and property are set here.
114   [aBookmarkItem setContainer:self
115                      property:AppleScript::kBookmarkItemsProperty];
117   BookmarkModel* model = [self bookmarkModel];
118   if (!model)
119     return;
121   GURL url = GURL(base::SysNSStringToUTF8([aBookmarkItem URL]));
122   if (!url.is_valid()) {
123     AppleScript::SetError(AppleScript::errInvalidURL);
124     return;
125   }
127   const BookmarkNode* node = model->AddURL(bookmarkNode_,
128                                            bookmarkNode_->child_count(),
129                                            base::string16(),
130                                            url);
131   if (!node) {
132     AppleScript::SetError(AppleScript::errCreateBookmarkItem);
133     return;
134   }
136   [aBookmarkItem setBookmarkNode:node];
139 - (void)insertInBookmarkItems:(BookmarkItemAppleScript*)aBookmarkItem
140                       atIndex:(int)index {
141   // This method gets called when a new bookmark item is created so
142   // the container and property are set here.
143   [aBookmarkItem setContainer:self
144                      property:AppleScript::kBookmarkItemsProperty];
145   int position = [self calculatePositionOfBookmarkItemAt:index];
147   BookmarkModel* model = [self bookmarkModel];
148   if (!model)
149     return;
151   GURL url(base::SysNSStringToUTF8([aBookmarkItem URL]));
152   if (!url.is_valid()) {
153     AppleScript::SetError(AppleScript::errInvalidURL);
154     return;
155   }
157   const BookmarkNode* node = model->AddURL(bookmarkNode_,
158                                            position,
159                                            base::string16(),
160                                            url);
161   if (!node) {
162     AppleScript::SetError(AppleScript::errCreateBookmarkItem);
163     return;
164   }
166   [aBookmarkItem setBookmarkNode:node];
169 - (void)removeFromBookmarkItemsAtIndex:(int)index {
170   int position = [self calculatePositionOfBookmarkItemAt:index];
172   BookmarkModel* model = [self bookmarkModel];
173   if (!model)
174     return;
176   model->Remove(bookmarkNode_->GetChild(position));
179 - (int)calculatePositionOfBookmarkFolderAt:(int)index {
180   // Traverse through all the child nodes till the required node is found and
181   // return its position.
182   // AppleScript is 1-based therefore index is incremented by 1.
183   ++index;
184   int count = -1;
185   while (index) {
186     if (bookmarkNode_->GetChild(++count)->is_folder())
187       --index;
188   }
189   return count;
192 - (int)calculatePositionOfBookmarkItemAt:(int)index {
193   // Traverse through all the child nodes till the required node is found and
194   // return its position.
195   // AppleScript is 1-based therefore index is incremented by 1.
196   ++index;
197   int count = -1;
198   while (index) {
199     if (bookmarkNode_->GetChild(++count)->is_url())
200       --index;
201   }
202   return count;
205 @end