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.
5 #ifndef UI_APP_LIST_APP_LIST_ITEM_LIST_H_
6 #define UI_APP_LIST_APP_LIST_ITEM_LIST_H_
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"
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
{
27 virtual ~AppListItemList();
29 void AddObserver(AppListItemListObserver
* observer
);
30 void RemoveObserver(AppListItemListObserver
* observer
);
32 // Finds item matching |id|. NOTE: Requires a linear search.
33 AppListItem
* FindItem(const std::string
& id
);
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
);
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(AppListItem
* item
);
44 // Inserts |item| at the |index| into |app_list_items_|. Takes ownership of
45 // |item|. Triggers observers_.OnListItemAdded().
46 void InsertItemAt(AppListItem
* item
, size_t index
);
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
);
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
57 void DeleteItemsByType(const char* type
);
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
<AppListItem
> RemoveItem(const std::string
& id
);
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
<AppListItem
> RemoveItemAt(size_t index
);
67 // Moves item at |from_index| to |to_index|.
68 // Triggers observers_.OnListItemMoved().
69 void MoveItem(size_t from_index
, size_t to_index
);
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(AppListItem
* item
,
74 const syncer::StringOrdinal
& new_position
);
76 AppListItem
* item_at(size_t index
) {
77 DCHECK_LT(index
, app_list_items_
.size());
78 return app_list_items_
[index
];
80 size_t item_count() const { return app_list_items_
.size(); }
83 // Deletes item at |index| and signals observers.
84 void DeleteItemAt(size_t index
);
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(AppListItem
* item
);
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
);
95 ScopedVector
<AppListItem
> app_list_items_
;
96 ObserverList
<AppListItemListObserver
> observers_
;
98 DISALLOW_COPY_AND_ASSIGN(AppListItemList
);
101 } // namespace app_list
103 #endif // UI_APP_LIST_APP_LIST_ITEM_LIST_H_