Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / bookmarks / bookmark_editor_controller.mm
blobdf3a5f9a50b182fc34fae710dcaca9c6271c721a
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.
5 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_editor_controller.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/sys_string_conversions.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/bookmarks/bookmark_utils.h"
12 #include "components/bookmarks/browser/bookmark_expanded_state_tracker.h"
13 #include "components/bookmarks/browser/bookmark_model.h"
14 #include "components/url_formatter/url_fixer.h"
15 #include "components/user_prefs/user_prefs.h"
17 using bookmarks::BookmarkExpandedStateTracker;
18 using bookmarks::BookmarkModel;
19 using bookmarks::BookmarkNode;
21 @interface BookmarkEditorController (Private)
23 // Grab the url from the text field and convert.
24 - (GURL)GURLFromUrlField;
26 @end
28 @implementation BookmarkEditorController
30 @synthesize displayURL = displayURL_;
32 + (NSSet*)keyPathsForValuesAffectingOkEnabled {
33   return [NSSet setWithObject:@"displayURL"];
36 - (id)initWithParentWindow:(NSWindow*)parentWindow
37                    profile:(Profile*)profile
38                     parent:(const BookmarkNode*)parent
39                       node:(const BookmarkNode*)node
40                        url:(const GURL&)url
41                      title:(const base::string16&)title
42              configuration:(BookmarkEditor::Configuration)configuration {
43   if ((self = [super initWithParentWindow:parentWindow
44                                   nibName:@"BookmarkEditor"
45                                   profile:profile
46                                    parent:parent
47                                       url:url
48                                     title:title
49                             configuration:configuration])) {
50     // "Add Page..." has no "node" so this may be NULL.
51     node_ = node;
52   }
53   return self;
56 - (void)dealloc {
57   [displayURL_ release];
58   [super dealloc];
61 - (void)awakeFromNib {
62   NSTextFieldCell* nameFieldCell_ = [nameTextField_ cell];
63   [nameFieldCell_ setUsesSingleLineMode:YES];
65   // Set text fields to match our bookmark.  If the node is NULL we arrived here
66   // from an "Add Page..." item in a context menu.
67   if (node_) {
68     [self setInitialName:base::SysUTF16ToNSString(node_->GetTitle())];
69     PrefService* prefs = [self profile] ?
70         user_prefs::UserPrefs::Get([self profile]) :
71         NULL;
72     base::string16 urlString =
73         chrome::FormatBookmarkURLForDisplay(node_->url(), prefs);
74     initialUrl_.reset([base::SysUTF16ToNSString(urlString) retain]);
75   } else {
76     GURL url = [self url];
77     [self setInitialName:base::SysUTF16ToNSString([self title])];
78     if (url.is_valid())
79       initialUrl_.reset([[NSString stringWithUTF8String:url.spec().c_str()]
80                           retain]);
81   }
82   [self setDisplayURL:initialUrl_];
83   [super awakeFromNib];
84   [self expandNodes:
85       [self bookmarkModel]->expanded_state_tracker()->GetExpandedNodes()];
88 - (void)nodeRemoved:(const BookmarkNode*)node
89          fromParent:(const BookmarkNode*)parent
91   // Be conservative; it is needed (e.g. "Add Page...")
92   node_ = NULL;
93   [self cancel:self];
96 #pragma mark Bookmark Editing
98 // If possible, return a valid GURL from the URL text field.
99 - (GURL)GURLFromUrlField {
100   NSString* url = [self displayURL];
101   return url_formatter::FixupURL([url UTF8String], std::string());
104 // Enable the OK button if there is a valid URL.
105 - (BOOL)okEnabled {
106   BOOL okEnabled = NO;
107   if ([[self displayURL] length]) {
108     GURL newURL = [self GURLFromUrlField];
109     okEnabled = (newURL.is_valid()) ? YES : NO;
110   }
111   if (okEnabled)
112     [urlField_ setBackgroundColor:[NSColor whiteColor]];
113   else
114     [urlField_ setBackgroundColor:[NSColor colorWithCalibratedRed:1.0
115                                                             green:0.67
116                                                              blue:0.67
117                                                             alpha:1.0]];
118   return okEnabled;
121 // The bookmark's URL is assumed to be valid (otherwise the OK button
122 // should not be enabled). Previously existing bookmarks for which the
123 // parent has not changed are updated in-place. Those for which the parent
124 // has changed are removed with a new node created under the new parent.
125 // Called by -[BookmarkEditorBaseController ok:].
126 - (NSNumber*)didCommit {
127   NSString* name = [[self displayName] stringByTrimmingCharactersInSet:
128                     [NSCharacterSet newlineCharacterSet]];
129   base::string16 newTitle = base::SysNSStringToUTF16(name);
130   const BookmarkNode* newParentNode = [self selectedNode];
131   GURL newURL = [self GURLFromUrlField];
132   if (!newURL.is_valid()) {
133     // Shouldn't be reached -- OK button should be disabled if not valid!
134     NOTREACHED();
135     return [NSNumber numberWithBool:NO];
136   }
138   // Determine where the new/replacement bookmark is to go.
139   BookmarkModel* model = [self bookmarkModel];
140   // If there was an old node then we update the node, and move it to its new
141   // parent if the parent has changed (rather than deleting it from the old
142   // parent and adding to the new -- which also prevents the 'poofing' that
143   // occurs when a node is deleted).
144   if (node_) {
145     model->SetURL(node_, newURL);
146     model->SetTitle(node_, newTitle);
147     const BookmarkNode* oldParentNode = [self parentNode];
148     if (newParentNode != oldParentNode)
149       model->Move(node_, newParentNode, newParentNode->child_count());
150   } else {
151     // Otherwise, add a new bookmark at the end of the newly selected folder.
152     model->AddURL(newParentNode, newParentNode->child_count(), newTitle,
153                   newURL);
154   }
156   // Update the expanded state.
157   BookmarkExpandedStateTracker::Nodes expanded_nodes = [self getExpandedNodes];
158   [self bookmarkModel]->expanded_state_tracker()->
159       SetExpandedNodes(expanded_nodes);
160   return [NSNumber numberWithBool:YES];
163 - (NSColor *)urlFieldColor {
164   return [urlField_ backgroundColor];
167 @end  // BookmarkEditorController