Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / bookmarks / recently_used_folders_combo_model.cc
blobdfcedf0c89d6f74d21d23a3cc8296295a8f8b0fc
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.
5 #include "chrome/browser/ui/bookmarks/recently_used_folders_combo_model.h"
7 #include "chrome/browser/bookmarks/bookmark_model.h"
8 #include "chrome/browser/bookmarks/bookmark_utils.h"
9 #include "content/public/browser/user_metrics.h"
10 #include "grit/generated_resources.h"
11 #include "ui/base/l10n/l10n_util.h"
12 #include "ui/base/models/combobox_model_observer.h"
14 namespace {
16 // Max number of most recently used folders.
17 const size_t kMaxMRUFolders = 5;
19 } // namespace
21 struct RecentlyUsedFoldersComboModel::Item {
22 enum Type {
23 TYPE_NODE,
24 TYPE_SEPARATOR,
25 TYPE_CHOOSE_ANOTHER_FOLDER
28 Item(const BookmarkNode* node, Type type);
29 ~Item();
31 bool operator==(const Item& item) const;
33 const BookmarkNode* node;
34 Type type;
37 RecentlyUsedFoldersComboModel::Item::Item(const BookmarkNode* node,
38 Type type)
39 : node(node),
40 type(type) {
43 RecentlyUsedFoldersComboModel::Item::~Item() {}
45 bool RecentlyUsedFoldersComboModel::Item::operator==(const Item& item) const {
46 return item.node == node && item.type == type;
49 RecentlyUsedFoldersComboModel::RecentlyUsedFoldersComboModel(
50 BookmarkModel* model,
51 const BookmarkNode* node)
52 : bookmark_model_(model),
53 node_parent_index_(0) {
54 bookmark_model_->AddObserver(this);
55 // Use + 2 to account for bookmark bar and other node.
56 std::vector<const BookmarkNode*> nodes =
57 bookmark_utils::GetMostRecentlyModifiedFolders(model, kMaxMRUFolders + 2);
59 for (size_t i = 0; i < nodes.size(); ++i)
60 items_.push_back(Item(nodes[i], Item::TYPE_NODE));
62 // We special case the placement of these, so remove them from the list, then
63 // fix up the order.
64 RemoveNode(model->bookmark_bar_node());
65 RemoveNode(model->mobile_node());
66 RemoveNode(model->other_node());
67 RemoveNode(node->parent());
69 // Make the parent the first item, unless it's a permanent node, which is
70 // added below.
71 if (!model->is_permanent_node(node->parent()))
72 items_.insert(items_.begin(), Item(node->parent(), Item::TYPE_NODE));
74 // Make sure we only have kMaxMRUFolders in the first chunk.
75 if (items_.size() > kMaxMRUFolders)
76 items_.erase(items_.begin() + kMaxMRUFolders, items_.end());
78 // And put the bookmark bar and other nodes at the end of the list.
79 items_.push_back(Item(model->bookmark_bar_node(), Item::TYPE_NODE));
80 items_.push_back(Item(model->other_node(), Item::TYPE_NODE));
81 if (model->mobile_node()->IsVisible())
82 items_.push_back(Item(model->mobile_node(), Item::TYPE_NODE));
83 #if defined(USE_AURA) || defined(TOOLKIT_GTK)
84 items_.push_back(Item(NULL, Item::TYPE_SEPARATOR));
85 #endif
86 items_.push_back(Item(NULL, Item::TYPE_CHOOSE_ANOTHER_FOLDER));
88 std::vector<Item>::iterator it = std::find(items_.begin(),
89 items_.end(),
90 Item(node->parent(),
91 Item::TYPE_NODE));
92 node_parent_index_ = static_cast<int>(it - items_.begin());
95 RecentlyUsedFoldersComboModel::~RecentlyUsedFoldersComboModel() {
96 bookmark_model_->RemoveObserver(this);
99 int RecentlyUsedFoldersComboModel::GetItemCount() const {
100 return static_cast<int>(items_.size());
103 base::string16 RecentlyUsedFoldersComboModel::GetItemAt(int index) {
104 switch (items_[index].type) {
105 case Item::TYPE_NODE:
106 return items_[index].node->GetTitle();
107 case Item::TYPE_SEPARATOR:
108 // This function should not be called for separators.
109 NOTREACHED();
110 return base::string16();
111 case Item::TYPE_CHOOSE_ANOTHER_FOLDER:
112 return l10n_util::GetStringUTF16(
113 IDS_BOOKMARK_BUBBLE_CHOOSER_ANOTHER_FOLDER);
115 NOTREACHED();
116 return base::string16();
119 bool RecentlyUsedFoldersComboModel::IsItemSeparatorAt(int index) {
120 return items_[index].type == Item::TYPE_SEPARATOR;
123 int RecentlyUsedFoldersComboModel::GetDefaultIndex() const {
124 return node_parent_index_;
127 void RecentlyUsedFoldersComboModel::AddObserver(
128 ui::ComboboxModelObserver* observer) {
129 observers_.AddObserver(observer);
132 void RecentlyUsedFoldersComboModel::RemoveObserver(
133 ui::ComboboxModelObserver* observer) {
134 observers_.RemoveObserver(observer);
137 void RecentlyUsedFoldersComboModel::BookmarkModelLoaded(BookmarkModel* model,
138 bool ids_reassigned) {}
140 void RecentlyUsedFoldersComboModel::BookmarkModelBeingDeleted(
141 BookmarkModel* model) {
144 void RecentlyUsedFoldersComboModel::BookmarkNodeMoved(
145 BookmarkModel* model,
146 const BookmarkNode* old_parent,
147 int old_index,
148 const BookmarkNode* new_parent,
149 int new_index) {
152 void RecentlyUsedFoldersComboModel::BookmarkNodeAdded(
153 BookmarkModel* model,
154 const BookmarkNode* parent,
155 int index) {
158 void RecentlyUsedFoldersComboModel::OnWillRemoveBookmarks(
159 BookmarkModel* model,
160 const BookmarkNode* parent,
161 int old_index,
162 const BookmarkNode* node) {
163 // Changing is rare enough that we don't attempt to readjust the contents.
164 // Update |items_| so we aren't left pointing to a deleted node.
165 bool changed = false;
166 for (std::vector<Item>::iterator i = items_.begin();
167 i != items_.end();) {
168 if (i->type == Item::TYPE_NODE && i->node->HasAncestor(node)) {
169 i = items_.erase(i);
170 changed = true;
171 } else {
172 ++i;
175 if (changed) {
176 FOR_EACH_OBSERVER(ui::ComboboxModelObserver, observers_,
177 OnComboboxModelChanged(this));
181 void RecentlyUsedFoldersComboModel::BookmarkNodeRemoved(
182 BookmarkModel* model,
183 const BookmarkNode* parent,
184 int old_index,
185 const BookmarkNode* node) {
188 void RecentlyUsedFoldersComboModel::BookmarkNodeChanged(
189 BookmarkModel* model,
190 const BookmarkNode* node) {
193 void RecentlyUsedFoldersComboModel::BookmarkNodeFaviconChanged(
194 BookmarkModel* model,
195 const BookmarkNode* node) {
198 void RecentlyUsedFoldersComboModel::BookmarkNodeChildrenReordered(
199 BookmarkModel* model,
200 const BookmarkNode* node) {
203 void RecentlyUsedFoldersComboModel::BookmarkAllNodesRemoved(
204 BookmarkModel* model) {
205 // Changing is rare enough that we don't attempt to readjust the contents.
206 // Update |items_| so we aren't left pointing to a deleted node.
207 bool changed = false;
208 for (std::vector<Item>::iterator i = items_.begin();
209 i != items_.end();) {
210 if (i->type == Item::TYPE_NODE &&
211 !bookmark_model_->is_permanent_node(i->node)) {
212 i = items_.erase(i);
213 changed = true;
214 } else {
215 ++i;
218 if (changed) {
219 FOR_EACH_OBSERVER(ui::ComboboxModelObserver, observers_,
220 OnComboboxModelChanged(this));
224 void RecentlyUsedFoldersComboModel::MaybeChangeParent(
225 const BookmarkNode* node,
226 int selected_index) {
227 if (items_[selected_index].type != Item::TYPE_NODE)
228 return;
230 const BookmarkNode* new_parent = GetNodeAt(selected_index);
231 if (new_parent != node->parent()) {
232 content::RecordAction(
233 base::UserMetricsAction("BookmarkBubble_ChangeParent"));
234 bookmark_model_->Move(node, new_parent, new_parent->child_count());
238 const BookmarkNode* RecentlyUsedFoldersComboModel::GetNodeAt(int index) {
239 if (index < 0 || index >= static_cast<int>(items_.size()))
240 return NULL;
241 return items_[index].node;
244 void RecentlyUsedFoldersComboModel::RemoveNode(const BookmarkNode* node) {
245 std::vector<Item>::iterator it = std::find(items_.begin(),
246 items_.end(),
247 Item(node, Item::TYPE_NODE));
248 if (it != items_.end())
249 items_.erase(it);