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/string16.h" 8#include "base/sys_string_conversions.h" 9#include "chrome/browser/bookmarks/bookmark_model.h" 10#include "ui/base/l10n/l10n_util.h" 11 12@interface BookmarkEditorController (Private) 13 14// Grab the url from the text field and convert. 15- (GURL)GURLFromUrlField; 16 17@end 18 19@implementation BookmarkEditorController 20 21@synthesize displayURL = displayURL_; 22 23+ (NSSet*)keyPathsForValuesAffectingOkEnabled { 24 return [NSSet setWithObject:@"displayURL"]; 25} 26 27- (id)initWithParentWindow:(NSWindow*)parentWindow 28 profile:(Profile*)profile 29 parent:(const BookmarkNode*)parent 30 node:(const BookmarkNode*)node 31 configuration:(BookmarkEditor::Configuration)configuration { 32 if ((self = [super initWithParentWindow:parentWindow 33 nibName:@"BookmarkEditor" 34 profile:profile 35 parent:parent 36 configuration:configuration])) { 37 // "Add Page..." has no "node" so this may be NULL. 38 node_ = node; 39 } 40 return self; 41} 42 43- (void)dealloc { 44 [displayURL_ release]; 45 [super dealloc]; 46} 47 48- (void)awakeFromNib { 49 // Set text fields to match our bookmark. If the node is NULL we 50 // arrived here from an "Add Page..." item in a context menu. 51 if (node_) { 52 [self setInitialName:base::SysUTF16ToNSString(node_->GetTitle())]; 53 std::string url_string = node_->GetURL().possibly_invalid_spec(); 54 initialUrl_.reset([[NSString stringWithUTF8String:url_string.c_str()] 55 retain]); 56 } else { 57 initialUrl_.reset([@"" retain]); 58 } 59 [self setDisplayURL:initialUrl_]; 60 [super awakeFromNib]; 61} 62 63- (void)nodeRemoved:(const BookmarkNode*)node 64 fromParent:(const BookmarkNode*)parent 65{ 66 // Be conservative; it is needed (e.g. "Add Page...") 67 node_ = NULL; 68 [self cancel:self]; 69} 70 71#pragma mark Bookmark Editing 72 73// If possible, return a valid GURL from the URL text field. 74- (GURL)GURLFromUrlField { 75 NSString* url = [self displayURL]; 76 GURL newURL = GURL([url UTF8String]); 77 if (!newURL.is_valid()) { 78 // Mimic observed friendliness from Windows 79 newURL = GURL([[NSString stringWithFormat:@"http://%@", url] UTF8String]); 80 } 81 return newURL; 82} 83 84// Enable the OK button if there is a valid URL. 85- (BOOL)okEnabled { 86 BOOL okEnabled = NO; 87 if ([[self displayURL] length]) { 88 GURL newURL = [self GURLFromUrlField]; 89 okEnabled = (newURL.is_valid()) ? YES : NO; 90 } 91 if (okEnabled) 92 [urlField_ setBackgroundColor:[NSColor whiteColor]]; 93 else 94 [urlField_ setBackgroundColor:[NSColor colorWithCalibratedRed:1.0 95 green:0.67 96 blue:0.67 97 alpha:1.0]]; 98 return okEnabled; 99} 100 101// The the bookmark's URL is assumed to be valid (otherwise the OK button 102// should not be enabled). Previously existing bookmarks for which the 103// parent has not changed are updated in-place. Those for which the parent 104// has changed are removed with a new node created under the new parent. 105// Called by -[BookmarkEditorBaseController ok:]. 106- (NSNumber*)didCommit { 107 NSString* name = [[self displayName] stringByTrimmingCharactersInSet: 108 [NSCharacterSet newlineCharacterSet]]; 109 string16 newTitle = base::SysNSStringToUTF16(name); 110 const BookmarkNode* newParentNode = [self selectedNode]; 111 GURL newURL = [self GURLFromUrlField]; 112 if (!newURL.is_valid()) { 113 // Shouldn't be reached -- OK button should be disabled if not valid! 114 NOTREACHED(); 115 return [NSNumber numberWithBool:NO]; 116 } 117 118 // Determine where the new/replacement bookmark is to go. 119 BookmarkModel* model = [self bookmarkModel]; 120 // If there was an old node then we update the node, and move it to its new 121 // parent if the parent has changed (rather than deleting it from the old 122 // parent and adding to the new -- which also prevents the 'poofing' that 123 // occurs when a node is deleted). 124 if (node_) { 125 model->SetURL(node_, newURL); 126 model->SetTitle(node_, newTitle); 127 const BookmarkNode* oldParentNode = [self parentNode]; 128 if (newParentNode != oldParentNode) 129 model->Move(node_, newParentNode, newParentNode->child_count()); 130 } else { 131 // Otherwise, add a new bookmark at the end of the newly selected folder. 132 model->AddURL(newParentNode, newParentNode->child_count(), newTitle, 133 newURL); 134 } 135 return [NSNumber numberWithBool:YES]; 136} 137 138- (NSColor *)urlFieldColor { 139 return [urlField_ backgroundColor]; 140} 141 142@end // BookmarkEditorController 143 144