Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(300)

Side by Side Diff: chrome/browser/cocoa/bookmark_editor_controller.mm

Issue 160628: On bookmark edit, the OK button is now disabled if the entered URL is... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 11 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/logging.h" 5 #include "base/logging.h"
6 #include "base/mac_util.h" 6 #include "base/mac_util.h"
7 #include "base/sys_string_conversions.h" 7 #include "base/sys_string_conversions.h"
8 #include "chrome/browser/bookmarks/bookmark_editor.h" 8 #include "chrome/browser/bookmarks/bookmark_editor.h"
9 #include "chrome/browser/bookmarks/bookmark_model.h" 9 #include "chrome/browser/bookmarks/bookmark_model.h"
10 #include "chrome/browser/profile.h" 10 #include "chrome/browser/profile.h"
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 std::string url_string = node_->GetURL().possibly_invalid_spec(); 65 std::string url_string = node_->GetURL().possibly_invalid_spec();
66 initialUrl_.reset([[NSString stringWithUTF8String:url_string.c_str()] 66 initialUrl_.reset([[NSString stringWithUTF8String:url_string.c_str()]
67 retain]); 67 retain]);
68 } else { 68 } else {
69 initialName_.reset([@"" retain]); 69 initialName_.reset([@"" retain]);
70 initialUrl_.reset([@"" retain]); 70 initialUrl_.reset([@"" retain]);
71 } 71 }
72 [nameField_ setStringValue:initialName_]; 72 [nameField_ setStringValue:initialName_];
73 [urlField_ setStringValue:initialUrl_]; 73 [urlField_ setStringValue:initialUrl_];
74 74
75 // Get a ping when the URL text changes;
76 // trigger an initial ping to set things up.
77 [urlField_ setDelegate:self];
78 [self controlTextDidChange:nil];
79
75 if (configuration_ == BookmarkEditor::SHOW_TREE) { 80 if (configuration_ == BookmarkEditor::SHOW_TREE) {
76 // build the tree et al 81 // build the tree et al
77 NOTIMPLEMENTED(); 82 NOTIMPLEMENTED();
78 } else { 83 } else {
79 // Remember the NSBrowser's height; we will shrink our frame by that 84 // Remember the NSBrowser's height; we will shrink our frame by that
80 // much. 85 // much.
81 NSRect frame = [[self window] frame]; 86 NSRect frame = [[self window] frame];
82 CGFloat browserHeight = [browser_ frame].size.height; 87 CGFloat browserHeight = [browser_ frame].size.height;
83 frame.size.height -= browserHeight; 88 frame.size.height -= browserHeight;
84 frame.origin.y += browserHeight; 89 frame.origin.y += browserHeight;
(...skipping 27 matching lines...) Expand all
112 117
113 // TODO(jrg) 118 // TODO(jrg)
114 - (IBAction)newFolder:(id)sender { 119 - (IBAction)newFolder:(id)sender {
115 NOTIMPLEMENTED(); 120 NOTIMPLEMENTED();
116 } 121 }
117 122
118 - (IBAction)cancel:(id)sender { 123 - (IBAction)cancel:(id)sender {
119 [NSApp endSheet:[self window]]; 124 [NSApp endSheet:[self window]];
120 } 125 }
121 126
127 // If possible, return a valid GURL from the URL text field.
128 - (GURL)GURLFromUrlField {
129 NSString *url = [urlField_ stringValue];
130 GURL newURL = GURL([url UTF8String]);
131 if (!newURL.is_valid()) {
132 // Mimic observed friendliness from Windows
133 newURL = GURL([[NSString stringWithFormat:@"http://%@", url] UTF8String]);
134 }
135 return newURL;
136 }
137
138 // When the URL changes we may enable or disable the OK button.
139 // We set ourselves as the delegate of urlField_ so this gets called.
140 // (Yes, setting ourself as a delegate automatically registers us for
141 // the notification.)
142 - (void)controlTextDidChange:(NSNotification *)aNotification {
143 GURL newURL = [self GURLFromUrlField];
144 if (newURL.is_valid()) {
145 [okButton_ setEnabled:YES];
146 } else {
147 [okButton_ setEnabled:NO];
148 }
149 }
150
122 // TODO(jrg): Once the tree is available edits may be more extensive 151 // TODO(jrg): Once the tree is available edits may be more extensive
123 // than just name/url. 152 // than just name/url.
124 - (IBAction)ok:(id)sender { 153 - (IBAction)ok:(id)sender {
125 NSString *name = [nameField_ stringValue]; 154 NSString *name = [nameField_ stringValue];
126 NSString *url = [urlField_ stringValue]; 155 NSString *url = [urlField_ stringValue];
127 156
128 if ((![name isEqual:initialName_]) || 157 if ((![name isEqual:initialName_]) ||
129 (![url isEqual:initialUrl_])) { 158 (![url isEqual:initialUrl_])) {
130 std::wstring newTitle = base::SysNSStringToWide(name); 159 std::wstring newTitle = base::SysNSStringToWide(name);
131 GURL newURL = GURL([url UTF8String]); 160 GURL newURL = [self GURLFromUrlField];
132 if (!newURL.is_valid()) { 161 if (!newURL.is_valid()) {
133 // Mimic observed friendliness from Windows 162 // Shouldn't be reached -- OK button disabled if not valid!
134 newURL = GURL([[NSString stringWithFormat:@"http://%@", url] UTF8String]); 163 NOTREACHED();
135 } 164 return;
136 if (!newURL.is_valid()) {
137 // Silently ignoring a bad URL is unfriendly.
138 newURL = GURL();
139 } 165 }
140 int index = 0; 166 int index = 0;
141 BookmarkModel* model = profile_->GetBookmarkModel(); 167 BookmarkModel* model = profile_->GetBookmarkModel();
142 if (node_) { 168 if (node_) {
143 index = parentNode_->IndexOfChild(node_); 169 index = parentNode_->IndexOfChild(node_);
144 model->Remove(parentNode_, index); 170 model->Remove(parentNode_, index);
145 } else { 171 } else {
146 index = parentNode_->GetChildCount(); 172 index = parentNode_->GetChildCount();
147 } 173 }
148 const BookmarkNode* node = model->AddURL(parentNode_, index, 174 const BookmarkNode* node = model->AddURL(parentNode_, index,
149 newTitle, newURL); 175 newTitle, newURL);
150 // Honor handler semantics: callback on node creation 176 // Honor handler semantics: callback on node creation
151 if (handler_.get()) 177 if (handler_.get())
152 handler_->NodeCreated(node); 178 handler_->NodeCreated(node);
153 } 179 }
154 180
155 [NSApp endSheet:[self window]]; 181 [NSApp endSheet:[self window]];
156 } 182 }
157 183
158 - (void)didEndSheet:(NSWindow*)sheet 184 - (void)didEndSheet:(NSWindow*)sheet
159 returnCode:(int)returnCode 185 returnCode:(int)returnCode
160 contextInfo:(void*)contextInfo { 186 contextInfo:(void*)contextInfo {
187 // This is probably unnecessary but it feels cleaner since the
188 // delegate of a text field can be automatically registered for
189 // notifications.
190 [urlField_ setDelegate:nil];
191
161 [[self window] orderOut:self]; 192 [[self window] orderOut:self];
162 193
163 // BookmarkEditor::Show() will create us then run away. Unusually 194 // BookmarkEditor::Show() will create us then run away. Unusually
164 // for a controller, we are responsible for deallocating ourself. 195 // for a controller, we are responsible for deallocating ourself.
165 [self autorelease]; 196 [self autorelease];
166 } 197 }
167 198
168 199
169 - (NSString*)displayName { 200 - (NSString*)displayName {
170 return [nameField_ stringValue]; 201 return [nameField_ stringValue];
171 } 202 }
172 203
173 - (NSString*)displayURL { 204 - (NSString*)displayURL {
174 return [urlField_ stringValue]; 205 return [urlField_ stringValue];
175 } 206 }
176 207
177 - (void)setDisplayName:(NSString*)name { 208 - (void)setDisplayName:(NSString*)name {
178 [nameField_ setStringValue:name]; 209 [nameField_ setStringValue:name];
179 } 210 }
180 211
181 - (void)setDisplayURL:(NSString*)name { 212 - (void)setDisplayURL:(NSString*)name {
182 [urlField_ setStringValue:name]; 213 [urlField_ setStringValue:name];
214 [self controlTextDidChange:nil];
215 }
216
217 - (BOOL)okButtonEnabled {
218 return [okButton_ isEnabled];
183 } 219 }
184 220
185 @end // BookmarkEditorController 221 @end // BookmarkEditorController
186 222
OLDNEW
« no previous file with comments | « chrome/browser/cocoa/bookmark_editor_controller.h ('k') | chrome/browser/cocoa/bookmark_editor_controller_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698