[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / bookmarks / bookmark_editor_controller.mm
blob0ff15a86d6ca180e6f715f04f75deb363ceedbae
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 "chrome/common/net/url_fixer_upper.h"
13 #include "components/bookmarks/browser/bookmark_expanded_state_tracker.h"
14 #include "components/bookmarks/browser/bookmark_model.h"
15 #include "components/user_prefs/user_prefs.h"
16 #include "ui/base/l10n/l10n_util.h"
18 @interface BookmarkEditorController (Private)
20 // Grab the url from the text field and convert.
21 - (GURL)GURLFromUrlField;
23 @end
25 @implementation BookmarkEditorController
27 @synthesize displayURL = displayURL_;
29 + (NSSet*)keyPathsForValuesAffectingOkEnabled {
30   return [NSSet setWithObject:@"displayURL"];
33 - (id)initWithParentWindow:(NSWindow*)parentWindow
34                    profile:(Profile*)profile
35                     parent:(const BookmarkNode*)parent
36                       node:(const BookmarkNode*)node
37                        url:(const GURL&)url
38                      title:(const base::string16&)title
39              configuration:(BookmarkEditor::Configuration)configuration {
40   if ((self = [super initWithParentWindow:parentWindow
41                                   nibName:@"BookmarkEditor"
42                                   profile:profile
43                                    parent:parent
44                                       url:url
45                                     title:title
46                             configuration:configuration])) {
47     // "Add Page..." has no "node" so this may be NULL.
48     node_ = node;
49   }
50   return self;
53 - (void)dealloc {
54   [displayURL_ release];
55   [super dealloc];
58 - (void)awakeFromNib {
59   // Check if NSTextFieldCell supports the method. This check is in place as
60   // only 10.6 and greater support the setUsesSingleLineMode method.
61   // TODO(kushi.p): Remove this when the project hits a 10.6+ only state.
62   NSTextFieldCell* nameFieldCell_ = [nameTextField_ cell];
63   if ([nameFieldCell_
64           respondsToSelector:@selector(setUsesSingleLineMode:)]) {
65     [nameFieldCell_ setUsesSingleLineMode:YES];
66   }
68   // Set text fields to match our bookmark.  If the node is NULL we
69   // arrived here from an "Add Page..." item in a context menu.
70   if (node_) {
71     [self setInitialName:base::SysUTF16ToNSString(node_->GetTitle())];
72     PrefService* prefs = [self profile] ?
73         user_prefs::UserPrefs::Get([self profile]) :
74         NULL;
75     base::string16 urlString =
76         chrome::FormatBookmarkURLForDisplay(node_->url(), prefs);
77     initialUrl_.reset([base::SysUTF16ToNSString(urlString) retain]);
78   } else {
79     GURL url = [self url];
80     [self setInitialName:base::SysUTF16ToNSString([self title])];
81     if (url.is_valid())
82       initialUrl_.reset([[NSString stringWithUTF8String:url.spec().c_str()]
83                           retain]);
84   }
85   [self setDisplayURL:initialUrl_];
86   [super awakeFromNib];
87   [self expandNodes:
88       [self bookmarkModel]->expanded_state_tracker()->GetExpandedNodes()];
91 - (void)nodeRemoved:(const BookmarkNode*)node
92          fromParent:(const BookmarkNode*)parent
94   // Be conservative; it is needed (e.g. "Add Page...")
95   node_ = NULL;
96   [self cancel:self];
99 #pragma mark Bookmark Editing
101 // If possible, return a valid GURL from the URL text field.
102 - (GURL)GURLFromUrlField {
103   NSString* url = [self displayURL];
104   return URLFixerUpper::FixupURL([url UTF8String], std::string());
107 // Enable the OK button if there is a valid URL.
108 - (BOOL)okEnabled {
109   BOOL okEnabled = NO;
110   if ([[self displayURL] length]) {
111     GURL newURL = [self GURLFromUrlField];
112     okEnabled = (newURL.is_valid()) ? YES : NO;
113   }
114   if (okEnabled)
115     [urlField_ setBackgroundColor:[NSColor whiteColor]];
116   else
117     [urlField_ setBackgroundColor:[NSColor colorWithCalibratedRed:1.0
118                                                             green:0.67
119                                                              blue:0.67
120                                                             alpha:1.0]];
121   return okEnabled;
124 // The bookmark's URL is assumed to be valid (otherwise the OK button
125 // should not be enabled). Previously existing bookmarks for which the
126 // parent has not changed are updated in-place. Those for which the parent
127 // has changed are removed with a new node created under the new parent.
128 // Called by -[BookmarkEditorBaseController ok:].
129 - (NSNumber*)didCommit {
130   NSString* name = [[self displayName] stringByTrimmingCharactersInSet:
131                     [NSCharacterSet newlineCharacterSet]];
132   base::string16 newTitle = base::SysNSStringToUTF16(name);
133   const BookmarkNode* newParentNode = [self selectedNode];
134   GURL newURL = [self GURLFromUrlField];
135   if (!newURL.is_valid()) {
136     // Shouldn't be reached -- OK button should be disabled if not valid!
137     NOTREACHED();
138     return [NSNumber numberWithBool:NO];
139   }
141   // Determine where the new/replacement bookmark is to go.
142   BookmarkModel* model = [self bookmarkModel];
143   // If there was an old node then we update the node, and move it to its new
144   // parent if the parent has changed (rather than deleting it from the old
145   // parent and adding to the new -- which also prevents the 'poofing' that
146   // occurs when a node is deleted).
147   if (node_) {
148     model->SetURL(node_, newURL);
149     model->SetTitle(node_, newTitle);
150     const BookmarkNode* oldParentNode = [self parentNode];
151     if (newParentNode != oldParentNode)
152       model->Move(node_, newParentNode, newParentNode->child_count());
153   } else {
154     // Otherwise, add a new bookmark at the end of the newly selected folder.
155     model->AddURL(newParentNode, newParentNode->child_count(), newTitle,
156                   newURL);
157   }
159   // Update the expanded state.
160   BookmarkExpandedStateTracker::Nodes expanded_nodes = [self getExpandedNodes];
161   [self bookmarkModel]->expanded_state_tracker()->
162       SetExpandedNodes(expanded_nodes);
163   return [NSNumber numberWithBool:YES];
166 - (NSColor *)urlFieldColor {
167   return [urlField_ backgroundColor];
170 @end  // BookmarkEditorController