summaryrefslogtreecommitdiffstats
path: root/chrome/browser/bookmarks
diff options
context:
space:
mode:
authortfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-06-12 15:16:36 +0000
committertfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-06-12 15:16:36 +0000
commit7f1e52363a26ea88af22d7894d97d6581b4f2cdb (patch)
treef5a041ef55e2ad1aa5febf74b3d73085fed8d009 /chrome/browser/bookmarks
parente7480b637393aeecc29f6afde286994cfd4c90de (diff)
downloadchromium_src-7f1e52363a26ea88af22d7894d97d6581b4f2cdb.zip
chromium_src-7f1e52363a26ea88af22d7894d97d6581b4f2cdb.tar.gz
chromium_src-7f1e52363a26ea88af22d7894d97d6581b4f2cdb.tar.bz2
Split RecentlyUsedFolderComboModel into its own module, so we can share it
Views and Gtk. BUG=None TEST=manually Review URL: http://codereview.chromium.org/2673005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@49633 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/bookmarks')
-rw-r--r--chrome/browser/bookmarks/recently_used_folders_combo_model.cc74
-rw-r--r--chrome/browser/bookmarks/recently_used_folders_combo_model.h40
2 files changed, 114 insertions, 0 deletions
diff --git a/chrome/browser/bookmarks/recently_used_folders_combo_model.cc b/chrome/browser/bookmarks/recently_used_folders_combo_model.cc
new file mode 100644
index 0000000..cd6bfc1
--- /dev/null
+++ b/chrome/browser/bookmarks/recently_used_folders_combo_model.cc
@@ -0,0 +1,74 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/bookmarks/recently_used_folders_combo_model.h"
+
+#include "app/l10n_util.h"
+#include "chrome/browser/bookmarks/bookmark_utils.h"
+#include "grit/generated_resources.h"
+
+namespace {
+
+// Max number of most recently used folders.
+const size_t kMaxMRUFolders = 5;
+
+} // namespace
+
+RecentlyUsedFoldersComboModel::RecentlyUsedFoldersComboModel(
+ BookmarkModel* model, const BookmarkNode* node)
+ // Use + 2 to account for bookmark bar and other node.
+ : nodes_(bookmark_utils::GetMostRecentlyModifiedGroups(
+ model, kMaxMRUFolders + 2)),
+ node_parent_index_(0) {
+ // TODO(sky): bug 1173415 add a separator in the combobox here.
+
+ // We special case the placement of these, so remove them from the list, then
+ // fix up the order.
+ RemoveNode(model->GetBookmarkBarNode());
+ RemoveNode(model->other_node());
+ RemoveNode(node->GetParent());
+
+ // Make the parent the first item, unless it's the bookmark bar or other node.
+ if (node->GetParent() != model->GetBookmarkBarNode() &&
+ node->GetParent() != model->other_node()) {
+ nodes_.insert(nodes_.begin(), node->GetParent());
+ }
+
+ // Make sure we only have kMaxMRUFolders in the first chunk.
+ if (nodes_.size() > kMaxMRUFolders)
+ nodes_.erase(nodes_.begin() + kMaxMRUFolders, nodes_.end());
+
+ // And put the bookmark bar and other nodes at the end of the list.
+ nodes_.push_back(model->GetBookmarkBarNode());
+ nodes_.push_back(model->other_node());
+
+ std::vector<const BookmarkNode*>::iterator it = std::find(nodes_.begin(),
+ nodes_.end(),
+ node->GetParent());
+ node_parent_index_ = static_cast<int>(it - nodes_.begin());
+}
+
+int RecentlyUsedFoldersComboModel::GetItemCount() {
+ return static_cast<int>(nodes_.size() + 1);
+}
+
+std::wstring RecentlyUsedFoldersComboModel::GetItemAt(int index) {
+ if (index == static_cast<int>(nodes_.size()))
+ return l10n_util::GetString(IDS_BOOMARK_BUBBLE_CHOOSER_ANOTHER_FOLDER);
+ return nodes_[index]->GetTitle();
+}
+
+const BookmarkNode* RecentlyUsedFoldersComboModel::GetNodeAt(int index) {
+ if (index < 0 || index >= static_cast<int>(nodes_.size()))
+ return NULL;
+ return nodes_[index];
+}
+
+void RecentlyUsedFoldersComboModel::RemoveNode(const BookmarkNode* node) {
+ std::vector<const BookmarkNode*>::iterator it = std::find(nodes_.begin(),
+ nodes_.end(),
+ node);
+ if (it != nodes_.end())
+ nodes_.erase(it);
+}
diff --git a/chrome/browser/bookmarks/recently_used_folders_combo_model.h b/chrome/browser/bookmarks/recently_used_folders_combo_model.h
new file mode 100644
index 0000000..2a8dac0
--- /dev/null
+++ b/chrome/browser/bookmarks/recently_used_folders_combo_model.h
@@ -0,0 +1,40 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_RECENTLY_FOLDERS_COMBO_MODEL_H_
+#define CHROME_BROWSER_RECENTLY_FOLDERS_COMBO_MODEL_H_
+
+#include <vector>
+
+#include "app/combobox_model.h"
+#include "chrome/browser/bookmarks/bookmark_model.h"
+
+// Model for the combobox showing the list of folders to choose from. The
+// list always contains the bookmark bar, other node and parent. The list
+// also contains an extra item that shows the text 'Choose another folder...'.
+class RecentlyUsedFoldersComboModel : public ComboboxModel {
+ public:
+ RecentlyUsedFoldersComboModel(BookmarkModel* model, const BookmarkNode* node);
+
+ // Overridden from ComboboxModel:
+ virtual int GetItemCount();
+ virtual std::wstring GetItemAt(int index);
+
+ // Returns the node at the specified index.
+ const BookmarkNode* GetNodeAt(int index);
+
+ // Returns the index of the original parent folder.
+ int node_parent_index() const { return node_parent_index_; }
+
+ private:
+ // Removes |node| from |nodes_|. Does nothing if |node| is not in |nodes_|.
+ void RemoveNode(const BookmarkNode* node);
+
+ std::vector<const BookmarkNode*> nodes_;
+ int node_parent_index_;
+
+ DISALLOW_COPY_AND_ASSIGN(RecentlyUsedFoldersComboModel);
+};
+
+#endif // CHROME_BROWSER_RECENTLY_FOLDERS_COMBO_MODEL_H_