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.
50 typedef ui::ListModel
<SearchResult
> SearchResults
;
53 ~AppListModel() override
;
55 void AddObserver(AppListModelObserver
* observer
);
56 void RemoveObserver(AppListModelObserver
* observer
);
58 void SetStatus(Status status
);
60 void SetState(State state
);
61 State
state() { return state_
; }
63 // Finds the item matching |id|.
64 AppListItem
* FindItem(const std::string
& id
);
66 // Find a folder item matching |id|.
67 AppListFolderItem
* FindFolderItem(const std::string
& id
);
69 // Adds |item| to the model. The model takes ownership of |item|. Returns a
70 // pointer to the item that is safe to use (e.g. after passing ownership).
71 AppListItem
* AddItem(scoped_ptr
<AppListItem
> item
);
73 // Adds |item| to an existing folder or creates a new folder. If |folder_id|
74 // is empty, adds the item to the top level model instead. The model takes
75 // ownership of |item|. Returns a pointer to the item that is safe to use.
76 AppListItem
* AddItemToFolder(scoped_ptr
<AppListItem
> item
,
77 const std::string
& folder_id
);
79 // Merges two items. If the target item is a folder, the source item is added
80 // to the end of the target folder. Otherwise a new folder is created in the
81 // same position as the target item with the target item as the first item in
82 // the new folder and the source item as the second item. Returns the id of
83 // the target folder, or an empty string if the merge failed. The source item
84 // may already be in a folder. See also the comment for RemoveItemFromFolder.
85 // NOTE: This should only be called by the View code (not the sync code); it
86 // enforces folder restrictions (e.g. the target can not be an OEM folder).
87 const std::string
MergeItems(const std::string
& target_item_id
,
88 const std::string
& source_item_id
);
90 // Move |item| to the folder matching |folder_id| or to the top level if
91 // |folder_id| is empty. |item|->position will determine where the item
92 // is positioned. See also the comment for RemoveItemFromFolder.
93 void MoveItemToFolder(AppListItem
* item
, const std::string
& folder_id
);
95 // Move |item| to the folder matching |folder_id| or to the top level if
96 // |folder_id| is empty. The item will be inserted before |position| or at
97 // the end of the list if |position| is invalid. Note: |position| is copied
98 // in case it refers to the containing folder which may get deleted. See also
99 // the comment for RemoveItemFromFolder. Returns true if the item was moved.
100 // NOTE: This should only be called by the View code (not the sync code); it
101 // enforces folder restrictions (e.g. the source folder can not be type OEM).
102 bool MoveItemToFolderAt(AppListItem
* item
,
103 const std::string
& folder_id
,
104 syncer::StringOrdinal position
);
106 // Sets the position of |item| either in |top_level_item_list_| or the folder
107 // specified by |item|->folder_id(). If |new_position| is invalid, move the
108 // item to the end of the list.
109 void SetItemPosition(AppListItem
* item
,
110 const syncer::StringOrdinal
& new_position
);
112 // Sets the name of |item| and notifies observers.
113 void SetItemName(AppListItem
* item
, const std::string
& name
);
115 // Sets the name and short name of |item| and notifies observers.
116 void SetItemNameAndShortName(AppListItem
* item
,
117 const std::string
& name
,
118 const std::string
& short_name
);
120 // Deletes the item matching |id| from |top_level_item_list_| or from the
121 // appropriate folder.
122 void DeleteItem(const std::string
& id
);
124 // Call OnExtensionPreferenceChanged() for all items in the model.
125 void NotifyExtensionPreferenceChanged();
127 // Sets wither or not folder UI should be enabled. If |folders_enabled| is
128 // false, removes any non-OEM folders.
129 void SetFoldersEnabled(bool folders_enabled
);
131 // Filters the given |results| by |display_type|. The returned list is
132 // truncated to |max_results|.
133 static std::vector
<SearchResult
*> FilterSearchResultsByDisplayType(
134 SearchResults
* results
,
135 SearchResult::DisplayType display_type
,
138 AppListItemList
* top_level_item_list() { return top_level_item_list_
.get(); }
140 SearchBoxModel
* search_box() { return search_box_
.get(); }
141 SearchResults
* results() { return results_
.get(); }
142 Status
status() const { return status_
; }
143 bool folders_enabled() const { return folders_enabled_
; }
146 // AppListItemListObserver
147 void OnListItemMoved(size_t from_index
,
149 AppListItem
* item
) override
;
151 // Returns an existing folder matching |folder_id| or creates a new folder.
152 AppListFolderItem
* FindOrCreateFolderItem(const std::string
& folder_id
);
154 // Adds |item_ptr| to |top_level_item_list_| and notifies observers.
155 AppListItem
* AddItemToItemListAndNotify(
156 scoped_ptr
<AppListItem
> item_ptr
);
158 // Adds |item_ptr| to |top_level_item_list_| and notifies observers that an
159 // Update occured (e.g. item moved from a folder).
160 AppListItem
* AddItemToItemListAndNotifyUpdate(
161 scoped_ptr
<AppListItem
> item_ptr
);
163 // Adds |item_ptr| to |folder| and notifies observers.
164 AppListItem
* AddItemToFolderItemAndNotify(AppListFolderItem
* folder
,
165 scoped_ptr
<AppListItem
> item_ptr
);
167 // Removes |item| from |top_level_item_list_| or calls RemoveItemFromFolder if
168 // |item|->folder_id is set.
169 scoped_ptr
<AppListItem
> RemoveItem(AppListItem
* item
);
171 // Removes |item| from |folder|. If |folder| becomes empty, deletes |folder|
172 // from |top_level_item_list_|. Does NOT trigger observers, calling function
174 scoped_ptr
<AppListItem
> RemoveItemFromFolder(AppListFolderItem
* folder
,
177 scoped_ptr
<AppListItemList
> top_level_item_list_
;
179 scoped_ptr
<SearchBoxModel
> search_box_
;
180 scoped_ptr
<SearchResults
> results_
;
184 ObserverList
<AppListModelObserver
, true> observers_
;
185 bool folders_enabled_
;
187 DISALLOW_COPY_AND_ASSIGN(AppListModel
);
190 } // namespace app_list
192 #endif // UI_APP_LIST_APP_LIST_MODEL_H_