Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/bookmarks/browser/bookmark_utils.h" | 5 #include "components/bookmarks/browser/bookmark_utils.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/containers/hash_tables.h" | |
| 11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 12 #include "base/i18n/case_conversion.h" | 13 #include "base/i18n/case_conversion.h" |
| 13 #include "base/i18n/string_search.h" | 14 #include "base/i18n/string_search.h" |
| 14 #include "base/metrics/user_metrics_action.h" | 15 #include "base/metrics/user_metrics_action.h" |
| 15 #include "base/prefs/pref_service.h" | 16 #include "base/prefs/pref_service.h" |
| 17 #include "base/strings/string_util.h" | |
| 18 #include "base/strings/stringprintf.h" | |
| 16 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 18 #include "components/bookmarks/browser/bookmark_client.h" | 21 #include "components/bookmarks/browser/bookmark_client.h" |
| 19 #include "components/bookmarks/browser/bookmark_model.h" | 22 #include "components/bookmarks/browser/bookmark_model.h" |
| 20 #include "components/bookmarks/browser/scoped_group_bookmark_actions.h" | 23 #include "components/bookmarks/browser/scoped_group_bookmark_actions.h" |
| 21 #include "components/bookmarks/common/bookmark_pref_names.h" | 24 #include "components/bookmarks/common/bookmark_pref_names.h" |
| 22 #include "components/pref_registry/pref_registry_syncable.h" | 25 #include "components/pref_registry/pref_registry_syncable.h" |
| 23 #include "components/query_parser/query_parser.h" | 26 #include "components/query_parser/query_parser.h" |
| 24 #include "net/base/net_util.h" | 27 #include "net/base/net_util.h" |
| 25 #include "ui/base/clipboard/clipboard.h" | 28 #include "ui/base/clipboard/clipboard.h" |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 if (remove_nodes) { | 197 if (remove_nodes) { |
| 195 ScopedGroupBookmarkActions group_cut(model); | 198 ScopedGroupBookmarkActions group_cut(model); |
| 196 for (size_t i = 0; i < filtered_nodes.size(); ++i) { | 199 for (size_t i = 0; i < filtered_nodes.size(); ++i) { |
| 197 int index = filtered_nodes[i]->parent()->GetIndexOf(filtered_nodes[i]); | 200 int index = filtered_nodes[i]->parent()->GetIndexOf(filtered_nodes[i]); |
| 198 if (index > -1) | 201 if (index > -1) |
| 199 model->Remove(filtered_nodes[i]->parent(), index); | 202 model->Remove(filtered_nodes[i]->parent(), index); |
| 200 } | 203 } |
| 201 } | 204 } |
| 202 } | 205 } |
| 203 | 206 |
| 207 // Updates |title| such that |url| and |title| pair are unique among the | |
| 208 // children of |parent|. | |
| 209 void MakeTitleUnique(const BookmarkModel* model, | |
| 210 const BookmarkNode* parent, | |
| 211 const GURL& url, | |
| 212 base::string16* title) { | |
| 213 base::hash_set<base::string16> titles; | |
| 214 for (int i = 0; i < parent->child_count(); i++) { | |
| 215 const BookmarkNode* node = parent->GetChild(i); | |
| 216 if (node->is_url() && (url == node->url()) && | |
| 217 StartsWith(node->GetTitle(), *title, false)) { | |
| 218 titles.insert(node->GetTitle()); | |
| 219 } | |
| 220 } | |
| 221 | |
| 222 if (titles.find(*title) == titles.end()) | |
| 223 return; | |
| 224 | |
| 225 base::string16 new_title = *title; | |
|
sky
2014/10/08 15:30:27
new_title should be moved into for loop.
Deepak
2014/10/09 04:23:40
Done.
| |
| 226 for (size_t i = 0; i < titles.size(); i++) { | |
| 227 new_title = base::UTF8ToUTF16(base::StringPrintf( | |
| 228 "%s (%lu)", base::UTF16ToUTF8(*title).c_str(), i + 1)); | |
|
sky
2014/10/08 15:30:27
const base::string16 new_title(*title + base::ASCI
Deepak
2014/10/09 04:23:40
Acknowledged.
| |
| 229 if (titles.find(new_title) == titles.end()) { | |
| 230 *title = new_title; | |
| 231 return; | |
| 232 } | |
| 233 } | |
| 234 } | |
| 235 | |
| 204 void PasteFromClipboard(BookmarkModel* model, | 236 void PasteFromClipboard(BookmarkModel* model, |
| 205 const BookmarkNode* parent, | 237 const BookmarkNode* parent, |
| 206 int index) { | 238 int index) { |
| 207 if (!parent) | 239 if (!parent) |
| 208 return; | 240 return; |
| 209 | 241 |
| 210 BookmarkNodeData bookmark_data; | 242 BookmarkNodeData bookmark_data; |
| 211 if (!bookmark_data.ReadFromClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE)) { | 243 if (!bookmark_data.ReadFromClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE)) { |
| 212 GURL url = GetUrlFromClipboard(); | 244 GURL url = GetUrlFromClipboard(); |
| 213 if (!url.is_valid()) | 245 if (!url.is_valid()) |
| 214 return; | 246 return; |
| 215 BookmarkNode node(url); | 247 BookmarkNode node(url); |
| 216 node.SetTitle(base::ASCIIToUTF16(url.spec())); | 248 node.SetTitle(base::ASCIIToUTF16(url.spec())); |
| 217 bookmark_data = BookmarkNodeData(&node); | 249 bookmark_data = BookmarkNodeData(&node); |
| 218 } | 250 } |
| 219 if (index == -1) | 251 if (index == -1) |
| 220 index = parent->child_count(); | 252 index = parent->child_count(); |
| 221 ScopedGroupBookmarkActions group_paste(model); | 253 ScopedGroupBookmarkActions group_paste(model); |
| 254 | |
| 255 if (bookmark_data.elements.size() == 1 && | |
| 256 model->IsBookmarked(bookmark_data.elements[0].url)) { | |
| 257 MakeTitleUnique(model, | |
| 258 parent, | |
| 259 bookmark_data.elements[0].url, | |
| 260 &bookmark_data.elements[0].title); | |
| 261 } | |
| 262 | |
| 222 CloneBookmarkNode(model, bookmark_data.elements, parent, index, true); | 263 CloneBookmarkNode(model, bookmark_data.elements, parent, index, true); |
| 223 } | 264 } |
| 224 | 265 |
| 225 bool CanPasteFromClipboard(BookmarkModel* model, const BookmarkNode* node) { | 266 bool CanPasteFromClipboard(BookmarkModel* model, const BookmarkNode* node) { |
| 226 if (!node || !model->client()->CanBeEditedByUser(node)) | 267 if (!node || !model->client()->CanBeEditedByUser(node)) |
| 227 return false; | 268 return false; |
| 228 return (BookmarkNodeData::ClipboardContainsBookmarks() || | 269 return (BookmarkNodeData::ClipboardContainsBookmarks() || |
| 229 GetUrlFromClipboard().is_valid()); | 270 GetUrlFromClipboard().is_valid()); |
| 230 } | 271 } |
| 231 | 272 |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 456 } | 497 } |
| 457 return false; | 498 return false; |
| 458 } | 499 } |
| 459 | 500 |
| 460 const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64 id) { | 501 const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64 id) { |
| 461 // TODO(sky): TreeNode needs a method that visits all nodes using a predicate. | 502 // TODO(sky): TreeNode needs a method that visits all nodes using a predicate. |
| 462 return GetNodeByID(model->root_node(), id); | 503 return GetNodeByID(model->root_node(), id); |
| 463 } | 504 } |
| 464 | 505 |
| 465 } // namespace bookmarks | 506 } // namespace bookmarks |
| OLD | NEW |