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 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())
30 base::scoped_nsobject<BookmarkFolderAppleScript> bookmarkFolder(
31 [[BookmarkFolderAppleScript alloc] initWithBookmarkNode:node]);
32 [bookmarkFolder setContainer:self
33 property:AppleScript::kBookmarkFoldersProperty];
34 [bookmarkFolders addObject:bookmarkFolder];
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];
49 const BookmarkNode* node = model->AddFolder(bookmarkNode_,
50 bookmarkNode_->child_count(),
53 AppleScript::SetError(AppleScript::errCreateBookmarkFolder);
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];
71 const BookmarkNode* node = model->AddFolder(bookmarkNode_,
75 AppleScript::SetError(AppleScript::errCreateBookmarkFolder);
79 [aBookmarkFolder setBookmarkNode:node];
82 - (void)removeFromBookmarkFoldersAtIndex:(int)index {
83 int position = [self calculatePositionOfBookmarkFolderAt:index];
85 BookmarkModel* model = [self bookmarkModel];
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);
101 base::scoped_nsobject<BookmarkItemAppleScript> bookmarkItem(
102 [[BookmarkItemAppleScript alloc] initWithBookmarkNode:node]);
103 [bookmarkItem setContainer:self
104 property:AppleScript::kBookmarkItemsProperty];
105 [bookmarkItems addObject:bookmarkItem];
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];
121 GURL url = GURL(base::SysNSStringToUTF8([aBookmarkItem URL]));
122 if (!url.is_valid()) {
123 AppleScript::SetError(AppleScript::errInvalidURL);
127 const BookmarkNode* node = model->AddURL(bookmarkNode_,
128 bookmarkNode_->child_count(),
132 AppleScript::SetError(AppleScript::errCreateBookmarkItem);
136 [aBookmarkItem setBookmarkNode:node];
139 - (void)insertInBookmarkItems:(BookmarkItemAppleScript*)aBookmarkItem
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];
151 GURL url(base::SysNSStringToUTF8([aBookmarkItem URL]));
152 if (!url.is_valid()) {
153 AppleScript::SetError(AppleScript::errInvalidURL);
157 const BookmarkNode* node = model->AddURL(bookmarkNode_,
162 AppleScript::SetError(AppleScript::errCreateBookmarkItem);
166 [aBookmarkItem setBookmarkNode:node];
169 - (void)removeFromBookmarkItemsAtIndex:(int)index {
170 int position = [self calculatePositionOfBookmarkItemAt:index];
172 BookmarkModel* model = [self bookmarkModel];
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.
186 if (bookmarkNode_->GetChild(++count)->is_folder())
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.
199 if (bookmarkNode_->GetChild(++count)->is_url())