blob: 9ad57bdcae8ae4ac1c7e174c766ba749b3a04bd1 (
plain)
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
|
// Copyright (c) 2010 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/cocoa/applescript/bookmark_node_applescript.h"
#include "base/logging.h"
#include "base/sys_string_conversions.h"
#import "base/scoped_nsobject.h"
#import "chrome/browser/app_controller_mac.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#import "chrome/browser/chrome_browser_application_mac.h"
#import "chrome/browser/cocoa/applescript/error_applescript.h"
#include "chrome/browser/profile.h"
#import "chrome/browser/cocoa/applescript/bookmark_item_applescript.h"
@interface BookmarkNodeAppleScript()
@property (nonatomic, copy) NSString* tempTitle;
@end
@implementation BookmarkNodeAppleScript
@synthesize tempTitle = tempTitle_;
- (id)init {
if ((self = [super init])) {
BookmarkModel* model = [self bookmarkModel];
if (!model) {
[self release];
return nil;
}
scoped_nsobject<NSNumber> numID(
[[NSNumber alloc] initWithLongLong:model->next_node_id()]);
[self setUniqueID:numID];
[self setTempTitle:@""];
}
return self;
}
- (void)dealloc {
[tempTitle_ release];
[super dealloc];
}
- (id)initWithBookmarkNode:(const BookmarkNode*)aBookmarkNode {
if (!aBookmarkNode) {
[self release];
return nil;
}
if ((self = [super init])) {
// It is safe to be weak, if a bookmark item/folder goes away
// (eg user deleting a folder) the applescript runtime calls
// bookmarkFolders/bookmarkItems in BookmarkFolderAppleScript
// and this particular bookmark item/folder is never returned.
bookmarkNode_ = aBookmarkNode;
scoped_nsobject<NSNumber> numID(
[[NSNumber alloc] initWithLongLong:aBookmarkNode->id()]);
[self setUniqueID:numID];
}
return self;
}
- (void)setBookmarkNode:(const BookmarkNode*)aBookmarkNode {
DCHECK(aBookmarkNode);
// It is safe to be weak, if a bookmark item/folder goes away
// (eg user deleting a folder) the applescript runtime calls
// bookmarkFolders/bookmarkItems in BookmarkFolderAppleScript
// and this particular bookmark item/folder is never returned.
bookmarkNode_ = aBookmarkNode;
scoped_nsobject<NSNumber> numID(
[[NSNumber alloc] initWithLongLong:aBookmarkNode->id()]);
[self setUniqueID:numID];
[self setTitle:[self tempTitle]];
}
- (NSString*)title {
if (!bookmarkNode_)
return tempTitle_;
return base::SysUTF16ToNSString(bookmarkNode_->GetTitle());
}
- (void)setTitle:(NSString*)aTitle {
// If the scripter enters |make new bookmarks folder with properties
// {title:"foo"}|, the node has not yet been created so title is stored in the
// temp title.
if (!bookmarkNode_) {
[self setTempTitle:aTitle];
return;
}
BookmarkModel* model = [self bookmarkModel];
if (!model)
return;
model->SetTitle(bookmarkNode_, base::SysNSStringToUTF16(aTitle));
}
- (NSNumber*)index {
const BookmarkNode* parent = bookmarkNode_->GetParent();
int index = parent->IndexOfChild(bookmarkNode_);
// NOTE: AppleScript is 1-Based.
return [NSNumber numberWithInt:index+1];
}
- (BookmarkModel*)bookmarkModel {
AppController* appDelegate = [NSApp delegate];
Profile* defaultProfile = [appDelegate defaultProfile];
if (!defaultProfile) {
AppleScript::SetError(AppleScript::errGetProfile);
return NULL;
}
BookmarkModel* model = defaultProfile->GetBookmarkModel();
if (!model->IsLoaded()) {
AppleScript::SetError(AppleScript::errBookmarkModelLoad);
return NULL;
}
return model;
}
@end
|