From 88346bde9ea6850caaf91e6868813b3132e76b10 Mon Sep 17 00:00:00 2001 From: Alexander Schwinn Date: Sun, 24 Mar 2019 23:52:42 +0100 Subject: Move three methods from startup.c to xfsm-global.c/.h They will be reused on logout, shutdown, etc. (See Bug #10172) --- xfce4-session/xfsm-global.c | 295 ++++++++++++++++++++++++++++++++++++++++++ xfce4-session/xfsm-global.h | 3 + xfce4-session/xfsm-startup.c | 296 +------------------------------------------ 3 files changed, 300 insertions(+), 294 deletions(-) diff --git a/xfce4-session/xfsm-global.c b/xfce4-session/xfsm-global.c index c8106da0..c10ab973 100644 --- a/xfce4-session/xfsm-global.c +++ b/xfce4-session/xfsm-global.c @@ -42,6 +42,12 @@ #include #endif +#ifdef HAVE_ERRNO_H +#include +#endif + +#include + #include #include @@ -213,3 +219,292 @@ xfsm_g_value_free (GValue *value) } } + +static gboolean +xfsm_check_valid_exec (const gchar *exec) +{ + gboolean result = TRUE; + gchar *tmp; + gchar *p; + + if (*exec == '/') + { + result = (access (exec, X_OK) == 0); + } + else + { + tmp = g_strdup (exec); + p = strchr (tmp, ' '); + if (G_UNLIKELY (p != NULL)) + *p = '\0'; + + p = g_find_program_in_path (tmp); + g_free (tmp); + + if (G_UNLIKELY (p == NULL)) + { + result = FALSE; + } + else + { + result = (access (p, X_OK) == 0); + g_free (p); + } + } + + return result; +} + + + +static void +xfsm_startup_autostart_migrate (void) +{ + const gchar *entry; + gchar source_path[4096]; + gchar target_path[4096]; + gchar *source; + gchar *target; + FILE *fp; + GDir *dp; + + /* migrate the content */ + source = xfce_get_homefile ("Desktop", "Autostart/", NULL); + dp = g_dir_open (source, 0, NULL); + if (G_UNLIKELY (dp != NULL)) + { + /* check if the LOCATION-CHANGED.txt file exists and the target can be opened */ + g_snprintf (source_path, 4096, "%s/LOCATION-CHANGED.txt", source); + target = xfce_resource_save_location (XFCE_RESOURCE_CONFIG, "autostart/", TRUE); + if (G_LIKELY (target != NULL && !g_file_test (source_path, G_FILE_TEST_IS_REGULAR))) + { + g_message ("Trying to migrate autostart items from %s to %s...", source, target); + + for (;;) + { + entry = g_dir_read_name (dp); + if (entry == NULL) + break; + + /* determine full source and dest paths */ + g_snprintf (source_path, 4096, "%s%s", source, entry); + g_snprintf (target_path, 4096, "%s%s", target, entry); + + /* try to move the file */ + if (rename (source_path, target_path) < 0) + { + g_warning ("Failed to rename %s to %s: %s", + source_path, target_path, + g_strerror (errno)); + continue; + } + + /* check if the file is executable */ + if (!g_file_test (target_path, G_FILE_TEST_IS_EXECUTABLE)) + continue; + + /* generate a .desktop file for the executable file */ + g_snprintf (source_path, 4096, "%s.desktop", target_path); + if (!g_file_test (source_path, G_FILE_TEST_IS_REGULAR)) + { + fp = fopen (source_path, "w"); + if (G_LIKELY (fp != NULL)) + { + fprintf (fp, + "# This file was automatically generated for the autostart\n" + "# item %s\n" + "[Desktop Entry]\n" + "Type=Application\n" + "Exec=%s\n" + "Hidden=False\n" + "Terminal=False\n" + "StartupNotify=False\n" + "Version=0.9.4\n" + "Name=%s\n", + entry, target_path, entry); + fclose (fp); + } + else + { + g_warning ("Failed to create a .desktop file for %s: %s", + target_path, g_strerror (errno)); + } + } + } + + /* create the LOCATION-CHANGED.txt file to let the user know */ + g_snprintf (source_path, 4096, "%s/LOCATION-CHANGED.txt", source); + fp = fopen (source_path, "w"); + if (G_LIKELY (fp != NULL)) + { + g_fprintf (fp, _("The location and the format of the autostart directory has changed.\n" + "The new location is\n" + "\n" + " %s\n" + "\n" + "where you can place .desktop files to, that describe the applications\n" + "to start when you login to your Xfce desktop. The files in your old\n" + "autostart directory have been successfully migrated to the new\n" + "location.\n" + "You should delete this directory now.\n"), target); + fclose (fp); + } + + g_free (target); + } + + g_dir_close (dp); + } +} + + + +gint +xfsm_startup_autostart_xdg (gboolean start_at_spi) +{ + const gchar *try_exec; + const gchar *type; + const gchar *exec; + gboolean startup_notify; + gboolean terminal; + gboolean skip; + GError *error = NULL; + XfceRc *rc; + gchar **files; + gchar **only_show_in; + gchar **not_show_in; + gint started = 0; + gint n, m; + gchar *filename; + const gchar *pattern; + + /* migrate the old autostart location (if still present) */ + xfsm_startup_autostart_migrate (); + + /* pattern for only at-spi desktop files or everything */ + if (start_at_spi) + pattern = "autostart/at-spi-*.desktop"; + else + pattern = "autostart/*.desktop"; + + files = xfce_resource_match (XFCE_RESOURCE_CONFIG, pattern, TRUE); + for (n = 0; files[n] != NULL; ++n) + { + rc = xfce_rc_config_open (XFCE_RESOURCE_CONFIG, files[n], TRUE); + if (G_UNLIKELY (rc == NULL)) + continue; + + xfce_rc_set_group (rc, "Desktop Entry"); + + /* check the Hidden key */ + skip = xfce_rc_read_bool_entry (rc, "Hidden", FALSE); + if (G_LIKELY (!skip)) + { + xfsm_verbose("hidden set\n"); + + if (xfce_rc_read_bool_entry (rc, "X-XFCE-Autostart-Override", FALSE)) + { + /* override the OnlyShowIn check */ + skip = FALSE; + xfsm_verbose ("X-XFCE-Autostart-Override set, launching\n"); + } + else + { + /* check the OnlyShowIn setting */ + only_show_in = xfce_rc_read_list_entry (rc, "OnlyShowIn", ";"); + if (G_UNLIKELY (only_show_in != NULL)) + { + /* check if "XFCE" is specified */ + for (m = 0, skip = TRUE; only_show_in[m] != NULL; ++m) + { + if (g_ascii_strcasecmp (only_show_in[m], "XFCE") == 0) + { + skip = FALSE; + xfsm_verbose ("only show in XFCE set, launching\n"); + break; + } + } + + g_strfreev (only_show_in); + } + } + + /* check the NotShowIn setting */ + not_show_in = xfce_rc_read_list_entry (rc, "NotShowIn", ";"); + if (G_UNLIKELY (not_show_in != NULL)) + { + /* check if "Xfce" is not specified */ + for (m = 0; not_show_in[m] != NULL; ++m) + if (g_ascii_strcasecmp (not_show_in[m], "XFCE") == 0) + { + skip = TRUE; + xfsm_verbose ("NotShowIn Xfce set, skipping\n"); + break; + } + + g_strfreev (not_show_in); + } + + /* skip at-spi launchers if not in at-spi mode or don't skip + * them no matter what the OnlyShowIn key says if only + * launching at-spi */ + filename = g_path_get_basename (files[n]); + if (g_str_has_prefix (filename, "at-spi-")) + { + skip = !start_at_spi; + xfsm_verbose ("start_at_spi (a11y support), %s\n", skip ? "skipping" : "showing"); + } + g_free (filename); + } + + /* check the "Type" key */ + type = xfce_rc_read_entry (rc, "Type", NULL); + if (G_UNLIKELY (!skip && type != NULL && g_ascii_strcasecmp (type, "Application") != 0)) + { + skip = TRUE; + xfsm_verbose ("Type == Application, skipping\n"); + } + + /* check the "TryExec" key */ + try_exec = xfce_rc_read_entry (rc, "TryExec", NULL); + if (G_UNLIKELY (!skip && try_exec != NULL)) + { + skip = !xfsm_check_valid_exec (try_exec); + if (skip) + xfsm_verbose ("TryExec set and xfsm_check_valid_exec failed, skipping\n"); + } + + /* execute the item */ + exec = xfce_rc_read_entry (rc, "Exec", NULL); + if (G_LIKELY (!skip && exec != NULL)) + { + /* query launch parameters */ + startup_notify = xfce_rc_read_bool_entry (rc, "StartupNotify", FALSE); + terminal = xfce_rc_read_bool_entry (rc, "Terminal", FALSE); + + /* try to launch the command */ + xfsm_verbose ("Autostart: running command \"%s\"\n", exec); + if (!xfce_spawn_command_line_on_screen (gdk_screen_get_default (), + exec, + terminal, + startup_notify, + &error)) + { + g_warning ("Unable to launch \"%s\" (specified by %s): %s", exec, files[n], error->message); + xfsm_verbose ("Unable to launch \"%s\" (specified by %s): %s\n", exec, files[n], error->message); + g_error_free (error); + error = NULL; + } + else + { + ++started; + } + } + + /* cleanup */ + xfce_rc_close (rc); + } + g_strfreev (files); + + return started; +} diff --git a/xfce4-session/xfsm-global.h b/xfce4-session/xfsm-global.h index 610d5bf7..6bce6ff9 100644 --- a/xfce4-session/xfsm-global.h +++ b/xfce4-session/xfsm-global.h @@ -74,4 +74,7 @@ GdkPixbuf *xfsm_load_session_preview (const gchar *name); GValue *xfsm_g_value_new (GType gtype); void xfsm_g_value_free (GValue *value); + +gint xfsm_startup_autostart_xdg (gboolean start_at_spi); + #endif /* !__XFSM_GLOBAL_H__ */ diff --git a/xfce4-session/xfsm-startup.c b/xfce4-session/xfsm-startup.c index e487bcd9..04d3853c 100644 --- a/xfce4-session/xfsm-startup.c +++ b/xfce4-session/xfsm-startup.c @@ -467,304 +467,12 @@ figure_app_name (const gchar *program_path) -static gboolean -xfsm_check_valid_exec (const gchar *exec) -{ - gboolean result = TRUE; - gchar *tmp; - gchar *p; - - if (*exec == '/') - { - result = (access (exec, X_OK) == 0); - } - else - { - tmp = g_strdup (exec); - p = strchr (tmp, ' '); - if (G_UNLIKELY (p != NULL)) - *p = '\0'; - - p = g_find_program_in_path (tmp); - g_free (tmp); - - if (G_UNLIKELY (p == NULL)) - { - result = FALSE; - } - else - { - result = (access (p, X_OK) == 0); - g_free (p); - } - } - - return result; -} - - - -static void -xfsm_startup_autostart_migrate (void) -{ - const gchar *entry; - gchar source_path[4096]; - gchar target_path[4096]; - gchar *source; - gchar *target; - FILE *fp; - GDir *dp; - - /* migrate the content */ - source = xfce_get_homefile ("Desktop", "Autostart/", NULL); - dp = g_dir_open (source, 0, NULL); - if (G_UNLIKELY (dp != NULL)) - { - /* check if the LOCATION-CHANGED.txt file exists and the target can be opened */ - g_snprintf (source_path, 4096, "%s/LOCATION-CHANGED.txt", source); - target = xfce_resource_save_location (XFCE_RESOURCE_CONFIG, "autostart/", TRUE); - if (G_LIKELY (target != NULL && !g_file_test (source_path, G_FILE_TEST_IS_REGULAR))) - { - g_message ("Trying to migrate autostart items from %s to %s...", source, target); - - for (;;) - { - entry = g_dir_read_name (dp); - if (entry == NULL) - break; - - /* determine full source and dest paths */ - g_snprintf (source_path, 4096, "%s%s", source, entry); - g_snprintf (target_path, 4096, "%s%s", target, entry); - - /* try to move the file */ - if (rename (source_path, target_path) < 0) - { - g_warning ("Failed to rename %s to %s: %s", - source_path, target_path, - g_strerror (errno)); - continue; - } - - /* check if the file is executable */ - if (!g_file_test (target_path, G_FILE_TEST_IS_EXECUTABLE)) - continue; - - /* generate a .desktop file for the executable file */ - g_snprintf (source_path, 4096, "%s.desktop", target_path); - if (!g_file_test (source_path, G_FILE_TEST_IS_REGULAR)) - { - fp = fopen (source_path, "w"); - if (G_LIKELY (fp != NULL)) - { - fprintf (fp, - "# This file was automatically generated for the autostart\n" - "# item %s\n" - "[Desktop Entry]\n" - "Type=Application\n" - "Exec=%s\n" - "Hidden=False\n" - "Terminal=False\n" - "StartupNotify=False\n" - "Version=0.9.4\n" - "Name=%s\n", - entry, target_path, entry); - fclose (fp); - } - else - { - g_warning ("Failed to create a .desktop file for %s: %s", - target_path, g_strerror (errno)); - } - } - } - - /* create the LOCATION-CHANGED.txt file to let the user know */ - g_snprintf (source_path, 4096, "%s/LOCATION-CHANGED.txt", source); - fp = fopen (source_path, "w"); - if (G_LIKELY (fp != NULL)) - { - g_fprintf (fp, _("The location and the format of the autostart directory has changed.\n" - "The new location is\n" - "\n" - " %s\n" - "\n" - "where you can place .desktop files to, that describe the applications\n" - "to start when you login to your Xfce desktop. The files in your old\n" - "autostart directory have been successfully migrated to the new\n" - "location.\n" - "You should delete this directory now.\n"), target); - fclose (fp); - } - - g_free (target); - } - - g_dir_close (dp); - } -} - - - -static gint -xfsm_startup_autostart_xdg (XfsmManager *manager, - gboolean start_at_spi) -{ - const gchar *try_exec; - const gchar *type; - const gchar *exec; - gboolean startup_notify; - gboolean terminal; - gboolean skip; - GError *error = NULL; - XfceRc *rc; - gchar **files; - gchar **only_show_in; - gchar **not_show_in; - gint started = 0; - gint n, m; - gchar *filename; - const gchar *pattern; - - /* migrate the old autostart location (if still present) */ - xfsm_startup_autostart_migrate (); - - /* pattern for only at-spi desktop files or everything */ - if (start_at_spi) - pattern = "autostart/at-spi-*.desktop"; - else - pattern = "autostart/*.desktop"; - - files = xfce_resource_match (XFCE_RESOURCE_CONFIG, pattern, TRUE); - for (n = 0; files[n] != NULL; ++n) - { - rc = xfce_rc_config_open (XFCE_RESOURCE_CONFIG, files[n], TRUE); - if (G_UNLIKELY (rc == NULL)) - continue; - - xfce_rc_set_group (rc, "Desktop Entry"); - - /* check the Hidden key */ - skip = xfce_rc_read_bool_entry (rc, "Hidden", FALSE); - if (G_LIKELY (!skip)) - { - xfsm_verbose("hidden set\n"); - - if (xfce_rc_read_bool_entry (rc, "X-XFCE-Autostart-Override", FALSE)) - { - /* override the OnlyShowIn check */ - skip = FALSE; - xfsm_verbose ("X-XFCE-Autostart-Override set, launching\n"); - } - else - { - /* check the OnlyShowIn setting */ - only_show_in = xfce_rc_read_list_entry (rc, "OnlyShowIn", ";"); - if (G_UNLIKELY (only_show_in != NULL)) - { - /* check if "XFCE" is specified */ - for (m = 0, skip = TRUE; only_show_in[m] != NULL; ++m) - { - if (g_ascii_strcasecmp (only_show_in[m], "XFCE") == 0) - { - skip = FALSE; - xfsm_verbose ("only show in XFCE set, launching\n"); - break; - } - } - - g_strfreev (only_show_in); - } - } - - /* check the NotShowIn setting */ - not_show_in = xfce_rc_read_list_entry (rc, "NotShowIn", ";"); - if (G_UNLIKELY (not_show_in != NULL)) - { - /* check if "Xfce" is not specified */ - for (m = 0; not_show_in[m] != NULL; ++m) - if (g_ascii_strcasecmp (not_show_in[m], "XFCE") == 0) - { - skip = TRUE; - xfsm_verbose ("NotShowIn Xfce set, skipping\n"); - break; - } - - g_strfreev (not_show_in); - } - - /* skip at-spi launchers if not in at-spi mode or don't skip - * them no matter what the OnlyShowIn key says if only - * launching at-spi */ - filename = g_path_get_basename (files[n]); - if (g_str_has_prefix (filename, "at-spi-")) - { - skip = !start_at_spi; - xfsm_verbose ("start_at_spi (a11y support), %s\n", skip ? "skipping" : "showing"); - } - g_free (filename); - } - - /* check the "Type" key */ - type = xfce_rc_read_entry (rc, "Type", NULL); - if (G_UNLIKELY (!skip && type != NULL && g_ascii_strcasecmp (type, "Application") != 0)) - { - skip = TRUE; - xfsm_verbose ("Type == Application, skipping\n"); - } - - /* check the "TryExec" key */ - try_exec = xfce_rc_read_entry (rc, "TryExec", NULL); - if (G_UNLIKELY (!skip && try_exec != NULL)) - { - skip = !xfsm_check_valid_exec (try_exec); - if (skip) - xfsm_verbose ("TryExec set and xfsm_check_valid_exec failed, skipping\n"); - } - - /* execute the item */ - exec = xfce_rc_read_entry (rc, "Exec", NULL); - if (G_LIKELY (!skip && exec != NULL)) - { - /* query launch parameters */ - startup_notify = xfce_rc_read_bool_entry (rc, "StartupNotify", FALSE); - terminal = xfce_rc_read_bool_entry (rc, "Terminal", FALSE); - - /* try to launch the command */ - xfsm_verbose ("Autostart: running command \"%s\"\n", exec); - if (!xfce_spawn_command_line_on_screen (gdk_screen_get_default (), - exec, - terminal, - startup_notify, - &error)) - { - g_warning ("Unable to launch \"%s\" (specified by %s): %s", exec, files[n], error->message); - xfsm_verbose ("Unable to launch \"%s\" (specified by %s): %s\n", exec, files[n], error->message); - g_error_free (error); - error = NULL; - } - else - { - ++started; - } - } - - /* cleanup */ - xfce_rc_close (rc); - } - g_strfreev (files); - - return started; -} - - - static void xfsm_startup_autostart (XfsmManager *manager) { gint n; - n = xfsm_startup_autostart_xdg (manager, FALSE); + n = xfsm_startup_autostart_xdg (FALSE); if (n > 0) { @@ -870,7 +578,7 @@ xfsm_startup_at (XfsmManager *manager) gint n, i; /* start at-spi-dbus-bus and/or at-spi-registryd */ - n = xfsm_startup_autostart_xdg (manager, TRUE); + n = xfsm_startup_autostart_xdg (TRUE); if (n > 0) { -- cgit v1.2.1