summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/bookmark_editor_controller.mm
diff options
context:
space:
mode:
authorjrg@chromium.org <jrg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-07-21 23:57:19 +0000
committerjrg@chromium.org <jrg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-07-21 23:57:19 +0000
commitee01535e43949832b8fb57a74d892390e01d36dd (patch)
tree630574b28df21ef914369b9a16c0bfdca1b74527 /chrome/browser/cocoa/bookmark_editor_controller.mm
parentd7aef10706a8e8b5da86d48113136887b3ca97a1 (diff)
downloadchromium_src-ee01535e43949832b8fb57a74d892390e01d36dd.zip
chromium_src-ee01535e43949832b8fb57a74d892390e01d36dd.tar.gz
chromium_src-ee01535e43949832b8fb57a74d892390e01d36dd.tar.bz2
Implement bookmark editor. No tree display or hierarchy movement, but
name/url editing works. Get to the edotir from a context menu (Edit, Add Page). Also Implement Open All Bookmarks menu item. BUG=http://crbug.com/8381, http://crbug.com/17006 TEST=Add some bookmarks. Right-click on a bookmark and pick Edit. Test editing the name and URL. Make sure you can't add a bogus URL. Right-click on a bookmark or the bar and Add Page. Fill in name and URL fields to add a new bookmark. Right-click Open All Bookmarks and make sure it hoses your machine. Review URL: http://codereview.chromium.org/155874 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21241 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/cocoa/bookmark_editor_controller.mm')
-rw-r--r--chrome/browser/cocoa/bookmark_editor_controller.mm186
1 files changed, 186 insertions, 0 deletions
diff --git a/chrome/browser/cocoa/bookmark_editor_controller.mm b/chrome/browser/cocoa/bookmark_editor_controller.mm
new file mode 100644
index 0000000..704c56e
--- /dev/null
+++ b/chrome/browser/cocoa/bookmark_editor_controller.mm
@@ -0,0 +1,186 @@
+// Copyright (c) 2009 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.
+
+#include "base/logging.h"
+#include "base/mac_util.h"
+#include "base/sys_string_conversions.h"
+#include "chrome/browser/bookmarks/bookmark_editor.h"
+#include "chrome/browser/bookmarks/bookmark_model.h"
+#include "chrome/browser/profile.h"
+#import "chrome/browser/cocoa/bookmark_editor_controller.h"
+
+@interface BookmarkEditorController(Private)
+// Run the bookmark editor as a modal sheet. Does not block.
+- (void)runModal;
+@end
+
+// static; implemented for each platform.
+void BookmarkEditor::Show(gfx::NativeView parent_hwnd,
+ Profile* profile,
+ const BookmarkNode* parent,
+ const BookmarkNode* node,
+ Configuration configuration,
+ Handler* handler) {
+ NSWindow* window = [parent_hwnd window];
+ BookmarkEditorController* controller = [[BookmarkEditorController alloc]
+ initWithParentWindow:window
+ profile:profile
+ parent:parent
+ node:node
+ configuration:configuration
+ handler:handler];
+ [controller runModal];
+}
+
+
+@implementation BookmarkEditorController
+
+- (id)initWithParentWindow:(NSWindow*)parentWindow
+ profile:(Profile*)profile
+ parent:(const BookmarkNode*)parent
+ node:(const BookmarkNode*)node
+ configuration:(BookmarkEditor::Configuration)configuration
+ handler:(BookmarkEditor::Handler*)handler {
+ NSString* nibpath = [mac_util::MainAppBundle()
+ pathForResource:@"BookmarkEditor"
+ ofType:@"nib"];
+ if ((self = [super initWithWindowNibPath:nibpath owner:self])) {
+ parentWindow_ = parentWindow;
+ profile_ = profile;
+ parentNode_ = parent;
+ // "Add Page..." has no "node" so this may be NULL.
+ node_ = node;
+ configuration_ = configuration;
+ handler_.reset(handler);
+ }
+ return self;
+}
+
+- (void)awakeFromNib {
+ // Set text fields to match our bookmark. If the node is NULL we
+ // arrived here from an "Add Page..." item in a context menu.
+ if (node_) {
+ initialName_.reset([base::SysWideToNSString(node_->GetTitle()) retain]);
+ std::string url_string = node_->GetURL().possibly_invalid_spec();
+ initialUrl_.reset([[NSString stringWithUTF8String:url_string.c_str()]
+ retain]);
+ } else {
+ initialName_.reset([@"" retain]);
+ initialUrl_.reset([@"" retain]);
+ }
+ [nameField_ setStringValue:initialName_];
+ [urlField_ setStringValue:initialUrl_];
+
+ if (configuration_ == BookmarkEditor::SHOW_TREE) {
+ // build the tree et al
+ NOTIMPLEMENTED();
+ } else {
+ // Remember the NSBrowser's height; we will shrink our frame by that
+ // much.
+ NSRect frame = [[self window] frame];
+ CGFloat browserHeight = [browser_ frame].size.height;
+ frame.size.height -= browserHeight;
+ frame.origin.y += browserHeight;
+ // Remove the NSBrowser and "new folder" button.
+ [browser_ removeFromSuperview];
+ [newFolderButton_ removeFromSuperview];
+ // Finally, commit the size change.
+ [[self window] setFrame:frame display:YES];
+ }
+}
+
+/* TODO(jrg):
+// Implementing this informal protocol allows us to open the sheet
+// somewhere other than at the top of the window. NOTE: this means
+// that I, the controller, am also the window's delegate.
+- (NSRect)window:(NSWindow*)window willPositionSheet:(NSWindow*)sheet
+ usingRect:(NSRect)rect {
+ // adjust rect.origin.y to be the bottom of the toolbar
+ return rect;
+}
+*/
+
+// TODO(jrg): consider NSModalSession.
+- (void)runModal {
+ [NSApp beginSheet:[self window]
+ modalForWindow:parentWindow_
+ modalDelegate:self
+ didEndSelector:@selector(didEndSheet:returnCode:contextInfo:)
+ contextInfo:nil];
+}
+
+// TODO(jrg)
+- (IBAction)newFolder:(id)sender {
+ NOTIMPLEMENTED();
+}
+
+- (IBAction)cancel:(id)sender {
+ [NSApp endSheet:[self window]];
+}
+
+// TODO(jrg): Once the tree is available edits may be more extensive
+// than just name/url.
+- (IBAction)ok:(id)sender {
+ NSString *name = [nameField_ stringValue];
+ NSString *url = [urlField_ stringValue];
+
+ if ((![name isEqual:initialName_]) ||
+ (![url isEqual:initialUrl_])) {
+ std::wstring newTitle = base::SysNSStringToWide(name);
+ GURL newURL = GURL([url UTF8String]);
+ if (!newURL.is_valid()) {
+ // Mimic observed friendliness from Windows
+ newURL = GURL([[NSString stringWithFormat:@"http://%@", url] UTF8String]);
+ }
+ if (!newURL.is_valid()) {
+ // Silently ignoring a bad URL is unfriendly.
+ newURL = GURL();
+ }
+ int index = 0;
+ BookmarkModel* model = profile_->GetBookmarkModel();
+ if (node_) {
+ index = parentNode_->IndexOfChild(node_);
+ model->Remove(parentNode_, index);
+ } else {
+ index = parentNode_->GetChildCount();
+ }
+ const BookmarkNode* node = model->AddURL(parentNode_, index,
+ newTitle, newURL);
+ // Honor handler semantics: callback on node creation
+ if (handler_.get())
+ handler_->NodeCreated(node);
+ }
+
+ [NSApp endSheet:[self window]];
+}
+
+- (void)didEndSheet:(NSWindow*)sheet
+ returnCode:(int)returnCode
+ contextInfo:(void*)contextInfo {
+ [[self window] orderOut:self];
+
+ // BookmarkEditor::Show() will create us then run away. Unusually
+ // for a controller, we are responsible for deallocating ourself.
+ [self autorelease];
+}
+
+
+- (NSString*)displayName {
+ return [nameField_ stringValue];
+}
+
+- (NSString*)displayURL {
+ return [urlField_ stringValue];
+}
+
+- (void)setDisplayName:(NSString*)name {
+ [nameField_ setStringValue:name];
+}
+
+- (void)setDisplayURL:(NSString*)name {
+ [urlField_ setStringValue:name];
+}
+
+@end // BookmarkEditorController
+