[easytag] Return a GError when launching external programs



commit a9d84f50b48c2398e39800d355b0e4ea24e6bd5f
Author: David King <amigadave amigadave com>
Date:   Thu Nov 20 22:37:33 2014 +0000

    Return a GError when launching external programs
    
    Consistently return a GError when reporting failures to launch external
    applications, and allow the calling code to show the error to the user
    as necessary.

 src/application_window.c |   32 +++++++++++++++-
 src/browser.c            |   47 +++++++++++++++++++++--
 src/misc.c               |   92 ++++++++++++++++-----------------------------
 src/misc.h               |   46 +++++++++-------------
 4 files changed, 125 insertions(+), 92 deletions(-)
---
diff --git a/src/application_window.c b/src/application_window.c
index 2889853..3162a50 100644
--- a/src/application_window.c
+++ b/src/application_window.c
@@ -1470,12 +1470,42 @@ on_run_player_artist (GSimpleAction *action,
     et_browser_run_player_for_artist_list (ET_BROWSER (priv->browser));
 }
 
+static gboolean
+run_audio_player_using_directory (GError **error)
+{
+    GList *l;
+    GList *file_list = NULL;
+    gboolean res;
+
+    for (l = g_list_first (ETCore->ETFileList); l != NULL; l = g_list_next (l))
+    {
+        ET_File *etfile = (ET_File *)l->data;
+        gchar *path = ((File_Name *)etfile->FileNameCur->data)->value;
+        file_list = g_list_prepend (file_list, g_file_new_for_path (path));
+    }
+
+    file_list = g_list_reverse (file_list);
+
+    res = et_run_audio_player (file_list, error);
+
+    g_list_free_full (file_list, g_object_unref);
+
+    return res;
+}
+
 static void
 on_run_player_directory (GSimpleAction *action,
                          GVariant *variant,
                          gpointer user_data)
 {
-    Run_Audio_Player_Using_Directory ();
+    GError *error = NULL;
+
+    if (!run_audio_player_using_directory (&error))
+    {
+        Log_Print (LOG_ERROR, _("Failed to launch program ‘%s’"),
+                   error->message);
+        g_error_free (error);
+    }
 }
 
 static void
diff --git a/src/browser.c b/src/browser.c
index dfebd5b..4d61776 100644
--- a/src/browser.c
+++ b/src/browser.c
@@ -330,6 +330,7 @@ et_browser_run_player_for_album_list (EtBrowser *self)
     GtkTreeSelection *selection;
     GList *l;
     GList *file_list = NULL;
+    GError *error = NULL;
 
     priv = et_browser_get_instance_private (self);
 
@@ -352,7 +353,13 @@ et_browser_run_player_for_album_list (EtBrowser *self)
 
     file_list = g_list_reverse (file_list);
 
-    et_run_audio_player (file_list);
+    if (!et_run_audio_player (file_list, &error))
+    {
+        Log_Print (LOG_ERROR, _("Failed to launch program ‘%s’"),
+                   error->message);
+        g_error_free (error);
+    }
+
     g_list_free_full (file_list, g_object_unref);
 }
 
@@ -364,6 +371,7 @@ et_browser_run_player_for_artist_list (EtBrowser *self)
     GtkTreeSelection *selection;
     GList *l, *m;
     GList *file_list = NULL;
+    GError *error = NULL;
 
     priv = et_browser_get_instance_private (self);
 
@@ -388,7 +396,13 @@ et_browser_run_player_for_artist_list (EtBrowser *self)
 
     file_list = g_list_reverse (file_list);
 
-    et_run_audio_player (file_list);
+    if (!et_run_audio_player (file_list, &error))
+    {
+        Log_Print (LOG_ERROR, _("Failed to launch program ‘%s’"),
+                   error->message);
+        g_error_free (error);
+    }
+
     g_list_free_full (file_list, g_object_unref);
 }
 
@@ -400,6 +414,7 @@ et_browser_run_player_for_selection (EtBrowser *self)
     GList *l;
     GList *file_list = NULL;
     GtkTreeSelection *selection;
+    GError *error = NULL;
 
     priv = et_browser_get_instance_private (self);
 
@@ -415,7 +430,13 @@ et_browser_run_player_for_selection (EtBrowser *self)
 
     file_list = g_list_reverse (file_list);
 
-    et_run_audio_player (file_list);
+    if (!et_run_audio_player (file_list, &error))
+    {
+        Log_Print (LOG_ERROR, _("Failed to launch program ‘%s’"),
+                   error->message);
+        g_error_free (error);
+    }
+
     g_list_free_full (file_list, g_object_unref);
     g_list_free_full (selfilelist, (GDestroyNotify)gtk_tree_path_free);
 }
@@ -4560,6 +4581,7 @@ Run_Program_With_Directory (EtBrowser *self)
     gchar *current_directory;
     GList *args_list = NULL;
     gboolean program_ran;
+    GError *error = NULL;
 
     priv = et_browser_get_instance_private (self);
 
@@ -4574,7 +4596,7 @@ Run_Program_With_Directory (EtBrowser *self)
     // List of parameters (here only one! : the current directory)
     args_list = g_list_append(args_list,current_directory);
 
-    program_ran = et_run_program (program_name, args_list);
+    program_ran = et_run_program (program_name, args_list, &error);
     g_list_free(args_list);
 
     if (program_ran)
@@ -4595,6 +4617,13 @@ Run_Program_With_Directory (EtBrowser *self)
 
         g_free (msg);
     }
+    else
+    {
+        Log_Print (LOG_ERROR, _("Failed to launch program ‘%s’"),
+                   error->message);
+        g_clear_error (&error);
+    }
+
     g_free(program_name);
 }
 
@@ -4609,6 +4638,7 @@ Run_Program_With_Selected_Files (EtBrowser *self)
     GList   *args_list = NULL;
     GtkTreeIter iter;
     gboolean program_ran;
+    GError *error = NULL;
 
     priv = et_browser_get_instance_private (self);
 
@@ -4636,7 +4666,7 @@ Run_Program_With_Selected_Files (EtBrowser *self)
     }
 
     args_list = g_list_reverse (args_list);
-    program_ran = et_run_program (program_name, args_list);
+    program_ran = et_run_program (program_name, args_list, &error);
 
     g_list_free_full (selected_paths, (GDestroyNotify)gtk_tree_path_free);
     g_list_free(args_list);
@@ -4661,6 +4691,13 @@ Run_Program_With_Selected_Files (EtBrowser *self)
 
         g_free (msg);
     }
+    else
+    {
+        Log_Print (LOG_ERROR, _("Failed to launch program ‘%s’"),
+                   error->message);
+        g_clear_error (&error);
+    }
+
     g_free(program_name);
 }
 
diff --git a/src/misc.c b/src/misc.c
index da076ca..b35d511 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -1,20 +1,20 @@
-/*
- *  EasyTAG - Tag editor for MP3 and Ogg Vorbis files
- *  Copyright (C) 2000-2003  Jerome Couderc <easytag gmail com>
+/* EasyTAG - Tag editor for audio files
+ * Copyright (C) 2014  David King <amigadave amigadave com>
+ * Copyright (C) 2000-2003  Jerome Couderc <easytag gmail com>
  *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
  *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #include "config.h"
@@ -32,7 +32,6 @@
 #include "browser.h"
 #include "setting.h"
 #include "preferences_dialog.h"
-#include "log.h"
 #include "charset.h"
 
 #ifdef G_OS_WIN32
@@ -226,7 +225,9 @@ void Set_Unbusy_Cursor (void)
  *  - args_list : list of filename (with path)
  */
 gboolean
-et_run_program (const gchar *program_name, GList *args_list)
+et_run_program (const gchar *program_name,
+                GList *args_list,
+                GError **error)
 {
     gchar *program_tmp;
     const gchar *program_args;
@@ -234,13 +235,13 @@ et_run_program (const gchar *program_name, GList *args_list)
     guint n_program_args = 0;
     gsize i;
     GPid pid;
-    GError *error = NULL;
     gchar **argv;
     GList *l;
     gchar *program_path;
     gboolean res = FALSE;
 
     g_return_val_if_fail (program_name != NULL && args_list != NULL, FALSE);
+    g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
     /* Check if a name for the program has been supplied */
     if (!*program_name)
@@ -327,18 +328,12 @@ et_run_program (const gchar *program_name, GList *args_list)
     /* Execution ... */
     if (g_spawn_async (NULL, argv, NULL,
                        G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
-                       NULL, NULL, &pid, &error))
+                       NULL, NULL, &pid, error))
     {
         g_child_watch_add (pid, et_on_child_exited, NULL);
 
         res = TRUE;
     }
-    else
-    {
-        Log_Print (LOG_ERROR, _("Failed to launch program ‘%s’"),
-                   error->message);
-        g_clear_error (&error);
-    }
 
     g_strfreev (program_args_argv);
     g_free (program_path);
@@ -412,29 +407,25 @@ static void Open_File_Selection_Window (GtkWidget *entry, gchar *title, GtkFileC
     gtk_widget_destroy(FileSelectionWindow);
 }
 
-
-
-void
-et_run_audio_player (GList *files)
+gboolean
+et_run_audio_player (GList *files,
+                     GError **error)
 {
     GFileInfo *info;
-    GError *error = NULL;
     const gchar *content_type;
     GAppInfo *app_info;
     GdkAppLaunchContext *context;
 
-    g_return_if_fail (files != NULL);
+    g_return_val_if_fail (files != NULL, FALSE);
+    g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
     info = g_file_query_info (files->data,
                               G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
-                              G_FILE_QUERY_INFO_NONE, NULL, &error);
+                              G_FILE_QUERY_INFO_NONE, NULL, error);
 
-    if (error)
+    if (info == NULL)
     {
-        g_warning ("Unable to get content type for file: %s",
-                   error->message);
-        g_error_free (error);
-        return;
+        return FALSE;
     }
 
     content_type = g_file_info_get_content_type (info);
@@ -444,35 +435,18 @@ et_run_audio_player (GList *files)
     context = gdk_display_get_app_launch_context (gdk_display_get_default ());
 
     if (!g_app_info_launch (app_info, files, G_APP_LAUNCH_CONTEXT (context),
-                            &error))
+                            error))
     {
-        Log_Print (LOG_ERROR, _("Failed to launch program ‘%s’"),
-                   error->message);
-        g_error_free (error);
+        g_object_unref (context);
+        g_object_unref (app_info);
+
+        return FALSE;
     }
 
     g_object_unref (context);
     g_object_unref (app_info);
-}
-
-void
-Run_Audio_Player_Using_Directory (void)
-{
-    GList *l;
-    GList *file_list = NULL;
-
-    for (l = g_list_first (ETCore->ETFileList); l != NULL; l = g_list_next (l))
-    {
-        ET_File *etfile = (ET_File *)l->data;
-        gchar *path = ((File_Name *)etfile->FileNameCur->data)->value;
-        file_list = g_list_prepend (file_list, g_file_new_for_path (path));
-    }
 
-    file_list = g_list_reverse (file_list);
-
-    et_run_audio_player (file_list);
-
-    g_list_free_full (file_list, g_object_unref);
+    return TRUE;
 }
 
 /*
diff --git a/src/misc.h b/src/misc.h
index 6a21b0c..2291a38 100644
--- a/src/misc.h
+++ b/src/misc.h
@@ -1,35 +1,29 @@
-/* misc.h - 2000/06/28 */
-/*
- *  EasyTAG - Tag editor for MP3 and Ogg Vorbis files
- *  Copyright (C) 2000-2003  Jerome Couderc <easytag gmail com>
+/* EasyTAG - Tag editor for audio files
+ * Copyright (C) 2014  David King <amigadave amigadave com>
+ * Copyright (C) 2000-2003  Jerome Couderc <easytag gmail com>
  *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
  *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
-#ifndef __MISC_H__
-#define __MISC_H__
-
+#ifndef ET_MISC_H_
+#define ET_MISC_H_
 
 #include <gtk/gtk.h>
 
 G_BEGIN_DECLS
 
-/**************
- * Prototypes *
- **************/
-
 /*
  * Combobox misc functions
  */
@@ -47,8 +41,6 @@ void Init_Mouse_Cursor    (void);
 void Set_Busy_Cursor      (void);
 void Set_Unbusy_Cursor    (void);
 
-// Run Audio Player
-void Run_Audio_Player_Using_Directory (void);
 
 gchar *Convert_Duration (gulong duration);
 
@@ -56,8 +48,8 @@ goffset et_get_file_size (const gchar *filename);
 
 gint Combo_Alphabetic_Sort (GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer data);
 
-void et_run_audio_player (GList *files);
-gboolean et_run_program (const gchar *program_name, GList *args_list);
+gboolean et_run_audio_player (GList *files, GError **error);
+gboolean et_run_program (const gchar *program_name, GList *args_list, GError **error);
 
 void File_Selection_Window_For_File      (GtkWidget *entry);
 void File_Selection_Window_For_Directory (GtkWidget *entry);
@@ -69,4 +61,4 @@ void et_on_child_exited (GPid pid, gint status, gpointer user_data);
 
 G_END_DECLS
 
-#endif /* __MISC_H__ */
+#endif /* ET_MISC_H_ */


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