Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / app_list / app_list_model.h
blob3845f5ab87d5db3b22bcaed1c9630b9f70ba060f
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 UI_APP_LIST_APP_LIST_MODEL_H_
6 #define UI_APP_LIST_APP_LIST_MODEL_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/observer_list.h"
14 #include "ui/app_list/app_list_export.h"
15 #include "ui/app_list/app_list_item_list.h"
16 #include "ui/app_list/app_list_item_list_observer.h"
17 #include "ui/app_list/search_result.h"
18 #include "ui/base/models/list_model.h"
20 namespace app_list {
22 class AppListFolderItem;
23 class AppListItem;
24 class AppListItemList;
25 class AppListModelObserver;
26 class SearchBoxModel;
28 // Master model of app list that consists of three sub models: AppListItemList,
29 // SearchBoxModel and SearchResults. The AppListItemList sub model owns a list
30 // of AppListItems and is displayed in the grid view. SearchBoxModel is
31 // the model for SearchBoxView. SearchResults owns a list of SearchResult.
32 // NOTE: Currently this class observes |top_level_item_list_|. The View code may
33 // move entries in the item list directly (but can not add or remove them) and
34 // the model needs to notify its observers when this occurs.
35 class APP_LIST_EXPORT AppListModel : public AppListItemListObserver {
36 public:
37 enum Status {
38 STATUS_NORMAL,
39 STATUS_SYNCING, // Syncing apps or installing synced apps.
42 // Do not change the order of these as they are used for metrics.
43 enum State {
44 STATE_APPS = 0,
45 STATE_SEARCH_RESULTS,
46 STATE_START,
47 STATE_CUSTOM_LAUNCHER_PAGE,
48 // Add new values here.
50 INVALID_STATE,
51 STATE_LAST = INVALID_STATE,
54 typedef ui::ListModel<SearchResult> SearchResults;
56 AppListModel();
57 ~AppListModel() override;
59 void AddObserver(AppListModelObserver* observer);
60 void RemoveObserver(AppListModelObserver* observer);
62 void SetStatus(Status status);
64 void SetState(State state);
65 State state() { return state_; }
67 // Finds the item matching |id|.
68 AppListItem* FindItem(const std::string& id);
70 // Find a folder item matching |id|.
71 AppListFolderItem* FindFolderItem(const std::string& id);
73 // Adds |item| to the model. The model takes ownership of |item|. Returns a
74 // pointer to the item that is safe to use (e.g. after passing ownership).
75 AppListItem* AddItem(scoped_ptr<AppListItem> item);
77 // Adds |item| to an existing folder or creates a new folder. If |folder_id|
78 // is empty, adds the item to the top level model instead. The model takes
79 // ownership of |item|. Returns a pointer to the item that is safe to use.
80 AppListItem* AddItemToFolder(scoped_ptr<AppListItem> item,
81 const std::string& folder_id);
83 // Merges two items. If the target item is a folder, the source item is added
84 // to the end of the target folder. Otherwise a new folder is created in the
85 // same position as the target item with the target item as the first item in
86 // the new folder and the source item as the second item. Returns the id of
87 // the target folder, or an empty string if the merge failed. The source item
88 // may already be in a folder. See also the comment for RemoveItemFromFolder.
89 // NOTE: This should only be called by the View code (not the sync code); it
90 // enforces folder restrictions (e.g. the target can not be an OEM folder).
91 const std::string MergeItems(const std::string& target_item_id,
92 const std::string& source_item_id);
94 // Move |item| to the folder matching |folder_id| or to the top level if
95 // |folder_id| is empty. |item|->position will determine where the item
96 // is positioned. See also the comment for RemoveItemFromFolder.
97 void MoveItemToFolder(AppListItem* item, const std::string& folder_id);
99 // Move |item| to the folder matching |folder_id| or to the top level if
100 // |folder_id| is empty. The item will be inserted before |position| or at
101 // the end of the list if |position| is invalid. Note: |position| is copied
102 // in case it refers to the containing folder which may get deleted. See also
103 // the comment for RemoveItemFromFolder. Returns true if the item was moved.
104 // NOTE: This should only be called by the View code (not the sync code); it
105 // enforces folder restrictions (e.g. the source folder can not be type OEM).
106 bool MoveItemToFolderAt(AppListItem* item,
107 const std::string& folder_id,
108 syncer::StringOrdinal position);
110 // Sets the position of |item| either in |top_level_item_list_| or the folder
111 // specified by |item|->folder_id(). If |new_position| is invalid, move the
112 // item to the end of the list.
113 void SetItemPosition(AppListItem* item,
114 const syncer::StringOrdinal& new_position);
116 // Sets the name of |item| and notifies observers.
117 void SetItemName(AppListItem* item, const std::string& name);
119 // Sets the name and short name of |item| and notifies observers.
120 void SetItemNameAndShortName(AppListItem* item,
121 const std::string& name,
122 const std::string& short_name);
124 // Deletes the item matching |id| from |top_level_item_list_| or from the
125 // appropriate folder.
126 void DeleteItem(const std::string& id);
128 // Wrapper around DeleteItem() which will also clean up if its parent folder
129 // has a single child left.
130 void DeleteUninstalledItem(const std::string& id);
132 // Call OnExtensionPreferenceChanged() for all items in the model.
133 void NotifyExtensionPreferenceChanged();
135 // Sets whether or not the folder UI should be enabled. If |folders_enabled|
136 // is false, removes any non-OEM folders.
137 void SetFoldersEnabled(bool folders_enabled);
139 // Sets whether or not the custom launcher page should be enabled.
140 void SetCustomLauncherPageEnabled(bool enabled);
141 bool custom_launcher_page_enabled() const {
142 return custom_launcher_page_enabled_;
145 void set_custom_launcher_page_name(const std::string& name) {
146 custom_launcher_page_name_ = name;
149 const std::string& custom_launcher_page_name() const {
150 return custom_launcher_page_name_;
153 // Pushes a custom launcher page's subpage into the state stack in the model.
154 void PushCustomLauncherPageSubpage();
156 // If the state stack is not empty, pops a subpage from the stack and returns
157 // true. Returns false if the stack was empty.
158 bool PopCustomLauncherPageSubpage();
160 // Clears the custom launcher page's subpage state stack from the model.
161 void ClearCustomLauncherPageSubpages();
163 int custom_launcher_page_subpage_depth() {
164 return custom_launcher_page_subpage_depth_;
167 void SetSearchEngineIsGoogle(bool is_google);
168 bool search_engine_is_google() const { return search_engine_is_google_; }
170 // Filters the given |results| by |display_type|. The returned list is
171 // truncated to |max_results|.
172 static std::vector<SearchResult*> FilterSearchResultsByDisplayType(
173 SearchResults* results,
174 SearchResult::DisplayType display_type,
175 size_t max_results);
177 AppListItemList* top_level_item_list() { return top_level_item_list_.get(); }
179 SearchBoxModel* search_box() { return search_box_.get(); }
180 SearchResults* results() { return results_.get(); }
181 Status status() const { return status_; }
182 bool folders_enabled() const { return folders_enabled_; }
184 private:
185 // AppListItemListObserver
186 void OnListItemMoved(size_t from_index,
187 size_t to_index,
188 AppListItem* item) override;
190 // Returns an existing folder matching |folder_id| or creates a new folder.
191 AppListFolderItem* FindOrCreateFolderItem(const std::string& folder_id);
193 // Adds |item_ptr| to |top_level_item_list_| and notifies observers.
194 AppListItem* AddItemToItemListAndNotify(
195 scoped_ptr<AppListItem> item_ptr);
197 // Adds |item_ptr| to |top_level_item_list_| and notifies observers that an
198 // Update occured (e.g. item moved from a folder).
199 AppListItem* AddItemToItemListAndNotifyUpdate(
200 scoped_ptr<AppListItem> item_ptr);
202 // Adds |item_ptr| to |folder| and notifies observers.
203 AppListItem* AddItemToFolderItemAndNotify(AppListFolderItem* folder,
204 scoped_ptr<AppListItem> item_ptr);
206 // Removes |item| from |top_level_item_list_| or calls RemoveItemFromFolder if
207 // |item|->folder_id is set.
208 scoped_ptr<AppListItem> RemoveItem(AppListItem* item);
210 // Removes |item| from |folder|. If |folder| becomes empty, deletes |folder|
211 // from |top_level_item_list_|. Does NOT trigger observers, calling function
212 // must do so.
213 scoped_ptr<AppListItem> RemoveItemFromFolder(AppListFolderItem* folder,
214 AppListItem* item);
216 scoped_ptr<AppListItemList> top_level_item_list_;
218 scoped_ptr<SearchBoxModel> search_box_;
219 scoped_ptr<SearchResults> results_;
221 Status status_;
222 State state_;
223 base::ObserverList<AppListModelObserver, true> observers_;
224 bool folders_enabled_;
225 bool custom_launcher_page_enabled_;
226 std::string custom_launcher_page_name_;
227 bool search_engine_is_google_;
229 // The current number of subpages the custom launcher page has pushed.
230 int custom_launcher_page_subpage_depth_;
232 DISALLOW_COPY_AND_ASSIGN(AppListModel);
235 } // namespace app_list
237 #endif // UI_APP_LIST_APP_LIST_MODEL_H_