app_list_model.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef UI_APP_LIST_APP_LIST_MODEL_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define UI_APP_LIST_APP_LIST_MODEL_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/observer_list.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/app_list/app_list_export.h"
121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "ui/app_list/app_list_item_list.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/app_list/app_list_item_list_observer.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/models/list_model.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace app_list {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class AppListFolderItem;
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class AppListItem;
201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)class AppListItemList;
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AppListModelObserver;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SearchBoxModel;
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SearchResult;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Master model of app list that consists of three sub models: AppListItemList,
261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// SearchBoxModel and SearchResults. The AppListItemList sub model owns a list
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// of AppListItems and is displayed in the grid view. SearchBoxModel is
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the model for SearchBoxView. SearchResults owns a list of SearchResult.
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// NOTE: Currently this class observes |item_list_|. The View code may
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// move entries in the item list directly (but can not add or remove them) and
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// the model needs to notify its observers when this occurs.
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class APP_LIST_EXPORT AppListModel : public AppListItemListObserver {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum Status {
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATUS_NORMAL,
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATUS_SYNCING,  // Syncing apps or installing synced apps.
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef ui::ListModel<SearchResult> SearchResults;
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AppListModel();
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~AppListModel();
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AddObserver(AppListModelObserver* observer);
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RemoveObserver(AppListModelObserver* observer);
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetStatus(Status status);
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Finds the item matching |id|.
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AppListItem* FindItem(const std::string& id);
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Find a folder item matching |id|.
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AppListFolderItem* FindFolderItem(const std::string& id);
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Adds |item| to the model. The model takes ownership of |item|. Returns a
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // pointer to the item that is safe to use (e.g. after passing ownership).
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AppListItem* AddItem(scoped_ptr<AppListItem> item);
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Adds |item| to an existing folder or creates a new folder. If |folder_id|
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // is empty, adds the item to the top level model instead. The model takes
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // ownership of |item|. Returns a pointer to the item that is safe to use.
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AppListItem* AddItemToFolder(scoped_ptr<AppListItem> item,
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               const std::string& folder_id);
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Merges two items. If the target item is a folder, the source item is added
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // to the end of the target folder. Otherwise a new folder is created in the
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // same position as the target item with the target item as the first item in
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // the new folder and the source item as the second item. Returns the id of
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // the target folder. The source item may already be in a folder.
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // See also the comment for RemoveItemFromFolder.
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const std::string& MergeItems(const std::string& target_item_id,
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                const std::string& source_item_id);
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Move |item| to the folder matching |folder_id| or to the top level if
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |folder_id| is empty. |item|->position will determine where the item
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // is positioned. See also the comment for RemoveItemFromFolder.
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void MoveItemToFolder(AppListItem* item, const std::string& folder_id);
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Move |item| to the folder matching |folder_id| or to the top level if
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |folder_id| is empty. The item will be inserted before |position| or at
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // the end of the list if |position| is invalid. Note: |position| is copied
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // in case it refers to the containing folder which may get deleted. See also
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // the comment for RemoveItemFromFolder.
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void MoveItemToFolderAt(AppListItem* item,
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                          const std::string& folder_id,
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                          syncer::StringOrdinal position);
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Sets the position of |item| either in |item_list_| or the folder specified
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // by |item|->folder_id().
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SetItemPosition(AppListItem* item,
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       const syncer::StringOrdinal& new_position);
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Deletes the item matching |id| from |item_list_| or from its folder.
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void DeleteItem(const std::string& id);
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  AppListItemList* item_list() { return item_list_.get(); }
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SearchBoxModel* search_box() { return search_box_.get(); }
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SearchResults* results() { return results_.get(); }
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Status status() const { return status_; }
10068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // AppListItemListObserver
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void OnListItemMoved(size_t from_index,
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               size_t to_index,
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               AppListItem* item) OVERRIDE;
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns an existing folder matching |folder_id| or creates a new folder.
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AppListFolderItem* FindOrCreateFolderItem(const std::string& folder_id);
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Adds |item_ptr| to |item_list_| and notifies observers.
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AppListItem* AddItemToItemListAndNotify(
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_ptr<AppListItem> item_ptr);
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Adds |item_ptr| to |item_list_| and notifies observers that an Update
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // occured (e.g. item moved from a folder).
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AppListItem* AddItemToItemListAndNotifyUpdate(
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_ptr<AppListItem> item_ptr);
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Adds |item_ptr| to |folder| and notifies observers.
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AppListItem* AddItemToFolderItemAndNotify(AppListFolderItem* folder,
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                            scoped_ptr<AppListItem> item_ptr);
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Removes |item| from |item_list_| or calls RemoveItemFromFolder if
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |item|->folder_id is set.
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<AppListItem> RemoveItem(AppListItem* item);
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Removes |item| from |folder|. If |folder| becomes empty, deletes |folder|
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // from |item_list_|. Does NOT trigger observers, calling function must do so.
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<AppListItem> RemoveItemFromFolder(AppListFolderItem* folder,
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                               AppListItem* item);
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  scoped_ptr<AppListItemList> item_list_;
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<SearchBoxModel> search_box_;
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<SearchResults> results_;
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Status status_;
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ObserverList<AppListModelObserver> observers_;
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(AppListModel);
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace app_list
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // UI_APP_LIST_APP_LIST_MODEL_H_
145