[easytag/wip/application-window] Use GIO to launch open with process



commit 15760e35f3105467450faaf55688d5fa6c334d50
Author: David King <amigadave amigadave com>
Date:   Sat Apr 27 21:26:53 2013 +0100

    Use GIO to launch open with process
    
    Cut out platform-specific code to spawn a child process and instead use
    g_app_info_launch().

 TODO          |    1 -
 src/browser.c |  160 ++++++++++++++-------------------------------------------
 2 files changed, 38 insertions(+), 123 deletions(-)
---
diff --git a/TODO b/TODO
index 7f35978..bfd0d46 100644
--- a/TODO
+++ b/TODO
@@ -10,7 +10,6 @@ General tidying
 * Port file I/O to use GFile from GIO
 ** Additionally, make I/O asynchronous
 ** Always use the GLib encoding for filenames (convert for display)
-* Use g_spawn_async() or g_app_info_launch() to spawn processes
 * GObjectification
 * Avoid using gtk_events_pending(), use asynchronous operations instead
 * Add a test suite
diff --git a/src/browser.c b/src/browser.c
index aa6adb9..0ced710 100644
--- a/src/browser.c
+++ b/src/browser.c
@@ -4223,19 +4223,11 @@ Run_Program_With_Selected_Files (GtkWidget *combobox)
 static gboolean
 Run_Program (const gchar *program_name, GList *args_list)
 {
-#ifdef G_OS_WIN32
-    GList              *filelist;
-    gchar             **argv;
-    gint                argv_index = 0;
-    gchar              *argv_join;
-    gchar              *full_command;
-    STARTUPINFO         siStartupInfo;
-    PROCESS_INFORMATION piProcessInfo;
-#else /* !G_OS_WIN32 */
-    pid_t   pid;
-#endif /* !G_OS_WIN32 */
+    GdkAppLaunchContext *context;
+    GAppInfo *appinfo;
+    GList *files = NULL;
     GList *l;
-    gchar *program_path;
+    GError *error = NULL;
 
     g_return_val_if_fail (program_name != NULL && args_list != NULL, FALSE);
 
@@ -4257,130 +4249,54 @@ Run_Program (const gchar *program_name, GList *args_list)
         return FALSE;
     }
 
-    if ( !(program_path = Check_If_Executable_Exists(program_name)) )
-    {
-        GtkWidget *msgdialog;
+#if !GTK_CHECK_VERSION(3,0,0)
+    context = gdk_app_launch_context_new ();
+#else /* GTK_CHECK_VERSION(3,0,0) */
+    context = gdk_display_get_app_launch_context (gdk_display_get_default ());
+#endif /* GTK_CHECK_VERSION(3,0,0) */
+    appinfo = g_app_info_create_from_commandline (program_name, NULL,
+                                                  G_APP_INFO_CREATE_NONE,
+                                                  &error);
 
-        msgdialog = gtk_message_dialog_new(GTK_WINDOW(MainWindow),
-                                           GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
-                                           GTK_MESSAGE_ERROR,
-                                           GTK_BUTTONS_CLOSE,
-                                           _("The program '%s' cannot be found"),
-                                           program_name);
-        gtk_window_set_title(GTK_WINDOW(msgdialog),_("Program Name Error"));
+    if (error != NULL)
+    {
+        Log_Print (LOG_ERROR,
+                   _("Failed to launch program: %s"), error->message);
+        g_clear_error (&error);
+        g_object_unref (context);
+        g_object_unref (appinfo);
 
-        gtk_dialog_run(GTK_DIALOG(msgdialog));
-        gtk_widget_destroy(msgdialog);
         return FALSE;
     }
 
+    for (l = args_list; l != NULL; l = g_list_next (l))
+    {
+        files = g_list_prepend (files, g_file_new_for_path ((gchar *)l->data));
+    }
 
-#ifdef G_OS_WIN32
-    filelist = args_list;
-
-    // See documentation : http://c.developpez.com/faq/vc/?page=ProcessThread and 
http://www.answers.com/topic/createprocess
-    ZeroMemory(&siStartupInfo, sizeof(siStartupInfo));
-    siStartupInfo.cb = sizeof(siStartupInfo);
-    ZeroMemory(&piProcessInfo, sizeof(piProcessInfo));
-
-    argv = g_new0(gchar *,g_list_length(filelist) + 2); // "+2" for 1rst arg 'foo' and last arg 'NULL'
-    //argv[argv_index++] = "foo";
+    files = g_list_reverse (files);
 
-    // Load files as arguments
-    for (l = filelist; l != NULL; l = g_list_next (l))
+    if (g_app_info_supports_uris (appinfo))
     {
-        /* TODO: Use g_shell_quote() instead. */
-        // We must enclose filename between " because of possible (probable!) spaces in filenames"
-        argv[argv_index++] = g_strconcat ("\"", (gchar *)l->data, "\"", NULL);
+        g_app_info_launch_uris (appinfo, files, G_APP_LAUNCH_CONTEXT (context),
+                                &error);
     }
-    argv[argv_index] = NULL; // Ends the list of arguments
-
-    // Make a command line with all arguments (joins strings together to form one long string separated by a 
space)
-    argv_join = g_strjoinv(" ", argv);
-    // Build the full command to pass to CreateProcess (FIX ME : it will ignore args of program)
-    full_command = g_strconcat("\"",program_path,"\" ",argv_join,NULL);
-
-    //if (CreateProcess(program_path, // Here it doesn't seem to load all the selected files
-    //                  argv_join,
-    if (CreateProcess(NULL,
-                      full_command,
-                      NULL,
-                      NULL,
-                      FALSE,
-                      CREATE_DEFAULT_ERROR_MODE,
-                      NULL,
-                      NULL,
-                      &siStartupInfo,
-                      &piProcessInfo) == FALSE)
+    else
     {
-        gchar *error;
-
-        error = g_win32_error_message (GetLastError ());
-        Log_Print (LOG_ERROR, _("Cannot execute ā€˜%sā€™ (%s)"), program_name,
-                   error);
-        g_free (error);
+           g_app_info_launch (appinfo, files, G_APP_LAUNCH_CONTEXT (context),
+                           &error);
     }
 
-    // Free allocated parameters (for each filename)
-    for (argv_index = 1; argv[argv_index]; argv_index++)
-        g_free(argv[argv_index]);
-
-    g_free(argv_join);
-    g_free(full_command);
-    g_free(program_path);
-
-#else /* !G_OS_WIN32 */
-
-    g_free(program_path); // Freed as never used
-
-    pid = fork();
-    switch (pid)
+    if (error != NULL)
     {
-        case -1:
-            Log_Print(LOG_ERROR,_("Cannot fork another process\n"));
-            //exit(-1);
-            break;
-        case 0:
-        {
-            gchar **argv;
-            gint    argv_index = 0;
-            gchar **argv_user;
-            gint    argv_user_number;
-            gchar  *msg;
-
-            argv_user = g_strsplit(program_name," ",0); // the string may contains arguments, space is the 
delimiter
-            // Number of arguments into 'argv_user'
-            for (argv_user_number=0;argv_user[argv_user_number];argv_user_number++);
-
-            argv = g_new0(gchar *,argv_user_number + g_list_length(args_list) + 1); // +1 for NULL
-
-            // Load 'user' arguments (program name and more...)
-            while (argv_user[argv_index])
-            {
-                argv[argv_index] = argv_user[argv_index];
-                argv_index++;
-            }
-            // Load arguments from 'args_list'
-            for (l = args_list; l != NULL; l = g_list_next (l))
-            {
-                argv[argv_index] = (gchar *)l->data;
-                argv_index++;
-            }
-            argv[argv_index] = NULL;
-
-            // Execution ...
-            execvp(argv[0],argv);
-
-            msg = g_strdup_printf (_("Executed command: %s"), program_name);
-            Statusbar_Message(msg,TRUE);
-            g_free(msg);
-            //_exit(-1);
-            break;
-        }
-        default:
-            break;
+        Log_Print (LOG_ERROR, _("Failed to launch program: %s"),
+                   error->message);
+        g_clear_error (&error);
     }
-#endif /* !G_OS_WIN32 */
+
+    g_object_unref (context);
+    g_object_unref (appinfo);
+    g_list_free_full (files, g_object_unref);
 
     return TRUE;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]