Logo Search packages:      
Sourcecode: chromium-browser version File versions  Download package

geolocation_content_exceptions_window.cc

// 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/geolocation_content_exceptions_window.h"

#include "app/l10n_util.h"
#include "base/message_loop.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/geolocation/geolocation_content_settings_map.h"
#include "chrome/browser/gtk/gtk_util.h"
#include "gfx/gtk_util.h"
#include "grit/generated_resources.h"

namespace {

// Singleton for exception window.
GeolocationContentExceptionsWindow* instance = NULL;

}  // namespace

// static
void GeolocationContentExceptionsWindow::ShowExceptionsWindow(
    GtkWindow* parent, GeolocationContentSettingsMap* map) {
  DCHECK(map);

  if (!instance)
    instance = new GeolocationContentExceptionsWindow(parent, map);
}

GeolocationContentExceptionsWindow::GeolocationContentExceptionsWindow(
    GtkWindow* parent,
    GeolocationContentSettingsMap* map) {
  // Build the model adapters that translate views and TableModels into
  // something GTK can use.
  list_store_ = gtk_list_store_new(COL_COUNT, G_TYPE_STRING, G_TYPE_STRING);
  treeview_ = gtk_tree_view_new_with_model(GTK_TREE_MODEL(list_store_));
  g_object_unref(list_store_);

  // Set up the properties of the treeview
  GtkTreeViewColumn* hostname_column = gtk_tree_view_column_new_with_attributes(
      l10n_util::GetStringUTF8(IDS_EXCEPTIONS_HOSTNAME_HEADER).c_str(),
      gtk_cell_renderer_text_new(),
      "text", COL_HOSTNAME,
      NULL);
  gtk_tree_view_append_column(GTK_TREE_VIEW(treeview_), hostname_column);

  GtkTreeViewColumn* action_column = gtk_tree_view_column_new_with_attributes(
      l10n_util::GetStringUTF8(IDS_EXCEPTIONS_ACTION_HEADER).c_str(),
      gtk_cell_renderer_text_new(),
      "text", COL_ACTION,
      NULL);
  gtk_tree_view_append_column(GTK_TREE_VIEW(treeview_), action_column);

  treeview_selection_ = gtk_tree_view_get_selection(
      GTK_TREE_VIEW(treeview_));
  gtk_tree_selection_set_mode(treeview_selection_, GTK_SELECTION_MULTIPLE);
  g_signal_connect(treeview_selection_, "changed",
                   G_CALLBACK(OnTreeSelectionChangedThunk), this);

  // Bind |list_store_| to our C++ model.
  model_.reset(new GeolocationContentSettingsTableModel(map));
  model_adapter_.reset(new gtk_tree::TableAdapter(this, list_store_,
                                                  model_.get()));
  // Force a reload of everything to copy data into |list_store_|.
  model_adapter_->OnModelChanged();

  dialog_ = gtk_dialog_new_with_buttons(
      l10n_util::GetStringUTF8(IDS_GEOLOCATION_EXCEPTION_TITLE).c_str(),
      parent,
      static_cast<GtkDialogFlags>(GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR),
      GTK_STOCK_CLOSE,
      GTK_RESPONSE_CLOSE,
      NULL);
  gtk_window_set_default_size(GTK_WINDOW(dialog_), 500, 400);
  // Allow browser windows to go in front of the options dialog in metacity.
  gtk_window_set_type_hint(GTK_WINDOW(dialog_), GDK_WINDOW_TYPE_HINT_NORMAL);
  gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog_)->vbox),
                      gtk_util::kContentAreaSpacing);

  GtkWidget* hbox = gtk_hbox_new(FALSE, gtk_util::kControlSpacing);
  gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog_)->vbox), hbox);

  // Create a scrolled window to wrap the treeview widget.
  GtkWidget* scrolled = gtk_scrolled_window_new(NULL, NULL);
  gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled),
                                      GTK_SHADOW_ETCHED_IN);
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
                                 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
  gtk_container_add(GTK_CONTAINER(scrolled), treeview_);
  gtk_box_pack_start(GTK_BOX(hbox), scrolled, TRUE, TRUE, 0);

  GtkWidget* button_box = gtk_vbox_new(FALSE, gtk_util::kControlSpacing);

  remove_button_ = gtk_util::BuildDialogButton(dialog_,
                                               IDS_EXCEPTIONS_REMOVE_BUTTON,
                                               GTK_STOCK_REMOVE);

  g_signal_connect(remove_button_, "clicked", G_CALLBACK(RemoveThunk), this);
  gtk_box_pack_start(GTK_BOX(button_box), remove_button_, FALSE, FALSE, 0);

  remove_all_button_ = gtk_util::BuildDialogButton(
      dialog_,
      IDS_EXCEPTIONS_REMOVEALL_BUTTON,
      GTK_STOCK_CLEAR);
  g_signal_connect(remove_all_button_, "clicked", G_CALLBACK(RemoveAllThunk),
                   this);
  gtk_box_pack_start(GTK_BOX(button_box), remove_all_button_, FALSE, FALSE, 0);

  gtk_box_pack_start(GTK_BOX(hbox), button_box, FALSE, FALSE, 0);

  UpdateButtonState();

  gtk_widget_show_all(dialog_);

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

void GeolocationContentExceptionsWindow::SetColumnValues(int row,
                                                         GtkTreeIter* iter) {
  std::wstring hostname = model_->GetText(row, IDS_EXCEPTIONS_HOSTNAME_HEADER);
  gtk_list_store_set(list_store_, iter, COL_HOSTNAME,
                     WideToUTF8(hostname).c_str(), -1);

  std::wstring action = model_->GetText(row, IDS_EXCEPTIONS_ACTION_HEADER);
  gtk_list_store_set(list_store_, iter, COL_ACTION,
                     WideToUTF8(action).c_str(), -1);
}

void GeolocationContentExceptionsWindow::UpdateButtonState() {
  int row_count = gtk_tree_model_iter_n_children(
      GTK_TREE_MODEL(list_store_), NULL);

  GeolocationContentSettingsTableModel::Rows rows;
  GetSelectedRows(&rows);
  gtk_widget_set_sensitive(remove_button_, model_->CanRemoveExceptions(rows));
  gtk_widget_set_sensitive(remove_all_button_, row_count > 0);
}

void GeolocationContentExceptionsWindow::GetSelectedRows(
    GeolocationContentSettingsTableModel::Rows* rows) {
  std::set<int> indicies;
  gtk_tree::GetSelectedIndicies(treeview_selection_, &indicies);
  for (std::set<int>::iterator i = indicies.begin(); i != indicies.end(); ++i)
    rows->insert(*i);
}

void GeolocationContentExceptionsWindow::Remove(GtkWidget* widget) {
  GeolocationContentSettingsTableModel::Rows rows;
  GetSelectedRows(&rows);
  model_->RemoveExceptions(rows);
  UpdateButtonState();
}

void GeolocationContentExceptionsWindow::RemoveAll(GtkWidget* widget) {
  model_->RemoveAll();
  UpdateButtonState();
}

void GeolocationContentExceptionsWindow::OnWindowDestroy(GtkWidget* widget) {
  instance = NULL;
  MessageLoop::current()->DeleteSoon(FROM_HERE, this);
}

void GeolocationContentExceptionsWindow::OnTreeSelectionChanged(
    GtkWidget* selection) {
  UpdateButtonState();
}

Generated by  Doxygen 1.6.0   Back to index