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.
5 #ifndef CHROME_BROWSER_UI_SEARCH_ENGINES_TEMPLATE_URL_TABLE_MODEL_H_
6 #define CHROME_BROWSER_UI_SEARCH_ENGINES_TEMPLATE_URL_TABLE_MODEL_H_
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"
19 class TemplateURLService
;
25 // TemplateURLTableModel is the TableModel implementation used by
26 // KeywordEditorView to show the keywords in a TableView.
28 // TemplateURLTableModel has two columns, the first showing the description,
29 // the second the keyword.
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.
36 class TemplateURLTableModel
: public ui::TableModel
,
37 TemplateURLServiceObserver
{
39 TemplateURLTableModel(TemplateURLService
* template_url_service
,
40 FaviconService
* favicon_service
);
42 ~TemplateURLTableModel() override
;
44 // Reloads the entries from the TemplateURLService. This should ONLY be
45 // invoked if the TemplateURLService wasn't initially loaded and has been
49 // ui::TableModel overrides.
50 int RowCount() override
;
51 base::string16
GetText(int row
, int column
) override
;
52 gfx::ImageSkia
GetIcon(int row
) override
;
53 void SetObserver(ui::TableModelObserver
* observer
) override
;
54 bool HasGroups() override
;
55 Groups
GetGroups() override
;
56 int GetGroupID(int row
) override
;
58 // Removes the entry at the specified index.
59 void Remove(int index
);
61 // Adds a new entry at the specified index.
63 const base::string16
& short_name
,
64 const base::string16
& keyword
,
65 const std::string
& url
);
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
);
73 // Reloads the icon at the specified index.
74 void ReloadIcon(int index
);
76 // Returns the TemplateURL at the specified index.
77 TemplateURL
* GetTemplateURL(int index
);
79 // Returns the index of the TemplateURL, or -1 if it the TemplateURL is not
81 int IndexOfTemplateURL(const TemplateURL
* template_url
);
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
);
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
);
92 // If there is an observer, it's notified the selected row has changed.
93 void NotifyChanged(int index
);
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_
; }
98 // Returns the index of the last entry shown in the other search engines
100 int last_other_engine_index() const { return last_other_engine_index_
; }
105 // Notification that a model entry has fetched its icon.
106 void FaviconAvailable(ModelEntry
* entry
);
108 // TemplateURLServiceObserver notification.
109 void OnTemplateURLServiceChanged() override
;
111 // Removes the entry at |index| from |entries_| and returns the removed item.
112 scoped_ptr
<ModelEntry
> RemoveEntry(int index
);
114 // Adds |entry| to |entries_| at |index| and takes ownership.
115 void AddEntry(int index
, scoped_ptr
<ModelEntry
> entry
);
117 ui::TableModelObserver
* observer_
;
120 std::vector
<ModelEntry
*> entries_
;
122 // The model we're displaying entries from.
123 TemplateURLService
* template_url_service_
;
125 FaviconService
* favicon_service_
;
127 // Index of the last search engine in entries_. This is used to determine the
129 int last_search_engine_index_
;
131 // Index of the last other engine in entries_. This is used to determine the
133 int last_other_engine_index_
;
135 DISALLOW_COPY_AND_ASSIGN(TemplateURLTableModel
);
139 #endif // CHROME_BROWSER_UI_SEARCH_ENGINES_TEMPLATE_URL_TABLE_MODEL_H_