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.
4
5#ifndef UI_APP_LIST_APP_LIST_MODEL_H_
6#define UI_APP_LIST_APP_LIST_MODEL_H_
7
8#include <string>
9#include <vector>
10
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"
19
20namespace app_list {
21
22class AppListFolderItem;
23class AppListItem;
24class AppListItemList;
25class AppListModelObserver;
26class SearchBoxModel;
27
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.
35class APP_LIST_EXPORT AppListModel : public AppListItemListObserver {
36 public:
37  enum Status {
38    STATUS_NORMAL,
39    STATUS_SYNCING,  // Syncing apps or installing synced apps.
40  };
41
42  typedef ui::ListModel<SearchResult> SearchResults;
43
44  AppListModel();
45  virtual ~AppListModel();
46
47  void AddObserver(AppListModelObserver* observer);
48  void RemoveObserver(AppListModelObserver* observer);
49
50  void SetStatus(Status status);
51
52  // Finds the item matching |id|.
53  AppListItem* FindItem(const std::string& id);
54
55  // Find a folder item matching |id|.
56  AppListFolderItem* FindFolderItem(const std::string& id);
57
58  // Adds |item| to the model. The model takes ownership of |item|. Returns a
59  // pointer to the item that is safe to use (e.g. after passing ownership).
60  AppListItem* AddItem(scoped_ptr<AppListItem> item);
61
62  // Adds |item| to an existing folder or creates a new folder. If |folder_id|
63  // is empty, adds the item to the top level model instead. The model takes
64  // ownership of |item|. Returns a pointer to the item that is safe to use.
65  AppListItem* AddItemToFolder(scoped_ptr<AppListItem> item,
66                               const std::string& folder_id);
67
68  // Merges two items. If the target item is a folder, the source item is added
69  // to the end of the target folder. Otherwise a new folder is created in the
70  // same position as the target item with the target item as the first item in
71  // the new folder and the source item as the second item. Returns the id of
72  // the target folder, or an empty string if the merge failed. The source item
73  // may already be in a folder. See also the comment for RemoveItemFromFolder.
74  // NOTE: This should only be called by the View code (not the sync code); it
75  // enforces folder restrictions (e.g. the target can not be an OEM folder).
76  const std::string MergeItems(const std::string& target_item_id,
77                               const std::string& source_item_id);
78
79  // Move |item| to the folder matching |folder_id| or to the top level if
80  // |folder_id| is empty. |item|->position will determine where the item
81  // is positioned. See also the comment for RemoveItemFromFolder.
82  void MoveItemToFolder(AppListItem* item, const std::string& folder_id);
83
84  // Move |item| to the folder matching |folder_id| or to the top level if
85  // |folder_id| is empty. The item will be inserted before |position| or at
86  // the end of the list if |position| is invalid. Note: |position| is copied
87  // in case it refers to the containing folder which may get deleted. See also
88  // the comment for RemoveItemFromFolder. Returns true if the item was moved.
89  // NOTE: This should only be called by the View code (not the sync code); it
90  // enforces folder restrictions (e.g. the source folder can not be type OEM).
91  bool MoveItemToFolderAt(AppListItem* item,
92                          const std::string& folder_id,
93                          syncer::StringOrdinal position);
94
95  // Sets the position of |item| either in |top_level_item_list_| or the folder
96  // specified by |item|->folder_id(). If |new_position| is invalid, move the
97  // item to the end of the list.
98  void SetItemPosition(AppListItem* item,
99                       const syncer::StringOrdinal& new_position);
100
101  // Sets the name of |item| and notifies observers.
102  void SetItemName(AppListItem* item, const std::string& name);
103
104  // Sets the name and short name of |item| and notifies observers.
105  void SetItemNameAndShortName(AppListItem* item,
106                               const std::string& name,
107                               const std::string& short_name);
108
109  // Deletes the item matching |id| from |top_level_item_list_| or from the
110  // appropriate folder.
111  void DeleteItem(const std::string& id);
112
113  // Call OnExtensionPreferenceChanged() for all items in the model.
114  void NotifyExtensionPreferenceChanged();
115
116  // Sets wither or not folder UI should be enabled. If |folders_enabled| is
117  // false, removes any non-OEM folders.
118  void SetFoldersEnabled(bool folders_enabled);
119
120  // Filters the given |results| by |display_type|. The returned list is
121  // truncated to |max_results|.
122  static std::vector<SearchResult*> FilterSearchResultsByDisplayType(
123      SearchResults* results,
124      SearchResult::DisplayType display_type,
125      size_t max_results);
126
127  AppListItemList* top_level_item_list() { return top_level_item_list_.get(); }
128
129  SearchBoxModel* search_box() { return search_box_.get(); }
130  SearchResults* results() { return results_.get(); }
131  Status status() const { return status_; }
132  bool folders_enabled() const { return folders_enabled_; }
133
134 private:
135  // AppListItemListObserver
136  virtual void OnListItemMoved(size_t from_index,
137                               size_t to_index,
138                               AppListItem* item) OVERRIDE;
139
140  // Returns an existing folder matching |folder_id| or creates a new folder.
141  AppListFolderItem* FindOrCreateFolderItem(const std::string& folder_id);
142
143  // Adds |item_ptr| to |top_level_item_list_| and notifies observers.
144  AppListItem* AddItemToItemListAndNotify(
145      scoped_ptr<AppListItem> item_ptr);
146
147  // Adds |item_ptr| to |top_level_item_list_| and notifies observers that an
148  // Update occured (e.g. item moved from a folder).
149  AppListItem* AddItemToItemListAndNotifyUpdate(
150      scoped_ptr<AppListItem> item_ptr);
151
152  // Adds |item_ptr| to |folder| and notifies observers.
153  AppListItem* AddItemToFolderItemAndNotify(AppListFolderItem* folder,
154                                            scoped_ptr<AppListItem> item_ptr);
155
156  // Removes |item| from |top_level_item_list_| or calls RemoveItemFromFolder if
157  // |item|->folder_id is set.
158  scoped_ptr<AppListItem> RemoveItem(AppListItem* item);
159
160  // Removes |item| from |folder|. If |folder| becomes empty, deletes |folder|
161  // from |top_level_item_list_|. Does NOT trigger observers, calling function
162  // must do so.
163  scoped_ptr<AppListItem> RemoveItemFromFolder(AppListFolderItem* folder,
164                                               AppListItem* item);
165
166  scoped_ptr<AppListItemList> top_level_item_list_;
167
168  scoped_ptr<SearchBoxModel> search_box_;
169  scoped_ptr<SearchResults> results_;
170
171  Status status_;
172  ObserverList<AppListModelObserver, true> observers_;
173  bool folders_enabled_;
174
175  DISALLOW_COPY_AND_ASSIGN(AppListModel);
176};
177
178}  // namespace app_list
179
180#endif  // UI_APP_LIST_APP_LIST_MODEL_H_
181