- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / bookmarks / bookmark_editor_controller.mm
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/bookmarks/bookmark_editor_controller.h"
6
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/bookmarks/bookmark_expanded_state_tracker.h"
11 #include "chrome/browser/bookmarks/bookmark_model.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/bookmarks/bookmark_utils.h"
14 #include "chrome/common/net/url_fixer_upper.h"
15 #include "components/user_prefs/user_prefs.h"
16 #include "ui/base/l10n/l10n_util.h"
17
18 @interface BookmarkEditorController (Private)
19
20 // Grab the url from the text field and convert.
21 - (GURL)GURLFromUrlField;
22
23 @end
24
25 @implementation BookmarkEditorController
26
27 @synthesize displayURL = displayURL_;
28
29 + (NSSet*)keyPathsForValuesAffectingOkEnabled {
30   return [NSSet setWithObject:@"displayURL"];
31 }
32
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 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;
51 }
52
53 - (void)dealloc {
54   [displayURL_ release];
55   [super dealloc];
56 }
57
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   }
67
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     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()];
89 }
90
91 - (void)nodeRemoved:(const BookmarkNode*)node
92          fromParent:(const BookmarkNode*)parent
93 {
94   // Be conservative; it is needed (e.g. "Add Page...")
95   node_ = NULL;
96   [self cancel:self];
97 }
98
99 #pragma mark Bookmark Editing
100
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());
105 }
106
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;
122 }
123
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   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   }
140
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   }
158
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];
164 }
165
166 - (NSColor *)urlFieldColor {
167   return [urlField_ backgroundColor];
168 }
169
170 @end  // BookmarkEditorController
171