| 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(BookmarkInputWindowDialogController::Type type) { |
| 17 int title_id = 0; |
| 18 switch (type) { |
| 19 case BookmarkInputWindowDialogController::NEW_BOOKMARK_FOLDER: |
| 20 title_id = IDS_BOOKMARK_FOLDER_EDITOR_WINDOW_TITLE_NEW; |
| 21 break; |
| 22 case BookmarkInputWindowDialogController::EXISTING_BOOKMARK_FOLDER: |
| 23 title_id = IDS_BOOKMARK_FOLDER_EDITOR_WINDOW_TITLE; |
| 24 break; |
| 25 case BookmarkInputWindowDialogController::NEW_BOOKMARK_PAGE: |
| 26 case BookmarkInputWindowDialogController::EXISTING_BOOKMARK_PAGE: |
| 27 title_id = IDS_BOOKMARK_EDITOR_TITLE; |
| 28 break; |
| 29 default: |
| 30 NOTREACHED(); |
| 31 break; |
| 32 } |
| 33 return l10n_util::GetStringUTF16(title_id); |
| 34 } |
| 35 } // namespace |
| 36 |
| 37 BookmarkInputWindowDialogController::~BookmarkInputWindowDialogController() { |
| 38 if (model_) |
| 39 model_->RemoveObserver(this); |
| 40 } |
| 41 |
| 42 // static |
| 43 void BookmarkInputWindowDialogController::Show(Profile* profile, |
| 44 gfx::NativeWindow wnd, |
| 45 const BookmarkNode* node, |
| 46 int index, |
| 47 Type type) { |
| 48 // BookmarkInputWindowDialogController deletes itself when done. |
| 49 new BookmarkInputWindowDialogController(profile, wnd, node, index, type); |
| 50 } |
| 51 |
| 52 BookmarkInputWindowDialogController::BookmarkInputWindowDialogController( |
| 53 Profile* profile, |
| 54 gfx::NativeWindow wnd, |
| 55 const BookmarkNode* node, |
| 56 int index, |
| 57 Type type) |
| 58 : profile_(profile), |
| 59 model_(profile->GetBookmarkModel()), |
| 60 node_(node), |
| 61 index_(index), |
| 62 type_(type) { |
| 63 |
| 64 model_->AddObserver(this); |
| 65 |
| 66 InputWindowDialog::LabelContentsPairs label_contents_pairs; |
| 67 string16 name_label = |
| 68 l10n_util::GetStringUTF16(IDS_BOOKMARK_BAR_EDIT_FOLDER_LABEL); |
| 69 if (type == NEW_BOOKMARK_PAGE || type == EXISTING_BOOKMARK_PAGE) { |
| 70 string16 url_label = |
| 71 l10n_util::GetStringUTF16(IDS_BOOKMARK_EDITOR_URL_LABEL); |
| 72 string16 name; |
| 73 GURL url; |
| 74 if (type == NEW_BOOKMARK_PAGE) { |
| 75 bookmark_utils::GetURLAndTitleToBookmarkFromCurrentTab(profile, |
| 76 &url, |
| 77 &name); |
| 78 } else { |
| 79 url = node_->url(); |
| 80 name = 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 = (type == NEW_BOOKMARK_FOLDER) ? |
| 87 l10n_util::GetStringUTF16(IDS_BOOKMARK_EDITOR_NEW_FOLDER_NAME) : |
| 88 node_->GetTitle(); |
| 89 label_contents_pairs.push_back(std::make_pair(name_label, name)); |
| 90 } |
| 91 |
| 92 const InputWindowDialog::ButtonType button_type = |
| 93 (type == NEW_BOOKMARK_FOLDER || type == NEW_BOOKMARK_PAGE) ? |
| 94 InputWindowDialog::BUTTON_TYPE_ADD : InputWindowDialog::BUTTON_TYPE_SAVE; |
| 95 dialog_ = InputWindowDialog::Create(wnd, |
| 96 GetWindowTitle(type), |
| 97 label_contents_pairs, this, button_type); |
| 98 dialog_->Show(); |
| 99 } |
| 100 |
| 101 bool BookmarkInputWindowDialogController::IsValid( |
| 102 const InputWindowDialog::InputTexts& texts) { |
| 103 if (texts.size() != 1 && texts.size() != 2) { |
| 104 return false; |
| 105 } |
| 106 if (texts[0].empty() || (texts.size() == 2 && texts[1].empty())) { |
| 107 return false; |
| 108 } |
| 109 return true; |
| 110 } |
| 111 |
| 112 void BookmarkInputWindowDialogController::InputAccepted( |
| 113 const InputWindowDialog::InputTexts& texts) { |
| 114 if (type_ == NEW_BOOKMARK_FOLDER) { |
| 115 DCHECK_EQ(1U, texts.size()); |
| 116 model_->AddFolder(node_, index_, texts[0]); |
| 117 } else if (type_ == EXISTING_BOOKMARK_FOLDER) { |
| 118 DCHECK_EQ(1U, texts.size()); |
| 119 model_->SetTitle(node_, texts[0]); |
| 120 } else if (type_ == NEW_BOOKMARK_PAGE) { |
| 121 DCHECK_EQ(2U, texts.size()); |
| 122 model_->AddURL(node_, index_, texts[0], GURL(texts[1])); |
| 123 } else if (type_ == EXISTING_BOOKMARK_PAGE) { |
| 124 DCHECK_EQ(2U, texts.size()); |
| 125 model_->SetTitle(node_, texts[0]); |
| 126 model_->SetURL(node_, GURL(texts[1])); |
| 127 } |
| 128 } |
| 129 |
| 130 void BookmarkInputWindowDialogController::InputCanceled() { |
| 131 } |
| 132 |
| 133 void BookmarkInputWindowDialogController::BookmarkModelChanged() { |
| 134 dialog_->Close(); |
| 135 } |
| 136 |
| 137 void BookmarkInputWindowDialogController::BookmarkModelBeingDeleted( |
| 138 BookmarkModel* model) { |
| 139 model_->RemoveObserver(this); |
| 140 model_ = NULL; |
| 141 BookmarkModelChanged(); |
| 142 } |
| OLD | NEW |