Chromium Code Reviews| 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 model_->AddFolder(details_.parent_node, details_.index, texts[0]); | |
|
flackr
2011/11/07 15:19:15
At this point we could add details_.urls to the fo
mazda
2011/11/08 11:24:24
May I do it in another CL? Adding an argument make
flackr
2011/11/08 13:48:54
Of course, no problem.
| |
| 127 } else if (details_.type == BookmarkEditor::EditDetails::NEW_URL) { | |
| 128 DCHECK_EQ(2U, texts.size()); | |
| 129 model_->AddURL(details_.parent_node, details_.index, texts[0], | |
| 130 GURL(texts[1])); | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 void BookmarkInputWindowDialogController::InputCanceled() { | |
| 135 } | |
| 136 | |
| 137 void BookmarkInputWindowDialogController::BookmarkModelChanged() { | |
| 138 dialog_->Close(); | |
| 139 } | |
| 140 | |
| 141 void BookmarkInputWindowDialogController::BookmarkModelBeingDeleted( | |
| 142 BookmarkModel* model) { | |
| 143 model_->RemoveObserver(this); | |
| 144 model_ = NULL; | |
| 145 BookmarkModelChanged(); | |
| 146 } | |
| OLD | NEW |