Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / app_list / app_list_model.h
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 "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/observer_list.h"
11 #include "ui/app_list/app_list_export.h"
12 #include "ui/app_list/app_list_item_list.h"
13 #include "ui/app_list/app_list_item_list_observer.h"
14 #include "ui/base/models/list_model.h"
15
16 namespace app_list {
17
18 class AppListFolderItem;
19 class AppListItem;
20 class AppListItemList;
21 class AppListModelObserver;
22 class SearchBoxModel;
23 class SearchResult;
24
25 // Master model of app list that consists of three sub models: AppListItemList,
26 // SearchBoxModel and SearchResults. The AppListItemList sub model owns a list
27 // of AppListItems and is displayed in the grid view. SearchBoxModel is
28 // the model for SearchBoxView. SearchResults owns a list of SearchResult.
29 // NOTE: Currently this class observes |item_list_|. The View code may
30 // move entries in the item list directly (but can not add or remove them) and
31 // the model needs to notify its observers when this occurs.
32 class APP_LIST_EXPORT AppListModel : public AppListItemListObserver {
33  public:
34   enum Status {
35     STATUS_NORMAL,
36     STATUS_SYNCING,  // Syncing apps or installing synced apps.
37   };
38
39   typedef ui::ListModel<SearchResult> SearchResults;
40
41   AppListModel();
42   virtual ~AppListModel();
43
44   void AddObserver(AppListModelObserver* observer);
45   void RemoveObserver(AppListModelObserver* observer);
46
47   void SetStatus(Status status);
48
49   // Finds the item matching |id|.
50   AppListItem* FindItem(const std::string& id);
51
52   // Find a folder item matching |id|.
53   AppListFolderItem* FindFolderItem(const std::string& id);
54
55   // Adds |item| to the model. The model takes ownership of |item|. Returns a
56   // pointer to the item that is safe to use (e.g. after passing ownership).
57   AppListItem* AddItem(scoped_ptr<AppListItem> item);
58
59   // Adds |item| to an existing folder or creates a new folder. If |folder_id|
60   // is empty, adds the item to the top level model instead. The model takes
61   // ownership of |item|. Returns a pointer to the item that is safe to use.
62   AppListItem* AddItemToFolder(scoped_ptr<AppListItem> item,
63                                const std::string& folder_id);
64
65   // Merges two items. If the target item is a folder, the source item is added
66   // to the end of the target folder. Otherwise a new folder is created in the
67   // same position as the target item with the target item as the first item in
68   // the new folder and the source item as the second item. Returns the id of
69   // the target folder. The source item may already be in a folder.
70   // See also the comment for RemoveItemFromFolder.
71   const std::string& MergeItems(const std::string& target_item_id,
72                                 const std::string& source_item_id);
73
74   // Move |item| to the folder matching |folder_id| or to the top level if
75   // |folder_id| is empty. |item|->position will determine where the item
76   // is positioned. See also the comment for RemoveItemFromFolder.
77   void MoveItemToFolder(AppListItem* item, const std::string& folder_id);
78
79   // Move |item| to the folder matching |folder_id| or to the top level if
80   // |folder_id| is empty. The item will be inserted before |position| or at
81   // the end of the list if |position| is invalid. Note: |position| is copied
82   // in case it refers to the containing folder which may get deleted. See also
83   // the comment for RemoveItemFromFolder.
84   void MoveItemToFolderAt(AppListItem* item,
85                           const std::string& folder_id,
86                           syncer::StringOrdinal position);
87
88   // Sets the position of |item| either in |item_list_| or the folder specified
89   // by |item|->folder_id().
90   void SetItemPosition(AppListItem* item,
91                        const syncer::StringOrdinal& new_position);
92
93   // Deletes the item matching |id| from |item_list_| or from its folder.
94   void DeleteItem(const std::string& id);
95
96   AppListItemList* item_list() { return item_list_.get(); }
97   SearchBoxModel* search_box() { return search_box_.get(); }
98   SearchResults* results() { return results_.get(); }
99   Status status() const { return status_; }
100
101  private:
102   // AppListItemListObserver
103   virtual void OnListItemMoved(size_t from_index,
104                                size_t to_index,
105                                AppListItem* item) OVERRIDE;
106
107   // Returns an existing folder matching |folder_id| or creates a new folder.
108   AppListFolderItem* FindOrCreateFolderItem(const std::string& folder_id);
109
110   // Adds |item_ptr| to |item_list_| and notifies observers.
111   AppListItem* AddItemToItemListAndNotify(
112       scoped_ptr<AppListItem> item_ptr);
113
114   // Adds |item_ptr| to |item_list_| and notifies observers that an Update
115   // occured (e.g. item moved from a folder).
116   AppListItem* AddItemToItemListAndNotifyUpdate(
117       scoped_ptr<AppListItem> item_ptr);
118
119   // Adds |item_ptr| to |folder| and notifies observers.
120   AppListItem* AddItemToFolderItemAndNotify(AppListFolderItem* folder,
121                                             scoped_ptr<AppListItem> item_ptr);
122
123   // Removes |item| from |item_list_| or calls RemoveItemFromFolder if
124   // |item|->folder_id is set.
125   scoped_ptr<AppListItem> RemoveItem(AppListItem* item);
126
127   // Removes |item| from |folder|. If |folder| becomes empty, deletes |folder|
128   // from |item_list_|. Does NOT trigger observers, calling function must do so.
129   scoped_ptr<AppListItem> RemoveItemFromFolder(AppListFolderItem* folder,
130                                                AppListItem* item);
131
132   scoped_ptr<AppListItemList> item_list_;
133   scoped_ptr<SearchBoxModel> search_box_;
134   scoped_ptr<SearchResults> results_;
135
136   Status status_;
137   ObserverList<AppListModelObserver> observers_;
138
139   DISALLOW_COPY_AND_ASSIGN(AppListModel);
140 };
141
142 }  // namespace app_list
143
144 #endif  // UI_APP_LIST_APP_LIST_MODEL_H_