1// Copyright 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 ASH_SHELF_SHELF_MODEL_H_
6#define ASH_SHELF_SHELF_MODEL_H_
7
8#include "ash/ash_export.h"
9#include "ash/launcher/launcher_types.h"
10#include "base/basictypes.h"
11#include "base/observer_list.h"
12
13namespace ash {
14
15class ShelfModelObserver;
16
17// Model used by ShelfView.
18class ASH_EXPORT ShelfModel {
19 public:
20  enum Status {
21    STATUS_NORMAL,
22    // A status that indicates apps are syncing/loading.
23    STATUS_LOADING,
24  };
25
26  ShelfModel();
27  ~ShelfModel();
28
29  // Adds a new item to the model. Returns the resulting index.
30  int Add(const LauncherItem& item);
31
32  // Adds the item. |index| is the requested insertion index, which may be
33  // modified to meet type-based ordering. Returns the actual insertion index.
34  int AddAt(int index, const LauncherItem& item);
35
36  // Removes the item at |index|.
37  void RemoveItemAt(int index);
38
39  // Moves the item at |index| to |target_index|. |target_index| is in terms
40  // of the model *after* the item at |index| is removed.
41  void Move(int index, int target_index);
42
43  // Resets the item at the specified index. The item maintains its existing
44  // id and type.
45  void Set(int index, const LauncherItem& item);
46
47  // Returns the index of the item by id.
48  int ItemIndexByID(LauncherID id) const;
49
50  // Returns the |index| of the item matching |type| in |items_|.
51  // Returns -1 if the matching item is not found.
52  // Note: Requires a linear search.
53  int GetItemIndexForType(LauncherItemType type);
54
55  // Returns the index of the first panel or the index where the first panel
56  // would go if there are no panels.
57  int FirstPanelIndex() const;
58
59  // Returns the id assigned to the next item added.
60  LauncherID next_id() const { return next_id_; }
61
62  // Returns a reserved id which will not be used by the |ShelfModel|.
63  LauncherID reserve_external_id() { return next_id_++; }
64
65  // Returns an iterator into items() for the item with the specified id, or
66  // items().end() if there is no item with the specified id.
67  LauncherItems::const_iterator ItemByID(LauncherID id) const;
68
69  const LauncherItems& items() const { return items_; }
70  int item_count() const { return static_cast<int>(items_.size()); }
71
72  void SetStatus(Status status);
73  Status status() const { return status_; }
74
75  void AddObserver(ShelfModelObserver* observer);
76  void RemoveObserver(ShelfModelObserver* observer);
77
78 private:
79  // Makes sure |index| is in line with the type-based order of items. If that
80  // is not the case, adjusts index by shifting it to the valid range and
81  // returns the new value.
82  int ValidateInsertionIndex(LauncherItemType type, int index) const;
83
84  // ID assigned to the next item.
85  LauncherID next_id_;
86
87  LauncherItems items_;
88  Status status_;
89  ObserverList<ShelfModelObserver> observers_;
90
91  DISALLOW_COPY_AND_ASSIGN(ShelfModel);
92};
93
94}  // namespace ash
95
96#endif  // ASH_SHELF_SHELF_MODEL_H_
97