| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2010 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. |
| 4 |
| 5 #import "chrome/browser/cocoa/applescript/bookmark_folder_applescript.h" |
| 6 |
| 7 #import "base/scoped_nsobject.h" |
| 8 #include "base/sys_string_conversions.h" |
| 9 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 10 #import "chrome/browser/cocoa/applescript/bookmark_item_applescript.h" |
| 11 #import "chrome/browser/cocoa/applescript/constants_applescript.h" |
| 12 #include "chrome/browser/cocoa/applescript/error_applescript.h" |
| 13 #include "googleurl/src/gurl.h" |
| 14 |
| 15 @implementation BookmarkFolderAppleScript |
| 16 |
| 17 - (NSArray*)bookmarkFolders { |
| 18 NSMutableArray* bookmarkFolders = [NSMutableArray |
| 19 arrayWithCapacity:bookmarkNode_->GetChildCount()]; |
| 20 |
| 21 for (int i = 0; i < bookmarkNode_->GetChildCount(); ++i) { |
| 22 const BookmarkNode* node = bookmarkNode_->GetChild(i); |
| 23 |
| 24 if (!node->is_folder()) |
| 25 continue; |
| 26 scoped_nsobject<BookmarkFolderAppleScript> bookmarkFolder( |
| 27 [[BookmarkFolderAppleScript alloc] |
| 28 initWithBookmarkNode:node]); |
| 29 [bookmarkFolder setContainer:self |
| 30 property:AppleScript::kBookmarkFoldersProperty]; |
| 31 [bookmarkFolders addObject:bookmarkFolder]; |
| 32 } |
| 33 |
| 34 return bookmarkFolders; |
| 35 } |
| 36 |
| 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; |
| 45 |
| 46 const BookmarkNode* node = model->AddGroup(bookmarkNode_, |
| 47 bookmarkNode_->GetChildCount(), |
| 48 std::wstring()); |
| 49 if (!node) { |
| 50 AppleScript::SetError(AppleScript::errCreateBookmarkFolder); |
| 51 return; |
| 52 } |
| 53 |
| 54 [aBookmarkFolder setBookmarkNode:node]; |
| 55 } |
| 56 |
| 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]; |
| 63 |
| 64 BookmarkModel* model = [self bookmarkModel]; |
| 65 if (!model) |
| 66 return; |
| 67 |
| 68 const BookmarkNode* node = model->AddGroup(bookmarkNode_, |
| 69 position, |
| 70 std::wstring()); |
| 71 if (!node) { |
| 72 AppleScript::SetError(AppleScript::errCreateBookmarkFolder); |
| 73 return; |
| 74 } |
| 75 |
| 76 [aBookmarkFolder setBookmarkNode:node]; |
| 77 } |
| 78 |
| 79 - (void)removeFromBookmarkFoldersAtIndex:(int)index { |
| 80 int position = [self calculatePositionOfBookmarkFolderAt:index]; |
| 81 |
| 82 BookmarkModel* model = [self bookmarkModel]; |
| 83 if (!model) |
| 84 return; |
| 85 |
| 86 model->Remove(bookmarkNode_, position); |
| 87 } |
| 88 |
| 89 - (NSArray*)bookmarkItems { |
| 90 NSMutableArray* bookmarkItems = [NSMutableArray |
| 91 arrayWithCapacity:bookmarkNode_->GetChildCount()]; |
| 92 |
| 93 for (int i = 0; i < bookmarkNode_->GetChildCount(); ++i) { |
| 94 const BookmarkNode* node = bookmarkNode_->GetChild(i); |
| 95 |
| 96 if (!node->is_url()) |
| 97 continue; |
| 98 scoped_nsobject<BookmarkFolderAppleScript> bookmarkItem( |
| 99 [[BookmarkItemAppleScript alloc] |
| 100 initWithBookmarkNode:node]); |
| 101 [bookmarkItem setContainer:self |
| 102 property:AppleScript::kBookmarkItemsProperty]; |
| 103 [bookmarkItems addObject:bookmarkItem]; |
| 104 } |
| 105 |
| 106 return bookmarkItems; |
| 107 } |
| 108 |
| 109 - (void)insertInBookmarkItems:(BookmarkItemAppleScript*)aBookmarkItem { |
| 110 // This method gets called when a new bookmark item is created so |
| 111 // the container and property are set here. |
| 112 [aBookmarkItem setContainer:self |
| 113 property:AppleScript::kBookmarkItemsProperty]; |
| 114 |
| 115 BookmarkModel* model = [self bookmarkModel]; |
| 116 if (!model) |
| 117 return; |
| 118 |
| 119 GURL url = GURL(base::SysNSStringToUTF8([aBookmarkItem URL])); |
| 120 if (!url.is_valid()) { |
| 121 AppleScript::SetError(AppleScript::errInvalidURL); |
| 122 return; |
| 123 } |
| 124 |
| 125 const BookmarkNode* node = model->AddURL(bookmarkNode_, |
| 126 bookmarkNode_->GetChildCount(), |
| 127 std::wstring(), |
| 128 url); |
| 129 if (!node) { |
| 130 AppleScript::SetError(AppleScript::errCreateBookmarkItem); |
| 131 return; |
| 132 } |
| 133 |
| 134 [aBookmarkItem setBookmarkNode:node]; |
| 135 } |
| 136 |
| 137 - (void)insertInBookmarkItems:(BookmarkItemAppleScript*)aBookmarkItem |
| 138 atIndex:(int)index { |
| 139 // This method gets called when a new bookmark item is created so |
| 140 // the container and property are set here. |
| 141 [aBookmarkItem setContainer:self |
| 142 property:AppleScript::kBookmarkItemsProperty]; |
| 143 int position = [self calculatePositionOfBookmarkItemAt:index]; |
| 144 |
| 145 BookmarkModel* model = [self bookmarkModel]; |
| 146 if (!model) |
| 147 return; |
| 148 |
| 149 GURL url(base::SysNSStringToUTF8([aBookmarkItem URL])); |
| 150 if (!url.is_valid()) { |
| 151 AppleScript::SetError(AppleScript::errInvalidURL); |
| 152 return; |
| 153 } |
| 154 |
| 155 const BookmarkNode* node = model->AddURL(bookmarkNode_, |
| 156 position, |
| 157 std::wstring(), |
| 158 url); |
| 159 if (!node) { |
| 160 AppleScript::SetError(AppleScript::errCreateBookmarkItem); |
| 161 return; |
| 162 } |
| 163 |
| 164 [aBookmarkItem setBookmarkNode:node]; |
| 165 } |
| 166 |
| 167 - (void)removeFromBookmarkItemsAtIndex:(int)index { |
| 168 int position = [self calculatePositionOfBookmarkItemAt:index]; |
| 169 |
| 170 BookmarkModel* model = [self bookmarkModel]; |
| 171 if (!model) |
| 172 return; |
| 173 |
| 174 model->Remove(bookmarkNode_, position); |
| 175 } |
| 176 |
| 177 - (int)calculatePositionOfBookmarkFolderAt:(int)index { |
| 178 // Traverse through all the child nodes till the required node is found and |
| 179 // return its position. |
| 180 // AppleScript is 1-based therefore index is incremented by 1. |
| 181 ++index; |
| 182 int count = -1; |
| 183 while (index) { |
| 184 if (bookmarkNode_->GetChild(++count)->is_folder()) |
| 185 --index; |
| 186 } |
| 187 return count; |
| 188 } |
| 189 |
| 190 - (int)calculatePositionOfBookmarkItemAt:(int)index { |
| 191 // Traverse through all the child nodes till the required node is found and |
| 192 // return its position. |
| 193 // AppleScript is 1-based therefore index is incremented by 1. |
| 194 ++index; |
| 195 int count = -1; |
| 196 while (index) { |
| 197 if (bookmarkNode_->GetChild(++count)->is_url()) |
| 198 --index; |
| 199 } |
| 200 return count; |
| 201 } |
| 202 |
| 203 @end |
| OLD | NEW |