1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_UI_SEARCH_ENGINES_TEMPLATE_URL_TABLE_MODEL_H_ 6 #define CHROME_BROWSER_UI_SEARCH_ENGINES_TEMPLATE_URL_TABLE_MODEL_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/compiler_specific.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/strings/string16.h" 14 #include "components/search_engines/template_url_service_observer.h" 15 #include "ui/base/models/table_model.h" 16 17 class FaviconService; 18 class TemplateURL; 19 class TemplateURLService; 20 21 namespace gfx { 22 class ImageSkia; 23 } 24 25 // TemplateURLTableModel is the TableModel implementation used by 26 // KeywordEditorView to show the keywords in a TableView. 27 // 28 // TemplateURLTableModel has two columns, the first showing the description, 29 // the second the keyword. 30 // 31 // TemplateURLTableModel maintains a vector of ModelEntrys that correspond to 32 // each row in the tableview. Each ModelEntry wraps a TemplateURL, providing 33 // the favicon. The entries in the model are sorted such that non-generated 34 // appear first (grouped together) and are followed by generated keywords. 35 36 class TemplateURLTableModel : public ui::TableModel, 37 TemplateURLServiceObserver { 38 public: 39 TemplateURLTableModel(TemplateURLService* template_url_service, 40 FaviconService* favicon_service); 41 42 virtual ~TemplateURLTableModel(); 43 44 // Reloads the entries from the TemplateURLService. This should ONLY be 45 // invoked if the TemplateURLService wasn't initially loaded and has been 46 // loaded. 47 void Reload(); 48 49 // ui::TableModel overrides. 50 virtual int RowCount() OVERRIDE; 51 virtual base::string16 GetText(int row, int column) OVERRIDE; 52 virtual gfx::ImageSkia GetIcon(int row) OVERRIDE; 53 virtual void SetObserver(ui::TableModelObserver* observer) OVERRIDE; 54 virtual bool HasGroups() OVERRIDE; 55 virtual Groups GetGroups() OVERRIDE; 56 virtual int GetGroupID(int row) OVERRIDE; 57 58 // Removes the entry at the specified index. 59 void Remove(int index); 60 61 // Adds a new entry at the specified index. 62 void Add(int index, 63 const base::string16& short_name, 64 const base::string16& keyword, 65 const std::string& url); 66 67 // Update the entry at the specified index. 68 void ModifyTemplateURL(int index, 69 const base::string16& title, 70 const base::string16& keyword, 71 const std::string& url); 72 73 // Reloads the icon at the specified index. 74 void ReloadIcon(int index); 75 76 // Returns the TemplateURL at the specified index. 77 TemplateURL* GetTemplateURL(int index); 78 79 // Returns the index of the TemplateURL, or -1 if it the TemplateURL is not 80 // found. 81 int IndexOfTemplateURL(const TemplateURL* template_url); 82 83 // Moves the keyword at the specified index to be at the end of the main 84 // group. Returns the new index. If the entry is already in the main group, 85 // no action is taken, and the current index is returned. 86 int MoveToMainGroup(int index); 87 88 // Make the TemplateURL at |index| the default. Returns the new index, or -1 89 // if the index is invalid or it is already the default. 90 int MakeDefaultTemplateURL(int index); 91 92 // If there is an observer, it's notified the selected row has changed. 93 void NotifyChanged(int index); 94 95 // Returns the index of the last entry shown in the search engines group. 96 int last_search_engine_index() const { return last_search_engine_index_; } 97 98 // Returns the index of the last entry shown in the other search engines 99 // group. 100 int last_other_engine_index() const { return last_other_engine_index_; } 101 102 private: 103 class ModelEntry; 104 105 // Notification that a model entry has fetched its icon. 106 void FaviconAvailable(ModelEntry* entry); 107 108 // TemplateURLServiceObserver notification. 109 virtual void OnTemplateURLServiceChanged() OVERRIDE; 110 111 // Removes the entry at |index| from |entries_| and returns the removed item. 112 scoped_ptr<ModelEntry> RemoveEntry(int index); 113 114 // Adds |entry| to |entries_| at |index| and takes ownership. 115 void AddEntry(int index, scoped_ptr<ModelEntry> entry); 116 117 ui::TableModelObserver* observer_; 118 119 // The entries. 120 std::vector<ModelEntry*> entries_; 121 122 // The model we're displaying entries from. 123 TemplateURLService* template_url_service_; 124 125 FaviconService* favicon_service_; 126 127 // Index of the last search engine in entries_. This is used to determine the 128 // group boundaries. 129 int last_search_engine_index_; 130 131 // Index of the last other engine in entries_. This is used to determine the 132 // group boundaries. 133 int last_other_engine_index_; 134 135 DISALLOW_COPY_AND_ASSIGN(TemplateURLTableModel); 136 }; 137 138 139 #endif // CHROME_BROWSER_UI_SEARCH_ENGINES_TEMPLATE_URL_TABLE_MODEL_H_ 140