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 for (size_t i = 0; i < titles.size(); i++) { |
| 226 const base::string16 new_title(*title + |
| 227 base::ASCIIToUTF16(base::StringPrintf( |
| 228 " (%lu)", (unsigned long)(i + 1)))); |
| 229 if (titles.find(new_title) == titles.end()) { |
| 230 *title = new_title; |
| 231 return; |
| 232 } |
| 233 } |
| 234 NOTREACHED(); |
| 235 } |
| 236 |
204 void PasteFromClipboard(BookmarkModel* model, | 237 void PasteFromClipboard(BookmarkModel* model, |
205 const BookmarkNode* parent, | 238 const BookmarkNode* parent, |
206 int index) { | 239 int index) { |
207 if (!parent) | 240 if (!parent) |
208 return; | 241 return; |
209 | 242 |
210 BookmarkNodeData bookmark_data; | 243 BookmarkNodeData bookmark_data; |
211 if (!bookmark_data.ReadFromClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE)) { | 244 if (!bookmark_data.ReadFromClipboard(ui::CLIPBOARD_TYPE_COPY_PASTE)) { |
212 GURL url = GetUrlFromClipboard(); | 245 GURL url = GetUrlFromClipboard(); |
213 if (!url.is_valid()) | 246 if (!url.is_valid()) |
214 return; | 247 return; |
215 BookmarkNode node(url); | 248 BookmarkNode node(url); |
216 node.SetTitle(base::ASCIIToUTF16(url.spec())); | 249 node.SetTitle(base::ASCIIToUTF16(url.spec())); |
217 bookmark_data = BookmarkNodeData(&node); | 250 bookmark_data = BookmarkNodeData(&node); |
218 } | 251 } |
219 if (index == -1) | 252 if (index == -1) |
220 index = parent->child_count(); | 253 index = parent->child_count(); |
221 ScopedGroupBookmarkActions group_paste(model); | 254 ScopedGroupBookmarkActions group_paste(model); |
| 255 |
| 256 if (bookmark_data.elements.size() == 1 && |
| 257 model->IsBookmarked(bookmark_data.elements[0].url)) { |
| 258 MakeTitleUnique(model, |
| 259 parent, |
| 260 bookmark_data.elements[0].url, |
| 261 &bookmark_data.elements[0].title); |
| 262 } |
| 263 |
222 CloneBookmarkNode(model, bookmark_data.elements, parent, index, true); | 264 CloneBookmarkNode(model, bookmark_data.elements, parent, index, true); |
223 } | 265 } |
224 | 266 |
225 bool CanPasteFromClipboard(BookmarkModel* model, const BookmarkNode* node) { | 267 bool CanPasteFromClipboard(BookmarkModel* model, const BookmarkNode* node) { |
226 if (!node || !model->client()->CanBeEditedByUser(node)) | 268 if (!node || !model->client()->CanBeEditedByUser(node)) |
227 return false; | 269 return false; |
228 return (BookmarkNodeData::ClipboardContainsBookmarks() || | 270 return (BookmarkNodeData::ClipboardContainsBookmarks() || |
229 GetUrlFromClipboard().is_valid()); | 271 GetUrlFromClipboard().is_valid()); |
230 } | 272 } |
231 | 273 |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 } | 498 } |
457 return false; | 499 return false; |
458 } | 500 } |
459 | 501 |
460 const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64 id) { | 502 const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model, int64 id) { |
461 // TODO(sky): TreeNode needs a method that visits all nodes using a predicate. | 503 // TODO(sky): TreeNode needs a method that visits all nodes using a predicate. |
462 return GetNodeByID(model->root_node(), id); | 504 return GetNodeByID(model->root_node(), id); |
463 } | 505 } |
464 | 506 |
465 } // namespace bookmarks | 507 } // namespace bookmarks |
OLD | NEW |