app_list_model.cc revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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#include "ui/app_list/app_list_model.h"
6
7#include "ui/app_list/app_list_item_model.h"
8#include "ui/app_list/app_list_model_observer.h"
9#include "ui/app_list/search_box_model.h"
10#include "ui/app_list/search_result.h"
11
12namespace app_list {
13
14AppListModel::User::User() : active(false) {}
15
16AppListModel::User::~User() {}
17
18AppListModel::AppListModel()
19    : apps_(new Apps),
20      search_box_(new SearchBoxModel),
21      results_(new SearchResults),
22      signed_in_(false),
23      status_(STATUS_NORMAL) {
24}
25
26AppListModel::~AppListModel() {
27}
28
29void AppListModel::AddObserver(AppListModelObserver* observer) {
30  observers_.AddObserver(observer);
31}
32
33void AppListModel::RemoveObserver(AppListModelObserver* observer) {
34  observers_.RemoveObserver(observer);
35}
36
37void AppListModel::SetStatus(Status status) {
38  if (status_ == status)
39    return;
40
41  status_ = status;
42  FOR_EACH_OBSERVER(AppListModelObserver,
43                    observers_,
44                    OnAppListModelStatusChanged());
45}
46
47void AppListModel::SetUsers(const Users& users) {
48  users_ = users;
49  FOR_EACH_OBSERVER(AppListModelObserver,
50                    observers_,
51                    OnAppListModelUsersChanged());
52}
53
54void AppListModel::SetSignedIn(bool signed_in) {
55  if (signed_in_ == signed_in)
56    return;
57
58  signed_in_ = signed_in;
59  FOR_EACH_OBSERVER(AppListModelObserver,
60                    observers_,
61                    OnAppListModelSigninStatusChanged());
62}
63
64AppListItemModel* AppListModel::FindItem(const std::string& id) {
65  for (size_t i = 0; i < apps_->item_count(); ++i) {
66    AppListItemModel* item = apps_->GetItemAt(i);
67    if (item->id() == id)
68      return item;
69  }
70  return NULL;
71}
72
73void AppListModel::AddItem(AppListItemModel* item) {
74  std::string sort_order = item->GetSortOrder();
75  // Note: ui::ListModel is not a sorted list.
76  size_t index = 0;
77  for (; index < apps_->item_count(); ++index) {
78    if (sort_order < apps_->GetItemAt(index)->GetSortOrder())
79      break;
80  }
81  apps_->AddAt(index, item);
82}
83
84void AppListModel::DeleteItem(const std::string& id) {
85  for (size_t i = 0; i < apps_->item_count(); ++i) {
86    AppListItemModel* item = apps_->GetItemAt(i);
87    if (item->id() == id) {
88      apps_->DeleteAt(i);
89      return;
90    }
91  }
92}
93
94}  // namespace app_list
95