Home | History | Annotate | Download | only in app_list
      1 // Copyright (c) 2013 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_ITEM_LIST_H_
      6 #define UI_APP_LIST_APP_LIST_ITEM_LIST_H_
      7 
      8 #include <string>
      9 
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/memory/scoped_vector.h"
     12 #include "base/observer_list.h"
     13 #include "sync/api/string_ordinal.h"
     14 #include "ui/app_list/app_list_export.h"
     15 #include "ui/app_list/app_list_item_list_observer.h"
     16 
     17 namespace app_list {
     18 
     19 class AppListItemModel;
     20 
     21 // Class to manage items in the app list. Used both by AppListModel and
     22 // AppListFolderItem. Manages the position ordinal of items in the list, and
     23 // notifies observers when items in the list are added / deleted / moved.
     24 class APP_LIST_EXPORT AppListItemList {
     25  public:
     26   AppListItemList();
     27   virtual ~AppListItemList();
     28 
     29   void AddObserver(AppListItemListObserver* observer);
     30   void RemoveObserver(AppListItemListObserver* observer);
     31 
     32   // Finds item matching |id|. NOTE: Requires a linear search.
     33   AppListItemModel* FindItem(const std::string& id);
     34 
     35   // Finds the |index| of the the item matching |id| in |app_list_items_|.
     36   // Returns true if the matching item is found.
     37   // Note: Requires a linear search.
     38   bool FindItemIndex(const std::string& id, size_t* index);
     39 
     40   // Adds |item| to the end of |app_list_items_|. Takes ownership of |item|.
     41   // Triggers observers_.OnListItemAdded(). Returns the index of the added item.
     42   size_t AddItem(AppListItemModel* item);
     43 
     44   // Inserts |item| at the |index| into |app_list_items_|. Takes ownership of
     45   // |item|. Triggers observers_.OnListItemAdded().
     46   void InsertItemAt(AppListItemModel* item, size_t index);
     47 
     48   // Finds item matching |id| in |app_list_items_| (linear search) and deletes
     49   // it. Triggers observers_.OnListItemRemoved() after removing the item from
     50   // the list and before deleting it.
     51   void DeleteItem(const std::string& id);
     52 
     53   // Deletes all items matching |type| which must be a statically defined
     54   // type descriptor, e.g. AppListFolderItem::kAppType. If |type| is NULL,
     55   // deletes all items. Triggers observers_.OnListItemRemoved() for each item
     56   // as for DeleteItem.
     57   void DeleteItemsByType(const char* type);
     58 
     59   // Removes the item with matching |id| in |app_list_items_| without deleting
     60   // it. Returns a scoped pointer containing the removed item.
     61   scoped_ptr<AppListItemModel> RemoveItem(const std::string& id);
     62 
     63   // Removes the item at |index| from |app_list_items_| without deleting it.
     64   // Returns a scoped pointer containing the removed item.
     65   scoped_ptr<AppListItemModel> RemoveItemAt(size_t index);
     66 
     67   // Moves item at |from_index| to |to_index|.
     68   // Triggers observers_.OnListItemMoved().
     69   void MoveItem(size_t from_index, size_t to_index);
     70 
     71   // Sets the position of |item| which is expected to be a member of
     72   // |app_list_items_| and sorts the list accordingly.
     73   void SetItemPosition(AppListItemModel* item,
     74                        const syncer::StringOrdinal& new_position);
     75 
     76   AppListItemModel* item_at(size_t index) {
     77     DCHECK_LT(index, app_list_items_.size());
     78     return app_list_items_[index];
     79   }
     80   size_t item_count() const { return app_list_items_.size(); }
     81 
     82  private:
     83   // Deletes item at |index| and signals observers.
     84   void DeleteItemAt(size_t index);
     85 
     86   // If |item|->position() is not a valid ordinal, sets |item|->position()
     87   // to a valid ordinal after the last item in the list.
     88   void EnsureValidItemPosition(AppListItemModel* item);
     89 
     90   // Returns the index at which to insert an item in |app_list_items_| based on
     91   // |position| (which must be valid) and |id| (if the positions are equal).
     92   size_t GetItemSortOrderIndex(const syncer::StringOrdinal& position,
     93                                const std::string& id);
     94 
     95   ScopedVector<AppListItemModel> app_list_items_;
     96   ObserverList<AppListItemListObserver> observers_;
     97 
     98   DISALLOW_COPY_AND_ASSIGN(AppListItemList);
     99 };
    100 
    101 }  // namespace app_list
    102 
    103 #endif  // UI_APP_LIST_APP_LIST_ITEM_LIST_H_
    104