| 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 #import "chrome/browser/cocoa/edit_search_engine_cocoa_controller.h" |
| 6 |
| 7 #include "app/l10n_util_mac.h" |
| 8 #include "app/resource_bundle.h" |
| 9 #import "base/mac_util.h" |
| 10 #include "base/sys_string_conversions.h" |
| 11 #include "chrome/browser/search_engines/template_url_model.h" |
| 12 #include "grit/app_resources.h" |
| 13 #include "grit/generated_resources.h" |
| 14 |
| 15 @implementation EditSearchEngineCocoaController |
| 16 |
| 17 - (id)initWithProfile:(Profile*)profile |
| 18 delegate:(EditSearchEngineControllerDelegate*)delegate |
| 19 templateURL:(const TemplateURL*)url { |
| 20 DCHECK(profile); |
| 21 NSString* nibpath = [mac_util::MainAppBundle() |
| 22 pathForResource:@"EditSearchEngine" |
| 23 ofType:@"nib"]; |
| 24 if ((self = [super initWithWindowNibPath:nibpath owner:self])) { |
| 25 profile_ = profile; |
| 26 templateURL_ = url; |
| 27 controller_.reset( |
| 28 new EditSearchEngineController(templateURL_, delegate, profile_)); |
| 29 } |
| 30 return self; |
| 31 } |
| 32 |
| 33 - (void)awakeFromNib { |
| 34 DCHECK([self window]); |
| 35 DCHECK_EQ(self, [[self window] delegate]); |
| 36 ResourceBundle& bundle = ResourceBundle::GetSharedInstance(); |
| 37 goodImage_.reset([bundle.GetNSImageNamed(IDR_INPUT_GOOD) retain]); |
| 38 badImage_.reset([bundle.GetNSImageNamed(IDR_INPUT_ALERT) retain]); |
| 39 if (templateURL_) { |
| 40 // Defaults to |..._NEW_WINDOW_TITLE|. |
| 41 [[self window] setTitle:l10n_util::GetNSString( |
| 42 IDS_SEARCH_ENGINES_EDITOR_EDIT_WINDOW_TITLE)]; |
| 43 [nameField_ setStringValue: |
| 44 base::SysWideToNSString(templateURL_->short_name())]; |
| 45 [keywordField_ setStringValue: |
| 46 base::SysWideToNSString(templateURL_->keyword())]; |
| 47 [urlField_ setStringValue: |
| 48 base::SysWideToNSString(templateURL_->url()->DisplayURL())]; |
| 49 [urlField_ setEnabled:(templateURL_->prepopulate_id() == 0)]; |
| 50 } |
| 51 // When creating a new keyword, this will mark the fields as "invalid" and |
| 52 // will not let the user save. If this is an edit, then this will set all |
| 53 // the images to the "valid" state. |
| 54 [self validateFields]; |
| 55 } |
| 56 |
| 57 // When the window closes, clean ourselves up. |
| 58 - (void)windowWillClose:(NSNotification*)notif { |
| 59 [self autorelease]; |
| 60 } |
| 61 |
| 62 - (IBAction)cancel:(id)sender { |
| 63 [[self window] close]; |
| 64 } |
| 65 |
| 66 - (IBAction)save:(id)sender { |
| 67 DCHECK([self validateFields]); |
| 68 std::wstring title = base::SysNSStringToWide([nameField_ stringValue]); |
| 69 std::wstring keyword = base::SysNSStringToWide([keywordField_ stringValue]); |
| 70 std::wstring url = base::SysNSStringToWide([urlField_ stringValue]); |
| 71 controller_->AcceptAddOrEdit(title, keyword, url); |
| 72 [[self window] close]; |
| 73 } |
| 74 |
| 75 // Delegate method for the text fields. |
| 76 |
| 77 - (void)controlTextDidChange:(NSNotification*)notif { |
| 78 [self validateFields]; |
| 79 } |
| 80 |
| 81 - (void)controlTextDidEndEditing:(NSNotification*)notif { |
| 82 [self validateFields]; |
| 83 } |
| 84 |
| 85 // Private -------------------------------------------------------------------- |
| 86 |
| 87 // Sets the appropriate image and tooltip based on a boolean |valid|. |
| 88 - (void)setIsValid:(BOOL)valid |
| 89 toolTip:(int)messageID |
| 90 forImageView:(NSImageView*)imageView |
| 91 textField:(NSTextField*)textField { |
| 92 NSImage* image = (valid) ? goodImage_ : badImage_; |
| 93 [imageView setImage:image]; |
| 94 |
| 95 NSString* toolTip = nil; |
| 96 if (!valid) |
| 97 toolTip = l10n_util::GetNSString(messageID); |
| 98 [textField setToolTip:toolTip]; |
| 99 [imageView setToolTip:toolTip]; |
| 100 } |
| 101 |
| 102 // This sets the image state for all the controls and enables or disables the |
| 103 // done button. Returns YES if all the fields are valid. |
| 104 - (BOOL)validateFields { |
| 105 std::wstring title = base::SysNSStringToWide([nameField_ stringValue]); |
| 106 BOOL titleValid = controller_->IsTitleValid(title); |
| 107 [self setIsValid:titleValid |
| 108 toolTip:IDS_SEARCH_ENGINES_INVALID_TITLE_TT |
| 109 forImageView:nameImage_ |
| 110 textField:nameField_]; |
| 111 |
| 112 std::wstring keyword = base::SysNSStringToWide([keywordField_ stringValue]); |
| 113 BOOL keywordValid = controller_->IsKeywordValid(keyword); |
| 114 [self setIsValid:keywordValid |
| 115 toolTip:IDS_SEARCH_ENGINES_INVALID_KEYWORD_TT |
| 116 forImageView:keywordImage_ |
| 117 textField:keywordField_]; |
| 118 |
| 119 std::wstring url = base::SysNSStringToWide([urlField_ stringValue]); |
| 120 BOOL urlValid = controller_->IsURLValid(url); |
| 121 [self setIsValid:urlValid |
| 122 toolTip:IDS_SEARCH_ENGINES_INVALID_URL_TT |
| 123 forImageView:urlImage_ |
| 124 textField:urlField_]; |
| 125 |
| 126 BOOL isValid = (titleValid && keywordValid && urlValid); |
| 127 [doneButton_ setEnabled:isValid]; |
| 128 return isValid; |
| 129 } |
| 130 |
| 131 @end |
| OLD | NEW |