summaryrefslogtreecommitdiffstats
path: root/chrome/browser/gtk/options/content_exception_editor.cc
blob: e0dfc8396dff90e8dbc119c4fdddf19afa71c86b (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
// 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/content_exception_editor.h"

#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "base/message_loop.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/content_exceptions_table_model.h"
#include "chrome/browser/gtk/gtk_util.h"
#include "chrome/browser/host_content_settings_map.h"
#include "grit/app_resources.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"

ContentExceptionEditor::ContentExceptionEditor(
    GtkWindow* parent,
    ContentExceptionEditor::Delegate* delegate,
    ContentExceptionsTableModel* model,
    bool allow_off_the_record,
    int index,
    const HostContentSettingsMap::Pattern& pattern,
    ContentSetting setting,
    bool is_off_the_record)
    : delegate_(delegate),
      model_(model),
      cb_model_(model->content_type()),
      index_(index),
      pattern_(pattern),
      setting_(setting) {
  dialog_ = gtk_dialog_new_with_buttons(
      l10n_util::GetStringUTF8(is_new() ?
                               IDS_EXCEPTION_EDITOR_NEW_TITLE :
                               IDS_EXCEPTION_EDITOR_TITLE).c_str(),
      parent,
      // Non-modal.
      static_cast<GtkDialogFlags>(GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR),
      GTK_STOCK_CANCEL,
      GTK_RESPONSE_CANCEL,
      GTK_STOCK_OK,
      GTK_RESPONSE_OK,
      NULL);
  gtk_dialog_set_default_response(GTK_DIALOG(dialog_), GTK_RESPONSE_OK);

  entry_ = gtk_entry_new();
  gtk_entry_set_text(GTK_ENTRY(entry_), pattern_.AsString().c_str());
  g_signal_connect(entry_, "changed", G_CALLBACK(OnEntryChangedThunk), this);
  gtk_entry_set_activates_default(GTK_ENTRY(entry_), TRUE);

  pattern_image_ = gtk_image_new_from_pixbuf(NULL);

  action_combo_ = gtk_combo_box_new_text();
  for (int i = 0; i < cb_model_.GetItemCount(); ++i) {
    gtk_combo_box_append_text(GTK_COMBO_BOX(action_combo_),
        UTF16ToUTF8(cb_model_.GetItemAt(i)).c_str());
  }
  gtk_combo_box_set_active(GTK_COMBO_BOX(action_combo_),
                           cb_model_.IndexForSetting(setting_));

  otr_checkbox_ = gtk_check_button_new_with_label(
      l10n_util::GetStringUTF8(IDS_EXCEPTION_EDITOR_OTR_TITLE).c_str());
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(otr_checkbox_),
                               is_off_the_record);

  GtkWidget* table = gtk_util::CreateLabeledControlsGroup(
      NULL,
      l10n_util::GetStringUTF8(IDS_EXCEPTION_EDITOR_PATTERN_TITLE).c_str(),
      gtk_util::CreateEntryImageHBox(entry_, pattern_image_),
      l10n_util::GetStringUTF8(IDS_EXCEPTION_EDITOR_ACTION_TITLE).c_str(),
      action_combo_,
      NULL);
  if (allow_off_the_record) {
    gtk_table_attach(GTK_TABLE(table), otr_checkbox_,
                     0, 2, 2, 3, GTK_FILL, GTK_FILL, 0, 0);
  }
  gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog_)->vbox), table,
                     FALSE, FALSE, 0);

  gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog_)->vbox),
                      gtk_util::kContentAreaSpacing);

  // Prime the state of the buttons.
  OnEntryChanged(entry_);

  gtk_util::ShowDialog(dialog_);

  g_signal_connect(dialog_, "response", G_CALLBACK(OnResponseThunk), this);
  g_signal_connect(dialog_, "destroy", G_CALLBACK(OnWindowDestroyThunk), this);
}

bool ContentExceptionEditor::IsPatternValid(
    const HostContentSettingsMap::Pattern& pattern,
    bool is_off_the_record) const {
  bool is_valid_pattern = pattern.IsValid() &&
      (model_->IndexOfExceptionByPattern(pattern, is_off_the_record) == -1);

  return is_new() ? is_valid_pattern : (!pattern.AsString().empty() &&
      ((pattern_ == pattern) || is_valid_pattern));
}

void ContentExceptionEditor::UpdateImage(GtkWidget* image, bool is_valid) {
  return gtk_image_set_from_pixbuf(GTK_IMAGE(image),
      ResourceBundle::GetSharedInstance().GetPixbufNamed(
          is_valid ? IDR_INPUT_GOOD : IDR_INPUT_ALERT));
}

void ContentExceptionEditor::OnEntryChanged(GtkWidget* entry) {
  HostContentSettingsMap::Pattern new_pattern(
      gtk_entry_get_text(GTK_ENTRY(entry)));
  bool is_off_the_record =
      gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(otr_checkbox_));
  bool is_valid = IsPatternValid(new_pattern, is_off_the_record);
  gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog_),
                                    GTK_RESPONSE_OK, is_valid);
  UpdateImage(pattern_image_, is_valid);
}

void ContentExceptionEditor::OnResponse(GtkWidget* sender, int response_id) {
  if (response_id == GTK_RESPONSE_OK) {
    // Notify our delegate to update everything.
    HostContentSettingsMap::Pattern new_pattern(
        gtk_entry_get_text(GTK_ENTRY(entry_)));
    ContentSetting setting = cb_model_.SettingForIndex(
        gtk_combo_box_get_active(GTK_COMBO_BOX(action_combo_)));
    bool is_off_the_record =
        gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(otr_checkbox_));
    delegate_->AcceptExceptionEdit(
        new_pattern, setting, is_off_the_record, index_, is_new());
  }

  gtk_widget_destroy(dialog_);
}

void ContentExceptionEditor::OnWindowDestroy(GtkWidget* widget) {
  MessageLoop::current()->DeleteSoon(FROM_HERE, this);
}