OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/app_list/app_list_model.h" | 5 #include "ui/app_list/app_list_model.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "ui/app_list/app_list_folder_item.h" | 9 #include "ui/app_list/app_list_folder_item.h" |
10 #include "ui/app_list/app_list_item.h" | 10 #include "ui/app_list/app_list_item.h" |
11 #include "ui/app_list/app_list_model_observer.h" | 11 #include "ui/app_list/app_list_model_observer.h" |
12 #include "ui/app_list/search_box_model.h" | 12 #include "ui/app_list/search_box_model.h" |
13 #include "ui/app_list/search_result.h" | 13 #include "ui/app_list/search_result.h" |
14 | 14 |
15 namespace app_list { | 15 namespace app_list { |
16 | 16 |
17 AppListModel::AppListModel() | 17 AppListModel::AppListModel() |
18 : top_level_item_list_(new AppListItemList), | 18 : top_level_item_list_(new AppListItemList), |
19 search_box_(new SearchBoxModel), | 19 search_box_(new SearchBoxModel), |
20 results_(new SearchResults), | 20 results_(new SearchResults), |
21 status_(STATUS_NORMAL) { | 21 status_(STATUS_NORMAL), |
22 folders_enabled_(false) { | |
22 top_level_item_list_->AddObserver(this); | 23 top_level_item_list_->AddObserver(this); |
23 } | 24 } |
24 | 25 |
25 AppListModel::~AppListModel() { top_level_item_list_->RemoveObserver(this); } | 26 AppListModel::~AppListModel() { top_level_item_list_->RemoveObserver(this); } |
26 | 27 |
27 void AppListModel::AddObserver(AppListModelObserver* observer) { | 28 void AppListModel::AddObserver(AppListModelObserver* observer) { |
28 observers_.AddObserver(observer); | 29 observers_.AddObserver(observer); |
29 } | 30 } |
30 | 31 |
31 void AppListModel::RemoveObserver(AppListModelObserver* observer) { | 32 void AppListModel::RemoveObserver(AppListModelObserver* observer) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 } | 71 } |
71 | 72 |
72 AppListItem* AppListModel::AddItemToFolder(scoped_ptr<AppListItem> item, | 73 AppListItem* AppListModel::AddItemToFolder(scoped_ptr<AppListItem> item, |
73 const std::string& folder_id) { | 74 const std::string& folder_id) { |
74 if (folder_id.empty()) | 75 if (folder_id.empty()) |
75 return AddItem(item.Pass()); | 76 return AddItem(item.Pass()); |
76 DVLOG(2) << "AddItemToFolder: " << item->id() << ": " << folder_id; | 77 DVLOG(2) << "AddItemToFolder: " << item->id() << ": " << folder_id; |
77 DCHECK(!item->IsInFolder() || item->folder_id() == folder_id); | 78 DCHECK(!item->IsInFolder() || item->folder_id() == folder_id); |
78 DCHECK(item->GetItemType() != AppListFolderItem::kItemType); | 79 DCHECK(item->GetItemType() != AppListFolderItem::kItemType); |
79 AppListFolderItem* dest_folder = FindOrCreateFolderItem(folder_id); | 80 AppListFolderItem* dest_folder = FindOrCreateFolderItem(folder_id); |
81 if (!dest_folder) | |
82 return NULL; | |
80 DCHECK(!dest_folder->item_list()->FindItem(item->id())) | 83 DCHECK(!dest_folder->item_list()->FindItem(item->id())) |
81 << "Already in folder: " << dest_folder->id(); | 84 << "Already in folder: " << dest_folder->id(); |
82 return AddItemToFolderItemAndNotify(dest_folder, item.Pass()); | 85 return AddItemToFolderItemAndNotify(dest_folder, item.Pass()); |
83 } | 86 } |
84 | 87 |
85 const std::string AppListModel::MergeItems(const std::string& target_item_id, | 88 const std::string AppListModel::MergeItems(const std::string& target_item_id, |
86 const std::string& source_item_id) { | 89 const std::string& source_item_id) { |
90 if (!folders_enabled()) { | |
91 LOG(ERROR) << "MergeItems called with folders disabled."; | |
92 return ""; | |
93 } | |
87 DVLOG(2) << "MergeItems: " << source_item_id << " -> " << target_item_id; | 94 DVLOG(2) << "MergeItems: " << source_item_id << " -> " << target_item_id; |
88 // Find the target item. | 95 // Find the target item. |
89 AppListItem* target_item = FindItem(target_item_id); | 96 AppListItem* target_item = FindItem(target_item_id); |
90 if (!target_item) { | 97 if (!target_item) { |
91 LOG(ERROR) << "MergeItems: Target no longer exists."; | 98 LOG(ERROR) << "MergeItems: Target no longer exists."; |
92 return ""; | 99 return ""; |
93 } | 100 } |
94 CHECK(target_item->folder_id().empty()); | 101 CHECK(target_item->folder_id().empty()); |
95 | 102 |
96 AppListItem* source_item = FindItem(source_item_id); | 103 AppListItem* source_item = FindItem(source_item_id); |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
246 OnAppListItemWillBeDeleted(item)); | 253 OnAppListItemWillBeDeleted(item)); |
247 child_item.reset(); // Deletes item. | 254 child_item.reset(); // Deletes item. |
248 FOR_EACH_OBSERVER(AppListModelObserver, observers_, OnAppListItemDeleted()); | 255 FOR_EACH_OBSERVER(AppListModelObserver, observers_, OnAppListItemDeleted()); |
249 } | 256 } |
250 | 257 |
251 void AppListModel::NotifyExtensionPreferenceChanged() { | 258 void AppListModel::NotifyExtensionPreferenceChanged() { |
252 for (size_t i = 0; i < top_level_item_list_->item_count(); ++i) | 259 for (size_t i = 0; i < top_level_item_list_->item_count(); ++i) |
253 top_level_item_list_->item_at(i)->OnExtensionPreferenceChanged(); | 260 top_level_item_list_->item_at(i)->OnExtensionPreferenceChanged(); |
254 } | 261 } |
255 | 262 |
263 void AppListModel::SetFoldersEnabled(bool folders_enabled) { | |
264 folders_enabled_ = folders_enabled; | |
265 if (folders_enabled) | |
266 return; | |
267 // Remove child items from folders. | |
xiyuan
2014/06/02 19:57:49
I am not sure what is the best to do here. Can we
stevenjb
2014/06/02 20:25:11
We are only changing the in-memory model to match
xiyuan
2014/06/02 21:23:26
What if user disables sync then re-enables it in t
| |
268 std::vector<std::string> folder_ids; | |
269 for (size_t i = 0; i < top_level_item_list_->item_count(); ++i) { | |
270 AppListItem* item = top_level_item_list_->item_at(i); | |
271 if (item->GetItemType() != AppListFolderItem::kItemType) | |
272 continue; | |
273 AppListFolderItem* folder = static_cast<AppListFolderItem*>(item); | |
274 if (folder->folder_type() == AppListFolderItem::FOLDER_TYPE_OEM) | |
275 continue; // Do not remove OEM folders. | |
276 while (folder->item_list()->item_count()) { | |
277 scoped_ptr<AppListItem> child = folder->item_list()->RemoveItemAt(0); | |
278 child->set_folder_id(""); | |
279 AddItemToItemListAndNotifyUpdate(child.Pass()); | |
280 } | |
281 folder_ids.push_back(folder->id()); | |
282 } | |
283 // Delete folders. | |
284 for (size_t i = 0; i < folder_ids.size(); ++i) | |
285 DeleteItem(folder_ids[i]); | |
286 } | |
287 | |
256 // Private methods | 288 // Private methods |
257 | 289 |
258 void AppListModel::OnListItemMoved(size_t from_index, | 290 void AppListModel::OnListItemMoved(size_t from_index, |
259 size_t to_index, | 291 size_t to_index, |
260 AppListItem* item) { | 292 AppListItem* item) { |
261 FOR_EACH_OBSERVER(AppListModelObserver, | 293 FOR_EACH_OBSERVER(AppListModelObserver, |
262 observers_, | 294 observers_, |
263 OnAppListItemUpdated(item)); | 295 OnAppListItemUpdated(item)); |
264 } | 296 } |
265 | 297 |
266 AppListFolderItem* AppListModel::FindOrCreateFolderItem( | 298 AppListFolderItem* AppListModel::FindOrCreateFolderItem( |
267 const std::string& folder_id) { | 299 const std::string& folder_id) { |
268 if (folder_id.empty()) | 300 if (folder_id.empty()) |
269 return NULL; | 301 return NULL; |
270 | 302 |
271 AppListFolderItem* dest_folder = FindFolderItem(folder_id); | 303 AppListFolderItem* dest_folder = FindFolderItem(folder_id); |
272 if (dest_folder) | 304 if (dest_folder) |
273 return dest_folder; | 305 return dest_folder; |
274 | 306 |
307 if (!folders_enabled()) { | |
308 LOG(ERROR) << "Attempt to create folder item when disabled: " << folder_id; | |
309 return NULL; | |
310 } | |
311 | |
275 DVLOG(2) << "Creating new folder: " << folder_id; | 312 DVLOG(2) << "Creating new folder: " << folder_id; |
276 scoped_ptr<AppListFolderItem> new_folder( | 313 scoped_ptr<AppListFolderItem> new_folder( |
277 new AppListFolderItem(folder_id, AppListFolderItem::FOLDER_TYPE_NORMAL)); | 314 new AppListFolderItem(folder_id, AppListFolderItem::FOLDER_TYPE_NORMAL)); |
278 new_folder->set_position( | 315 new_folder->set_position( |
279 top_level_item_list_->CreatePositionBefore(syncer::StringOrdinal())); | 316 top_level_item_list_->CreatePositionBefore(syncer::StringOrdinal())); |
280 AppListItem* new_folder_item = | 317 AppListItem* new_folder_item = |
281 AddItemToItemListAndNotify(new_folder.PassAs<AppListItem>()); | 318 AddItemToItemListAndNotify(new_folder.PassAs<AppListItem>()); |
282 return static_cast<AppListFolderItem*>(new_folder_item); | 319 return static_cast<AppListFolderItem*>(new_folder_item); |
283 } | 320 } |
284 | 321 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
329 scoped_ptr<AppListItem> result = folder->item_list()->RemoveItem(item->id()); | 366 scoped_ptr<AppListItem> result = folder->item_list()->RemoveItem(item->id()); |
330 result->set_folder_id(""); | 367 result->set_folder_id(""); |
331 if (folder->item_list()->item_count() == 0) { | 368 if (folder->item_list()->item_count() == 0) { |
332 DVLOG(2) << "Deleting empty folder: " << folder->ToDebugString(); | 369 DVLOG(2) << "Deleting empty folder: " << folder->ToDebugString(); |
333 DeleteItem(folder_id); | 370 DeleteItem(folder_id); |
334 } | 371 } |
335 return result.Pass(); | 372 return result.Pass(); |
336 } | 373 } |
337 | 374 |
338 } // namespace app_list | 375 } // namespace app_list |
OLD | NEW |