15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_UI_SEARCH_ENGINES_TEMPLATE_URL_TABLE_MODEL_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_UI_SEARCH_ENGINES_TEMPLATE_URL_TABLE_MODEL_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
1258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch#include "base/memory/scoped_ptr.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string16.h"
14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/search_engines/template_url_service_observer.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/models/table_model.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
17116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass FaviconService;
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TemplateURL;
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TemplateURLService;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace gfx {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ImageSkia;
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TemplateURLTableModel is the TableModel implementation used by
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// KeywordEditorView to show the keywords in a TableView.
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TemplateURLTableModel has two columns, the first showing the description,
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the second the keyword.
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TemplateURLTableModel maintains a vector of ModelEntrys that correspond to
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// each row in the tableview. Each ModelEntry wraps a TemplateURL, providing
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the favicon. The entries in the model are sorted such that non-generated
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// appear first (grouped together) and are followed by generated keywords.
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TemplateURLTableModel : public ui::TableModel,
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     TemplateURLServiceObserver {
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  TemplateURLTableModel(TemplateURLService* template_url_service,
40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                        FaviconService* favicon_service);
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~TemplateURLTableModel();
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Reloads the entries from the TemplateURLService. This should ONLY be
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // invoked if the TemplateURLService wasn't initially loaded and has been
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // loaded.
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Reload();
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ui::TableModel overrides.
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int RowCount() OVERRIDE;
51a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual base::string16 GetText(int row, int column) OVERRIDE;
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual gfx::ImageSkia GetIcon(int row) OVERRIDE;
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetObserver(ui::TableModelObserver* observer) OVERRIDE;
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool HasGroups() OVERRIDE;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual Groups GetGroups() OVERRIDE;
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int GetGroupID(int row) OVERRIDE;
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes the entry at the specified index.
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Remove(int index);
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Adds a new entry at the specified index.
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Add(int index,
63a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)           const base::string16& short_name,
64a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)           const base::string16& keyword,
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           const std::string& url);
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Update the entry at the specified index.
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ModifyTemplateURL(int index,
69a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                         const base::string16& title,
70a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                         const base::string16& keyword,
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         const std::string& url);
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Reloads the icon at the specified index.
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ReloadIcon(int index);
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the TemplateURL at the specified index.
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURL* GetTemplateURL(int index);
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the index of the TemplateURL, or -1 if it the TemplateURL is not
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // found.
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int IndexOfTemplateURL(const TemplateURL* template_url);
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Moves the keyword at the specified index to be at the end of the main
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // group. Returns the new index.  If the entry is already in the main group,
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // no action is taken, and the current index is returned.
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int MoveToMainGroup(int index);
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Make the TemplateURL at |index| the default.  Returns the new index, or -1
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // if the index is invalid or it is already the default.
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int MakeDefaultTemplateURL(int index);
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If there is an observer, it's notified the selected row has changed.
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void NotifyChanged(int index);
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the index of the last entry shown in the search engines group.
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int last_search_engine_index() const { return last_search_engine_index_; }
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Returns the index of the last entry shown in the other search engines
99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // group.
100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int last_other_engine_index() const { return last_other_engine_index_; }
101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
103116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  class ModelEntry;
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Notification that a model entry has fetched its icon.
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void FaviconAvailable(ModelEntry* entry);
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TemplateURLServiceObserver notification.
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnTemplateURLServiceChanged() OVERRIDE;
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // Removes the entry at |index| from |entries_| and returns the removed item.
11258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  scoped_ptr<ModelEntry> RemoveEntry(int index);
11358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
11458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // Adds |entry| to |entries_| at |index| and takes ownership.
11558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  void AddEntry(int index, scoped_ptr<ModelEntry> entry);
11658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ui::TableModelObserver* observer_;
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The entries.
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<ModelEntry*> entries_;
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The model we're displaying entries from.
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLService* template_url_service_;
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
125116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  FaviconService* favicon_service_;
126116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Index of the last search engine in entries_. This is used to determine the
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // group boundaries.
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int last_search_engine_index_;
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
131868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Index of the last other engine in entries_. This is used to determine the
132868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // group boundaries.
133868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  int last_other_engine_index_;
134868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TemplateURLTableModel);
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_UI_SEARCH_ENGINES_TEMPLATE_URL_TABLE_MODEL_H_
140