1 // Copyright (c) 2012 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_node_applescript.h"
7 #include "base/logging.h"
8 #import "base/mac/scoped_nsobject.h"
9 #include "base/strings/sys_string_conversions.h"
10 #import "chrome/browser/app_controller_mac.h"
11 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
12 #import "chrome/browser/chrome_browser_application_mac.h"
13 #include "chrome/browser/profiles/profile.h"
14 #import "chrome/browser/ui/cocoa/applescript/bookmark_item_applescript.h"
15 #import "chrome/browser/ui/cocoa/applescript/error_applescript.h"
16 #include "components/bookmarks/browser/bookmark_model.h"
18 using bookmarks::BookmarkModel;
19 using bookmarks::BookmarkNode;
21 @interface BookmarkNodeAppleScript()
22 @property (nonatomic, copy) NSString* tempTitle;
25 @implementation BookmarkNodeAppleScript
27 @synthesize tempTitle = tempTitle_;
30 if ((self = [super init])) {
31 BookmarkModel* model = [self bookmarkModel];
37 base::scoped_nsobject<NSNumber> numID(
38 [[NSNumber alloc] initWithLongLong:model->next_node_id()]);
39 [self setUniqueID:numID];
40 [self setTempTitle:@""];
51 - (id)initWithBookmarkNode:(const BookmarkNode*)aBookmarkNode {
57 if ((self = [super init])) {
58 // It is safe to be weak, if a bookmark item/folder goes away
59 // (eg user deleting a folder) the applescript runtime calls
60 // bookmarkFolders/bookmarkItems in BookmarkFolderAppleScript
61 // and this particular bookmark item/folder is never returned.
62 bookmarkNode_ = aBookmarkNode;
64 base::scoped_nsobject<NSNumber> numID(
65 [[NSNumber alloc] initWithLongLong:aBookmarkNode->id()]);
66 [self setUniqueID:numID];
71 - (void)setBookmarkNode:(const BookmarkNode*)aBookmarkNode {
72 DCHECK(aBookmarkNode);
73 // It is safe to be weak, if a bookmark item/folder goes away
74 // (eg user deleting a folder) the applescript runtime calls
75 // bookmarkFolders/bookmarkItems in BookmarkFolderAppleScript
76 // and this particular bookmark item/folder is never returned.
77 bookmarkNode_ = aBookmarkNode;
79 base::scoped_nsobject<NSNumber> numID(
80 [[NSNumber alloc] initWithLongLong:aBookmarkNode->id()]);
81 [self setUniqueID:numID];
83 [self setTitle:[self tempTitle]];
90 return base::SysUTF16ToNSString(bookmarkNode_->GetTitle());
93 - (void)setTitle:(NSString*)aTitle {
94 // If the scripter enters |make new bookmarks folder with properties
95 // {title:"foo"}|, the node has not yet been created so title is stored in the
98 [self setTempTitle:aTitle];
102 BookmarkModel* model = [self bookmarkModel];
106 model->SetTitle(bookmarkNode_, base::SysNSStringToUTF16(aTitle));
110 const BookmarkNode* parent = bookmarkNode_->parent();
111 int index = parent->GetIndexOf(bookmarkNode_);
112 // NOTE: AppleScript is 1-Based.
113 return [NSNumber numberWithInt:index+1];
116 - (BookmarkModel*)bookmarkModel {
117 AppController* appDelegate = [NSApp delegate];
119 Profile* lastProfile = [appDelegate lastProfile];
121 AppleScript::SetError(AppleScript::errGetProfile);
125 BookmarkModel* model = BookmarkModelFactory::GetForProfile(lastProfile);
126 if (!model->loaded()) {
127 AppleScript::SetError(AppleScript::errBookmarkModelLoad);