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. 4 5#import "chrome/browser/ui/cocoa/applescript/bookmark_node_applescript.h" 6 7#include "base/logging.h" 8#import "base/memory/scoped_nsobject.h" 9#include "base/sys_string_conversions.h" 10#import "chrome/browser/app_controller_mac.h" 11#include "chrome/browser/bookmarks/bookmark_model.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 17@interface BookmarkNodeAppleScript() 18@property (nonatomic, copy) NSString* tempTitle; 19@end 20 21@implementation BookmarkNodeAppleScript 22 23@synthesize tempTitle = tempTitle_; 24 25- (id)init { 26 if ((self = [super init])) { 27 BookmarkModel* model = [self bookmarkModel]; 28 if (!model) { 29 [self release]; 30 return nil; 31 } 32 33 scoped_nsobject<NSNumber> numID( 34 [[NSNumber alloc] initWithLongLong:model->next_node_id()]); 35 [self setUniqueID:numID]; 36 [self setTempTitle:@""]; 37 } 38 return self; 39} 40 41- (void)dealloc { 42 [tempTitle_ release]; 43 [super dealloc]; 44} 45 46 47- (id)initWithBookmarkNode:(const BookmarkNode*)aBookmarkNode { 48 if (!aBookmarkNode) { 49 [self release]; 50 return nil; 51 } 52 53 if ((self = [super init])) { 54 // It is safe to be weak, if a bookmark item/folder goes away 55 // (eg user deleting a folder) the applescript runtime calls 56 // bookmarkFolders/bookmarkItems in BookmarkFolderAppleScript 57 // and this particular bookmark item/folder is never returned. 58 bookmarkNode_ = aBookmarkNode; 59 60 scoped_nsobject<NSNumber> numID( 61 [[NSNumber alloc] initWithLongLong:aBookmarkNode->id()]); 62 [self setUniqueID:numID]; 63 } 64 return self; 65} 66 67- (void)setBookmarkNode:(const BookmarkNode*)aBookmarkNode { 68 DCHECK(aBookmarkNode); 69 // It is safe to be weak, if a bookmark item/folder goes away 70 // (eg user deleting a folder) the applescript runtime calls 71 // bookmarkFolders/bookmarkItems in BookmarkFolderAppleScript 72 // and this particular bookmark item/folder is never returned. 73 bookmarkNode_ = aBookmarkNode; 74 75 scoped_nsobject<NSNumber> numID( 76 [[NSNumber alloc] initWithLongLong:aBookmarkNode->id()]); 77 [self setUniqueID:numID]; 78 79 [self setTitle:[self tempTitle]]; 80} 81 82- (NSString*)title { 83 if (!bookmarkNode_) 84 return tempTitle_; 85 86 return base::SysUTF16ToNSString(bookmarkNode_->GetTitle()); 87} 88 89- (void)setTitle:(NSString*)aTitle { 90 // If the scripter enters |make new bookmarks folder with properties 91 // {title:"foo"}|, the node has not yet been created so title is stored in the 92 // temp title. 93 if (!bookmarkNode_) { 94 [self setTempTitle:aTitle]; 95 return; 96 } 97 98 BookmarkModel* model = [self bookmarkModel]; 99 if (!model) 100 return; 101 102 model->SetTitle(bookmarkNode_, base::SysNSStringToUTF16(aTitle)); 103} 104 105- (NSNumber*)index { 106 const BookmarkNode* parent = bookmarkNode_->parent(); 107 int index = parent->GetIndexOf(bookmarkNode_); 108 // NOTE: AppleScript is 1-Based. 109 return [NSNumber numberWithInt:index+1]; 110} 111 112- (BookmarkModel*)bookmarkModel { 113 AppController* appDelegate = [NSApp delegate]; 114 115 Profile* defaultProfile = [appDelegate defaultProfile]; 116 if (!defaultProfile) { 117 AppleScript::SetError(AppleScript::errGetProfile); 118 return NULL; 119 } 120 121 BookmarkModel* model = defaultProfile->GetBookmarkModel(); 122 if (!model->IsLoaded()) { 123 AppleScript::SetError(AppleScript::errBookmarkModelLoad); 124 return NULL; 125 } 126 127 return model; 128} 129 130@end 131