| OLD | NEW |
| 1 // Copyright (c) 2006-2008 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 #ifndef CHROME_BROWSER_VIEWS_KEYWORD_EDITOR_VIEW_H_ | 5 #ifndef CHROME_BROWSER_SEARCH_ENGINES_KEYWORD_EDITOR_CONTROLLER_H_ |
| 6 #define CHROME_BROWSER_VIEWS_KEYWORD_EDITOR_VIEW_H_ | 6 #define CHROME_BROWSER_SEARCH_ENGINES_KEYWORD_EDITOR_CONTROLLER_H_ |
| 7 | 7 |
| 8 #include <Windows.h> | 8 #include <string> |
| 9 #include <map> | |
| 10 | 9 |
| 11 #include "app/table_model.h" | 10 #include "base/basictypes.h" |
| 12 #include "chrome/browser/search_engines/edit_search_engine_controller.h" | 11 #include "base/scoped_ptr.h" |
| 13 #include "chrome/browser/search_engines/template_url_model.h" | |
| 14 #include "views/controls/button/button.h" | |
| 15 #include "views/controls/table/table_view_observer.h" | |
| 16 #include "views/view.h" | |
| 17 #include "views/window/dialog_delegate.h" | |
| 18 | 12 |
| 19 namespace views { | 13 class Profile; |
| 20 class Label; | 14 class TemplateURL; |
| 21 class NativeButton; | |
| 22 } | |
| 23 | |
| 24 namespace { | |
| 25 class BorderView; | |
| 26 } | |
| 27 | |
| 28 class ModelEntry; | |
| 29 class SkBitmap; | |
| 30 class TemplateURLModel; | 15 class TemplateURLModel; |
| 31 class TemplateURLTableModel; | 16 class TemplateURLTableModel; |
| 32 | 17 |
| 33 // TemplateURLTableModel is the TableModel implementation used by | 18 class KeywordEditorController { |
| 34 // KeywordEditorView to show the keywords in a TableView. | |
| 35 // | |
| 36 // TemplateURLTableModel has two columns, the first showing the description, | |
| 37 // the second the keyword. | |
| 38 // | |
| 39 // TemplateURLTableModel maintains a vector of ModelEntrys that correspond to | |
| 40 // each row in the tableview. Each ModelEntry wraps a TemplateURL, providing | |
| 41 // the favicon. The entries in the model are sorted such that non-generated | |
| 42 // appear first (grouped together) and are followed by generated keywords. | |
| 43 | |
| 44 class TemplateURLTableModel : public TableModel { | |
| 45 public: | 19 public: |
| 46 explicit TemplateURLTableModel(TemplateURLModel* template_url_model); | 20 explicit KeywordEditorController(Profile* profile); |
| 47 | 21 ~KeywordEditorController(); |
| 48 virtual ~TemplateURLTableModel(); | |
| 49 | |
| 50 // Reloads the entries from the TemplateURLModel. This should ONLY be invoked | |
| 51 // if the TemplateURLModel wasn't initially loaded and has been loaded. | |
| 52 void Reload(); | |
| 53 | |
| 54 // TableModel overrides. | |
| 55 virtual int RowCount(); | |
| 56 virtual std::wstring GetText(int row, int column); | |
| 57 virtual SkBitmap GetIcon(int row); | |
| 58 virtual void SetObserver(TableModelObserver* observer); | |
| 59 virtual bool HasGroups(); | |
| 60 virtual Groups GetGroups(); | |
| 61 virtual int GetGroupID(int row); | |
| 62 | |
| 63 // Removes the entry at the specified index. This does NOT propagate the | |
| 64 // change to the backend. | |
| 65 void Remove(int index); | |
| 66 | |
| 67 // Adds a new entry at the specified index. This does not propagate the | |
| 68 // change to the backend. | |
| 69 void Add(int index, const TemplateURL* template_url); | |
| 70 | |
| 71 // Reloads the icon at the specified index. | |
| 72 void ReloadIcon(int index); | |
| 73 | |
| 74 // Returns The TemplateURL at the specified index. | |
| 75 const TemplateURL& GetTemplateURL(int index); | |
| 76 | |
| 77 // Returns the index of the TemplateURL, or -1 if it the TemplateURL is not | |
| 78 // found. | |
| 79 int IndexOfTemplateURL(const TemplateURL* template_url); | |
| 80 | |
| 81 // Moves the keyword at the specified index to be at the end of the main | |
| 82 // group. Returns the new index. This does nothing if the entry is already | |
| 83 // in the main group. | |
| 84 void MoveToMainGroup(int index); | |
| 85 | |
| 86 // If there is an observer, it's notified the selected row has changed. | |
| 87 void NotifyChanged(int index); | |
| 88 | |
| 89 TemplateURLModel* template_url_model() const { return template_url_model_; } | |
| 90 | |
| 91 // Returns the index of the last entry shown in the search engines group. | |
| 92 int last_search_engine_index() const { return last_search_engine_index_; } | |
| 93 | |
| 94 private: | |
| 95 friend class ModelEntry; | |
| 96 | |
| 97 // Notification that a model entry has fetched its icon. | |
| 98 void FavIconAvailable(ModelEntry* entry); | |
| 99 | |
| 100 TableModelObserver* observer_; | |
| 101 | |
| 102 // The entries. | |
| 103 std::vector<ModelEntry*> entries_; | |
| 104 | |
| 105 // The model we're displaying entries from. | |
| 106 TemplateURLModel* template_url_model_; | |
| 107 | |
| 108 // Index of the last search engine in entries_. This is used to determine the | |
| 109 // group boundaries. | |
| 110 int last_search_engine_index_; | |
| 111 | |
| 112 DISALLOW_EVIL_CONSTRUCTORS(TemplateURLTableModel); | |
| 113 }; | |
| 114 | |
| 115 // KeywordEditorView ---------------------------------------------------------- | |
| 116 | |
| 117 // KeywordEditorView allows the user to edit keywords. | |
| 118 | |
| 119 class KeywordEditorView : public views::View, | |
| 120 public views::TableViewObserver, | |
| 121 public views::ButtonListener, | |
| 122 public TemplateURLModelObserver, | |
| 123 public views::DialogDelegate, | |
| 124 public EditSearchEngineControllerDelegate { | |
| 125 friend class KeywordEditorViewTest; | |
| 126 FRIEND_TEST(KeywordEditorViewTest, MakeDefault); | |
| 127 public: | |
| 128 // Shows the KeywordEditorView for the specified profile. If there is a | |
| 129 // KeywordEditorView already open, it is closed and a new one is shown. | |
| 130 static void Show(Profile* profile); | |
| 131 | |
| 132 explicit KeywordEditorView(Profile* profile); | |
| 133 virtual ~KeywordEditorView(); | |
| 134 | |
| 135 // Overridden from EditSearchEngineControllerDelegate. | |
| 136 // Calls AddTemplateURL or ModifyTemplateURL as appropriate. | |
| 137 virtual void OnEditedKeyword(const TemplateURL* template_url, | |
| 138 const std::wstring& title, | |
| 139 const std::wstring& keyword, | |
| 140 const std::wstring& url); | |
| 141 | 22 |
| 142 // Invoked when the user succesfully fills out the add keyword dialog. | 23 // Invoked when the user succesfully fills out the add keyword dialog. |
| 143 // Propagates the change to the TemplateURLModel and updates the table model. | 24 // Propagates the change to the TemplateURLModel and updates the table model. |
| 144 void AddTemplateURL(const std::wstring& title, | 25 // Returns the index of the added URL. |
| 145 const std::wstring& keyword, | 26 int AddTemplateURL(const std::wstring& title, |
| 146 const std::wstring& url); | 27 const std::wstring& keyword, |
| 28 const std::wstring& url); |
| 147 | 29 |
| 148 // Invoked when the user modifies a TemplateURL. Updates the TemplateURLModel | 30 // Invoked when the user modifies a TemplateURL. Updates the TemplateURLModel |
| 149 // and table model appropriately. | 31 // and table model appropriately. |
| 150 void ModifyTemplateURL(const TemplateURL* template_url, | 32 void ModifyTemplateURL(const TemplateURL* template_url, |
| 151 const std::wstring& title, | 33 const std::wstring& title, |
| 152 const std::wstring& keyword, | 34 const std::wstring& keyword, |
| 153 const std::wstring& url); | 35 const std::wstring& url); |
| 154 | 36 |
| 155 // Overriden to invoke Layout. | 37 // Return true if the given |url| can be made the default. |
| 156 virtual gfx::Size GetPreferredSize(); | 38 bool CanMakeDefault(const TemplateURL* url) const; |
| 157 | 39 |
| 158 // DialogDelegate methods: | 40 // Return true if the given |url| can be removed. |
| 159 virtual bool CanResize() const; | 41 bool CanRemove(const TemplateURL* url) const; |
| 160 virtual std::wstring GetWindowTitle() const; | |
| 161 virtual int GetDialogButtons() const; | |
| 162 virtual bool Accept(); | |
| 163 virtual bool Cancel(); | |
| 164 virtual views::View* GetContentsView(); | |
| 165 | 42 |
| 166 // Returns the TemplateURLModel we're using. | 43 // Remove the TemplateURL at the specified index in the TableModel. |
| 167 TemplateURLModel* template_url_model() const { return url_model_; } | 44 void RemoveTemplateURL(int index); |
| 45 |
| 46 // Make the TemplateURL at the specified index (into the TableModel) the |
| 47 // default search provider. Return the new index, or -1 if nothing was done. |
| 48 int MakeDefaultTemplateURL(int index); |
| 49 |
| 50 // Return true if the |url_model_| data is loaded. |
| 51 bool loaded() const; |
| 52 |
| 53 // Return the TemplateURL corresponding to the |index| in the model. |
| 54 const TemplateURL* GetTemplateURL(int index) const; |
| 55 |
| 56 TemplateURLTableModel* table_model() { |
| 57 return table_model_.get(); |
| 58 } |
| 59 |
| 60 TemplateURLModel* url_model() const; |
| 168 | 61 |
| 169 private: | 62 private: |
| 170 void Init(); | |
| 171 | |
| 172 // Creates the layout and adds the views to it. | |
| 173 void InitLayoutManager(); | |
| 174 | |
| 175 // TableViewObserver method. Updates buttons contingent on the selection. | |
| 176 virtual void OnSelectionChanged(); | |
| 177 // Edits the selected item. | |
| 178 virtual void OnDoubleClick(); | |
| 179 | |
| 180 // Button::ButtonListener method. | |
| 181 virtual void ButtonPressed(views::Button* sender); | |
| 182 | |
| 183 // TemplateURLModelObserver notification. | |
| 184 virtual void OnTemplateURLModelChanged(); | |
| 185 | |
| 186 // Toggles whether the selected keyword is the default search provider. | |
| 187 void MakeDefaultSearchProvider(); | |
| 188 | |
| 189 // Make the TemplateURL at the specified index (into the TableModel) the | |
| 190 // default search provider. | |
| 191 void MakeDefaultSearchProvider(int index); | |
| 192 | |
| 193 // The profile. | 63 // The profile. |
| 194 Profile* profile_; | 64 Profile* profile_; |
| 195 | 65 |
| 196 // Model containing TemplateURLs. We listen for changes on this and propagate | |
| 197 // them to the table model. | |
| 198 TemplateURLModel* url_model_; | |
| 199 | |
| 200 // Model for the TableView. | 66 // Model for the TableView. |
| 201 scoped_ptr<TemplateURLTableModel> table_model_; | 67 scoped_ptr<TemplateURLTableModel> table_model_; |
| 202 | 68 |
| 203 // All the views are added as children, so that we don't need to delete | 69 DISALLOW_COPY_AND_ASSIGN(KeywordEditorController); |
| 204 // them directly. | |
| 205 views::TableView* table_view_; | |
| 206 views::NativeButton* add_button_; | |
| 207 views::NativeButton* edit_button_; | |
| 208 views::NativeButton* remove_button_; | |
| 209 views::NativeButton* make_default_button_; | |
| 210 | |
| 211 DISALLOW_COPY_AND_ASSIGN(KeywordEditorView); | |
| 212 }; | 70 }; |
| 213 | 71 |
| 214 #endif // CHROME_BROWSER_VIEWS_KEYWORD_EDITOR_VIEW_H_ | 72 #endif // CHROME_BROWSER_SEARCH_ENGINES_KEYWORD_EDITOR_CONTROLLER_H_ |
| OLD | NEW |