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_
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"
22 class AppListFolderItem
;
24 class AppListItemList
;
25 class AppListModelObserver
;
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
{
39 STATUS_SYNCING
, // Syncing apps or installing synced apps.
46 STATE_CUSTOM_LAUNCHER_PAGE
,
51 typedef ui::ListModel
<SearchResult
> SearchResults
;
54 ~AppListModel() override
;
56 void AddObserver(AppListModelObserver
* observer
);
57 void RemoveObserver(AppListModelObserver
* observer
);
59 void SetStatus(Status status
);
61 void SetState(State state
);
62 State
state() { return state_
; }
64 // Finds the item matching |id|.
65 AppListItem
* FindItem(const std::string
& id
);
67 // Find a folder item matching |id|.
68 AppListFolderItem
* FindFolderItem(const std::string
& id
);
70 // Adds |item| to the model. The model takes ownership of |item|. Returns a
71 // pointer to the item that is safe to use (e.g. after passing ownership).
72 AppListItem
* AddItem(scoped_ptr
<AppListItem
> item
);
74 // Adds |item| to an existing folder or creates a new folder. If |folder_id|
75 // is empty, adds the item to the top level model instead. The model takes
76 // ownership of |item|. Returns a pointer to the item that is safe to use.
77 AppListItem
* AddItemToFolder(scoped_ptr
<AppListItem
> item
,
78 const std::string
& folder_id
);
80 // Merges two items. If the target item is a folder, the source item is added
81 // to the end of the target folder. Otherwise a new folder is created in the
82 // same position as the target item with the target item as the first item in
83 // the new folder and the source item as the second item. Returns the id of
84 // the target folder, or an empty string if the merge failed. The source item
85 // may already be in a folder. See also the comment for RemoveItemFromFolder.
86 // NOTE: This should only be called by the View code (not the sync code); it
87 // enforces folder restrictions (e.g. the target can not be an OEM folder).
88 const std::string
MergeItems(const std::string
& target_item_id
,
89 const std::string
& source_item_id
);
91 // Move |item| to the folder matching |folder_id| or to the top level if
92 // |folder_id| is empty. |item|->position will determine where the item
93 // is positioned. See also the comment for RemoveItemFromFolder.
94 void MoveItemToFolder(AppListItem
* item
, const std::string
& folder_id
);
96 // Move |item| to the folder matching |folder_id| or to the top level if
97 // |folder_id| is empty. The item will be inserted before |position| or at
98 // the end of the list if |position| is invalid. Note: |position| is copied
99 // in case it refers to the containing folder which may get deleted. See also
100 // the comment for RemoveItemFromFolder. Returns true if the item was moved.
101 // NOTE: This should only be called by the View code (not the sync code); it
102 // enforces folder restrictions (e.g. the source folder can not be type OEM).
103 bool MoveItemToFolderAt(AppListItem
* item
,
104 const std::string
& folder_id
,
105 syncer::StringOrdinal position
);
107 // Sets the position of |item| either in |top_level_item_list_| or the folder
108 // specified by |item|->folder_id(). If |new_position| is invalid, move the
109 // item to the end of the list.
110 void SetItemPosition(AppListItem
* item
,
111 const syncer::StringOrdinal
& new_position
);
113 // Sets the name of |item| and notifies observers.
114 void SetItemName(AppListItem
* item
, const std::string
& name
);
116 // Sets the name and short name of |item| and notifies observers.
117 void SetItemNameAndShortName(AppListItem
* item
,
118 const std::string
& name
,
119 const std::string
& short_name
);
121 // Deletes the item matching |id| from |top_level_item_list_| or from the
122 // appropriate folder.
123 void DeleteItem(const std::string
& id
);
125 // Call OnExtensionPreferenceChanged() for all items in the model.
126 void NotifyExtensionPreferenceChanged();
128 // Sets whether or not the folder UI should be enabled. If |folders_enabled|
129 // is false, removes any non-OEM folders.
130 void SetFoldersEnabled(bool folders_enabled
);
132 // Sets whether or not the custom launcher page should be enabled.
133 void SetCustomLauncherPageEnabled(bool enabled
);
134 bool custom_launcher_page_enabled() { return custom_launcher_page_enabled_
; }
136 // Pushes a custom launcher page's subpage into the state stack in the model.
137 void PushCustomLauncherPageSubpage();
139 // If the state stack is not empty, pops a subpage from the stack and returns
140 // true. Returns false if the stack was empty.
141 bool PopCustomLauncherPageSubpage();
143 // Clears the custom launcher page's subpage state stack from the model.
144 void ClearCustomLauncherPageSubpages();
146 int custom_launcher_page_subpage_depth() {
147 return custom_launcher_page_subpage_depth_
;
150 // Filters the given |results| by |display_type|. The returned list is
151 // truncated to |max_results|.
152 static std::vector
<SearchResult
*> FilterSearchResultsByDisplayType(
153 SearchResults
* results
,
154 SearchResult::DisplayType display_type
,
157 AppListItemList
* top_level_item_list() { return top_level_item_list_
.get(); }
159 SearchBoxModel
* search_box() { return search_box_
.get(); }
160 SearchResults
* results() { return results_
.get(); }
161 Status
status() const { return status_
; }
162 bool folders_enabled() const { return folders_enabled_
; }
165 // AppListItemListObserver
166 void OnListItemMoved(size_t from_index
,
168 AppListItem
* item
) override
;
170 // Returns an existing folder matching |folder_id| or creates a new folder.
171 AppListFolderItem
* FindOrCreateFolderItem(const std::string
& folder_id
);
173 // Adds |item_ptr| to |top_level_item_list_| and notifies observers.
174 AppListItem
* AddItemToItemListAndNotify(
175 scoped_ptr
<AppListItem
> item_ptr
);
177 // Adds |item_ptr| to |top_level_item_list_| and notifies observers that an
178 // Update occured (e.g. item moved from a folder).
179 AppListItem
* AddItemToItemListAndNotifyUpdate(
180 scoped_ptr
<AppListItem
> item_ptr
);
182 // Adds |item_ptr| to |folder| and notifies observers.
183 AppListItem
* AddItemToFolderItemAndNotify(AppListFolderItem
* folder
,
184 scoped_ptr
<AppListItem
> item_ptr
);
186 // Removes |item| from |top_level_item_list_| or calls RemoveItemFromFolder if
187 // |item|->folder_id is set.
188 scoped_ptr
<AppListItem
> RemoveItem(AppListItem
* item
);
190 // Removes |item| from |folder|. If |folder| becomes empty, deletes |folder|
191 // from |top_level_item_list_|. Does NOT trigger observers, calling function
193 scoped_ptr
<AppListItem
> RemoveItemFromFolder(AppListFolderItem
* folder
,
196 scoped_ptr
<AppListItemList
> top_level_item_list_
;
198 scoped_ptr
<SearchBoxModel
> search_box_
;
199 scoped_ptr
<SearchResults
> results_
;
203 ObserverList
<AppListModelObserver
, true> observers_
;
204 bool folders_enabled_
;
205 bool custom_launcher_page_enabled_
;
207 // The current number of subpages the custom launcher page has pushed.
208 int custom_launcher_page_subpage_depth_
;
210 DISALLOW_COPY_AND_ASSIGN(AppListModel
);
213 } // namespace app_list
215 #endif // UI_APP_LIST_APP_LIST_MODEL_H_