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 @interface BookmarkNodeAppleScript()
19 @property (nonatomic, copy) NSString* tempTitle;
22 @implementation BookmarkNodeAppleScript
24 @synthesize tempTitle = tempTitle_;
27 if ((self = [super init])) {
28 BookmarkModel* model = [self bookmarkModel];
34 base::scoped_nsobject<NSNumber> numID(
35 [[NSNumber alloc] initWithLongLong:model->next_node_id()]);
36 [self setUniqueID:numID];
37 [self setTempTitle:@""];
48 - (id)initWithBookmarkNode:(const BookmarkNode*)aBookmarkNode {
54 if ((self = [super init])) {
55 // It is safe to be weak, if a bookmark item/folder goes away
56 // (eg user deleting a folder) the applescript runtime calls
57 // bookmarkFolders/bookmarkItems in BookmarkFolderAppleScript
58 // and this particular bookmark item/folder is never returned.
59 bookmarkNode_ = aBookmarkNode;
61 base::scoped_nsobject<NSNumber> numID(
62 [[NSNumber alloc] initWithLongLong:aBookmarkNode->id()]);
63 [self setUniqueID:numID];
68 - (void)setBookmarkNode:(const BookmarkNode*)aBookmarkNode {
69 DCHECK(aBookmarkNode);
70 // It is safe to be weak, if a bookmark item/folder goes away
71 // (eg user deleting a folder) the applescript runtime calls
72 // bookmarkFolders/bookmarkItems in BookmarkFolderAppleScript
73 // and this particular bookmark item/folder is never returned.
74 bookmarkNode_ = aBookmarkNode;
76 base::scoped_nsobject<NSNumber> numID(
77 [[NSNumber alloc] initWithLongLong:aBookmarkNode->id()]);
78 [self setUniqueID:numID];
80 [self setTitle:[self tempTitle]];
87 return base::SysUTF16ToNSString(bookmarkNode_->GetTitle());
90 - (void)setTitle:(NSString*)aTitle {
91 // If the scripter enters |make new bookmarks folder with properties
92 // {title:"foo"}|, the node has not yet been created so title is stored in the
95 [self setTempTitle:aTitle];
99 BookmarkModel* model = [self bookmarkModel];
103 model->SetTitle(bookmarkNode_, base::SysNSStringToUTF16(aTitle));
107 const BookmarkNode* parent = bookmarkNode_->parent();
108 int index = parent->GetIndexOf(bookmarkNode_);
109 // NOTE: AppleScript is 1-Based.
110 return [NSNumber numberWithInt:index+1];
113 - (BookmarkModel*)bookmarkModel {
114 AppController* appDelegate = [NSApp delegate];
116 Profile* lastProfile = [appDelegate lastProfile];
118 AppleScript::SetError(AppleScript::errGetProfile);
122 BookmarkModel* model = BookmarkModelFactory::GetForProfile(lastProfile);
123 if (!model->loaded()) {
124 AppleScript::SetError(AppleScript::errBookmarkModelLoad);