Allow app list tiles to show search results in the experimental app list.
[chromium-blink-merge.git] / ui / app_list / app_list_model.cc
blob2d0f58f64500196d931c7badf513c7dd7f4a5843
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 #include "ui/app_list/app_list_model.h"
7 #include <string>
9 #include "ui/app_list/app_list_folder_item.h"
10 #include "ui/app_list/app_list_item.h"
11 #include "ui/app_list/app_list_model_observer.h"
12 #include "ui/app_list/search_box_model.h"
14 namespace app_list {
16 AppListModel::AppListModel()
17 : top_level_item_list_(new AppListItemList),
18 search_box_(new SearchBoxModel),
19 results_(new SearchResults),
20 status_(STATUS_NORMAL),
21 folders_enabled_(false) {
22 top_level_item_list_->AddObserver(this);
25 AppListModel::~AppListModel() { top_level_item_list_->RemoveObserver(this); }
27 void AppListModel::AddObserver(AppListModelObserver* observer) {
28 observers_.AddObserver(observer);
31 void AppListModel::RemoveObserver(AppListModelObserver* observer) {
32 observers_.RemoveObserver(observer);
35 void AppListModel::SetStatus(Status status) {
36 if (status_ == status)
37 return;
39 status_ = status;
40 FOR_EACH_OBSERVER(AppListModelObserver,
41 observers_,
42 OnAppListModelStatusChanged());
45 AppListItem* AppListModel::FindItem(const std::string& id) {
46 AppListItem* item = top_level_item_list_->FindItem(id);
47 if (item)
48 return item;
49 for (size_t i = 0; i < top_level_item_list_->item_count(); ++i) {
50 AppListItem* child_item =
51 top_level_item_list_->item_at(i)->FindChildItem(id);
52 if (child_item)
53 return child_item;
55 return NULL;
58 AppListFolderItem* AppListModel::FindFolderItem(const std::string& id) {
59 AppListItem* item = top_level_item_list_->FindItem(id);
60 if (item && item->GetItemType() == AppListFolderItem::kItemType)
61 return static_cast<AppListFolderItem*>(item);
62 DCHECK(!item);
63 return NULL;
66 AppListItem* AppListModel::AddItem(scoped_ptr<AppListItem> item) {
67 DCHECK(!item->IsInFolder());
68 DCHECK(!top_level_item_list()->FindItem(item->id()));
69 return AddItemToItemListAndNotify(item.Pass());
72 AppListItem* AppListModel::AddItemToFolder(scoped_ptr<AppListItem> item,
73 const std::string& folder_id) {
74 if (folder_id.empty())
75 return AddItem(item.Pass());
76 DVLOG(2) << "AddItemToFolder: " << item->id() << ": " << folder_id;
77 DCHECK(!item->IsInFolder() || item->folder_id() == folder_id);
78 DCHECK(item->GetItemType() != AppListFolderItem::kItemType);
79 AppListFolderItem* dest_folder = FindOrCreateFolderItem(folder_id);
80 if (!dest_folder)
81 return NULL;
82 DCHECK(!dest_folder->item_list()->FindItem(item->id()))
83 << "Already in folder: " << dest_folder->id();
84 return AddItemToFolderItemAndNotify(dest_folder, item.Pass());
87 const std::string AppListModel::MergeItems(const std::string& target_item_id,
88 const std::string& source_item_id) {
89 if (!folders_enabled()) {
90 LOG(ERROR) << "MergeItems called with folders disabled.";
91 return "";
93 DVLOG(2) << "MergeItems: " << source_item_id << " -> " << target_item_id;
94 // Find the target item.
95 AppListItem* target_item = FindItem(target_item_id);
96 if (!target_item) {
97 LOG(ERROR) << "MergeItems: Target no longer exists.";
98 return "";
100 CHECK(target_item->folder_id().empty());
102 AppListItem* source_item = FindItem(source_item_id);
103 if (!source_item) {
104 LOG(ERROR) << "MergeItems: Source no longer exists.";
105 return "";
108 // If the target item is a folder, just add the source item to it.
109 if (target_item->GetItemType() == AppListFolderItem::kItemType) {
110 AppListFolderItem* target_folder =
111 static_cast<AppListFolderItem*>(target_item);
112 if (target_folder->folder_type() == AppListFolderItem::FOLDER_TYPE_OEM) {
113 LOG(WARNING) << "MergeItems called with OEM folder as target";
114 return "";
116 scoped_ptr<AppListItem> source_item_ptr = RemoveItem(source_item);
117 source_item_ptr->set_position(
118 target_folder->item_list()->CreatePositionBefore(
119 syncer::StringOrdinal()));
120 AddItemToFolderItemAndNotify(target_folder, source_item_ptr.Pass());
121 return target_folder->id();
124 // Otherwise remove the source item and target item from their current
125 // location, they will become owned by the new folder.
126 scoped_ptr<AppListItem> source_item_ptr = RemoveItem(source_item);
127 CHECK(source_item_ptr);
128 scoped_ptr<AppListItem> target_item_ptr =
129 top_level_item_list_->RemoveItem(target_item_id);
130 CHECK(target_item_ptr);
132 // Create a new folder in the same location as the target item.
133 std::string new_folder_id = AppListFolderItem::GenerateId();
134 DVLOG(2) << "Creating folder for merge: " << new_folder_id;
135 scoped_ptr<AppListItem> new_folder_ptr(new AppListFolderItem(
136 new_folder_id, AppListFolderItem::FOLDER_TYPE_NORMAL));
137 new_folder_ptr->set_position(target_item_ptr->position());
138 AppListFolderItem* new_folder = static_cast<AppListFolderItem*>(
139 AddItemToItemListAndNotify(new_folder_ptr.Pass()));
141 // Add the items to the new folder.
142 target_item_ptr->set_position(
143 new_folder->item_list()->CreatePositionBefore(
144 syncer::StringOrdinal()));
145 AddItemToFolderItemAndNotify(new_folder, target_item_ptr.Pass());
146 source_item_ptr->set_position(
147 new_folder->item_list()->CreatePositionBefore(
148 syncer::StringOrdinal()));
149 AddItemToFolderItemAndNotify(new_folder, source_item_ptr.Pass());
151 return new_folder->id();
154 void AppListModel::MoveItemToFolder(AppListItem* item,
155 const std::string& folder_id) {
156 DVLOG(2) << "MoveItemToFolder: " << folder_id
157 << " <- " << item->ToDebugString();
158 if (item->folder_id() == folder_id)
159 return;
160 AppListFolderItem* dest_folder = FindOrCreateFolderItem(folder_id);
161 scoped_ptr<AppListItem> item_ptr = RemoveItem(item);
162 if (dest_folder)
163 AddItemToFolderItemAndNotify(dest_folder, item_ptr.Pass());
164 else
165 AddItemToItemListAndNotifyUpdate(item_ptr.Pass());
168 bool AppListModel::MoveItemToFolderAt(AppListItem* item,
169 const std::string& folder_id,
170 syncer::StringOrdinal position) {
171 DVLOG(2) << "MoveItemToFolderAt: " << folder_id
172 << "[" << position.ToDebugString() << "]"
173 << " <- " << item->ToDebugString();
174 if (item->folder_id() == folder_id)
175 return false;
176 AppListFolderItem* src_folder = FindOrCreateFolderItem(item->folder_id());
177 if (src_folder &&
178 src_folder->folder_type() == AppListFolderItem::FOLDER_TYPE_OEM) {
179 LOG(WARNING) << "MoveItemToFolderAt called with OEM folder as source";
180 return false;
182 AppListFolderItem* dest_folder = FindOrCreateFolderItem(folder_id);
183 scoped_ptr<AppListItem> item_ptr = RemoveItem(item);
184 if (dest_folder) {
185 item_ptr->set_position(
186 dest_folder->item_list()->CreatePositionBefore(position));
187 AddItemToFolderItemAndNotify(dest_folder, item_ptr.Pass());
188 } else {
189 item_ptr->set_position(
190 top_level_item_list_->CreatePositionBefore(position));
191 AddItemToItemListAndNotifyUpdate(item_ptr.Pass());
193 return true;
196 void AppListModel::SetItemPosition(AppListItem* item,
197 const syncer::StringOrdinal& new_position) {
198 if (!item->IsInFolder()) {
199 top_level_item_list_->SetItemPosition(item, new_position);
200 // Note: this will trigger OnListItemMoved which will signal observers.
201 // (This is done this way because some View code still moves items within
202 // the item list directly).
203 return;
205 AppListFolderItem* folder = FindFolderItem(item->folder_id());
206 DCHECK(folder);
207 folder->item_list()->SetItemPosition(item, new_position);
208 FOR_EACH_OBSERVER(AppListModelObserver,
209 observers_,
210 OnAppListItemUpdated(item));
213 void AppListModel::SetItemName(AppListItem* item, const std::string& name) {
214 item->SetName(name);
215 DVLOG(2) << "AppListModel::SetItemName: " << item->ToDebugString();
216 FOR_EACH_OBSERVER(AppListModelObserver,
217 observers_,
218 OnAppListItemUpdated(item));
221 void AppListModel::SetItemNameAndShortName(AppListItem* item,
222 const std::string& name,
223 const std::string& short_name) {
224 item->SetNameAndShortName(name, short_name);
225 DVLOG(2) << "AppListModel::SetItemNameAndShortName: "
226 << item->ToDebugString();
227 FOR_EACH_OBSERVER(AppListModelObserver,
228 observers_,
229 OnAppListItemUpdated(item));
232 void AppListModel::DeleteItem(const std::string& id) {
233 AppListItem* item = FindItem(id);
234 if (!item)
235 return;
236 if (!item->IsInFolder()) {
237 DCHECK_EQ(0u, item->ChildItemCount())
238 << "Invalid call to DeleteItem for item with children: " << id;
239 FOR_EACH_OBSERVER(AppListModelObserver,
240 observers_,
241 OnAppListItemWillBeDeleted(item));
242 top_level_item_list_->DeleteItem(id);
243 FOR_EACH_OBSERVER(AppListModelObserver, observers_, OnAppListItemDeleted());
244 return;
246 AppListFolderItem* folder = FindFolderItem(item->folder_id());
247 DCHECK(folder) << "Folder not found for item: " << item->ToDebugString();
248 scoped_ptr<AppListItem> child_item = RemoveItemFromFolder(folder, item);
249 DCHECK_EQ(item, child_item.get());
250 FOR_EACH_OBSERVER(AppListModelObserver,
251 observers_,
252 OnAppListItemWillBeDeleted(item));
253 child_item.reset(); // Deletes item.
254 FOR_EACH_OBSERVER(AppListModelObserver, observers_, OnAppListItemDeleted());
257 void AppListModel::NotifyExtensionPreferenceChanged() {
258 for (size_t i = 0; i < top_level_item_list_->item_count(); ++i)
259 top_level_item_list_->item_at(i)->OnExtensionPreferenceChanged();
262 void AppListModel::SetFoldersEnabled(bool folders_enabled) {
263 folders_enabled_ = folders_enabled;
264 if (folders_enabled)
265 return;
266 // Remove child items from folders.
267 std::vector<std::string> folder_ids;
268 for (size_t i = 0; i < top_level_item_list_->item_count(); ++i) {
269 AppListItem* item = top_level_item_list_->item_at(i);
270 if (item->GetItemType() != AppListFolderItem::kItemType)
271 continue;
272 AppListFolderItem* folder = static_cast<AppListFolderItem*>(item);
273 if (folder->folder_type() == AppListFolderItem::FOLDER_TYPE_OEM)
274 continue; // Do not remove OEM folders.
275 while (folder->item_list()->item_count()) {
276 scoped_ptr<AppListItem> child = folder->item_list()->RemoveItemAt(0);
277 child->set_folder_id("");
278 AddItemToItemListAndNotifyUpdate(child.Pass());
280 folder_ids.push_back(folder->id());
282 // Delete folders.
283 for (size_t i = 0; i < folder_ids.size(); ++i)
284 DeleteItem(folder_ids[i]);
287 std::vector<SearchResult*> AppListModel::FilterSearchResultsByDisplayType(
288 SearchResults* results,
289 SearchResult::DisplayType display_type,
290 size_t max_results) {
291 std::vector<SearchResult*> matches;
292 for (size_t i = 0; i < results->item_count(); ++i) {
293 SearchResult* item = results->GetItemAt(i);
294 if (item->display_type() == display_type) {
295 matches.push_back(item);
296 if (matches.size() == max_results)
297 break;
300 return matches;
303 // Private methods
305 void AppListModel::OnListItemMoved(size_t from_index,
306 size_t to_index,
307 AppListItem* item) {
308 FOR_EACH_OBSERVER(AppListModelObserver,
309 observers_,
310 OnAppListItemUpdated(item));
313 AppListFolderItem* AppListModel::FindOrCreateFolderItem(
314 const std::string& folder_id) {
315 if (folder_id.empty())
316 return NULL;
318 AppListFolderItem* dest_folder = FindFolderItem(folder_id);
319 if (dest_folder)
320 return dest_folder;
322 if (!folders_enabled()) {
323 LOG(ERROR) << "Attempt to create folder item when disabled: " << folder_id;
324 return NULL;
327 DVLOG(2) << "Creating new folder: " << folder_id;
328 scoped_ptr<AppListFolderItem> new_folder(
329 new AppListFolderItem(folder_id, AppListFolderItem::FOLDER_TYPE_NORMAL));
330 new_folder->set_position(
331 top_level_item_list_->CreatePositionBefore(syncer::StringOrdinal()));
332 AppListItem* new_folder_item =
333 AddItemToItemListAndNotify(new_folder.PassAs<AppListItem>());
334 return static_cast<AppListFolderItem*>(new_folder_item);
337 AppListItem* AppListModel::AddItemToItemListAndNotify(
338 scoped_ptr<AppListItem> item_ptr) {
339 DCHECK(!item_ptr->IsInFolder());
340 AppListItem* item = top_level_item_list_->AddItem(item_ptr.Pass());
341 FOR_EACH_OBSERVER(AppListModelObserver,
342 observers_,
343 OnAppListItemAdded(item));
344 return item;
347 AppListItem* AppListModel::AddItemToItemListAndNotifyUpdate(
348 scoped_ptr<AppListItem> item_ptr) {
349 DCHECK(!item_ptr->IsInFolder());
350 AppListItem* item = top_level_item_list_->AddItem(item_ptr.Pass());
351 FOR_EACH_OBSERVER(AppListModelObserver,
352 observers_,
353 OnAppListItemUpdated(item));
354 return item;
357 AppListItem* AppListModel::AddItemToFolderItemAndNotify(
358 AppListFolderItem* folder,
359 scoped_ptr<AppListItem> item_ptr) {
360 AppListItem* item = folder->item_list()->AddItem(item_ptr.Pass());
361 item->set_folder_id(folder->id());
362 FOR_EACH_OBSERVER(AppListModelObserver,
363 observers_,
364 OnAppListItemUpdated(item));
365 return item;
368 scoped_ptr<AppListItem> AppListModel::RemoveItem(AppListItem* item) {
369 if (!item->IsInFolder())
370 return top_level_item_list_->RemoveItem(item->id());
372 AppListFolderItem* folder = FindFolderItem(item->folder_id());
373 return RemoveItemFromFolder(folder, item);
376 scoped_ptr<AppListItem> AppListModel::RemoveItemFromFolder(
377 AppListFolderItem* folder,
378 AppListItem* item) {
379 std::string folder_id = folder->id();
380 DCHECK_EQ(item->folder_id(), folder_id);
381 scoped_ptr<AppListItem> result = folder->item_list()->RemoveItem(item->id());
382 result->set_folder_id("");
383 if (folder->item_list()->item_count() == 0) {
384 DVLOG(2) << "Deleting empty folder: " << folder->ToDebugString();
385 DeleteItem(folder_id);
387 return result.Pass();
390 } // namespace app_list