OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/logging.h" |
| 6 #include "base/mac_util.h" |
| 7 #include "base/sys_string_conversions.h" |
| 8 #include "chrome/browser/bookmarks/bookmark_editor.h" |
| 9 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 10 #include "chrome/browser/profile.h" |
| 11 #import "chrome/browser/cocoa/bookmark_editor_controller.h" |
| 12 |
| 13 @interface BookmarkEditorController(Private) |
| 14 // Run the bookmark editor as a modal sheet. Does not block. |
| 15 - (void)runModal; |
| 16 @end |
| 17 |
| 18 // static; implemented for each platform. |
| 19 void BookmarkEditor::Show(gfx::NativeView parent_hwnd, |
| 20 Profile* profile, |
| 21 const BookmarkNode* parent, |
| 22 const BookmarkNode* node, |
| 23 Configuration configuration, |
| 24 Handler* handler) { |
| 25 NSWindow* window = [parent_hwnd window]; |
| 26 BookmarkEditorController* controller = [[BookmarkEditorController alloc] |
| 27 initWithParentWindow:window |
| 28 profile:profile |
| 29 parent:parent |
| 30 node:node |
| 31 configuration:configuration |
| 32 handler:handler]; |
| 33 [controller runModal]; |
| 34 } |
| 35 |
| 36 |
| 37 @implementation BookmarkEditorController |
| 38 |
| 39 - (id)initWithParentWindow:(NSWindow*)parentWindow |
| 40 profile:(Profile*)profile |
| 41 parent:(const BookmarkNode*)parent |
| 42 node:(const BookmarkNode*)node |
| 43 configuration:(BookmarkEditor::Configuration)configuration |
| 44 handler:(BookmarkEditor::Handler*)handler { |
| 45 NSString* nibpath = [mac_util::MainAppBundle() |
| 46 pathForResource:@"BookmarkEditor" |
| 47 ofType:@"nib"]; |
| 48 if ((self = [super initWithWindowNibPath:nibpath owner:self])) { |
| 49 parentWindow_ = parentWindow; |
| 50 profile_ = profile; |
| 51 parentNode_ = parent; |
| 52 // "Add Page..." has no "node" so this may be NULL. |
| 53 node_ = node; |
| 54 configuration_ = configuration; |
| 55 handler_.reset(handler); |
| 56 } |
| 57 return self; |
| 58 } |
| 59 |
| 60 - (void)awakeFromNib { |
| 61 // Set text fields to match our bookmark. If the node is NULL we |
| 62 // arrived here from an "Add Page..." item in a context menu. |
| 63 if (node_) { |
| 64 initialName_.reset([base::SysWideToNSString(node_->GetTitle()) retain]); |
| 65 std::string url_string = node_->GetURL().possibly_invalid_spec(); |
| 66 initialUrl_.reset([[NSString stringWithUTF8String:url_string.c_str()] |
| 67 retain]); |
| 68 } else { |
| 69 initialName_.reset([@"" retain]); |
| 70 initialUrl_.reset([@"" retain]); |
| 71 } |
| 72 [nameField_ setStringValue:initialName_]; |
| 73 [urlField_ setStringValue:initialUrl_]; |
| 74 |
| 75 if (configuration_ == BookmarkEditor::SHOW_TREE) { |
| 76 // build the tree et al |
| 77 NOTIMPLEMENTED(); |
| 78 } else { |
| 79 // Remember the NSBrowser's height; we will shrink our frame by that |
| 80 // much. |
| 81 NSRect frame = [[self window] frame]; |
| 82 CGFloat browserHeight = [browser_ frame].size.height; |
| 83 frame.size.height -= browserHeight; |
| 84 frame.origin.y += browserHeight; |
| 85 // Remove the NSBrowser and "new folder" button. |
| 86 [browser_ removeFromSuperview]; |
| 87 [newFolderButton_ removeFromSuperview]; |
| 88 // Finally, commit the size change. |
| 89 [[self window] setFrame:frame display:YES]; |
| 90 } |
| 91 } |
| 92 |
| 93 /* TODO(jrg): |
| 94 // Implementing this informal protocol allows us to open the sheet |
| 95 // somewhere other than at the top of the window. NOTE: this means |
| 96 // that I, the controller, am also the window's delegate. |
| 97 - (NSRect)window:(NSWindow*)window willPositionSheet:(NSWindow*)sheet |
| 98 usingRect:(NSRect)rect { |
| 99 // adjust rect.origin.y to be the bottom of the toolbar |
| 100 return rect; |
| 101 } |
| 102 */ |
| 103 |
| 104 // TODO(jrg): consider NSModalSession. |
| 105 - (void)runModal { |
| 106 [NSApp beginSheet:[self window] |
| 107 modalForWindow:parentWindow_ |
| 108 modalDelegate:self |
| 109 didEndSelector:@selector(didEndSheet:returnCode:contextInfo:) |
| 110 contextInfo:nil]; |
| 111 } |
| 112 |
| 113 // TODO(jrg) |
| 114 - (IBAction)newFolder:(id)sender { |
| 115 NOTIMPLEMENTED(); |
| 116 } |
| 117 |
| 118 - (IBAction)cancel:(id)sender { |
| 119 [NSApp endSheet:[self window]]; |
| 120 } |
| 121 |
| 122 // TODO(jrg): Once the tree is available edits may be more extensive |
| 123 // than just name/url. |
| 124 - (IBAction)ok:(id)sender { |
| 125 NSString *name = [nameField_ stringValue]; |
| 126 NSString *url = [urlField_ stringValue]; |
| 127 |
| 128 if ((![name isEqual:initialName_]) || |
| 129 (![url isEqual:initialUrl_])) { |
| 130 std::wstring newTitle = base::SysNSStringToWide(name); |
| 131 GURL newURL = GURL([url UTF8String]); |
| 132 if (!newURL.is_valid()) { |
| 133 // Mimic observed friendliness from Windows |
| 134 newURL = GURL([[NSString stringWithFormat:@"http://%@", url] UTF8String]); |
| 135 } |
| 136 if (!newURL.is_valid()) { |
| 137 // Silently ignoring a bad URL is unfriendly. |
| 138 newURL = GURL(); |
| 139 } |
| 140 int index = 0; |
| 141 BookmarkModel* model = profile_->GetBookmarkModel(); |
| 142 if (node_) { |
| 143 index = parentNode_->IndexOfChild(node_); |
| 144 model->Remove(parentNode_, index); |
| 145 } else { |
| 146 index = parentNode_->GetChildCount(); |
| 147 } |
| 148 const BookmarkNode* node = model->AddURL(parentNode_, index, |
| 149 newTitle, newURL); |
| 150 // Honor handler semantics: callback on node creation |
| 151 if (handler_.get()) |
| 152 handler_->NodeCreated(node); |
| 153 } |
| 154 |
| 155 [NSApp endSheet:[self window]]; |
| 156 } |
| 157 |
| 158 - (void)didEndSheet:(NSWindow*)sheet |
| 159 returnCode:(int)returnCode |
| 160 contextInfo:(void*)contextInfo { |
| 161 [[self window] orderOut:self]; |
| 162 |
| 163 // BookmarkEditor::Show() will create us then run away. Unusually |
| 164 // for a controller, we are responsible for deallocating ourself. |
| 165 [self autorelease]; |
| 166 } |
| 167 |
| 168 |
| 169 - (NSString*)displayName { |
| 170 return [nameField_ stringValue]; |
| 171 } |
| 172 |
| 173 - (NSString*)displayURL { |
| 174 return [urlField_ stringValue]; |
| 175 } |
| 176 |
| 177 - (void)setDisplayName:(NSString*)name { |
| 178 [nameField_ setStringValue:name]; |
| 179 } |
| 180 |
| 181 - (void)setDisplayURL:(NSString*)name { |
| 182 [urlField_ setStringValue:name]; |
| 183 } |
| 184 |
| 185 @end // BookmarkEditorController |
| 186 |
OLD | NEW |