| 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 "ui/app_list/app_list_folder_item.h" |
| 7 #include "ui/app_list/app_list_item.h" | 8 #include "ui/app_list/app_list_item.h" |
| 8 #include "ui/app_list/app_list_model_observer.h" | 9 #include "ui/app_list/app_list_model_observer.h" |
| 9 #include "ui/app_list/search_box_model.h" | 10 #include "ui/app_list/search_box_model.h" |
| 10 #include "ui/app_list/search_result.h" | 11 #include "ui/app_list/search_result.h" |
| 11 | 12 |
| 12 namespace app_list { | 13 namespace app_list { |
| 13 | 14 |
| 14 AppListModel::AppListModel() | 15 AppListModel::AppListModel() |
| 15 : item_list_(new AppListItemList), | 16 : item_list_(new AppListItemList), |
| 16 search_box_(new SearchBoxModel), | 17 search_box_(new SearchBoxModel), |
| 17 results_(new SearchResults), | 18 results_(new SearchResults), |
| 18 status_(STATUS_NORMAL) { | 19 status_(STATUS_NORMAL) { |
| 20 item_list_->AddObserver(this); |
| 19 } | 21 } |
| 20 | 22 |
| 21 AppListModel::~AppListModel() { | 23 AppListModel::~AppListModel() { |
| 24 item_list_->RemoveObserver(this); |
| 22 } | 25 } |
| 23 | 26 |
| 24 void AppListModel::AddObserver(AppListModelObserver* observer) { | 27 void AppListModel::AddObserver(AppListModelObserver* observer) { |
| 25 observers_.AddObserver(observer); | 28 observers_.AddObserver(observer); |
| 26 } | 29 } |
| 27 | 30 |
| 28 void AppListModel::RemoveObserver(AppListModelObserver* observer) { | 31 void AppListModel::RemoveObserver(AppListModelObserver* observer) { |
| 29 observers_.RemoveObserver(observer); | 32 observers_.RemoveObserver(observer); |
| 30 } | 33 } |
| 31 | 34 |
| 32 void AppListModel::SetStatus(Status status) { | 35 void AppListModel::SetStatus(Status status) { |
| 33 if (status_ == status) | 36 if (status_ == status) |
| 34 return; | 37 return; |
| 35 | 38 |
| 36 status_ = status; | 39 status_ = status; |
| 37 FOR_EACH_OBSERVER(AppListModelObserver, | 40 FOR_EACH_OBSERVER(AppListModelObserver, |
| 38 observers_, | 41 observers_, |
| 39 OnAppListModelStatusChanged()); | 42 OnAppListModelStatusChanged()); |
| 40 } | 43 } |
| 41 | 44 |
| 45 AppListItem* AppListModel::FindItem(const std::string& id) { |
| 46 return item_list_->FindItem(id); |
| 47 } |
| 48 |
| 49 void AppListModel::AddItem(AppListItem* item) { |
| 50 DCHECK(!item_list()->FindItem(item->id())); |
| 51 AddItemToItemList(item); |
| 52 } |
| 53 |
| 54 const std::string& AppListModel::MergeItems(const std::string& target_item_id, |
| 55 const std::string& source_item_id) { |
| 56 // First, remove the source item from |item_list_|. |
| 57 scoped_ptr<AppListItem> source_item_ptr = |
| 58 item_list_->RemoveItem(source_item_id); |
| 59 |
| 60 // Next, find the target item. |
| 61 AppListItem* target_item = item_list_->FindItem(target_item_id); |
| 62 DCHECK(target_item); |
| 63 |
| 64 // If the target item is a folder, just add |source_item| to it. |
| 65 if (target_item->GetItemType() == AppListFolderItem::kItemType) { |
| 66 AppListFolderItem* target_folder = |
| 67 static_cast<AppListFolderItem*>(target_item); |
| 68 AddItemToFolder(target_folder, source_item_ptr.release()); |
| 69 return target_folder->id(); |
| 70 } |
| 71 |
| 72 // Otherwise, remove the target item from |item_list_|, it will become owned |
| 73 // by the new folder. |
| 74 scoped_ptr<AppListItem> target_item_ptr = |
| 75 item_list_->RemoveItem(target_item_id); |
| 76 |
| 77 // Create a new folder in the same location as the target item. |
| 78 AppListFolderItem* new_folder = |
| 79 new AppListFolderItem(AppListFolderItem::GenerateId()); |
| 80 new_folder->set_position(target_item->position()); |
| 81 AddItemToItemList(new_folder); |
| 82 |
| 83 // Add the items to the new folder. |
| 84 AddItemToFolder(new_folder, target_item_ptr.release()); |
| 85 AddItemToFolder(new_folder, source_item_ptr.release()); |
| 86 |
| 87 return new_folder->id(); |
| 88 } |
| 89 |
| 90 void AppListModel::SetItemPosition(AppListItem* item, |
| 91 const syncer::StringOrdinal& new_position) { |
| 92 item_list_->SetItemPosition(item, new_position); |
| 93 // Note: this will trigger OnListItemMoved which will signal observers. |
| 94 // (This is done this way because some View code still moves items within |
| 95 // the item list directly). |
| 96 } |
| 97 |
| 98 void AppListModel::DeleteItem(const std::string& id) { |
| 99 AppListItem* item = FindItem(id); |
| 100 if (!item) |
| 101 return; |
| 102 FOR_EACH_OBSERVER(AppListModelObserver, |
| 103 observers_, |
| 104 OnAppListItemWillBeDeleted(item)); |
| 105 item_list_->DeleteItem(id); |
| 106 } |
| 107 |
| 108 // Private methods |
| 109 |
| 110 void AppListModel::OnListItemMoved(size_t from_index, |
| 111 size_t to_index, |
| 112 AppListItem* item) { |
| 113 FOR_EACH_OBSERVER(AppListModelObserver, |
| 114 observers_, |
| 115 OnAppListItemUpdated(item)); |
| 116 } |
| 117 |
| 118 syncer::StringOrdinal AppListModel::GetLastItemPosition() { |
| 119 size_t count = item_list_->item_count(); |
| 120 if (count == 0) |
| 121 return syncer::StringOrdinal::CreateInitialOrdinal(); |
| 122 return item_list_->item_at(count - 1)->position(); |
| 123 } |
| 124 |
| 125 void AppListModel::AddItemToItemList(AppListItem* item) { |
| 126 item_list_->AddItem(item); |
| 127 FOR_EACH_OBSERVER(AppListModelObserver, |
| 128 observers_, |
| 129 OnAppListItemAdded(item)); |
| 130 } |
| 131 |
| 132 void AppListModel::AddItemToFolder(AppListFolderItem* folder, |
| 133 AppListItem* item) { |
| 134 folder->item_list()->AddItem(item); |
| 135 } |
| 136 |
| 42 } // namespace app_list | 137 } // namespace app_list |
| OLD | NEW |