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 "chrome/browser/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 explicit TemplateURLTableModel(TemplateURLService
* template_url_service
);
41 virtual ~TemplateURLTableModel();
43 // Reloads the entries from the TemplateURLService. This should ONLY be
44 // invoked if the TemplateURLService wasn't initially loaded and has been
48 // ui::TableModel overrides.
49 virtual int RowCount() OVERRIDE
;
50 virtual base::string16
GetText(int row
, int column
) OVERRIDE
;
51 virtual gfx::ImageSkia
GetIcon(int row
) OVERRIDE
;
52 virtual void SetObserver(ui::TableModelObserver
* observer
) OVERRIDE
;
53 virtual bool HasGroups() OVERRIDE
;
54 virtual Groups
GetGroups() OVERRIDE
;
55 virtual int GetGroupID(int row
) OVERRIDE
;
57 // Removes the entry at the specified index.
58 void Remove(int index
);
60 // Adds a new entry at the specified index.
62 const base::string16
& short_name
,
63 const base::string16
& keyword
,
64 const std::string
& url
);
66 // Update the entry at the specified index.
67 void ModifyTemplateURL(int index
,
68 const base::string16
& title
,
69 const base::string16
& keyword
,
70 const std::string
& url
);
72 // Reloads the icon at the specified index.
73 void ReloadIcon(int index
);
75 // Returns the TemplateURL at the specified index.
76 TemplateURL
* GetTemplateURL(int index
);
78 // Returns the index of the TemplateURL, or -1 if it the TemplateURL is not
80 int IndexOfTemplateURL(const TemplateURL
* template_url
);
82 // Moves the keyword at the specified index to be at the end of the main
83 // group. Returns the new index. If the entry is already in the main group,
84 // no action is taken, and the current index is returned.
85 int MoveToMainGroup(int index
);
87 // Make the TemplateURL at |index| the default. Returns the new index, or -1
88 // if the index is invalid or it is already the default.
89 int MakeDefaultTemplateURL(int index
);
91 // If there is an observer, it's notified the selected row has changed.
92 void NotifyChanged(int index
);
94 TemplateURLService
* template_url_service() const {
95 return template_url_service_
;
98 // Returns the index of the last entry shown in the search engines group.
99 int last_search_engine_index() const { return last_search_engine_index_
; }
101 // Returns the index of the last entry shown in the other search engines
103 int last_other_engine_index() const { return last_other_engine_index_
; }
106 friend class ModelEntry
;
108 // Notification that a model entry has fetched its icon.
109 void FaviconAvailable(ModelEntry
* entry
);
111 // TemplateURLServiceObserver notification.
112 virtual void OnTemplateURLServiceChanged() OVERRIDE
;
114 // Removes the entry at |index| from |entries_| and returns the removed item.
115 scoped_ptr
<ModelEntry
> RemoveEntry(int index
);
117 // Adds |entry| to |entries_| at |index| and takes ownership.
118 void AddEntry(int index
, scoped_ptr
<ModelEntry
> entry
);
120 ui::TableModelObserver
* observer_
;
123 std::vector
<ModelEntry
*> entries_
;
125 // The model we're displaying entries from.
126 TemplateURLService
* template_url_service_
;
128 // Index of the last search engine in entries_. This is used to determine the
130 int last_search_engine_index_
;
132 // Index of the last other engine in entries_. This is used to determine the
134 int last_other_engine_index_
;
136 DISALLOW_COPY_AND_ASSIGN(TemplateURLTableModel
);
140 #endif // CHROME_BROWSER_UI_SEARCH_ENGINES_TEMPLATE_URL_TABLE_MODEL_H_