app_list_model.h revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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 UI_APP_LIST_APP_LIST_MODEL_H_
6#define UI_APP_LIST_APP_LIST_MODEL_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/observer_list.h"
13#include "ui/app_list/app_list_export.h"
14#include "ui/app_list/app_list_item_list.h"
15#include "ui/app_list/app_list_item_list_observer.h"
16#include "ui/base/models/list_model.h"
17
18namespace app_list {
19
20class AppListFolderItem;
21class AppListItem;
22class AppListItemList;
23class AppListModelObserver;
24class SearchBoxModel;
25class SearchResult;
26
27// Master model of app list that consists of three sub models: AppListItemList,
28// SearchBoxModel and SearchResults. The AppListItemList sub model owns a list
29// of AppListItems and is displayed in the grid view. SearchBoxModel is
30// the model for SearchBoxView. SearchResults owns a list of SearchResult.
31// NOTE: Currently this class observes |top_level_item_list_|. The View code may
32// move entries in the item list directly (but can not add or remove them) and
33// the model needs to notify its observers when this occurs.
34class APP_LIST_EXPORT AppListModel : public AppListItemListObserver {
35 public:
36  enum Status {
37    STATUS_NORMAL,
38    STATUS_SYNCING,  // Syncing apps or installing synced apps.
39  };
40
41  typedef ui::ListModel<SearchResult> SearchResults;
42
43  AppListModel();
44  virtual ~AppListModel();
45
46  void AddObserver(AppListModelObserver* observer);
47  void RemoveObserver(AppListModelObserver* observer);
48
49  void SetStatus(Status status);
50
51  // Finds the item matching |id|.
52  AppListItem* FindItem(const std::string& id);
53
54  // Find a folder item matching |id|.
55  AppListFolderItem* FindFolderItem(const std::string& id);
56
57  // Adds |item| to the model. The model takes ownership of |item|. Returns a
58  // pointer to the item that is safe to use (e.g. after passing ownership).
59  AppListItem* AddItem(scoped_ptr<AppListItem> item);
60
61  // Adds |item| to an existing folder or creates a new folder. If |folder_id|
62  // is empty, adds the item to the top level model instead. The model takes
63  // ownership of |item|. Returns a pointer to the item that is safe to use.
64  AppListItem* AddItemToFolder(scoped_ptr<AppListItem> item,
65                               const std::string& folder_id);
66
67  // Merges two items. If the target item is a folder, the source item is added
68  // to the end of the target folder. Otherwise a new folder is created in the
69  // same position as the target item with the target item as the first item in
70  // the new folder and the source item as the second item. Returns the id of
71  // the target folder, or an empty string if the merge failed. The source item
72  // may already be in a folder. See also the comment for RemoveItemFromFolder.
73  // NOTE: This should only be called by the View code (not the sync code); it
74  // enforces folder restrictions (e.g. the target can not be an OEM folder).
75  const std::string MergeItems(const std::string& target_item_id,
76                               const std::string& source_item_id);
77
78  // Move |item| to the folder matching |folder_id| or to the top level if
79  // |folder_id| is empty. |item|->position will determine where the item
80  // is positioned. See also the comment for RemoveItemFromFolder.
81  void MoveItemToFolder(AppListItem* item, const std::string& folder_id);
82
83  // Move |item| to the folder matching |folder_id| or to the top level if
84  // |folder_id| is empty. The item will be inserted before |position| or at
85  // the end of the list if |position| is invalid. Note: |position| is copied
86  // in case it refers to the containing folder which may get deleted. See also
87  // the comment for RemoveItemFromFolder. Returns true if the item was moved.
88  // NOTE: This should only be called by the View code (not the sync code); it
89  // enforces folder restrictions (e.g. the source folder can not be type OEM).
90  bool MoveItemToFolderAt(AppListItem* item,
91                          const std::string& folder_id,
92                          syncer::StringOrdinal position);
93
94  // Sets the position of |item| either in |top_level_item_list_| or the folder
95  // specified by |item|->folder_id(). If |new_position| is invalid, move the
96  // item to the end of the list.
97  void SetItemPosition(AppListItem* item,
98                       const syncer::StringOrdinal& new_position);
99
100  // Sets the name of |item| and notifies observers.
101  void SetItemName(AppListItem* item, const std::string& name);
102
103  // Sets the name and short name of |item| and notifies observers.
104  void SetItemNameAndShortName(AppListItem* item,
105                               const std::string& name,
106                               const std::string& short_name);
107
108  // Deletes the item matching |id| from |top_level_item_list_| or from the
109  // appropriate folder.
110  void DeleteItem(const std::string& id);
111
112  // Call OnExtensionPreferenceChanged() for all items in the model.
113  void NotifyExtensionPreferenceChanged();
114
115  // Sets wither or not folder UI should be enabled. If |folders_enabled| is
116  // false, removes any non-OEM folders.
117  void SetFoldersEnabled(bool folders_enabled);
118
119  AppListItemList* top_level_item_list() { return top_level_item_list_.get(); }
120
121  SearchBoxModel* search_box() { return search_box_.get(); }
122  SearchResults* results() { return results_.get(); }
123  Status status() const { return status_; }
124  bool folders_enabled() const { return folders_enabled_; }
125
126 private:
127  // AppListItemListObserver
128  virtual void OnListItemMoved(size_t from_index,
129                               size_t to_index,
130                               AppListItem* item) OVERRIDE;
131
132  // Returns an existing folder matching |folder_id| or creates a new folder.
133  AppListFolderItem* FindOrCreateFolderItem(const std::string& folder_id);
134
135  // Adds |item_ptr| to |top_level_item_list_| and notifies observers.
136  AppListItem* AddItemToItemListAndNotify(
137      scoped_ptr<AppListItem> item_ptr);
138
139  // Adds |item_ptr| to |top_level_item_list_| and notifies observers that an
140  // Update occured (e.g. item moved from a folder).
141  AppListItem* AddItemToItemListAndNotifyUpdate(
142      scoped_ptr<AppListItem> item_ptr);
143
144  // Adds |item_ptr| to |folder| and notifies observers.
145  AppListItem* AddItemToFolderItemAndNotify(AppListFolderItem* folder,
146                                            scoped_ptr<AppListItem> item_ptr);
147
148  // Removes |item| from |top_level_item_list_| or calls RemoveItemFromFolder if
149  // |item|->folder_id is set.
150  scoped_ptr<AppListItem> RemoveItem(AppListItem* item);
151
152  // Removes |item| from |folder|. If |folder| becomes empty, deletes |folder|
153  // from |top_level_item_list_|. Does NOT trigger observers, calling function
154  // must do so.
155  scoped_ptr<AppListItem> RemoveItemFromFolder(AppListFolderItem* folder,
156                                               AppListItem* item);
157
158  scoped_ptr<AppListItemList> top_level_item_list_;
159
160  scoped_ptr<SearchBoxModel> search_box_;
161  scoped_ptr<SearchResults> results_;
162
163  Status status_;
164  ObserverList<AppListModelObserver> observers_;
165  bool folders_enabled_;
166
167  DISALLOW_COPY_AND_ASSIGN(AppListModel);
168};
169
170}  // namespace app_list
171
172#endif  // UI_APP_LIST_APP_LIST_MODEL_H_
173