From 7f388237dcb657405c4c1e66f924f2495c0880e2 Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Wed, 16 Jul 2008 22:32:37 +0200 Subject: [PATCH] Reintroduce Download Manager preference --- midori/midori-preferences.c | 68 ++++++++++++++++++++++++++++++++----- midori/midori-websettings.c | 28 +++++++-------- midori/midori-websettings.h | 4 --- midori/midori-webview.c | 29 ++++++++++++++++ midori/sokoke.c | 55 ++++++++++++++++++++++++++++++ midori/sokoke.h | 4 +++ 6 files changed, 161 insertions(+), 27 deletions(-) diff --git a/midori/midori-preferences.c b/midori/midori-preferences.c index dfa3328a..5b86cd99 100644 --- a/midori/midori-preferences.c +++ b/midori/midori-preferences.c @@ -68,9 +68,12 @@ static void clear_button_clicked_cb (GtkWidget* button, GtkWidget* file_chooser) { gtk_file_chooser_set_uri (GTK_FILE_CHOOSER (file_chooser), ""); - /* Emit "file-set" manually for Gtk doesn't emit it otherwise - FIXME: file-set is Gtk+ >= 2.12 */ + /* Emit signal manually for Gtk doesn't emit it otherwise */ + #if GTK_CHECK_VERSION (2, 12, 0) g_signal_emit_by_name (file_chooser, "file-set"); + #else + g_signal_emit_by_name (file_chooser, "selection-changed"); + #endif } static void @@ -173,6 +176,34 @@ midori_preferences_new (GtkWindow* parent, return GTK_WIDGET (preferences); } +static gboolean +proxy_download_manager_icon_cb (GtkWidget* entry, + GdkEventFocus* event, + GtkImage* icon) +{ + const gchar* program; + gchar* path; + + program = gtk_entry_get_text (GTK_ENTRY (entry)); + path = g_find_program_in_path (program); + + if (path) + { + if (gtk_icon_theme_has_icon (gtk_icon_theme_get_for_screen ( + gtk_widget_get_screen (entry)), program)) + gtk_image_set_from_icon_name (icon, program, GTK_ICON_SIZE_MENU); + else + gtk_image_set_from_stock (icon, GTK_STOCK_EXECUTE, GTK_ICON_SIZE_MENU); + g_free (path); + } + else if (program && *program) + gtk_image_set_from_stock (icon, GTK_STOCK_STOP, GTK_ICON_SIZE_MENU); + else + gtk_image_clear (icon); + + return FALSE; +} + /** * midori_preferences_set_settings: * @settings: the settings @@ -185,6 +216,17 @@ void midori_preferences_set_settings (MidoriPreferences* preferences, MidoriWebSettings* settings) { + GtkSizeGroup* sizegroup; + GtkWidget* page; + GtkWidget* frame; + GtkWidget* table; + GtkWidget* align; + GtkWidget* label; + GtkWidget* button; + GtkWidget* entry; + GtkWidget* hbox; + gint icon_width, icon_height; + g_return_if_fail (MIDORI_IS_PREFERENCES (preferences)); g_return_if_fail (MIDORI_IS_WEB_SETTINGS (settings)); @@ -192,10 +234,7 @@ midori_preferences_set_settings (MidoriPreferences* preferences, preferences->notebook = gtk_notebook_new (); gtk_container_set_border_width (GTK_CONTAINER (preferences->notebook), 6); - GtkSizeGroup* sizegroup = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL); - GtkWidget* page; GtkWidget* frame; GtkWidget* table; GtkWidget* align; - GtkWidget* label; GtkWidget* button; - GtkWidget* entry; GtkWidget* hbox; + sizegroup = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL); #define PAGE_NEW(__label) page = gtk_vbox_new (FALSE, 0); \ gtk_container_set_border_width (GTK_CONTAINER (page), 5); \ gtk_notebook_append_page (GTK_NOTEBOOK (preferences->notebook), page, \ @@ -243,8 +282,20 @@ midori_preferences_set_settings (MidoriPreferences* preferences, INDENTED_ADD (label, 0, 1, 0, 1); button = katze_property_proxy (settings, "download-folder", "folder"); FILLED_ADD (button, 1, 2, 0, 1); - button = katze_property_proxy (settings, "show-download-notification", "blurb"); - SPANNED_ADD (button, 0, 2, 1, 2); + label = katze_property_label (settings, "download-manager"); + INDENTED_ADD (label, 0, 1, 1, 2); + hbox = gtk_hbox_new (FALSE, 4); + button = gtk_image_new (); + gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (button), + GTK_ICON_SIZE_MENU, &icon_width, &icon_height); + gtk_widget_set_size_request (button, icon_width, icon_height); + gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 4); + entry = katze_property_proxy (settings, "download-manager", NULL); + gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0); + proxy_download_manager_icon_cb (entry, NULL, GTK_IMAGE (button)); + g_signal_connect (entry, "focus-out-event", + G_CALLBACK (proxy_download_manager_icon_cb), button); + FILLED_ADD (hbox, 1, 2, 1, 2); /* Page "Appearance" */ PAGE_NEW (_("Appearance")); @@ -260,7 +311,6 @@ midori_preferences_set_settings (MidoriPreferences* preferences, FILLED_ADD (hbox, 1, 2, 0, 1); label = katze_property_label (settings, "minimum-font-size"); INDENTED_ADD (label, 0, 1, 1, 2); - hbox = gtk_hbox_new (FALSE, 4); entry = katze_property_proxy (settings, "minimum-font-size", NULL); INDENTED_ADD (entry, 1, 2, 1, 2); label = katze_property_label (settings, "preferred-encoding"); diff --git a/midori/midori-websettings.c b/midori/midori-websettings.c index 02625460..d87b8db2 100644 --- a/midori/midori-websettings.c +++ b/midori/midori-websettings.c @@ -43,7 +43,7 @@ struct _MidoriWebSettings MidoriStartup load_on_startup; gchar* homepage; gchar* download_folder; - gboolean show_download_notification; + gchar* download_manager; gchar* location_entry_search; MidoriPreferredEncoding preferred_encoding; @@ -96,7 +96,7 @@ enum PROP_LOAD_ON_STARTUP, PROP_HOMEPAGE, PROP_DOWNLOAD_FOLDER, - PROP_SHOW_DOWNLOAD_NOTIFICATION, + PROP_DOWNLOAD_MANAGER, PROP_LOCATION_ENTRY_SEARCH, PROP_PREFERRED_ENCODING, @@ -423,13 +423,13 @@ midori_web_settings_class_init (MidoriWebSettingsClass* class) G_PARAM_READABLE)); g_object_class_install_property (gobject_class, - PROP_SHOW_DOWNLOAD_NOTIFICATION, - g_param_spec_boolean ( - "show-download-notification", - _("Show Download Notification"), - _("Show a notification window for finished downloads"), - TRUE, - G_PARAM_READABLE)); + PROP_DOWNLOAD_MANAGER, + g_param_spec_string ( + "download-manager", + _("Download Manager"), + _("An external download manager"), + NULL, + flags)); g_object_class_install_property (gobject_class, PROP_LOCATION_ENTRY_SEARCH, @@ -705,8 +705,8 @@ midori_web_settings_set_property (GObject* object, case PROP_DOWNLOAD_FOLDER: katze_assign (web_settings->download_folder, g_value_dup_string (value)); break; - case PROP_SHOW_DOWNLOAD_NOTIFICATION: - web_settings->show_download_notification = g_value_get_boolean (value); + case PROP_DOWNLOAD_MANAGER: + katze_assign (web_settings->download_manager, g_value_dup_string (value)); break; case PROP_LOCATION_ENTRY_SEARCH: katze_assign (web_settings->location_entry_search, g_value_dup_string (value)); @@ -863,8 +863,8 @@ midori_web_settings_get_property (GObject* object, case PROP_DOWNLOAD_FOLDER: g_value_set_string (value, web_settings->download_folder); break; - case PROP_SHOW_DOWNLOAD_NOTIFICATION: - g_value_set_boolean (value, web_settings->show_download_notification); + case PROP_DOWNLOAD_MANAGER: + g_value_set_string (value, web_settings->download_manager); break; case PROP_LOCATION_ENTRY_SEARCH: g_value_set_string (value, web_settings->location_entry_search); @@ -964,7 +964,7 @@ midori_web_settings_copy (MidoriWebSettings* web_settings) "load-on-startup", web_settings->load_on_startup, "homepage", web_settings->homepage, "download-folder", web_settings->download_folder, - "show-download-notification", web_settings->show_download_notification, + "download-manager", web_settings->download_manager, "location-entry-search", web_settings->location_entry_search, "preferred-encoding", web_settings->preferred_encoding, diff --git a/midori/midori-websettings.h b/midori/midori-websettings.h index 35405ad3..cecab796 100644 --- a/midori/midori-websettings.h +++ b/midori/midori-websettings.h @@ -107,10 +107,6 @@ midori_accept_cookies_get_type (void) G_GNUC_CONST; struct _MidoriWebSettingsClass { WebKitWebSettingsClass parent_class; - - /* Signals */ - void - (*dummy) (MidoriWebSettings* web_settings); }; GType diff --git a/midori/midori-webview.c b/midori/midori-webview.c index 18ef6a87..3ed6cc0b 100644 --- a/midori/midori-webview.c +++ b/midori/midori-webview.c @@ -411,6 +411,19 @@ midori_web_view_menu_new_window_activate_cb (GtkWidget* widget, g_signal_emit (web_view, signals[NEW_WINDOW], 0, uri); } +static void +midori_web_view_menu_download_activate_cb (GtkWidget* widget, + MidoriWebView* web_view) +{ + gchar* program; + const gchar* uri; + + g_object_get (web_view->settings, "download-manager", &program, NULL); + uri = g_object_get_data (G_OBJECT (widget), "uri"); + sokoke_spawn_program (program, uri); + g_free (program); +} + static void webkit_web_view_populate_popup_cb (GtkWidget* web_view, GtkWidget* menu) @@ -422,6 +435,7 @@ webkit_web_view_populate_popup_cb (GtkWidget* web_view, GtkWidget* icon; gchar* text; GList* items; + gchar* program; uri = midori_web_view_get_link_uri (MIDORI_WEB_VIEW (web_view)); if (uri) @@ -450,6 +464,21 @@ webkit_web_view_populate_popup_cb (GtkWidget* web_view, /* hack to disable non-functional Download File */ gtk_widget_set_sensitive (menuitem, FALSE); g_list_free (items); + g_object_get (MIDORI_WEB_VIEW (web_view)->settings, + "download-manager", &program, NULL); + if (program && *program) + { + menuitem = gtk_image_menu_item_new_with_mnemonic ( + _("Download Link with Download _Manager")); + icon = gtk_image_new_from_stock (GTK_STOCK_SAVE_AS, + GTK_ICON_SIZE_MENU); + gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), icon); + gtk_menu_shell_insert (GTK_MENU_SHELL (menu), menuitem, 4); + g_object_set_data (G_OBJECT (menuitem), "uri", (gchar*)uri); + g_signal_connect (menuitem, "activate", + G_CALLBACK (midori_web_view_menu_download_activate_cb), web_view); + gtk_widget_show (menuitem); + } } if (!uri && webkit_web_view_has_selection (WEBKIT_WEB_VIEW (web_view))) diff --git a/midori/sokoke.c b/midori/sokoke.c index e267eed1..49eeaa3d 100644 --- a/midori/sokoke.c +++ b/midori/sokoke.c @@ -24,6 +24,61 @@ #include #include +static void +error_dialog (const gchar* short_message, + const gchar* detailed_message) +{ + GtkWidget* dialog = gtk_message_dialog_new ( + NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, short_message); + gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), + "%s", detailed_message); + gtk_widget_show (dialog); + g_signal_connect_swapped (dialog, "response", + G_CALLBACK (gtk_widget_destroy), dialog); + + +} + +gboolean +sokoke_spawn_program (const gchar* command, + const gchar* argument) +{ + gchar* argument_escaped; + gchar* command_ready; + gchar** argv; + GError* error; + + argument_escaped = g_shell_quote (argument); + if (strstr (command, "%s")) + command_ready = g_strdup_printf (command, argument_escaped); + else + command_ready = g_strconcat (command, " ", argument_escaped, NULL); + + error = NULL; + if (!g_shell_parse_argv (command_ready, NULL, &argv, &error)) + { + error_dialog (_("Could not run external program."), error->message); + g_error_free (error); + g_free (command_ready); + g_free (argument_escaped); + return FALSE; + } + + error = NULL; + if (!g_spawn_async (NULL, argv, NULL, + (GSpawnFlags)G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD, + NULL, NULL, NULL, &error)) + { + error_dialog (_("Could not run external program."), error->message); + g_error_free (error); + } + + g_strfreev (argv); + g_free (command_ready); + g_free (argument_escaped); + return TRUE; +} + gchar* sokoke_magic_uri (const gchar* uri, MidoriWebList* search_engines) diff --git a/midori/sokoke.h b/midori/sokoke.h index c4cbbce0..172b19b4 100644 --- a/midori/sokoke.h +++ b/midori/sokoke.h @@ -19,6 +19,10 @@ /* Many themes need this hack for small toolbars to work */ #define GTK_ICON_SIZE_SMALL_TOOLBAR GTK_ICON_SIZE_BUTTON +gboolean +sokoke_spawn_program (const gchar* command, + const gchar* argument); + gchar* sokoke_magic_uri (const gchar* uri, MidoriWebList* search_engines);