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"
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())
27 base::scoped_nsobject<BookmarkFolderAppleScript> bookmarkFolder(
28 [[BookmarkFolderAppleScript alloc] initWithBookmarkNode:node]);
29 [bookmarkFolder setContainer:self
30 property:AppleScript::kBookmarkFoldersProperty];
31 [bookmarkFolders addObject:bookmarkFolder];
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];
46 const BookmarkNode* node = model->AddFolder(bookmarkNode_,
47 bookmarkNode_->child_count(),
50 AppleScript::SetError(AppleScript::errCreateBookmarkFolder);
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];
68 const BookmarkNode* node = model->AddFolder(bookmarkNode_,
72 AppleScript::SetError(AppleScript::errCreateBookmarkFolder);
76 [aBookmarkFolder setBookmarkNode:node];
79 - (void)removeFromBookmarkFoldersAtIndex:(int)index {
80 int position = [self calculatePositionOfBookmarkFolderAt:index];
82 BookmarkModel* model = [self bookmarkModel];
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);
98 base::scoped_nsobject<BookmarkItemAppleScript> bookmarkItem(
99 [[BookmarkItemAppleScript alloc] initWithBookmarkNode:node]);
100 [bookmarkItem setContainer:self
101 property:AppleScript::kBookmarkItemsProperty];
102 [bookmarkItems addObject:bookmarkItem];
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];
118 GURL url = GURL(base::SysNSStringToUTF8([aBookmarkItem URL]));
119 if (!url.is_valid()) {
120 AppleScript::SetError(AppleScript::errInvalidURL);
124 const BookmarkNode* node = model->AddURL(bookmarkNode_,
125 bookmarkNode_->child_count(),
129 AppleScript::SetError(AppleScript::errCreateBookmarkItem);
133 [aBookmarkItem setBookmarkNode:node];
136 - (void)insertInBookmarkItems:(BookmarkItemAppleScript*)aBookmarkItem
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];
148 GURL url(base::SysNSStringToUTF8([aBookmarkItem URL]));
149 if (!url.is_valid()) {
150 AppleScript::SetError(AppleScript::errInvalidURL);
154 const BookmarkNode* node = model->AddURL(bookmarkNode_,
159 AppleScript::SetError(AppleScript::errCreateBookmarkItem);
163 [aBookmarkItem setBookmarkNode:node];
166 - (void)removeFromBookmarkItemsAtIndex:(int)index {
167 int position = [self calculatePositionOfBookmarkItemAt:index];
169 BookmarkModel* model = [self bookmarkModel];
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.
183 if (bookmarkNode_->GetChild(++count)->is_folder())
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.
196 if (bookmarkNode_->GetChild(++count)->is_url())