Home | History | Annotate | Download | only in bookmarks
      1 // Copyright (c) 2011 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 "grit/generated_resources.h"
      9 #include "ui/base/l10n/l10n_util.h"
     10 
     11 namespace {
     12 
     13 // Max number of most recently used folders.
     14 const size_t kMaxMRUFolders = 5;
     15 
     16 }  // namespace
     17 
     18 RecentlyUsedFoldersComboModel::RecentlyUsedFoldersComboModel(
     19     BookmarkModel* model, const BookmarkNode* node)
     20     // Use + 2 to account for bookmark bar and other node.
     21     : nodes_(bookmark_utils::GetMostRecentlyModifiedFolders(
     22           model, kMaxMRUFolders + 2)),
     23       node_parent_index_(0) {
     24   // TODO(sky): bug 1173415 add a separator in the combobox here.
     25 
     26   // We special case the placement of these, so remove them from the list, then
     27   // fix up the order.
     28   RemoveNode(model->GetBookmarkBarNode());
     29   RemoveNode(model->other_node());
     30   RemoveNode(node->parent());
     31 
     32   // Make the parent the first item, unless it's the bookmark bar or other node.
     33   if (node->parent() != model->GetBookmarkBarNode() &&
     34       node->parent() != model->other_node()) {
     35     nodes_.insert(nodes_.begin(), node->parent());
     36   }
     37 
     38   // Make sure we only have kMaxMRUFolders in the first chunk.
     39   if (nodes_.size() > kMaxMRUFolders)
     40     nodes_.erase(nodes_.begin() + kMaxMRUFolders, nodes_.end());
     41 
     42   // And put the bookmark bar and other nodes at the end of the list.
     43   nodes_.push_back(model->GetBookmarkBarNode());
     44   nodes_.push_back(model->other_node());
     45 
     46   std::vector<const BookmarkNode*>::iterator it = std::find(nodes_.begin(),
     47                                                             nodes_.end(),
     48                                                             node->parent());
     49   node_parent_index_ = static_cast<int>(it - nodes_.begin());
     50 }
     51 
     52 RecentlyUsedFoldersComboModel::~RecentlyUsedFoldersComboModel() {}
     53 
     54 int RecentlyUsedFoldersComboModel::GetItemCount() {
     55   return static_cast<int>(nodes_.size() + 1);
     56 }
     57 
     58 string16 RecentlyUsedFoldersComboModel::GetItemAt(int index) {
     59   if (index == static_cast<int>(nodes_.size()))
     60     return l10n_util::GetStringUTF16(IDS_BOOMARK_BUBBLE_CHOOSER_ANOTHER_FOLDER);
     61   return nodes_[index]->GetTitle();
     62 }
     63 
     64 const BookmarkNode* RecentlyUsedFoldersComboModel::GetNodeAt(int index) {
     65   if (index < 0 || index >= static_cast<int>(nodes_.size()))
     66     return NULL;
     67   return nodes_[index];
     68 }
     69 
     70 void RecentlyUsedFoldersComboModel::RemoveNode(const BookmarkNode* node) {
     71   std::vector<const BookmarkNode*>::iterator it = std::find(nodes_.begin(),
     72                                                             nodes_.end(),
     73                                                             node);
     74   if (it != nodes_.end())
     75     nodes_.erase(it);
     76 }
     77