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