| 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/ui/views/options/exceptions_page_view.h" | |
| 6 | |
| 7 #include "base/string_util.h" | |
| 8 #include "base/utf_string_conversions.h" | |
| 9 #include "chrome/browser/prefs/pref_service.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "chrome/common/pref_names.h" | |
| 12 #include "grit/generated_resources.h" | |
| 13 #include "ui/base/l10n/l10n_util.h" | |
| 14 #include "ui/base/text/text_elider.h" | |
| 15 #include "views/background.h" | |
| 16 #include "views/controls/button/native_button.h" | |
| 17 #include "views/layout/grid_layout.h" | |
| 18 #include "views/layout/layout_constants.h" | |
| 19 | |
| 20 using views::ColumnSet; | |
| 21 using views::GridLayout; | |
| 22 using webkit_glue::PasswordForm; | |
| 23 | |
| 24 /////////////////////////////////////////////////////////////////////////////// | |
| 25 // ExceptionsTableModel | |
| 26 ExceptionsTableModel::ExceptionsTableModel(Profile* profile) | |
| 27 : PasswordsTableModel(profile) { | |
| 28 } | |
| 29 | |
| 30 ExceptionsTableModel::~ExceptionsTableModel() { | |
| 31 } | |
| 32 | |
| 33 string16 ExceptionsTableModel::GetText(int row, int col_id) { | |
| 34 DCHECK_EQ(col_id, IDS_PASSWORDS_PAGE_VIEW_SITE_COLUMN); | |
| 35 return PasswordsTableModel::GetText(row, col_id); | |
| 36 } | |
| 37 | |
| 38 int ExceptionsTableModel::CompareValues(int row1, int row2, | |
| 39 int col_id) { | |
| 40 DCHECK_EQ(col_id, IDS_PASSWORDS_PAGE_VIEW_SITE_COLUMN); | |
| 41 return PasswordsTableModel::CompareValues(row1, row2, col_id); | |
| 42 } | |
| 43 | |
| 44 void ExceptionsTableModel::GetAllExceptionsForProfile() { | |
| 45 DCHECK(!pending_login_query_); | |
| 46 pending_login_query_ = password_store()->GetBlacklistLogins(this); | |
| 47 } | |
| 48 | |
| 49 void ExceptionsTableModel::OnPasswordStoreRequestDone( | |
| 50 int handle, const std::vector<webkit_glue::PasswordForm*>& result) { | |
| 51 DCHECK_EQ(pending_login_query_, handle); | |
| 52 pending_login_query_ = NULL; | |
| 53 | |
| 54 STLDeleteElements<PasswordRows>(&saved_signons_); | |
| 55 std::string languages = profile_->GetPrefs()->GetString( | |
| 56 prefs::kAcceptLanguages); | |
| 57 for (size_t i = 0; i < result.size(); ++i) { | |
| 58 saved_signons_.push_back(new PasswordRow( | |
| 59 ui::SortedDisplayURL(result[i]->origin, languages), result[i])); | |
| 60 } | |
| 61 if (observer_) | |
| 62 observer_->OnModelChanged(); | |
| 63 if (row_count_observer_) | |
| 64 row_count_observer_->OnRowCountChanged(RowCount()); | |
| 65 } | |
| 66 | |
| 67 /////////////////////////////////////////////////////////////////////////////// | |
| 68 // ExceptionsPageView, public | |
| 69 ExceptionsPageView::ExceptionsPageView(Profile* profile) | |
| 70 : OptionsPageView(profile), | |
| 71 ALLOW_THIS_IN_INITIALIZER_LIST(show_button_( | |
| 72 this, | |
| 73 UTF16ToWide(l10n_util::GetStringUTF16( | |
| 74 IDS_PASSWORDS_PAGE_VIEW_SHOW_BUTTON)), | |
| 75 UTF16ToWide(l10n_util::GetStringUTF16( | |
| 76 IDS_PASSWORDS_PAGE_VIEW_HIDE_BUTTON)))), | |
| 77 ALLOW_THIS_IN_INITIALIZER_LIST(remove_button_( | |
| 78 this, | |
| 79 UTF16ToWide(l10n_util::GetStringUTF16( | |
| 80 IDS_PASSWORDS_PAGE_VIEW_REMOVE_BUTTON)))), | |
| 81 ALLOW_THIS_IN_INITIALIZER_LIST(remove_all_button_( | |
| 82 this, | |
| 83 UTF16ToWide(l10n_util::GetStringUTF16( | |
| 84 IDS_PASSWORDS_PAGE_VIEW_REMOVE_ALL_BUTTON)))), | |
| 85 table_model_(profile), | |
| 86 table_view_(NULL) { | |
| 87 } | |
| 88 | |
| 89 ExceptionsPageView::~ExceptionsPageView() { | |
| 90 // The model is going away, prevent the table from accessing it. | |
| 91 if (table_view_) | |
| 92 table_view_->SetModel(NULL); | |
| 93 } | |
| 94 | |
| 95 void ExceptionsPageView::OnSelectionChanged() { | |
| 96 bool has_selection = table_view_->SelectedRowCount() > 0; | |
| 97 remove_button_.SetEnabled(has_selection); | |
| 98 } | |
| 99 | |
| 100 void ExceptionsPageView::ButtonPressed( | |
| 101 views::Button* sender, const views::Event& event) { | |
| 102 // Close will result in our destruction. | |
| 103 if (sender == &remove_all_button_) { | |
| 104 table_model_.ForgetAndRemoveAllSignons(); | |
| 105 return; | |
| 106 } | |
| 107 | |
| 108 // The following require a selection (and only one, since table is single- | |
| 109 // select only). | |
| 110 views::TableSelectionIterator iter = table_view_->SelectionBegin(); | |
| 111 int row = *iter; | |
| 112 DCHECK(++iter == table_view_->SelectionEnd()); | |
| 113 | |
| 114 if (sender == &remove_button_) { | |
| 115 table_model_.ForgetAndRemoveSignon(row); | |
| 116 } else { | |
| 117 NOTREACHED() << "Invalid button."; | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 void ExceptionsPageView::OnRowCountChanged(size_t rows) { | |
| 122 remove_all_button_.SetEnabled(rows > 0); | |
| 123 } | |
| 124 | |
| 125 /////////////////////////////////////////////////////////////////////////////// | |
| 126 // ExceptionsPageView, protected | |
| 127 void ExceptionsPageView::InitControlLayout() { | |
| 128 SetupButtons(); | |
| 129 SetupTable(); | |
| 130 | |
| 131 // Do the layout thing. | |
| 132 GridLayout* layout = GridLayout::CreatePanel(this); | |
| 133 SetLayoutManager(layout); | |
| 134 | |
| 135 const int top_column_set_id = 0; | |
| 136 | |
| 137 // Design the grid. | |
| 138 ColumnSet* column_set = layout->AddColumnSet(top_column_set_id); | |
| 139 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1, | |
| 140 GridLayout::USE_PREF, 0, 0); | |
| 141 column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing); | |
| 142 column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0, | |
| 143 GridLayout::USE_PREF, 0, 0); | |
| 144 | |
| 145 // Fill the grid. | |
| 146 layout->StartRow(0, top_column_set_id); | |
| 147 layout->AddView(table_view_, 1, 6, GridLayout::FILL, | |
| 148 GridLayout::FILL); | |
| 149 layout->AddView(&remove_button_); | |
| 150 layout->StartRowWithPadding(0, top_column_set_id, 0, | |
| 151 views::kRelatedControlVerticalSpacing); | |
| 152 layout->SkipColumns(1); | |
| 153 layout->AddView(&remove_all_button_); | |
| 154 layout->StartRowWithPadding(0, top_column_set_id, 0, | |
| 155 views::kRelatedControlVerticalSpacing); | |
| 156 | |
| 157 layout->SkipColumns(1); | |
| 158 layout->AddView(&show_button_); | |
| 159 layout->AddPaddingRow(1, 0); | |
| 160 | |
| 161 // Ask the database for exception data. | |
| 162 table_model_.GetAllExceptionsForProfile(); | |
| 163 } | |
| 164 | |
| 165 /////////////////////////////////////////////////////////////////////////////// | |
| 166 // ExceptionsPageView, private | |
| 167 void ExceptionsPageView::SetupButtons() { | |
| 168 // Disable all buttons in the first place. | |
| 169 remove_button_.set_parent_owned(false); | |
| 170 remove_button_.SetEnabled(false); | |
| 171 | |
| 172 remove_all_button_.set_parent_owned(false); | |
| 173 remove_all_button_.SetEnabled(false); | |
| 174 | |
| 175 show_button_.set_parent_owned(false); | |
| 176 show_button_.SetEnabled(false); | |
| 177 show_button_.SetVisible(false); | |
| 178 } | |
| 179 | |
| 180 void ExceptionsPageView::SetupTable() { | |
| 181 // Tell the table model we are concerned about how many rows it has. | |
| 182 table_model_.set_row_count_observer(this); | |
| 183 | |
| 184 // Creates the different columns for the table. | |
| 185 // The float resize values are the result of much tinkering. | |
| 186 std::vector<ui::TableColumn> columns; | |
| 187 columns.push_back(ui::TableColumn(IDS_PASSWORDS_PAGE_VIEW_SITE_COLUMN, | |
| 188 ui::TableColumn::LEFT, -1, 0.55f)); | |
| 189 columns.back().sortable = true; | |
| 190 table_view_ = new views::TableView(&table_model_, columns, views::TEXT_ONLY, | |
| 191 true, true, true); | |
| 192 // Make the table initially sorted by host. | |
| 193 views::TableView::SortDescriptors sort; | |
| 194 sort.push_back(views::TableView::SortDescriptor( | |
| 195 IDS_PASSWORDS_PAGE_VIEW_SITE_COLUMN, true)); | |
| 196 table_view_->SetSortDescriptors(sort); | |
| 197 table_view_->SetObserver(this); | |
| 198 } | |
| OLD | NEW |