From cfb9e30e4137274ff0ae7de154ce543b9f67519c Mon Sep 17 00:00:00 2001 From: monsta Date: Tue, 26 Sep 2017 17:51:47 +0300 Subject: make file/folder launchers work again fixes https://github.com/mate-desktop/mate-panel/issues/590 reverts a part of 7ee450758e4f2bee13aa8ff967a5795a6dcdc91f --- mate-panel/launcher.c | 206 +++++++++++++++++++------------- mate-panel/launcher.h | 5 +- mate-panel/libpanel-util/panel-launch.c | 10 +- mate-panel/libpanel-util/panel-launch.h | 2 +- mate-panel/mate-desktop-item-edit.c | 9 +- mate-panel/panel-ditem-editor.c | 8 +- mate-panel/panel-ditem-editor.h | 2 + 7 files changed, 149 insertions(+), 93 deletions(-) diff --git a/mate-panel/launcher.c b/mate-panel/launcher.c index c59b67a8..07e52e40 100644 --- a/mate-panel/launcher.c +++ b/mate-panel/launcher.c @@ -105,10 +105,10 @@ launch_url (Launcher *launcher) GdkScreen *screen; g_return_if_fail (launcher != NULL); - g_return_if_fail (launcher->app_info != NULL); + g_return_if_fail (launcher->key_file != NULL); /* FIXME panel_ditem_launch() should be enough for this! */ - url = g_desktop_app_info_get_string (launcher->app_info, G_KEY_FILE_DESKTOP_KEY_URL); + url = panel_key_file_get_string (launcher->key_file, "URL"); screen = launcher_get_screen (launcher); @@ -136,10 +136,10 @@ launcher_launch (Launcher *launcher, char *type; g_return_if_fail (launcher != NULL); - g_return_if_fail (launcher->app_info != NULL); + g_return_if_fail (launcher->key_file != NULL); if (action == NULL) { - type = g_desktop_app_info_get_string (launcher->app_info, G_KEY_FILE_DESKTOP_KEY_TYPE); + type = panel_key_file_get_string (launcher->key_file, "Type"); } else { type = NULL; } @@ -149,7 +149,7 @@ launcher_launch (Launcher *launcher, else { GError *error = NULL; - panel_app_info_launch (launcher->app_info, NULL, + panel_launch_key_file (launcher->key_file, NULL, launcher_get_screen (launcher), action, &error); if (error) { GtkWidget *error_dialog; @@ -199,7 +199,7 @@ drag_data_received_cb (GtkWidget *widget, file_list = g_list_prepend (file_list, uris[i]); file_list = g_list_reverse (file_list); - panel_app_info_launch (launcher->app_info, file_list, + panel_launch_key_file (launcher->key_file, file_list, launcher_get_screen (launcher), NULL, &error); g_list_free (file_list); @@ -245,7 +245,13 @@ free_launcher (gpointer data) { Launcher *launcher = data; - g_clear_object(&launcher->app_info); + if (launcher->key_file) + g_key_file_free (launcher->key_file); + launcher->key_file = NULL; + + if (launcher->location != NULL) + g_free (launcher->location); + launcher->location = NULL; g_free (launcher); } @@ -253,18 +259,15 @@ free_launcher (gpointer data) void panel_launcher_delete (Launcher *launcher) { - const gchar *location; - - location = g_desktop_app_info_get_filename (launcher->app_info); - if (!location) + if (!launcher->location) return; /* do not remove the file if it's not in the user's launchers path */ - if (panel_launcher_is_in_personal_path (location)) { + if (panel_launcher_is_in_personal_path (launcher->location)) { GError *error; GFile *file; - file = panel_launcher_get_gfile (location); + file = panel_launcher_get_gfile (launcher->location); error = NULL; if (!g_file_delete (file, NULL, &error)) { @@ -371,12 +374,11 @@ drag_data_get_cb (GtkWidget *widget, guint time, Launcher *launcher) { - const char *location; + char *location; g_return_if_fail (launcher != NULL); - g_return_if_fail (launcher->app_info != NULL); - location = g_desktop_app_info_get_filename (launcher->app_info); + location = launcher->location; if (info == TARGET_URI_LIST) { char *uri[2]; @@ -433,15 +435,20 @@ clicked_cb (Launcher *launcher, static Launcher * create_launcher (const char *location) { - GDesktopAppInfo *app_info; + GKeyFile *key_file; + gboolean loaded = FALSE; Launcher *launcher; + GError *error = NULL; + char *new_location; if (!location) { g_printerr (_("No URI provided for panel launcher desktop file\n")); return NULL; } - app_info = NULL; + new_location = NULL; + key_file = g_key_file_new (); + if (!strchr (location, G_DIR_SEPARATOR)) { /* try to first load a file in our config directory, and if it * doesn't exist there, try to find it in the xdg data dirs */ @@ -452,26 +459,49 @@ create_launcher (const char *location) if (!g_file_test (path, G_FILE_TEST_EXISTS)) { g_free (path); path = panel_g_lookup_in_applications_dirs (location); + /* it's important to keep the full path if the desktop + * file comes from a data dir: when the user will edit + * it, we'll want to save it in PANEL_LAUNCHERS_PATH + * with a random name (and not evolution.desktop, eg) + * and having only a basename as location will make + * this impossible */ + if (path) + new_location = g_strdup (path); } if (path) { - app_info = g_desktop_app_info_new_from_filename (path); + loaded = g_key_file_load_from_file (key_file, path, + G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS, + &error); g_free (path); } } else - app_info = g_desktop_app_info_new_from_filename (location); + loaded = panel_key_file_load_from_uri (key_file, location, + G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS, + &error); + + if (!loaded) { + g_printerr (_("Unable to open desktop file %s for panel launcher%s%s\n"), + location, + error ? ": " : "", + error ? error->message : ""); + if (error) + g_error_free (error); - if (!app_info) { - g_printerr (_("Unable to open desktop file %s for panel launcher\n"), - location); + g_key_file_free (key_file); + g_free (new_location); return NULL; /*button is null*/ } + if (!new_location) + new_location = g_strdup (location); + launcher = g_new0 (Launcher, 1); launcher->info = NULL; launcher->button = NULL; - launcher->app_info = app_info; + launcher->location = new_location; + launcher->key_file = key_file; launcher->prop_dialog = NULL; launcher->destroy_handler = 0; @@ -510,36 +540,24 @@ create_launcher (const char *location) return launcher; } +#if GLIB_CHECK_VERSION (2, 38, 0) static void -setup_button (Launcher *launcher) +setup_actions (Launcher *launcher) { - const char *comment; - const char *name; - char *str; - char *icon; - char *unescaped_str; -#if GLIB_CHECK_VERSION (2, 38, 0) + GDesktopAppInfo *app_info; const gchar * const *actions; const gchar * const *ptr; -#endif - - g_return_if_fail (launcher != NULL); - mate_panel_applet_clear_user_menu (launcher->info); - - mate_panel_applet_add_callback (launcher->info, - "launch", - "system-run", - _("_Launch"), - NULL); + app_info = g_desktop_app_info_new_from_keyfile (launcher->key_file); + if (app_info == NULL) + return; -#if GLIB_CHECK_VERSION (2, 38, 0) - actions = g_desktop_app_info_list_actions (launcher->app_info); + actions = g_desktop_app_info_list_actions (app_info); ptr = actions; for(; *ptr != NULL; ptr++) { const gchar *action = *ptr; gchar *callback = g_strdup_printf("launch-action_%s", action); - gchar *action_name = g_desktop_app_info_get_action_name (launcher->app_info, action); + gchar *action_name = g_desktop_app_info_get_action_name (app_info, action); mate_panel_applet_add_callback (launcher->info, callback, NULL, @@ -548,6 +566,32 @@ setup_button (Launcher *launcher) g_free (callback); g_free (action_name); } + + g_object_unref (app_info); +} +#endif + +static void +setup_button (Launcher *launcher) +{ + char *comment; + char *name; + char *str; + char *icon; + char *unescaped_str; + + g_return_if_fail (launcher != NULL); + + mate_panel_applet_clear_user_menu (launcher->info); + + mate_panel_applet_add_callback (launcher->info, + "launch", + "system-run", + _("_Launch"), + NULL); + +#if GLIB_CHECK_VERSION (2, 38, 0) + setup_actions (launcher); #endif mate_panel_applet_add_callback (launcher->info, @@ -556,8 +600,10 @@ setup_button (Launcher *launcher) _("_Properties"), launcher_properties_enabled); - name = g_app_info_get_name (G_APP_INFO(launcher->app_info)); - comment = g_app_info_get_description (G_APP_INFO(launcher->app_info)); + name = panel_key_file_get_locale_string (launcher->key_file, "Name"); + comment = panel_key_file_get_locale_string (launcher->key_file, + "Comment"); + /* Setup tooltip */ if (!PANEL_GLIB_STR_EMPTY (name) && !PANEL_GLIB_STR_EMPTY (comment)) str = g_strdup_printf ("%s\n%s", name, comment); @@ -585,7 +631,7 @@ setup_button (Launcher *launcher) g_free (str); /* Setup icon */ - icon = g_desktop_app_info_get_string (launcher->app_info, G_KEY_FILE_DESKTOP_KEY_ICON); + icon = panel_key_file_get_locale_string (launcher->key_file, "Icon"); if (icon && icon[0] == '\0') { g_free (icon); icon = NULL; @@ -593,11 +639,15 @@ setup_button (Launcher *launcher) if (!icon) { gchar *exec; - exec = g_desktop_app_info_get_string (launcher->app_info, G_KEY_FILE_DESKTOP_KEY_EXEC); - icon = guess_icon_from_exec (button_widget_get_icon_theme (BUTTON_WIDGET (launcher->button)), - exec); + exec = panel_key_file_get_string (launcher->key_file, "Exec"); + if (exec && exec[0] != '\0') { + icon = guess_icon_from_exec (button_widget_get_icon_theme (BUTTON_WIDGET (launcher->button)), + exec); + } + g_free (exec); } + if (!icon) icon = g_strdup (PANEL_ICON_LAUNCHER); @@ -654,18 +704,16 @@ launcher_command_changed (PanelDItemEditor *dialog, { char *exec; char *old_exec; - GKeyFile *key_file; GKeyFile *revert_key_file; - key_file = panel_ditem_editor_get_key_file (dialog); revert_key_file = panel_ditem_editor_get_revert_key_file (dialog); if (revert_key_file) { - exec = panel_key_file_get_string (key_file, "Exec"); + exec = panel_key_file_get_string (launcher->key_file, "Exec"); old_exec = panel_key_file_get_string (revert_key_file, "Exec"); if (!old_exec || !exec || strcmp (old_exec, exec)) - panel_key_file_remove_key (key_file, + panel_key_file_remove_key (launcher->key_file, "StartupNotify"); g_free (exec); @@ -697,8 +745,7 @@ launcher_save_uri (PanelDItemEditor *dialog, launcher = (Launcher *) data; if (launcher) { - const gchar *location = g_desktop_app_info_get_filename (launcher->app_info); - new_uri = panel_launcher_find_writable_uri (location, + new_uri = panel_launcher_find_writable_uri (launcher->location, exec_or_uri); } else @@ -721,23 +768,24 @@ launcher_saved (GtkWidget *dialog, Launcher *launcher) { const char *uri; - gchar *location; char *filename; uri = panel_ditem_editor_get_uri (PANEL_DITEM_EDITOR (dialog)); filename = panel_launcher_get_filename(uri); + if (filename) + uri = filename; - g_settings_set_string (launcher->info->settings, PANEL_OBJECT_LAUNCHER_LOCATION_KEY, filename); - - g_free (filename); + if (uri && launcher->location && strcmp (uri, launcher->location)) { + g_settings_set_string (launcher->info->settings, PANEL_OBJECT_LAUNCHER_LOCATION_KEY, uri); - g_object_unref (launcher->app_info); + if (launcher->location) + g_free (launcher->location); - location = g_filename_from_uri (uri, NULL, NULL); - launcher->app_info = g_desktop_app_info_new_from_filename (location); - g_free (location); + launcher->location = g_strdup (uri); + } - setup_button (launcher); + if (filename) + g_free (filename); } static void @@ -754,8 +802,6 @@ launcher_error_reported (GtkWidget *dialog, void launcher_properties (Launcher *launcher) { - gchar *uri; - if (launcher->prop_dialog != NULL) { gtk_window_set_screen (GTK_WINDOW (launcher->prop_dialog), gtk_widget_get_screen (launcher->button)); @@ -763,15 +809,10 @@ launcher_properties (Launcher *launcher) return; } - g_return_if_fail (launcher->app_info != NULL); - - const gchar *location = g_desktop_app_info_get_filename (launcher->app_info); - - uri = g_filename_to_uri (location, NULL, NULL); launcher->prop_dialog = panel_ditem_editor_new (NULL, - uri, + launcher->key_file, + launcher->location, _("Launcher Properties")); - g_free (uri); panel_widget_register_open_dialog (PANEL_WIDGET (gtk_widget_get_parent (launcher->info->widget)), @@ -905,9 +946,13 @@ launcher_new_saved (GtkWidget *dialog, uri = panel_ditem_editor_get_uri (PANEL_DITEM_EDITOR (dialog)); filename = panel_launcher_get_filename (uri); - panel_launcher_create (panel->toplevel, pos, filename); + if (filename) + uri = filename; - g_free (filename); + panel_launcher_create (panel->toplevel, pos, uri); + + if (filename) + g_free (filename); } void @@ -922,7 +967,7 @@ ask_about_launcher (const char *file, if (panel_lockdown_get_disable_command_line ()) return; - dialog = panel_ditem_editor_new (NULL, NULL, + dialog = panel_ditem_editor_new (NULL, NULL, NULL, _("Create Launcher")); panel_widget_register_open_dialog (panel, dialog); @@ -1101,12 +1146,11 @@ find_launcher (const char *path) launcher = info->data; - if (launcher->app_info == NULL) + if (launcher->key_file == NULL) continue; - const gchar *location = g_desktop_app_info_get_filename (launcher->app_info); - if (location != NULL && - strcmp (location, path) == 0) + if (launcher->location != NULL && + strcmp (launcher->location, path) == 0) return launcher; } diff --git a/mate-panel/launcher.h b/mate-panel/launcher.h index 784fe876..2d3594bb 100644 --- a/mate-panel/launcher.h +++ b/mate-panel/launcher.h @@ -14,8 +14,6 @@ #include "applet.h" #include "panel-widget.h" -#include - #ifdef __cplusplus extern "C" { #endif @@ -24,7 +22,8 @@ typedef struct { AppletInfo *info; GtkWidget *button; - GDesktopAppInfo *app_info; + char *location; + GKeyFile *key_file; GtkWidget *prop_dialog; GSList *error_dialogs; diff --git a/mate-panel/libpanel-util/panel-launch.c b/mate-panel/libpanel-util/panel-launch.c index ab330888..66632dc9 100644 --- a/mate-panel/libpanel-util/panel-launch.c +++ b/mate-panel/libpanel-util/panel-launch.c @@ -163,23 +163,29 @@ panel_app_info_launch_uri (GDesktopAppInfo *appinfo, } gboolean -panel_app_info_launch (GDesktopAppInfo *appinfo, +panel_launch_key_file (GKeyFile *keyfile, GList *uri_list, GdkScreen *screen, const gchar *action, GError **error) { + GDesktopAppInfo *appinfo; gboolean retval; - g_return_val_if_fail (appinfo != NULL, FALSE); + g_return_val_if_fail (keyfile != NULL, FALSE); g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + appinfo = g_desktop_app_info_new_from_keyfile (keyfile); + if (appinfo == NULL) + return FALSE; + retval = panel_app_info_launch_uris (appinfo, uri_list, screen, action, gtk_get_current_event_time (), error); + g_object_unref (appinfo); return retval; } diff --git a/mate-panel/libpanel-util/panel-launch.h b/mate-panel/libpanel-util/panel-launch.h index b98b8ca4..653167c1 100644 --- a/mate-panel/libpanel-util/panel-launch.h +++ b/mate-panel/libpanel-util/panel-launch.h @@ -45,7 +45,7 @@ gboolean panel_app_info_launch_uri (GDesktopAppInfo *appinfo, guint32 timestamp, GError **error); -gboolean panel_app_info_launch (GDesktopAppInfo *appinfo, +gboolean panel_launch_key_file (GKeyFile *keyfile, GList *uri_list, GdkScreen *screen, const gchar *action, diff --git a/mate-panel/mate-desktop-item-edit.c b/mate-panel/mate-desktop-item-edit.c index d5d768f4..4860cf84 100644 --- a/mate-panel/mate-desktop-item-edit.c +++ b/mate-panel/mate-desktop-item-edit.c @@ -140,7 +140,7 @@ main (int argc, char * argv[]) if (type == G_FILE_TYPE_DIRECTORY && create_new) { - dlg = panel_ditem_editor_new (NULL, NULL, + dlg = panel_ditem_editor_new (NULL, NULL, NULL, _("Create Launcher")); g_object_set_data_full (G_OBJECT (dlg), "dir", g_strdup (path), @@ -164,13 +164,14 @@ main (int argc, char * argv[]) ".directory") && !create_new) { dlg = panel_ditem_editor_new_directory (NULL, + NULL, uri, _("Directory Properties")); } else if (type == G_FILE_TYPE_REGULAR && g_str_has_suffix (desktops [i], ".desktop") && !create_new) { - dlg = panel_ditem_editor_new (NULL, uri, + dlg = panel_ditem_editor_new (NULL, NULL, uri, _("Launcher Properties")); } else if (type == G_FILE_TYPE_REGULAR && create_new) { @@ -187,13 +188,13 @@ main (int argc, char * argv[]) } else if (g_str_has_suffix (desktops [i], ".directory")) { /* a non-existant file. Well we can still edit that * sort of. We will just create it new */ - dlg = panel_ditem_editor_new_directory (NULL, uri, + dlg = panel_ditem_editor_new_directory (NULL, NULL, uri, _("Directory Properties")); } else if (g_str_has_suffix (desktops [i], ".desktop")) { /* a non-existant file. Well we can still edit that * sort of. We will just create it new */ - dlg = panel_ditem_editor_new (NULL, uri, + dlg = panel_ditem_editor_new (NULL, NULL, uri, _("Create Launcher")); } else { diff --git a/mate-panel/panel-ditem-editor.c b/mate-panel/panel-ditem-editor.c index 02916a9f..1961962e 100644 --- a/mate-panel/panel-ditem-editor.c +++ b/mate-panel/panel-ditem-editor.c @@ -1637,6 +1637,7 @@ panel_ditem_editor_load_uri (PanelDItemEditor *dialog, static GtkWidget * panel_ditem_editor_new_full (GtkWindow *parent, + GKeyFile *key_file, const char *uri, const char *title, gboolean type_directory) @@ -1645,6 +1646,7 @@ panel_ditem_editor_new_full (GtkWindow *parent, dialog = g_object_new (PANEL_TYPE_DITEM_EDITOR, "title", title, + "keyfile", key_file, "uri", uri, "type-directory", type_directory, NULL); @@ -1657,19 +1659,21 @@ panel_ditem_editor_new_full (GtkWindow *parent, GtkWidget * panel_ditem_editor_new (GtkWindow *parent, + GKeyFile *key_file, const char *uri, const char *title) { - return panel_ditem_editor_new_full (parent, uri, + return panel_ditem_editor_new_full (parent, key_file, uri, title, FALSE); } GtkWidget * panel_ditem_editor_new_directory (GtkWindow *parent, + GKeyFile *key_file, const char *uri, const char *title) { - return panel_ditem_editor_new_full (parent, uri, + return panel_ditem_editor_new_full (parent, key_file, uri, title, TRUE); } diff --git a/mate-panel/panel-ditem-editor.h b/mate-panel/panel-ditem-editor.h index f08ec14b..4b0a13c1 100644 --- a/mate-panel/panel-ditem-editor.h +++ b/mate-panel/panel-ditem-editor.h @@ -85,10 +85,12 @@ typedef char * (*PanelDitemSaveUri) (PanelDItemEditor *dialog, gpointer data); GType panel_ditem_editor_get_type (void); GtkWidget *panel_ditem_editor_new (GtkWindow *parent, + GKeyFile *key_file, const char *uri, const char *title); GtkWidget *panel_ditem_editor_new_directory (GtkWindow *parent, + GKeyFile *key_file, const char *uri, const char *title); -- cgit v1.2.1