- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / gtk / extensions / extension_uninstall_dialog_gtk.cc
1 // Copyright (c) 2012 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 // Currently this file is only used for the uninstall prompt. The install prompt
6 // code is in extension_install_prompt2_gtk.cc.
7
8 #include "chrome/browser/extensions/extension_uninstall_dialog.h"
9
10 #include <gtk/gtk.h>
11
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/browser/ui/gtk/browser_window_gtk.h"
17 #include "chrome/common/extensions/extension.h"
18 #include "grit/generated_resources.h"
19 #include "ui/base/gtk/gtk_hig_constants.h"
20 #include "ui/base/l10n/l10n_util.h"
21 #include "ui/gfx/gtk_util.h"
22
23 namespace {
24
25 // GTK implementation of the uninstall dialog.
26 class ExtensionUninstallDialogGtk : public ExtensionUninstallDialog {
27  public:
28   ExtensionUninstallDialogGtk(Profile* profile,
29                               Browser* browser,
30                               Delegate* delegate);
31   virtual ~ExtensionUninstallDialogGtk() OVERRIDE;
32
33  private:
34   virtual void Show() OVERRIDE;
35
36   CHROMEGTK_CALLBACK_1(ExtensionUninstallDialogGtk, void, OnResponse, int);
37
38   GtkWidget* dialog_;
39 };
40
41 ExtensionUninstallDialogGtk::ExtensionUninstallDialogGtk(
42     Profile* profile,
43     Browser* browser,
44     ExtensionUninstallDialog::Delegate* delegate)
45     : ExtensionUninstallDialog(profile, browser, delegate),
46       dialog_(NULL) {}
47
48 void ExtensionUninstallDialogGtk::Show() {
49   if (!browser_) {
50     delegate_->ExtensionUninstallCanceled();
51     return;
52   }
53   BrowserWindow* browser_window = browser_->window();
54   if (!browser_window) {
55     delegate_->ExtensionUninstallCanceled();
56     return;
57   }
58
59   // Build the dialog.
60   dialog_ = gtk_dialog_new_with_buttons(
61       l10n_util::GetStringUTF8(IDS_EXTENSION_UNINSTALL_PROMPT_TITLE).c_str(),
62       browser_window->GetNativeWindow(),
63       GTK_DIALOG_MODAL,
64       GTK_STOCK_CANCEL,
65       GTK_RESPONSE_CLOSE,
66       l10n_util::GetStringUTF8(IDS_EXTENSION_PROMPT_UNINSTALL_BUTTON).c_str(),
67       GTK_RESPONSE_ACCEPT,
68       NULL);
69 #if !GTK_CHECK_VERSION(2, 22, 0)
70   gtk_dialog_set_has_separator(GTK_DIALOG(dialog_), FALSE);
71 #endif
72
73   // Create a two column layout.
74   GtkWidget* content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog_));
75   gtk_box_set_spacing(GTK_BOX(content_area), ui::kContentAreaSpacing);
76
77   GtkWidget* icon_hbox = gtk_hbox_new(FALSE, ui::kContentAreaSpacing);
78   gtk_box_pack_start(GTK_BOX(content_area), icon_hbox, TRUE, TRUE, 0);
79
80   // Put Icon in the left column.
81   GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(*icon_.bitmap());
82   GtkWidget* icon = gtk_image_new_from_pixbuf(pixbuf);
83   g_object_unref(pixbuf);
84   gtk_box_pack_start(GTK_BOX(icon_hbox), icon, TRUE, TRUE, 0);
85
86   // Create a new vbox for the right column.
87   GtkWidget* right_column_area = gtk_vbox_new(FALSE, 0);
88   gtk_box_pack_start(GTK_BOX(icon_hbox), right_column_area, TRUE, TRUE, 0);
89
90   std::string heading_text = l10n_util::GetStringFUTF8(
91       IDS_EXTENSION_UNINSTALL_PROMPT_HEADING, UTF8ToUTF16(extension_->name()));
92   GtkWidget* heading_label = gtk_label_new(heading_text.c_str());
93   gtk_misc_set_alignment(GTK_MISC(heading_label), 0.0, 0.5);
94   gtk_box_pack_start(GTK_BOX(right_column_area), heading_label, TRUE, TRUE, 0);
95
96   g_signal_connect(dialog_, "response", G_CALLBACK(OnResponseThunk), this);
97   gtk_window_set_resizable(GTK_WINDOW(dialog_), FALSE);
98   gtk_widget_show_all(dialog_);
99 }
100
101 ExtensionUninstallDialogGtk::~ExtensionUninstallDialogGtk() {
102   delegate_ = NULL;
103   if (dialog_) {
104     gtk_widget_destroy(dialog_);
105     dialog_ = NULL;
106   }
107 }
108
109 void ExtensionUninstallDialogGtk::OnResponse(
110     GtkWidget* dialog, int response_id) {
111   CHECK_EQ(dialog_, dialog);
112
113   gtk_widget_destroy(dialog_);
114   dialog_ = NULL;
115
116   if (delegate_) {
117     if (response_id == GTK_RESPONSE_ACCEPT)
118       delegate_->ExtensionUninstallAccepted();
119     else
120       delegate_->ExtensionUninstallCanceled();
121   }
122 }
123
124 }  // namespace
125
126 // static
127 // Platform specific implementation of the uninstall dialog show method.
128 ExtensionUninstallDialog* ExtensionUninstallDialog::Create(
129     Profile* profile, Browser* browser, Delegate* delegate) {
130   return new ExtensionUninstallDialogGtk(profile, browser, delegate);
131 }