| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/bookmarks/bookmark_utils.h" | 5 #include "chrome/browser/bookmarks/bookmark_utils.h" |
| 6 | 6 |
| 7 #include "app/clipboard/clipboard.h" | 7 #include "app/clipboard/clipboard.h" |
| 8 #include "app/drag_drop_types.h" | 8 #include "app/drag_drop_types.h" |
| 9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
| 10 #include "app/tree_node_iterator.h" | 10 #include "app/tree_node_iterator.h" |
| (...skipping 487 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 const std::wstring& languages) { | 498 const std::wstring& languages) { |
| 499 std::vector<std::wstring> words; | 499 std::vector<std::wstring> words; |
| 500 QueryParser parser; | 500 QueryParser parser; |
| 501 parser.ExtractQueryWords(l10n_util::ToLower(text), &words); | 501 parser.ExtractQueryWords(l10n_util::ToLower(text), &words); |
| 502 if (words.empty()) | 502 if (words.empty()) |
| 503 return false; | 503 return false; |
| 504 | 504 |
| 505 return (node->is_url() && DoesBookmarkContainWords(node, words, languages)); | 505 return (node->is_url() && DoesBookmarkContainWords(node, words, languages)); |
| 506 } | 506 } |
| 507 | 507 |
| 508 const BookmarkNode* ApplyEditsWithNoGroupChange(BookmarkModel* model, | 508 static const BookmarkNode* CreateNewNode(BookmarkModel* model, |
| 509 const BookmarkNode* parent, const BookmarkNode* node, | 509 const BookmarkNode* parent, const BookmarkEditor::EditDetails& details, |
| 510 const std::wstring& new_title, const GURL& new_url, | 510 const std::wstring& new_title, const GURL& new_url, |
| 511 BookmarkEditor::Handler* handler) { | 511 BookmarkEditor::Handler* handler) { |
| 512 const BookmarkNode* old_parent = node ? node->GetParent() : NULL; | 512 const BookmarkNode* node; |
| 513 const int old_index = old_parent ? old_parent->IndexOfChild(node) : -1; | 513 if (details.type == BookmarkEditor::EditDetails::NEW_URL) { |
| 514 node = model->AddURL(parent, parent->GetChildCount(), new_title, new_url); |
| 515 } else if (details.type == BookmarkEditor::EditDetails::NEW_FOLDER) { |
| 516 node = model->AddGroup(parent, parent->GetChildCount(), new_title); |
| 517 for (size_t i = 0; i < details.urls.size(); ++i) { |
| 518 model->AddURL(node, node->GetChildCount(), details.urls[i].second, |
| 519 details.urls[i].first); |
| 520 } |
| 521 // TODO(sky): update parent modified time. |
| 522 } else { |
| 523 NOTREACHED(); |
| 524 return NULL; |
| 525 } |
| 514 | 526 |
| 515 if (!node) { | 527 if (handler) |
| 516 node = | 528 handler->NodeCreated(node); |
| 517 model->AddURL(parent, parent->GetChildCount(), new_title, new_url); | 529 return node; |
| 530 } |
| 518 | 531 |
| 519 if (handler) | 532 const BookmarkNode* ApplyEditsWithNoGroupChange(BookmarkModel* model, |
| 520 handler->NodeCreated(node); | 533 const BookmarkNode* parent, const BookmarkEditor::EditDetails& details, |
| 521 return node; | 534 const std::wstring& new_title, const GURL& new_url, |
| 535 BookmarkEditor::Handler* handler) { |
| 536 if (details.type == BookmarkEditor::EditDetails::NEW_URL || |
| 537 details.type == BookmarkEditor::EditDetails::NEW_FOLDER) { |
| 538 return CreateNewNode(model, parent, details, new_title, new_url, handler); |
| 522 } | 539 } |
| 523 | 540 |
| 541 const BookmarkNode* node = details.existing_node; |
| 542 DCHECK(node); |
| 543 const BookmarkNode* old_parent = node->GetParent(); |
| 544 int old_index = old_parent ? old_parent->IndexOfChild(node) : -1; |
| 545 |
| 524 // If we're not showing the tree we only need to modify the node. | 546 // If we're not showing the tree we only need to modify the node. |
| 525 if (old_index == -1) { | 547 if (old_index == -1) { |
| 526 NOTREACHED(); | 548 NOTREACHED(); |
| 527 return node; | 549 return node; |
| 528 } | 550 } |
| 529 | 551 |
| 530 if (new_url != node->GetURL()) { | 552 if (new_url != node->GetURL()) { |
| 553 // TODO(sky): need SetURL on the model. |
| 531 const BookmarkNode* new_node = model->AddURLWithCreationTime(old_parent, | 554 const BookmarkNode* new_node = model->AddURLWithCreationTime(old_parent, |
| 532 old_index, new_title, new_url, node->date_added()); | 555 old_index, new_title, new_url, node->date_added()); |
| 533 model->Remove(old_parent, old_index + 1); | 556 model->Remove(old_parent, old_index + 1); |
| 534 return new_node; | 557 return new_node; |
| 535 } else { | 558 } else { |
| 536 model->SetTitle(node, new_title); | 559 model->SetTitle(node, new_title); |
| 537 } | 560 } |
| 538 return node; | 561 return node; |
| 539 } | 562 } |
| 540 | 563 |
| 541 const BookmarkNode* ApplyEditsWithPossibleGroupChange(BookmarkModel* model, | 564 const BookmarkNode* ApplyEditsWithPossibleGroupChange(BookmarkModel* model, |
| 542 const BookmarkNode* new_parent, const BookmarkNode* node, | 565 const BookmarkNode* new_parent, const BookmarkEditor::EditDetails& details, |
| 543 const std::wstring& new_title, const GURL& new_url, | 566 const std::wstring& new_title, const GURL& new_url, |
| 544 BookmarkEditor::Handler* handler) { | 567 BookmarkEditor::Handler* handler) { |
| 545 const BookmarkNode* old_parent = node ? node->GetParent() : NULL; | 568 if (details.type == BookmarkEditor::EditDetails::NEW_URL || |
| 546 const int old_index = old_parent ? old_parent->IndexOfChild(node) : -1; | 569 details.type == BookmarkEditor::EditDetails::NEW_FOLDER) { |
| 570 return CreateNewNode(model, new_parent, details, new_title, new_url, |
| 571 handler); |
| 572 } |
| 573 |
| 574 const BookmarkNode* node = details.existing_node; |
| 575 DCHECK(node); |
| 576 const BookmarkNode* old_parent = node->GetParent(); |
| 577 int old_index = old_parent->IndexOfChild(node); |
| 547 const BookmarkNode* return_node = node; | 578 const BookmarkNode* return_node = node; |
| 548 if (node) { | 579 |
| 549 Time date_added = node->date_added(); | 580 Time date_added = node->date_added(); |
| 550 if (new_parent == node->GetParent()) { | 581 if (new_parent == node->GetParent()) { |
| 551 // The parent is the same. | 582 // The parent is the same. |
| 552 if (node->is_url() && new_url != node->GetURL()) { | 583 if (node->is_url() && new_url != node->GetURL()) { |
| 553 model->Remove(old_parent, old_index); | |
| 554 return_node = model->AddURLWithCreationTime(old_parent, old_index, | |
| 555 new_title, new_url, date_added); | |
| 556 } else { | |
| 557 model->SetTitle(node, new_title); | |
| 558 } | |
| 559 } else if (node->is_url() && new_url != node->GetURL()) { | |
| 560 // The parent and URL changed. | |
| 561 model->Remove(old_parent, old_index); | 584 model->Remove(old_parent, old_index); |
| 562 return_node = model->AddURLWithCreationTime(new_parent, | 585 return_node = model->AddURLWithCreationTime(old_parent, old_index, |
| 563 new_parent->GetChildCount(), new_title, new_url, date_added); | 586 new_title, new_url, date_added); |
| 564 } else { | 587 } else { |
| 565 // The parent and title changed. Move the node and change the title. | |
| 566 model->Move(node, new_parent, new_parent->GetChildCount()); | |
| 567 model->SetTitle(node, new_title); | 588 model->SetTitle(node, new_title); |
| 568 } | 589 } |
| 590 } else if (node->is_url() && new_url != node->GetURL()) { |
| 591 // The parent and URL changed. |
| 592 model->Remove(old_parent, old_index); |
| 593 return_node = model->AddURLWithCreationTime(new_parent, |
| 594 new_parent->GetChildCount(), new_title, new_url, date_added); |
| 569 } else { | 595 } else { |
| 570 // We're adding a new URL. | 596 // The parent and title changed. Move the node and change the title. |
| 571 return_node = | 597 model->Move(node, new_parent, new_parent->GetChildCount()); |
| 572 model->AddURL(new_parent, new_parent->GetChildCount(), new_title, | 598 model->SetTitle(node, new_title); |
| 573 new_url); | |
| 574 if (handler) | |
| 575 handler->NodeCreated(return_node); | |
| 576 } | 599 } |
| 577 return return_node; | 600 return return_node; |
| 578 } | 601 } |
| 579 | 602 |
| 580 // Formerly in BookmarkBarView | 603 // Formerly in BookmarkBarView |
| 581 void ToggleWhenVisible(Profile* profile) { | 604 void ToggleWhenVisible(Profile* profile) { |
| 582 PrefService* prefs = profile->GetPrefs(); | 605 PrefService* prefs = profile->GetPrefs(); |
| 583 const bool always_show = !prefs->GetBoolean(prefs::kShowBookmarkBar); | 606 const bool always_show = !prefs->GetBoolean(prefs::kShowBookmarkBar); |
| 584 | 607 |
| 585 // The user changed when the bookmark bar is shown, update the preferences. | 608 // The user changed when the bookmark bar is shown, update the preferences. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 611 prefs->RegisterIntegerPref(prefs::kBookmarkTablePathWidth, -1); | 634 prefs->RegisterIntegerPref(prefs::kBookmarkTablePathWidth, -1); |
| 612 } | 635 } |
| 613 | 636 |
| 614 void GetURLAndTitleToBookmark(TabContents* tab_contents, | 637 void GetURLAndTitleToBookmark(TabContents* tab_contents, |
| 615 GURL* url, | 638 GURL* url, |
| 616 std::wstring* title) { | 639 std::wstring* title) { |
| 617 *url = tab_contents->GetURL(); | 640 *url = tab_contents->GetURL(); |
| 618 *title = UTF16ToWideHack(tab_contents->GetTitle()); | 641 *title = UTF16ToWideHack(tab_contents->GetTitle()); |
| 619 } | 642 } |
| 620 | 643 |
| 621 const BookmarkNode* CreateBookmarkForAllTabs(Browser* browser) { | 644 void GetURLsForOpenTabs(Browser* browser, |
| 622 BookmarkModel* model = browser->profile()->GetBookmarkModel(); | 645 std::vector<std::pair<GURL, std::wstring> >* urls) { |
| 623 DCHECK(model && model->IsLoaded()); | |
| 624 const BookmarkNode* parent = model->GetParentForNewNodes(); | |
| 625 const BookmarkNode* folder = model->AddGroup( | |
| 626 parent, parent->GetChildCount(), | |
| 627 l10n_util::GetString(IDS_BOOMARK_EDITOR_NEW_FOLDER_NAME)); | |
| 628 for (int i = 0; i < browser->tab_count(); ++i) { | 646 for (int i = 0; i < browser->tab_count(); ++i) { |
| 629 GURL url; | 647 std::pair<GURL, std::wstring> entry; |
| 630 std::wstring title; | 648 GetURLAndTitleToBookmark(browser->GetTabContentsAt(i), &(entry.first), |
| 631 GetURLAndTitleToBookmark(browser->GetTabContentsAt(i), &url, &title); | 649 &(entry.second)); |
| 632 model->AddURL(folder, folder->GetChildCount(), title, url); | 650 urls->push_back(entry); |
| 633 } | 651 } |
| 634 return folder; | |
| 635 } | 652 } |
| 636 | 653 |
| 637 } // namespace bookmark_utils | 654 } // namespace bookmark_utils |
| OLD | NEW |