| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/bookmarks/bookmark_input_window_dialog_controller.h" |
| 6 |
| 7 #include "base/string16.h" |
| 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 10 #include "chrome/browser/bookmarks/bookmark_utils.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "grit/generated_resources.h" |
| 13 #include "ui/base/l10n/l10n_util.h" |
| 14 |
| 15 namespace { |
| 16 string16 GetWindowTitle(const BookmarkEditor::EditDetails& details) { |
| 17 int title_id = 0; |
| 18 switch (details.type) { |
| 19 case BookmarkEditor::EditDetails::EXISTING_NODE: |
| 20 if (details.existing_node->is_url()) { |
| 21 title_id = IDS_BOOKMARK_EDITOR_TITLE; |
| 22 } else { |
| 23 title_id = IDS_BOOKMARK_FOLDER_EDITOR_WINDOW_TITLE; |
| 24 } |
| 25 break; |
| 26 case BookmarkEditor::EditDetails::NEW_URL: |
| 27 title_id = IDS_BOOKMARK_EDITOR_TITLE; |
| 28 break; |
| 29 case BookmarkEditor::EditDetails::NEW_FOLDER: |
| 30 title_id = IDS_BOOKMARK_FOLDER_EDITOR_WINDOW_TITLE_NEW; |
| 31 break; |
| 32 default: |
| 33 NOTREACHED(); |
| 34 break; |
| 35 } |
| 36 return l10n_util::GetStringUTF16(title_id); |
| 37 } |
| 38 } // namespace |
| 39 |
| 40 BookmarkInputWindowDialogController::~BookmarkInputWindowDialogController() { |
| 41 if (model_) |
| 42 model_->RemoveObserver(this); |
| 43 } |
| 44 |
| 45 // static |
| 46 void BookmarkInputWindowDialogController::Show( |
| 47 Profile* profile, |
| 48 gfx::NativeWindow wnd, |
| 49 const BookmarkEditor::EditDetails& details) { |
| 50 // BookmarkInputWindowDialogController deletes itself when done. |
| 51 new BookmarkInputWindowDialogController(profile, wnd, details); |
| 52 } |
| 53 |
| 54 BookmarkInputWindowDialogController::BookmarkInputWindowDialogController( |
| 55 Profile* profile, |
| 56 gfx::NativeWindow wnd, |
| 57 const BookmarkEditor::EditDetails& details) |
| 58 : profile_(profile), |
| 59 model_(profile->GetBookmarkModel()), |
| 60 details_(details) { |
| 61 |
| 62 model_->AddObserver(this); |
| 63 |
| 64 InputWindowDialog::LabelContentsPairs label_contents_pairs; |
| 65 string16 name_label = |
| 66 l10n_util::GetStringUTF16(IDS_BOOKMARK_BAR_EDIT_FOLDER_LABEL); |
| 67 if ((details.type == BookmarkEditor::EditDetails::EXISTING_NODE && |
| 68 details.existing_node->is_url()) || |
| 69 details.type == BookmarkEditor::EditDetails::NEW_URL) { |
| 70 string16 url_label = |
| 71 l10n_util::GetStringUTF16(IDS_BOOKMARK_EDITOR_URL_LABEL); |
| 72 string16 name; |
| 73 GURL url; |
| 74 if (details.type == BookmarkEditor::EditDetails::NEW_URL) { |
| 75 bookmark_utils::GetURLAndTitleToBookmarkFromCurrentTab(profile, |
| 76 &url, |
| 77 &name); |
| 78 } else { |
| 79 url = details.existing_node->url(); |
| 80 name = details.existing_node->GetTitle(); |
| 81 } |
| 82 label_contents_pairs.push_back(std::make_pair(name_label, name)); |
| 83 label_contents_pairs.push_back(std::make_pair(url_label, |
| 84 UTF8ToUTF16(url.spec()))); |
| 85 } else { |
| 86 string16 name = (details.type == BookmarkEditor::EditDetails::NEW_FOLDER) ? |
| 87 l10n_util::GetStringUTF16(IDS_BOOKMARK_EDITOR_NEW_FOLDER_NAME) : |
| 88 details.existing_node->GetTitle(); |
| 89 label_contents_pairs.push_back(std::make_pair(name_label, name)); |
| 90 } |
| 91 |
| 92 const InputWindowDialog::ButtonType button_type = |
| 93 (details.type == BookmarkEditor::EditDetails::NEW_URL || |
| 94 details.type == BookmarkEditor::EditDetails::NEW_FOLDER) ? |
| 95 InputWindowDialog::BUTTON_TYPE_ADD : InputWindowDialog::BUTTON_TYPE_SAVE; |
| 96 dialog_ = InputWindowDialog::Create(wnd, |
| 97 GetWindowTitle(details), |
| 98 label_contents_pairs, this, button_type); |
| 99 dialog_->Show(); |
| 100 } |
| 101 |
| 102 bool BookmarkInputWindowDialogController::IsValid( |
| 103 const InputWindowDialog::InputTexts& texts) { |
| 104 if (texts.size() != 1 && texts.size() != 2) { |
| 105 return false; |
| 106 } |
| 107 if (texts[0].empty() || (texts.size() == 2 && texts[1].empty())) { |
| 108 return false; |
| 109 } |
| 110 return true; |
| 111 } |
| 112 |
| 113 void BookmarkInputWindowDialogController::InputAccepted( |
| 114 const InputWindowDialog::InputTexts& texts) { |
| 115 if (details_.type == BookmarkEditor::EditDetails::EXISTING_NODE) { |
| 116 if (details_.existing_node->is_url()) { |
| 117 DCHECK_EQ(2U, texts.size()); |
| 118 model_->SetTitle(details_.existing_node, texts[0]); |
| 119 model_->SetURL(details_.existing_node, GURL(texts[1])); |
| 120 } else { |
| 121 DCHECK_EQ(1U, texts.size()); |
| 122 model_->SetTitle(details_.existing_node, texts[0]); |
| 123 } |
| 124 } else if (details_.type == BookmarkEditor::EditDetails::NEW_FOLDER) { |
| 125 DCHECK_EQ(1U, texts.size()); |
| 126 // TODO(mazda): Add |details_.urls| to AddFolder. |
| 127 model_->AddFolder(details_.parent_node, details_.index, texts[0]); |
| 128 } else if (details_.type == BookmarkEditor::EditDetails::NEW_URL) { |
| 129 DCHECK_EQ(2U, texts.size()); |
| 130 model_->AddURL(details_.parent_node, details_.index, texts[0], |
| 131 GURL(texts[1])); |
| 132 } |
| 133 } |
| 134 |
| 135 void BookmarkInputWindowDialogController::InputCanceled() { |
| 136 } |
| 137 |
| 138 void BookmarkInputWindowDialogController::BookmarkModelChanged() { |
| 139 dialog_->Close(); |
| 140 } |
| 141 |
| 142 void BookmarkInputWindowDialogController::BookmarkModelBeingDeleted( |
| 143 BookmarkModel* model) { |
| 144 model_->RemoveObserver(this); |
| 145 model_ = NULL; |
| 146 BookmarkModelChanged(); |
| 147 } |
| OLD | NEW |