OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/gtk/create_application_shortcuts_dialog_gtk.h" | 5 #include "chrome/browser/gtk/create_application_shortcuts_dialog_gtk.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
10 #include "base/environment.h" | 10 #include "base/environment.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "chrome/browser/browser_thread.h" | 12 #include "chrome/browser/browser_thread.h" |
13 #include "chrome/browser/gtk/gtk_util.h" | 13 #include "chrome/browser/gtk/gtk_util.h" |
14 #include "chrome/browser/shell_integration.h" | 14 #include "chrome/browser/shell_integration.h" |
15 #include "chrome/browser/tab_contents/tab_contents.h" | 15 #include "chrome/browser/tab_contents/tab_contents.h" |
16 #include "chrome/browser/tab_contents/tab_contents_delegate.h" | 16 #include "chrome/browser/tab_contents/tab_contents_delegate.h" |
17 #include "chrome/browser/web_applications/web_app.h" | 17 #include "chrome/browser/web_applications/web_app.h" |
| 18 #include "chrome/common/extensions/extension.h" |
| 19 #include "chrome/common/extensions/extension_resource.h" |
18 #include "gfx/gtk_util.h" | 20 #include "gfx/gtk_util.h" |
19 #include "grit/chromium_strings.h" | 21 #include "grit/chromium_strings.h" |
20 #include "grit/generated_resources.h" | 22 #include "grit/generated_resources.h" |
21 #include "grit/locale_settings.h" | 23 #include "grit/locale_settings.h" |
22 | 24 |
23 namespace { | 25 namespace { |
24 | 26 |
25 // Size (in pixels) of the icon preview. | 27 // Size (in pixels) of the icon preview. |
26 const int kIconPreviewSizePixels = 32; | 28 const int kIconPreviewSizePixels = 32; |
27 | 29 |
28 // Height (in lines) of the shortcut description label. | 30 // Height (in lines) of the shortcut description label. |
29 const int kDescriptionLabelHeightLines = 3; | 31 const int kDescriptionLabelHeightLines = 3; |
30 | 32 |
31 } // namespace | 33 } // namespace |
32 | 34 |
33 // static | 35 // static |
34 void CreateApplicationShortcutsDialogGtk::Show(GtkWindow* parent, | 36 void CreateWebApplicationShortcutsDialogGtk::Show(GtkWindow* parent, |
35 TabContents* tab_contents) { | 37 TabContents* tab_contents) { |
36 new CreateApplicationShortcutsDialogGtk(parent, tab_contents); | 38 new CreateWebApplicationShortcutsDialogGtk(parent, tab_contents); |
37 } | 39 } |
38 | 40 |
| 41 void CreateChromeApplicationShortcutsDialogGtk::Show(GtkWindow* parent, |
| 42 const Extension* app) { |
| 43 new CreateChromeApplicationShortcutsDialogGtk(parent, app); |
| 44 } |
| 45 |
| 46 |
39 CreateApplicationShortcutsDialogGtk::CreateApplicationShortcutsDialogGtk( | 47 CreateApplicationShortcutsDialogGtk::CreateApplicationShortcutsDialogGtk( |
40 GtkWindow* parent, | 48 GtkWindow* parent) |
41 TabContents* tab_contents) | 49 : parent_(parent), |
42 : tab_contents_(tab_contents), | 50 error_dialog_(NULL) { |
43 error_dialog_(NULL) { | |
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
45 | 52 |
46 // Will be balanced by Release later. | 53 // Will be balanced by Release later. |
47 AddRef(); | 54 AddRef(); |
| 55 } |
48 | 56 |
49 // Get shortcut information now, it's needed for our UI. | 57 void CreateApplicationShortcutsDialogGtk::CreateIconPixBuf( |
50 web_app::GetShortcutInfoForTab(tab_contents_, &shortcut_info_); | 58 const SkBitmap& bitmap) { |
51 | |
52 // Prepare the icon. Try to scale it if it's too small, otherwise it would | 59 // Prepare the icon. Try to scale it if it's too small, otherwise it would |
53 // look weird. | 60 // look weird. |
54 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(&shortcut_info_.favicon); | 61 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(&shortcut_info_.favicon); |
55 int pixbuf_width = gdk_pixbuf_get_width(pixbuf); | 62 int pixbuf_width = gdk_pixbuf_get_width(pixbuf); |
56 int pixbuf_height = gdk_pixbuf_get_height(pixbuf); | 63 int pixbuf_height = gdk_pixbuf_get_height(pixbuf); |
57 if (pixbuf_width == pixbuf_height && pixbuf_width < kIconPreviewSizePixels) { | 64 if (pixbuf_width == pixbuf_height && pixbuf_width < kIconPreviewSizePixels) { |
58 // Only scale the pixbuf if it's a square (for simplicity). | 65 // Only scale the pixbuf if it's a square (for simplicity). |
59 // Generally it should be square, if it's a favicon. | 66 // Generally it should be square, if it's a favicon or app icon. |
60 // Use the highest quality interpolation. The scaling is | 67 // Use the highest quality interpolation. The scaling is |
61 // going to have low quality anyway, because the initial image | 68 // going to have low quality anyway, because the initial image |
62 // is likely small. | 69 // is likely small. |
63 favicon_pixbuf_ = gdk_pixbuf_scale_simple(pixbuf, | 70 favicon_pixbuf_ = gdk_pixbuf_scale_simple(pixbuf, |
64 kIconPreviewSizePixels, | 71 kIconPreviewSizePixels, |
65 kIconPreviewSizePixels, | 72 kIconPreviewSizePixels, |
66 GDK_INTERP_HYPER); | 73 GDK_INTERP_HYPER); |
67 g_object_unref(pixbuf); | 74 g_object_unref(pixbuf); |
68 } else { | 75 } else { |
69 favicon_pixbuf_ = pixbuf; | 76 favicon_pixbuf_ = pixbuf; |
70 } | 77 } |
| 78 } |
71 | 79 |
| 80 void CreateApplicationShortcutsDialogGtk::CreateDialogBox(GtkWindow* parent) { |
72 // Build the dialog. | 81 // Build the dialog. |
73 create_dialog_ = gtk_dialog_new_with_buttons( | 82 create_dialog_ = gtk_dialog_new_with_buttons( |
74 l10n_util::GetStringUTF8(IDS_CREATE_SHORTCUTS_TITLE).c_str(), | 83 l10n_util::GetStringUTF8(IDS_CREATE_SHORTCUTS_TITLE).c_str(), |
75 parent, | 84 parent, |
76 (GtkDialogFlags) (GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR), | 85 (GtkDialogFlags) (GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR), |
77 GTK_STOCK_CANCEL, | 86 GTK_STOCK_CANCEL, |
78 GTK_RESPONSE_REJECT, | 87 GTK_RESPONSE_REJECT, |
79 NULL); | 88 NULL); |
80 gtk_widget_realize(create_dialog_); | 89 gtk_widget_realize(create_dialog_); |
81 gtk_util::SetWindowSizeFromResources(GTK_WINDOW(create_dialog_), | 90 gtk_util::SetWindowSizeFromResources(GTK_WINDOW(create_dialog_), |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 if (response == GTK_RESPONSE_ACCEPT) { | 173 if (response == GTK_RESPONSE_ACCEPT) { |
165 shortcut_info_.create_on_desktop = | 174 shortcut_info_.create_on_desktop = |
166 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(desktop_checkbox_)); | 175 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(desktop_checkbox_)); |
167 shortcut_info_.create_in_applications_menu = | 176 shortcut_info_.create_in_applications_menu = |
168 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(menu_checkbox_)); | 177 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(menu_checkbox_)); |
169 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 178 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
170 NewRunnableMethod(this, | 179 NewRunnableMethod(this, |
171 &CreateApplicationShortcutsDialogGtk::CreateDesktopShortcut, | 180 &CreateApplicationShortcutsDialogGtk::CreateDesktopShortcut, |
172 shortcut_info_)); | 181 shortcut_info_)); |
173 | 182 |
174 if (tab_contents_->delegate()) | 183 OnCreatedShortcut(); |
175 tab_contents_->delegate()->ConvertContentsToApplication(tab_contents_); | |
176 } else { | 184 } else { |
177 Release(); | 185 Release(); |
178 } | 186 } |
179 } | 187 } |
180 | 188 |
181 void CreateApplicationShortcutsDialogGtk::OnErrorDialogResponse( | 189 void CreateApplicationShortcutsDialogGtk::OnErrorDialogResponse( |
182 GtkWidget* widget, int response) { | 190 GtkWidget* widget, int response) { |
183 Release(); | 191 Release(); |
184 } | 192 } |
185 | 193 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 | 254 |
247 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(desktop_checkbox_)) || | 255 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(desktop_checkbox_)) || |
248 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(menu_checkbox_))) { | 256 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(menu_checkbox_))) { |
249 can_accept = TRUE; | 257 can_accept = TRUE; |
250 } | 258 } |
251 | 259 |
252 gtk_dialog_set_response_sensitive(GTK_DIALOG(create_dialog_), | 260 gtk_dialog_set_response_sensitive(GTK_DIALOG(create_dialog_), |
253 GTK_RESPONSE_ACCEPT, | 261 GTK_RESPONSE_ACCEPT, |
254 can_accept); | 262 can_accept); |
255 } | 263 } |
| 264 |
| 265 CreateWebApplicationShortcutsDialogGtk::CreateWebApplicationShortcutsDialogGtk( |
| 266 GtkWindow* parent, |
| 267 TabContents* tab_contents) |
| 268 : CreateApplicationShortcutsDialogGtk(parent), |
| 269 tab_contents_(tab_contents) { |
| 270 |
| 271 // Get shortcut information now, it's needed for our UI. |
| 272 web_app::GetShortcutInfoForTab(tab_contents_, &shortcut_info_); |
| 273 CreateIconPixBuf(shortcut_info_.favicon); |
| 274 |
| 275 CreateDialogBox(parent); |
| 276 } |
| 277 |
| 278 void CreateWebApplicationShortcutsDialogGtk::OnCreatedShortcut() { |
| 279 if (tab_contents_->delegate()) |
| 280 tab_contents_->delegate()->ConvertContentsToApplication(tab_contents_); |
| 281 } |
| 282 |
| 283 CreateChromeApplicationShortcutsDialogGtk:: |
| 284 CreateChromeApplicationShortcutsDialogGtk( |
| 285 GtkWindow* parent, |
| 286 const Extension* app) |
| 287 : CreateApplicationShortcutsDialogGtk(parent), |
| 288 app_(app), |
| 289 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)) { |
| 290 |
| 291 // Get shortcut information now, it's needed for our UI. |
| 292 shortcut_info_.extension_id = UTF8ToUTF16(app_->id()); |
| 293 shortcut_info_.url = GURL(app_->launch_web_url()); |
| 294 shortcut_info_.title = UTF8ToUTF16(app_->name()); |
| 295 shortcut_info_.description = UTF8ToUTF16(app_->description()); |
| 296 |
| 297 // Get the icon. |
| 298 const gfx::Size max_size(kIconPreviewSizePixels, kIconPreviewSizePixels); |
| 299 ExtensionResource icon_resource = app_->GetIconResource( |
| 300 kIconPreviewSizePixels, ExtensionIconSet::MATCH_BIGGER); |
| 301 |
| 302 // If no icon exists that is the desired size or larger, get the |
| 303 // largest icon available: |
| 304 if (icon_resource.empty()) |
| 305 icon_resource = app_->GetIconResource( |
| 306 kIconPreviewSizePixels, ExtensionIconSet::MATCH_SMALLER); |
| 307 |
| 308 tracker_.LoadImage(app_, |
| 309 icon_resource, |
| 310 max_size, |
| 311 ImageLoadingTracker::DONT_CACHE); |
| 312 } |
| 313 |
| 314 // Called by tracker_ when the app's icon is loaded. |
| 315 void CreateChromeApplicationShortcutsDialogGtk::OnImageLoaded( |
| 316 SkBitmap* image, ExtensionResource resource, int index) { |
| 317 if (image->isNull()) { |
| 318 NOTREACHED() << "Corrupt image in profile?"; |
| 319 return; |
| 320 } |
| 321 shortcut_info_.favicon = *image; |
| 322 |
| 323 CreateIconPixBuf(*image); |
| 324 CreateDialogBox(parent_); |
| 325 } |
OLD | NEW |