summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/bookmark_item.mm
blob: f59022ae68a7166a2fe30f8fcdff396d5cbc8b8b (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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// 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 "bookmark_item.h"

#include "app/resource_bundle.h"
#include "base/mac_util.h"
#include "base/sys_string_conversions.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/browser_window.h"
#import "chrome/browser/cocoa/bookmark_manager_controller.h"
#include "grit/app_resources.h"
#include "grit/theme_resources.h"
#include "skia/ext/skia_utils_mac.h"


static GURL ConvertToGURL(NSString* urlStr) {
  DCHECK(urlStr);
  GURL url(base::SysNSStringToUTF8(urlStr));
  if (!url.is_valid()) {
    // Mimic observed friendliness from Windows
    urlStr = [@"http://" stringByAppendingString:urlStr];
    GURL fixedURL(base::SysNSStringToUTF8(urlStr));
    if (fixedURL.is_valid())
      url = fixedURL;
  }
  return url;
}


@implementation BookmarkItem

@synthesize node = node_;

- (id)initWithBookmarkNode:(const BookmarkNode*)node
                   manager:(BookmarkManagerController*)manager {
  DCHECK(manager);
  self = [super init];
  if (self) {
    node_ = node;
    manager_ = manager;
  }
  return self;
}

- (NSString*)description {
  return [NSString stringWithFormat:@"%@[%@]", [self class], [self title]];
}

- (NSString*)title {
  return base::SysWideToNSString(node_->GetTitle());
}

- (void)setTitle:(NSString*)title {
  DCHECK(![self isFixed]);
  [manager_ bookmarkModel]->SetTitle(node_, base::SysNSStringToWide(title));
}

- (NSString*)URLString {
  GURL url = node_->GetURL();
  if (url.is_empty())
    return nil;
  return base::SysUTF8ToNSString(url.possibly_invalid_spec());
}

- (void)setURLString:(NSString*)urlStr {
  DCHECK(![self isFolder]);
  DCHECK(![self isFixed]);
  GURL url = ConvertToGURL(urlStr);
  if (url == node_->GetURL() || !url.is_valid())
    return;

  icon_.reset(nil);
  // Create a new node with the updated URL.
  BookmarkModel* model = [manager_ bookmarkModel];
  const BookmarkNode* parent = node_->GetParent();
  int index = parent->IndexOfChild(node_);
  node_ = model->AddURLWithCreationTime(parent, index, node_->GetTitle(),
                                        url, node_->date_added());
  // Tell the manager that the new node maps to me.
  [manager_ remapItem:self forNode:node_];
  // Remove my old node.
  model->Remove(parent, index + 1);
}

- (BookmarkItem*)parent {
  const BookmarkNode* parent = node_->GetParent();
  if (!parent || parent->IsRoot())
    return nil;
  return [manager_ itemFromNode:parent];
}

- (NSString*)folderPath {
  scoped_nsobject<NSMutableArray> path([[NSMutableArray alloc] init]);
  for (BookmarkItem* folder = [self parent]; folder; folder = [folder parent]) {
    [path insertObject:[folder title] atIndex:0];
  }
  return [path componentsJoinedByString:@"/"];
}

- (BOOL)isFolder {
  return node_->is_folder();
}

- (NSUInteger)numberOfChildren {
  return node_->GetChildCount();
}

- (BookmarkItem*)childAtIndex:(NSUInteger)index {
  return [manager_ itemFromNode:node_->GetChild(index)];
}

- (NSUInteger)indexOfChild:(BookmarkItem*)child {
  DCHECK(child);
  int index = node_->IndexOfChild([child node]);
  return index >= 0 ? index : NSNotFound;
}

- (BOOL)hasDescendant:(BookmarkItem*)item {
  while (item) {
    if (item == self)
      return YES;
    item = [item parent];
  }
  return NO;
}

- (BOOL)childAtIndexIsFolder:(NSUInteger)index {
  return node_->GetChild(index)->is_folder();
}

- (NSUInteger)numberOfChildFolders {
  int nChildren = [self numberOfChildren];
  NSUInteger nFolders = 0;
  for (int i = 0; i < nChildren; i++) {
    if ([self childAtIndexIsFolder:i])
      ++nFolders;
  }
  return nFolders;
}

- (BookmarkItem*)childFolderAtIndex:(NSUInteger)index {
  NSUInteger nChildren = [self numberOfChildren];
  for (NSUInteger i = 0; i < nChildren; i++) {
    if ([self childAtIndexIsFolder:i]) {
      if (index-- == 0)
        return [self childAtIndex:i];
    }
  }
  NOTREACHED();
  return nil;
}

- (NSUInteger)indexOfChildFolder:(BookmarkItem*)child {
  const BookmarkNode* childNode = [child node];
  DCHECK(childNode);
  NSUInteger folderIndex = 0;
  NSUInteger nChildren = [self numberOfChildren];
  for (NSUInteger i = 0; i < nChildren; i++) {
    if ([self childAtIndexIsFolder:i]) {
      if ([self childAtIndex:i] == child)
        return folderIndex;
      ++folderIndex;
    }
  }
  return NSNotFound;
}

+ (NSImage*)defaultIcon {
  static NSImage* sDefaultFavIcon = [ResourceBundle::GetSharedInstance().
      GetNSImageNamed(IDR_DEFAULT_FAVICON) retain];
  return sDefaultFavIcon;
}

+ (NSImage*)folderIcon {
  static NSImage* sFolderIcon = [ResourceBundle::GetSharedInstance().
      GetNSImageNamed(IDR_BOOKMARK_BAR_FOLDER) retain];
  return sFolderIcon;
}

- (NSImage*)icon {
  if (!icon_) {
    // Lazily load icon.
    NSImage* icon = [[self class] defaultIcon];
    if ([self isFolder]) {
      icon = [[self class] folderIcon];
    } else if (node_ && node_->is_url()) {
      const SkBitmap& skIcon = [manager_ bookmarkModel]->GetFavIcon(node_);
      if (!skIcon.isNull()) {
        icon = gfx::SkBitmapToNSImage(skIcon);
      }
    }
    icon_.reset([icon retain]);
  }
  return icon_;
}

- (id)persistentID {
  DCHECK(node_);
  return [NSNumber numberWithLongLong:node_->id()];
}

- (BookmarkItem*)itemWithPersistentID:(id)persistentID {
  if ([persistentID isKindOfClass:[NSNumber class]]) {
    int64 nodeID = [persistentID longLongValue];
    const BookmarkNode* node = [manager_ bookmarkModel]->GetNodeByID(nodeID);
    return [manager_ itemFromNode:node];
  }
  NOTREACHED();
  return nil;
}

- (BOOL)isFake {
  return NO;
}

- (BOOL)isFixed {
  BookmarkModel* model = [manager_ bookmarkModel];
  return node_ == model->GetBookmarkBarNode() || node_ == model->other_node();
}

// Open a bookmark, by having Chrome open a tab on its URL.
- (BOOL)open {
  if (!node_ || !node_->is_url())
    return NO;
  GURL url = node_->GetURL();
  if (url.is_empty())
    return NO;

  Browser* browser = BrowserList::GetLastActive();
  // if no browser window exists then create one with no tabs to be filled in
  if (!browser) {
    browser = Browser::Create([manager_ profile]);
    browser->window()->Show();
  }
  browser->OpenURL(url, GURL(), NEW_FOREGROUND_TAB,
                   PageTransition::AUTO_BOOKMARK);
  return YES;
}

- (void)moveItem:(BookmarkItem*)item toIndex:(int)index {
  DCHECK(![item isFixed]);
  const BookmarkNode* srcNode = [item node];
  DCHECK(srcNode);
  DCHECK(node_);
  [manager_ bookmarkModel]->Move(srcNode, node_, index);
}

- (BookmarkItem*)addBookmarkWithTitle:(NSString*)title
                                  URL:(NSString*)urlString
                              atIndex:(int)index{
  DCHECK(title);
  DCHECK_GT([urlString length], 0U);
  DCHECK([self isFolder]);
  DCHECK(node_);
  GURL url = ConvertToGURL(urlString);
  if (!url.is_valid())
    return nil;
  BookmarkModel* model = [manager_ bookmarkModel];
  const BookmarkNode* node = model->AddURL([self node],
                                           index,
                                           base::SysNSStringToWide(title),
                                           url);
  return [manager_ itemFromNode:node];
}

- (BookmarkItem*)addFolderWithTitle:(NSString*)title
                            atIndex:(int)index {
  DCHECK(title);
  DCHECK([self isFolder]);
  DCHECK(node_);
  BookmarkModel* model = [manager_ bookmarkModel];
  const BookmarkNode* node = model->AddGroup([self node],
                                             index,
                                             base::SysNSStringToWide(title));
  return [manager_ itemFromNode:node];
}

- (BOOL)removeChild:(BookmarkItem*)child {
  DCHECK(![child isFixed]);
  if (!node_)
    return NO;
  const BookmarkNode* childNode = [child node];
  if (!childNode)
    return NO;
  int index = node_->IndexOfChild(childNode);
  if (index < 0)
    return NO;
  [manager_ bookmarkModel]->Remove(node_, index);
  return YES;
}

- (void)nodeChanged {
  icon_.reset(nil);
}

@end


@implementation FakeBookmarkItem

@synthesize title = title_, parent = parent_, children = children_;

- (id)initWithTitle:(NSString*)title
               icon:(NSImage*)icon
            manager:(BookmarkManagerController*)manager  {
  self = [super initWithBookmarkNode:nil manager:manager];
  if (self) {
    title_ = [title copy];
    children_ = [[NSMutableArray alloc] init];
    icon_.reset([icon retain]);
  }
  return self;
}

- (void)dealloc {
  [title_ release];
  [children_ release];
  [super dealloc];
}

- (BOOL)isFolder {
  return children_ != nil;
}

- (NSUInteger)numberOfChildren {
  return [children_ count];
}

- (BookmarkItem*)childAtIndex:(NSUInteger)index {
  return [children_ objectAtIndex:index];
}

- (NSUInteger)indexOfChild:(BookmarkItem*)child {
  return [children_ indexOfObjectIdenticalTo:child];
}

- (BOOL)childAtIndexIsFolder:(NSUInteger)index {
  return [[children_ objectAtIndex:index] isFolder];
}

- (NSString*)URLString {
  return nil;
}

- (void)setURLString:(NSString*)urlStr {
  NOTREACHED();
}

- (BOOL)isFake {
  return YES;
}

- (BOOL)isFixed {
  return YES;
}

@end