[easytag/wip/unstable: 395/396] Use GIO to launch audio player process



commit 4fed8aad0ddfe57503431c9927a09314536fb48c
Author: David King <amigadave amigadave com>
Date:   Sat Apr 27 20:35:08 2013 +0100

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

 src/misc.c |  135 +++++++++++++++--------------------------------------------
 1 files changed, 35 insertions(+), 100 deletions(-)
---
diff --git a/src/misc.c b/src/misc.c
index 7bc79d4..82a58c2 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -752,21 +752,13 @@ static void Open_File_Selection_Window (GtkWidget *entry, gchar *title, GtkFileC
 static void
 Run_Audio_Player_Using_File_List (GList *etfilelist)
 {
-    gchar  **argv;
-    gint     argv_index = 0;
     GList *l;
     ET_File *etfile;
     gchar   *filename;
-    gchar   *program_path;
-#ifdef G_OS_WIN32
-    gchar              *argv_join;
-    STARTUPINFO         siStartupInfo;
-    PROCESS_INFORMATION piProcessInfo;
-#else /* !G_OS_WIN32 */
-    pid_t   pid;
-    gchar **argv_user;
-    gint    argv_user_number;
-#endif /* !G_OS_WIN32 */
+    GdkAppLaunchContext *context;
+    GAppInfo *appinfo;
+    GError *error = NULL;
+    GList *files = NULL;
 
     g_return_if_fail (etfilelist != NULL);
 
@@ -787,113 +779,56 @@ Run_Audio_Player_Using_File_List (GList *etfilelist)
         return;
     }
 
-    if ( !(program_path = Check_If_Executable_Exists(AUDIO_FILE_PLAYER)) )
+#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 (AUDIO_FILE_PLAYER, NULL,
+                                                  G_APP_INFO_CREATE_NONE,
+                                                  &error);
+
+    if (error != NULL)
     {
-        gchar *msg = g_strdup_printf(_("The program '%s' cannot be found"),AUDIO_FILE_PLAYER);
-        Log_Print (LOG_ERROR, "%s", msg);
-        g_free(msg);
+        Log_Print (LOG_ERROR,
+                   _("Failed to launch audio player: %s"), error->message);
+        g_clear_error (&error);
+        g_object_unref (context);
+        g_object_unref (appinfo);
+
         return;
     }
-    g_free(program_path);
-
-#ifdef G_OS_WIN32
-    // 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(etfilelist) + 2); // "+2" for 1rst arg 'foo' and last arg 'NULL'
-    argv[argv_index++] = "foo";
 
-    // Load files as arguments
     for (l = etfilelist; l != NULL; l = g_list_next (l))
     {
         etfile = (ET_File *)l->data;
         filename = ((File_Name *)etfile->FileNameCur->data)->value;
-        //filename_utf8 = ((File_Name *)etfile->FileNameCur->data)->value_utf8;
-        /* TODO: Use g_shell_quote() instead. */
-        // We must enclose filename between quotes, because of possible (probable!) spaces in filenames"
-        argv[argv_index++] = g_strconcat("\"", filename, "\"", NULL);
+        files = g_list_prepend (files, g_file_new_for_path (filename));
     }
-    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);
+    files = g_list_reverse (files);
 
-    if (CreateProcess(AUDIO_FILE_PLAYER,
-                      argv_join,
-                      NULL,
-                      NULL,
-                      FALSE,
-                      CREATE_DEFAULT_ERROR_MODE,
-                      NULL,
-                      NULL,
-                      &siStartupInfo,
-                      &piProcessInfo) == FALSE)
+    if (g_app_info_supports_uris (appinfo))
     {
-        gchar *error;
-
-        error = g_win32_error_message (GetLastError ());
-        Log_Print (LOG_ERROR, _("Cannot execute ā€˜%sā€™ (%s)"), AUDIO_FILE_PLAYER,
-                   error);
-        g_free (error);
+        g_app_info_launch_uris (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);
-
-#else /* !G_OS_WIN32 */
-    argv_user = g_strsplit(AUDIO_FILE_PLAYER," ",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++);
-
-    /* +1 for NULL. */
-    argv = g_new0 (gchar *,
-                   argv_user_number + g_list_length (etfilelist) + 1);
-
-    // Load 'user' arguments (program name and more...)
-    while (argv_user[argv_index])
-    {
-        argv[argv_index] = argv_user[argv_index];
-        argv_index++;
-    }
-
-    // Load files as arguments
-    for (l = etfilelist; l != NULL; l = g_list_next (l))
+    else
     {
-        etfile = (ET_File *)l->data;
-        filename = ((File_Name *)etfile->FileNameCur->data)->value;
-        //filename_utf8 = ((File_Name *)etfile->FileNameCur->data)->value_utf8;
-        argv[argv_index++] = filename;
+           g_app_info_launch (appinfo, files, G_APP_LAUNCH_CONTEXT (context),
+                           &error);
     }
-    argv[argv_index] = NULL; // Ends the list of arguments
 
-    pid = fork();
-    switch (pid)
+    if (error != NULL)
     {
-        case -1:
-            Log_Print(LOG_ERROR,_("Cannot fork another process"));
-            break;
-        case 0:
-        {
-            if (execvp(argv[0],argv) == -1)
-            {
-                Log_Print (LOG_ERROR, _("Cannot execute ā€˜%sā€™ (%s)"), argv[0],
-                           g_strerror (errno));
-            }
-            g_strfreev(argv_user);
-            _exit(1);
-            break;
-        }
-        default:
-            break;
+        Log_Print (LOG_ERROR, _("Failed to launch audio player: %s"),
+                   error->message);
+        g_clear_error (&error);
     }
-#endif /* !G_OS_WIN32 */
 
-    g_free(argv);
+    g_object_unref (context);
+    g_object_unref (appinfo);
+    g_list_free_full (files, g_object_unref);
 }
 
 void Run_Audio_Player_Using_Directory (void)


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