summaryrefslogtreecommitdiffstats
path: root/chrome/browser/gtk/options/passwords_page_gtk.cc
blob: 02f53b3dc22d8445f609611608e53e15d2b33a6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/gtk/options/passwords_page_gtk.h"

#include <string>

#include "app/gtk_util.h"
#include "app/l10n_util.h"
#include "base/stl_util-inl.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/gtk/gtk_tree.h"
#include "chrome/browser/gtk/gtk_util.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/common/notification_details.h"
#include "chrome/common/notification_type.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "gfx/gtk_util.h"
#include "grit/app_resources.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "net/base/net_util.h"

namespace {

// Initial width of the first column.
const int kSiteColumnInitialSize = 265;

// Column ids for |password_list_store_|.
enum {
  COL_SITE,
  COL_USERNAME,
  COL_COUNT,
};

}  // anonymous namespace

///////////////////////////////////////////////////////////////////////////////
// PasswordsPageGtk, public:

PasswordsPageGtk::PasswordsPageGtk(Profile* profile)
    : populater(this), password_showing_(false), profile_(profile) {
  allow_show_passwords_.Init(prefs::kPasswordManagerAllowShowPasswords,
                             profile->GetPrefs(),
                             this);

  remove_button_ = gtk_button_new_with_label(
      l10n_util::GetStringUTF8(IDS_PASSWORDS_PAGE_VIEW_REMOVE_BUTTON).c_str());
  gtk_widget_set_sensitive(remove_button_, FALSE);
  g_signal_connect(remove_button_, "clicked",
                   G_CALLBACK(OnRemoveButtonClickedThunk), this);
  remove_all_button_ = gtk_button_new_with_label(l10n_util::GetStringUTF8(
          IDS_PASSWORDS_PAGE_VIEW_REMOVE_ALL_BUTTON).c_str());
  gtk_widget_set_sensitive(remove_all_button_, FALSE);
  g_signal_connect(remove_all_button_, "clicked",
                   G_CALLBACK(OnRemoveAllButtonClickedThunk), this);

  // We start with the "hide password" text but change it in the realize event.
  show_password_button_ = gtk_button_new_with_label(
      l10n_util::GetStringUTF8(IDS_PASSWORDS_PAGE_VIEW_HIDE_BUTTON).c_str());
  gtk_widget_set_no_show_all(show_password_button_, true);
  gtk_widget_set_sensitive(show_password_button_, FALSE);
  g_signal_connect(show_password_button_, "clicked",
                   G_CALLBACK(OnShowPasswordButtonClickedThunk), this);
  g_signal_connect(show_password_button_, "realize",
                   G_CALLBACK(OnShowPasswordButtonRealizedThunk), this);

  password_ = gtk_label_new("");
  gtk_label_set_selectable(GTK_LABEL(password_), TRUE);
  gtk_widget_set_no_show_all(password_, true);

  GtkWidget* buttons = gtk_vbox_new(FALSE, gtk_util::kControlSpacing);
  gtk_box_pack_start(GTK_BOX(buttons), remove_button_, FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(buttons), remove_all_button_, FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(buttons), show_password_button_, FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(buttons), password_, FALSE, FALSE, 0);

  GtkWidget* scroll_window = gtk_scrolled_window_new(NULL, NULL);
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll_window),
                                 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
  gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll_window),
                                      GTK_SHADOW_ETCHED_IN);

  // Sets password_tree_ among other things.
  InitPasswordTree();
  gtk_container_add(GTK_CONTAINER(scroll_window), password_tree_);

  page_ = gtk_hbox_new(FALSE, gtk_util::kControlSpacing);
  gtk_container_set_border_width(GTK_CONTAINER(page_),
                                 gtk_util::kContentAreaBorder);
  gtk_box_pack_end(GTK_BOX(page_), buttons, FALSE, FALSE, 0);
  gtk_box_pack_end(GTK_BOX(page_), scroll_window, TRUE, TRUE, 0);

  // Initialize UI state based on current preference values.
  OnPrefChanged(prefs::kPasswordManagerAllowShowPasswords);
}

PasswordsPageGtk::~PasswordsPageGtk() {
  STLDeleteElements(&password_list_);
}

///////////////////////////////////////////////////////////////////////////////
// PasswordsPageGtk, private:

void PasswordsPageGtk::InitPasswordTree() {
  password_list_store_ = gtk_list_store_new(COL_COUNT,
                                            G_TYPE_STRING,
                                            G_TYPE_STRING);
  password_list_sort_ = gtk_tree_model_sort_new_with_model(
      GTK_TREE_MODEL(password_list_store_));
  gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(password_list_sort_),
                                  COL_SITE, CompareSite, this, NULL);
  gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(password_list_sort_),
                                  COL_USERNAME, CompareUsername, this, NULL);
  password_tree_ = gtk_tree_view_new_with_model(password_list_sort_);
  g_object_unref(password_list_store_);
  g_object_unref(password_list_sort_);
  gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(password_tree_), TRUE);

  password_selection_ = gtk_tree_view_get_selection(
      GTK_TREE_VIEW(password_tree_));
  gtk_tree_selection_set_mode(password_selection_,
                              GTK_SELECTION_SINGLE);
  g_signal_connect(password_selection_, "changed",
                   G_CALLBACK(OnPasswordSelectionChangedThunk), this);

  GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes(
      l10n_util::GetStringUTF8(IDS_PASSWORDS_PAGE_VIEW_SITE_COLUMN).c_str(),
      gtk_cell_renderer_text_new(),
      "text", COL_SITE,
      NULL);
  gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
  gtk_tree_view_column_set_resizable(column, TRUE);
  gtk_tree_view_column_set_fixed_width(column, kSiteColumnInitialSize);
  gtk_tree_view_column_set_sort_column_id(column, COL_SITE);
  gtk_tree_view_append_column(GTK_TREE_VIEW(password_tree_), column);

  column = gtk_tree_view_column_new_with_attributes(
      l10n_util::GetStringUTF8(IDS_PASSWORDS_PAGE_VIEW_USERNAME_COLUMN).c_str(),
      gtk_cell_renderer_text_new(),
      "text", COL_USERNAME,
      NULL);
  gtk_tree_view_column_set_sort_column_id(column, COL_USERNAME);
  gtk_tree_view_append_column(GTK_TREE_VIEW(password_tree_), column);
  populater.populate();
}

PasswordStore* PasswordsPageGtk::GetPasswordStore() {
    return profile_->GetPasswordStore(Profile::EXPLICIT_ACCESS);
}

void PasswordsPageGtk::SetPasswordList(
    const std::vector<webkit_glue::PasswordForm*>& result) {
  std::string languages =
      profile_->GetPrefs()->GetString(prefs::kAcceptLanguages);
  gtk_list_store_clear(password_list_store_);
  STLDeleteElements(&password_list_);
  password_list_ = result;
  for (size_t i = 0; i < result.size(); ++i) {
    GtkTreeIter iter;
    gtk_list_store_insert_with_values(password_list_store_, &iter, (gint) i,
        COL_SITE,
        UTF16ToUTF8(net::FormatUrl(result[i]->origin, languages)).c_str(),
        COL_USERNAME, UTF16ToUTF8(result[i]->username_value).c_str(), -1);
  }
  gtk_widget_set_sensitive(remove_all_button_, result.size() > 0);
}

void PasswordsPageGtk::HidePassword() {
  password_showing_ = false;
  gtk_label_set_text(GTK_LABEL(password_), "");
  gtk_button_set_label(GTK_BUTTON(show_password_button_),
      l10n_util::GetStringUTF8(IDS_PASSWORDS_PAGE_VIEW_SHOW_BUTTON).c_str());
}

void PasswordsPageGtk::Observe(NotificationType type,
                               const NotificationSource& source,
                               const NotificationDetails& details) {
  DCHECK_EQ(NotificationType::PREF_CHANGED, type.value);
  const std::string* pref_name = Details<std::string>(details).ptr();
  OnPrefChanged(*pref_name);
}

void PasswordsPageGtk::OnPrefChanged(const std::string& pref_name) {
  if (pref_name == prefs::kPasswordManagerAllowShowPasswords) {
    if (allow_show_passwords_.GetValue()) {
      gtk_widget_show(show_password_button_);
      gtk_widget_show(password_);
    } else {
      HidePassword();
      gtk_widget_hide(show_password_button_);
      gtk_widget_hide(password_);
    }
  } else {
    NOTREACHED();
  }
}

void PasswordsPageGtk::OnRemoveButtonClicked(GtkWidget* widget) {
  GtkTreeIter iter;
  if (!gtk_tree_selection_get_selected(password_selection_,
                                       NULL, &iter)) {
    NOTREACHED();
    return;
  }

  GtkTreePath* path = gtk_tree_model_get_path(
      GTK_TREE_MODEL(password_list_sort_), &iter);
  gint index = gtk_tree::GetTreeSortChildRowNumForPath(
      password_list_sort_, path);
  gtk_tree_path_free(path);

  GtkTreeIter child_iter;
  gtk_tree_model_sort_convert_iter_to_child_iter(
      GTK_TREE_MODEL_SORT(password_list_sort_), &child_iter, &iter);

  // Remove from GTK list, DB, and vector.
  gtk_list_store_remove(password_list_store_, &child_iter);
  GetPasswordStore()->RemoveLogin(*password_list_[index]);
  delete password_list_[index];
  password_list_.erase(password_list_.begin() + index);

  gtk_widget_set_sensitive(remove_all_button_, password_list_.size() > 0);
}

void PasswordsPageGtk::OnRemoveAllButtonClicked(GtkWidget* widget) {
  GtkWindow* window = GTK_WINDOW(gtk_widget_get_toplevel(page_));
  GtkWidget* confirm = gtk_message_dialog_new(
      window,
      static_cast<GtkDialogFlags>(
          GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
      GTK_MESSAGE_QUESTION,
      GTK_BUTTONS_YES_NO,
      "%s",
      l10n_util::GetStringUTF8(
          IDS_PASSWORDS_PAGE_VIEW_TEXT_DELETE_ALL_PASSWORDS).c_str());
  gtk_util::ApplyMessageDialogQuirks(confirm);
  gtk_window_set_title(GTK_WINDOW(confirm), l10n_util::GetStringUTF8(
          IDS_PASSWORDS_PAGE_VIEW_CAPTION_DELETE_ALL_PASSWORDS).c_str());
  g_signal_connect(confirm, "response",
                   G_CALLBACK(OnRemoveAllConfirmResponseThunk), this);
  gtk_widget_show_all(confirm);
}

void PasswordsPageGtk::OnRemoveAllConfirmResponse(GtkWidget* confirm,
                                                  gint response) {
  bool confirmed = false;
  switch (response) {
    case GTK_RESPONSE_YES:
      confirmed = true;
      break;
    default:
      break;
  }
  gtk_widget_destroy(confirm);
  if (!confirmed)
    return;

  // Remove from GTK list, DB, and vector.
  PasswordStore* store = GetPasswordStore();
  gtk_list_store_clear(password_list_store_);
  for (size_t i = 0; i < password_list_.size(); ++i)
    store->RemoveLogin(*password_list_[i]);
  STLDeleteElements(&password_list_);
  gtk_widget_set_sensitive(remove_all_button_, FALSE);
}

void PasswordsPageGtk::OnShowPasswordButtonClicked(GtkWidget* widget) {
  if (password_showing_ || !allow_show_passwords_.GetValue()) {
    // Hide the password.
    HidePassword();
    return;
  }
  // Show the password.
  password_showing_ = true;
  GtkTreeIter iter;
  if (!gtk_tree_selection_get_selected(password_selection_,
                                       NULL, &iter)) {
    NOTREACHED();
    return;
  }
  GtkTreePath* path = gtk_tree_model_get_path(
      GTK_TREE_MODEL(password_list_sort_), &iter);
  gint index = gtk_tree::GetTreeSortChildRowNumForPath(
      password_list_sort_, path);
  gtk_tree_path_free(path);
  std::string pass = UTF16ToUTF8(password_list_[index]->password_value);
  gtk_label_set_text(GTK_LABEL(password_), pass.c_str());
  gtk_button_set_label(GTK_BUTTON(show_password_button_),
      l10n_util::GetStringUTF8(IDS_PASSWORDS_PAGE_VIEW_HIDE_BUTTON).c_str());
}

void PasswordsPageGtk::OnShowPasswordButtonRealized(GtkWidget* widget) {
  // We have just realized the "show password" button, so we can now accurately
  // find out how big it needs to be in order to accomodate both the "show" and
  // "hide" labels. (This requires font information to work correctly.) The
  // button starts with the "hide" label so we only have to change it once.
  GtkRequisition hide_size, show_size;
  // Get the size request of the button with the "hide password" text.
  gtk_widget_size_request(show_password_button_, &hide_size);
  gtk_button_set_label(GTK_BUTTON(show_password_button_),
      l10n_util::GetStringUTF8(IDS_PASSWORDS_PAGE_VIEW_SHOW_BUTTON).c_str());
  // Get the size request of the button with the "show password" text.
  gtk_widget_size_request(show_password_button_, &show_size);
  // Determine the maximum width and height.
  if (hide_size.width > show_size.width)
    show_size.width = hide_size.width;
  if (hide_size.height > show_size.height)
    show_size.height = hide_size.height;
  // Force the button to be large enough for both labels.
  gtk_widget_set_size_request(show_password_button_, show_size.width,
                              show_size.height);
}

void PasswordsPageGtk::OnPasswordSelectionChanged(GtkTreeSelection* selection) {
  // No matter how the selection changed, we want to hide the old password.
  HidePassword();

  GtkTreeIter iter;
  if (!gtk_tree_selection_get_selected(selection, NULL, &iter)) {
    gtk_widget_set_sensitive(show_password_button_, FALSE);
    gtk_widget_set_sensitive(remove_button_, FALSE);
    return;
  }
  gtk_widget_set_sensitive(show_password_button_, TRUE);
  gtk_widget_set_sensitive(remove_button_, TRUE);
}

// static
gint PasswordsPageGtk::CompareSite(GtkTreeModel* model,
                                   GtkTreeIter* a, GtkTreeIter* b,
                                   gpointer window) {
  int row1 = gtk_tree::GetRowNumForIter(model, a);
  int row2 = gtk_tree::GetRowNumForIter(model, b);
  PasswordsPageGtk* page = reinterpret_cast<PasswordsPageGtk*>(window);
  return page->password_list_[row1]->origin.spec().compare(
         page->password_list_[row2]->origin.spec());
}

// static
gint PasswordsPageGtk::CompareUsername(GtkTreeModel* model,
                                       GtkTreeIter* a, GtkTreeIter* b,
                                       gpointer window) {
  int row1 = gtk_tree::GetRowNumForIter(model, a);
  int row2 = gtk_tree::GetRowNumForIter(model, b);
  PasswordsPageGtk* page = reinterpret_cast<PasswordsPageGtk*>(window);
  return page->password_list_[row1]->username_value.compare(
         page->password_list_[row2]->username_value);
}

void PasswordsPageGtk::PasswordListPopulater::populate() {
  DCHECK(!pending_login_query_);
  PasswordStore* store = page_->GetPasswordStore();
  pending_login_query_ = store->GetAutofillableLogins(this);
}

void PasswordsPageGtk::PasswordListPopulater::OnPasswordStoreRequestDone(
    int handle, const std::vector<webkit_glue::PasswordForm*>& result) {
  DCHECK_EQ(pending_login_query_, handle);
  pending_login_query_ = 0;
  page_->SetPasswordList(result);
}