• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "chrome/browser/search_engines/template_url_service_observer.h"
15 #include "ui/base/models/table_model.h"
16 
17 class ModelEntry;
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   explicit TemplateURLTableModel(TemplateURLService* template_url_service);
40 
41   virtual ~TemplateURLTableModel();
42 
43   // Reloads the entries from the TemplateURLService. This should ONLY be
44   // invoked if the TemplateURLService wasn't initially loaded and has been
45   // loaded.
46   void Reload();
47 
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;
56 
57   // Removes the entry at the specified index.
58   void Remove(int index);
59 
60   // Adds a new entry at the specified index.
61   void Add(int index,
62            const base::string16& short_name,
63            const base::string16& keyword,
64            const std::string& url);
65 
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);
71 
72   // Reloads the icon at the specified index.
73   void ReloadIcon(int index);
74 
75   // Returns the TemplateURL at the specified index.
76   TemplateURL* GetTemplateURL(int index);
77 
78   // Returns the index of the TemplateURL, or -1 if it the TemplateURL is not
79   // found.
80   int IndexOfTemplateURL(const TemplateURL* template_url);
81 
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);
86 
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);
90 
91   // If there is an observer, it's notified the selected row has changed.
92   void NotifyChanged(int index);
93 
template_url_service()94   TemplateURLService* template_url_service() const {
95     return template_url_service_;
96   }
97 
98   // Returns the index of the last entry shown in the search engines group.
last_search_engine_index()99   int last_search_engine_index() const { return last_search_engine_index_; }
100 
101   // Returns the index of the last entry shown in the other search engines
102   // group.
last_other_engine_index()103   int last_other_engine_index() const { return last_other_engine_index_; }
104 
105  private:
106   friend class ModelEntry;
107 
108   // Notification that a model entry has fetched its icon.
109   void FaviconAvailable(ModelEntry* entry);
110 
111   // TemplateURLServiceObserver notification.
112   virtual void OnTemplateURLServiceChanged() OVERRIDE;
113 
114   // Removes the entry at |index| from |entries_| and returns the removed item.
115   scoped_ptr<ModelEntry> RemoveEntry(int index);
116 
117   // Adds |entry| to |entries_| at |index| and takes ownership.
118   void AddEntry(int index, scoped_ptr<ModelEntry> entry);
119 
120   ui::TableModelObserver* observer_;
121 
122   // The entries.
123   std::vector<ModelEntry*> entries_;
124 
125   // The model we're displaying entries from.
126   TemplateURLService* template_url_service_;
127 
128   // Index of the last search engine in entries_. This is used to determine the
129   // group boundaries.
130   int last_search_engine_index_;
131 
132   // Index of the last other engine in entries_. This is used to determine the
133   // group boundaries.
134   int last_other_engine_index_;
135 
136   DISALLOW_COPY_AND_ASSIGN(TemplateURLTableModel);
137 };
138 
139 
140 #endif  // CHROME_BROWSER_UI_SEARCH_ENGINES_TEMPLATE_URL_TABLE_MODEL_H_
141