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