| OLD | NEW |
| (Empty) |
| 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 "chrome/browser/bookmarks/recently_used_folders_combo_model.h" | |
| 6 | |
| 7 #include "chrome/browser/bookmarks/bookmark_utils.h" | |
| 8 #include "content/public/browser/user_metrics.h" | |
| 9 #include "grit/generated_resources.h" | |
| 10 #include "ui/base/l10n/l10n_util.h" | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 // Max number of most recently used folders. | |
| 15 const size_t kMaxMRUFolders = 5; | |
| 16 | |
| 17 } // namespace | |
| 18 | |
| 19 struct RecentlyUsedFoldersComboModel::Item { | |
| 20 enum Type { | |
| 21 TYPE_NODE, | |
| 22 TYPE_SEPARATOR, | |
| 23 TYPE_CHOOSE_ANOTHER_FOLDER | |
| 24 }; | |
| 25 | |
| 26 Item(const BookmarkNode* node, Type type); | |
| 27 ~Item(); | |
| 28 | |
| 29 bool operator==(const Item& item) const; | |
| 30 | |
| 31 const BookmarkNode* node; | |
| 32 Type type; | |
| 33 }; | |
| 34 | |
| 35 RecentlyUsedFoldersComboModel::Item::Item(const BookmarkNode* node, | |
| 36 Type type) | |
| 37 : node(node), | |
| 38 type(type) { | |
| 39 } | |
| 40 | |
| 41 RecentlyUsedFoldersComboModel::Item::~Item() {} | |
| 42 | |
| 43 bool RecentlyUsedFoldersComboModel::Item::operator==(const Item& item) const { | |
| 44 return item.node == node && item.type == type; | |
| 45 } | |
| 46 | |
| 47 RecentlyUsedFoldersComboModel::RecentlyUsedFoldersComboModel( | |
| 48 BookmarkModel* model, | |
| 49 const BookmarkNode* node) | |
| 50 : bookmark_model_(model), | |
| 51 node_parent_index_(0) { | |
| 52 // Use + 2 to account for bookmark bar and other node. | |
| 53 std::vector<const BookmarkNode*> nodes = | |
| 54 bookmark_utils::GetMostRecentlyModifiedFolders(model, kMaxMRUFolders + 2); | |
| 55 | |
| 56 for (size_t i = 0; i < nodes.size(); ++i) | |
| 57 items_.push_back(Item(nodes[i], Item::TYPE_NODE)); | |
| 58 | |
| 59 // We special case the placement of these, so remove them from the list, then | |
| 60 // fix up the order. | |
| 61 RemoveNode(model->bookmark_bar_node()); | |
| 62 RemoveNode(model->mobile_node()); | |
| 63 RemoveNode(model->other_node()); | |
| 64 RemoveNode(node->parent()); | |
| 65 | |
| 66 // Make the parent the first item, unless it's a permanent node, which is | |
| 67 // added below. | |
| 68 if (!model->is_permanent_node(node->parent())) | |
| 69 items_.insert(items_.begin(), Item(node->parent(), Item::TYPE_NODE)); | |
| 70 | |
| 71 // Make sure we only have kMaxMRUFolders in the first chunk. | |
| 72 if (items_.size() > kMaxMRUFolders) | |
| 73 items_.erase(items_.begin() + kMaxMRUFolders, items_.end()); | |
| 74 | |
| 75 // And put the bookmark bar and other nodes at the end of the list. | |
| 76 items_.push_back(Item(model->bookmark_bar_node(), Item::TYPE_NODE)); | |
| 77 items_.push_back(Item(model->other_node(), Item::TYPE_NODE)); | |
| 78 if (model->mobile_node()->IsVisible()) | |
| 79 items_.push_back(Item(model->mobile_node(), Item::TYPE_NODE)); | |
| 80 #if defined(USE_AURA) || defined(TOOLKIT_GTK) | |
| 81 items_.push_back(Item(NULL, Item::TYPE_SEPARATOR)); | |
| 82 #endif | |
| 83 items_.push_back(Item(NULL, Item::TYPE_CHOOSE_ANOTHER_FOLDER)); | |
| 84 | |
| 85 std::vector<Item>::iterator it = std::find(items_.begin(), | |
| 86 items_.end(), | |
| 87 Item(node->parent(), | |
| 88 Item::TYPE_NODE)); | |
| 89 node_parent_index_ = static_cast<int>(it - items_.begin()); | |
| 90 } | |
| 91 | |
| 92 RecentlyUsedFoldersComboModel::~RecentlyUsedFoldersComboModel() {} | |
| 93 | |
| 94 int RecentlyUsedFoldersComboModel::GetItemCount() const { | |
| 95 return static_cast<int>(items_.size()); | |
| 96 } | |
| 97 | |
| 98 string16 RecentlyUsedFoldersComboModel::GetItemAt(int index) { | |
| 99 switch (items_[index].type) { | |
| 100 case Item::TYPE_NODE: | |
| 101 return items_[index].node->GetTitle(); | |
| 102 case Item::TYPE_SEPARATOR: | |
| 103 // This function should not be called for separators. | |
| 104 NOTREACHED(); | |
| 105 return string16(); | |
| 106 case Item::TYPE_CHOOSE_ANOTHER_FOLDER: | |
| 107 return l10n_util::GetStringUTF16( | |
| 108 IDS_BOOKMARK_BUBBLE_CHOOSER_ANOTHER_FOLDER); | |
| 109 } | |
| 110 NOTREACHED(); | |
| 111 return string16(); | |
| 112 } | |
| 113 | |
| 114 bool RecentlyUsedFoldersComboModel::IsItemSeparatorAt(int index) { | |
| 115 return items_[index].type == Item::TYPE_SEPARATOR; | |
| 116 } | |
| 117 | |
| 118 int RecentlyUsedFoldersComboModel::GetDefaultIndex() const { | |
| 119 return node_parent_index_; | |
| 120 } | |
| 121 | |
| 122 void RecentlyUsedFoldersComboModel::MaybeChangeParent( | |
| 123 const BookmarkNode* node, | |
| 124 int selected_index) { | |
| 125 if (items_[selected_index].type != Item::TYPE_NODE) | |
| 126 return; | |
| 127 | |
| 128 const BookmarkNode* new_parent = GetNodeAt(selected_index); | |
| 129 if (new_parent != node->parent()) { | |
| 130 content::RecordAction( | |
| 131 content::UserMetricsAction("BookmarkBubble_ChangeParent")); | |
| 132 bookmark_model_->Move(node, new_parent, new_parent->child_count()); | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 const BookmarkNode* RecentlyUsedFoldersComboModel::GetNodeAt(int index) { | |
| 137 if (index < 0 || index >= static_cast<int>(items_.size())) | |
| 138 return NULL; | |
| 139 return items_[index].node; | |
| 140 } | |
| 141 | |
| 142 void RecentlyUsedFoldersComboModel::RemoveNode(const BookmarkNode* node) { | |
| 143 std::vector<Item>::iterator it = std::find(items_.begin(), | |
| 144 items_.end(), | |
| 145 Item(node, Item::TYPE_NODE)); | |
| 146 if (it != items_.end()) | |
| 147 items_.erase(it); | |
| 148 } | |
| OLD | NEW |