OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/ui/bookmarks/bookmark_utils.h" | 5 #include "chrome/browser/ui/bookmarks/bookmark_utils.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 } | 380 } |
381 | 381 |
382 int GetBookmarkDragOperation(content::BrowserContext* browser_context, | 382 int GetBookmarkDragOperation(content::BrowserContext* browser_context, |
383 const BookmarkNode* node) { | 383 const BookmarkNode* node) { |
384 PrefService* prefs = user_prefs::UserPrefs::Get(browser_context); | 384 PrefService* prefs = user_prefs::UserPrefs::Get(browser_context); |
385 Profile* profile = Profile::FromBrowserContext(browser_context); | 385 Profile* profile = Profile::FromBrowserContext(browser_context); |
386 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile); | 386 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile); |
387 | 387 |
388 int move = ui::DragDropTypes::DRAG_MOVE; | 388 int move = ui::DragDropTypes::DRAG_MOVE; |
389 if (!prefs->GetBoolean(bookmarks::prefs::kEditBookmarksEnabled) || | 389 if (!prefs->GetBoolean(bookmarks::prefs::kEditBookmarksEnabled) || |
390 !model->client()->CanBeEditedByUser(node)) { | 390 !model->CanBeEditedByUser(node)) { |
391 move = ui::DragDropTypes::DRAG_NONE; | 391 move = ui::DragDropTypes::DRAG_NONE; |
392 } | 392 } |
393 if (node->is_url()) | 393 if (node->is_url()) |
394 return ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_LINK | move; | 394 return ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_LINK | move; |
395 return ui::DragDropTypes::DRAG_COPY | move; | 395 return ui::DragDropTypes::DRAG_COPY | move; |
396 } | 396 } |
397 | 397 |
398 int GetPreferredBookmarkDropOperation(int source_operations, int operations) { | 398 int GetPreferredBookmarkDropOperation(int source_operations, int operations) { |
399 int common_ops = (source_operations & operations); | 399 int common_ops = (source_operations & operations); |
400 if (!common_ops) | 400 if (!common_ops) |
(...skipping 15 matching lines...) Expand all Loading... |
416 const base::FilePath& profile_path = profile->GetPath(); | 416 const base::FilePath& profile_path = profile->GetPath(); |
417 | 417 |
418 if (data.IsFromProfilePath(profile_path) && data.size() > 1) | 418 if (data.IsFromProfilePath(profile_path) && data.size() > 1) |
419 // Currently only accept one dragged node at a time. | 419 // Currently only accept one dragged node at a time. |
420 return ui::DragDropTypes::DRAG_NONE; | 420 return ui::DragDropTypes::DRAG_NONE; |
421 | 421 |
422 if (!IsValidBookmarkDropLocation(profile, data, parent, index)) | 422 if (!IsValidBookmarkDropLocation(profile, data, parent, index)) |
423 return ui::DragDropTypes::DRAG_NONE; | 423 return ui::DragDropTypes::DRAG_NONE; |
424 | 424 |
425 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile); | 425 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile); |
426 if (!model->client()->CanBeEditedByUser(parent)) | 426 if (!model->CanBeEditedByUser(parent)) |
427 return ui::DragDropTypes::DRAG_NONE; | 427 return ui::DragDropTypes::DRAG_NONE; |
428 | 428 |
429 const BookmarkNode* dragged_node = | 429 const BookmarkNode* dragged_node = |
430 data.GetFirstNode(model, profile->GetPath()); | 430 data.GetFirstNode(model, profile->GetPath()); |
431 if (dragged_node) { | 431 if (dragged_node) { |
432 // User is dragging from this profile. | 432 // User is dragging from this profile. |
433 if (!model->client()->CanBeEditedByUser(dragged_node)) { | 433 if (!model->CanBeEditedByUser(dragged_node)) { |
434 // Do a copy instead of a move when dragging bookmarks that the user can't | 434 // Do a copy instead of a move when dragging bookmarks that the user can't |
435 // modify. | 435 // modify. |
436 return ui::DragDropTypes::DRAG_COPY; | 436 return ui::DragDropTypes::DRAG_COPY; |
437 } | 437 } |
438 return ui::DragDropTypes::DRAG_MOVE; | 438 return ui::DragDropTypes::DRAG_MOVE; |
439 } | 439 } |
440 | 440 |
441 // User is dragging from another app, copy. | 441 // User is dragging from another app, copy. |
442 return GetPreferredBookmarkDropOperation(event.source_operations(), | 442 return GetPreferredBookmarkDropOperation(event.source_operations(), |
443 ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_LINK); | 443 ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_LINK); |
444 } | 444 } |
445 | 445 |
446 bool IsValidBookmarkDropLocation(Profile* profile, | 446 bool IsValidBookmarkDropLocation(Profile* profile, |
447 const bookmarks::BookmarkNodeData& data, | 447 const bookmarks::BookmarkNodeData& data, |
448 const BookmarkNode* drop_parent, | 448 const BookmarkNode* drop_parent, |
449 int index) { | 449 int index) { |
450 if (!drop_parent->is_folder()) { | 450 if (!drop_parent->is_folder()) { |
451 NOTREACHED(); | 451 NOTREACHED(); |
452 return false; | 452 return false; |
453 } | 453 } |
454 | 454 |
455 if (!data.is_valid()) | 455 if (!data.is_valid()) |
456 return false; | 456 return false; |
457 | 457 |
458 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile); | 458 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile); |
459 if (!model->client()->CanBeEditedByUser(drop_parent)) | 459 if (!model->CanBeEditedByUser(drop_parent)) |
460 return false; | 460 return false; |
461 | 461 |
462 const base::FilePath& profile_path = profile->GetPath(); | 462 const base::FilePath& profile_path = profile->GetPath(); |
463 if (data.IsFromProfilePath(profile_path)) { | 463 if (data.IsFromProfilePath(profile_path)) { |
464 std::vector<const BookmarkNode*> nodes = data.GetNodes(model, profile_path); | 464 std::vector<const BookmarkNode*> nodes = data.GetNodes(model, profile_path); |
465 for (size_t i = 0; i < nodes.size(); ++i) { | 465 for (size_t i = 0; i < nodes.size(); ++i) { |
466 // Don't allow the drop if the user is attempting to drop on one of the | 466 // Don't allow the drop if the user is attempting to drop on one of the |
467 // nodes being dragged. | 467 // nodes being dragged. |
468 const BookmarkNode* node = nodes[i]; | 468 const BookmarkNode* node = nodes[i]; |
469 int node_index = (drop_parent == node->parent()) ? | 469 int node_index = (drop_parent == node->parent()) ? |
470 drop_parent->GetIndexOf(nodes[i]) : -1; | 470 drop_parent->GetIndexOf(nodes[i]) : -1; |
471 if (node_index != -1 && (index == node_index || index == node_index + 1)) | 471 if (node_index != -1 && (index == node_index || index == node_index + 1)) |
472 return false; | 472 return false; |
473 | 473 |
474 // drop_parent can't accept a child that is an ancestor. | 474 // drop_parent can't accept a child that is an ancestor. |
475 if (drop_parent->HasAncestor(node)) | 475 if (drop_parent->HasAncestor(node)) |
476 return false; | 476 return false; |
477 } | 477 } |
478 return true; | 478 return true; |
479 } | 479 } |
480 // From another profile, always accept. | 480 // From another profile, always accept. |
481 return true; | 481 return true; |
482 } | 482 } |
483 | 483 |
484 } // namespace chrome | 484 } // namespace chrome |
OLD | NEW |