[easytag/wip/gerror: 1/8] Use GError in FLAC tagging code



commit 054ca780cb42c7001edc095875e68757b4cabe89
Author: David King <amigadave amigadave com>
Date:   Tue Sep 23 22:28:36 2014 +0100

    Use GError in FLAC tagging code
    
    Avoid several uses of Log_Print().

 src/et_core.c          |   18 +++++++++++++--
 src/tags/flac_header.c |   54 ++++++++++++++++++++++++++++++++---------------
 src/tags/flac_header.h |    2 +-
 src/tags/flac_tag.c    |   42 ++++++++++++++++++++++---------------
 src/tags/flac_tag.h    |   52 +++++++++++++++++++--------------------------
 5 files changed, 100 insertions(+), 68 deletions(-)
---
diff --git a/src/et_core.c b/src/et_core.c
index 1588650..31fa6bb 100644
--- a/src/et_core.c
+++ b/src/et_core.c
@@ -546,7 +546,13 @@ GList *ET_Add_File_To_File_List (gchar *filename)
 #endif
 #ifdef ENABLE_FLAC
         case FLAC_TAG:
-            Flac_Tag_Read_File_Tag(filename,FileTag);
+            if (!flac_tag_read_file_tag (filename, FileTag, &error))
+            {
+                Log_Print (LOG_ERROR,
+                           _("Error reading tag from FLAC file ‘%s’: %s"),
+                           filename_utf8, error->message);
+                g_clear_error (&error);
+            }
             break;
 #endif
         case APE_TAG:
@@ -603,7 +609,13 @@ GList *ET_Add_File_To_File_List (gchar *filename)
 #endif
 #ifdef ENABLE_FLAC
         case FLAC_FILE:
-            Flac_Header_Read_File_Info(filename,ETFileInfo);
+            if (!flac_header_read_file_info (filename, ETFileInfo, &error))
+            {
+                Log_Print (LOG_ERROR,
+                           _("Error while querying information for file ‘%s’: %s"),
+                           filename_utf8, error->message);
+                g_error_free (error);
+            }
             break;
 #endif
         case MPC_FILE:
@@ -3435,7 +3447,7 @@ ET_Save_File_Tag_To_HD (ET_File *ETFile, GError **error)
 #endif
 #ifdef ENABLE_FLAC
         case FLAC_TAG:
-            state = Flac_Tag_Write_File_Tag(ETFile);
+            state = flac_tag_write_file_tag (ETFile, error);
             break;
 #endif
         case APE_TAG:
diff --git a/src/tags/flac_header.c b/src/tags/flac_header.c
index a850617..2149b62 100644
--- a/src/tags/flac_header.c
+++ b/src/tags/flac_header.c
@@ -1,21 +1,20 @@
-/* flac_header.c - 2002/07/03 */
-/*
- *  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.
  */
 
 /*
@@ -32,6 +31,7 @@
 #include <gtk/gtk.h>
 #include <glib/gi18n.h>
 #include <FLAC/all.h>
+#include <errno.h>
 
 #include "easytag.h"
 #include "et_core.h"
@@ -78,7 +78,9 @@ static void error_callback_   (const FLAC__StreamDecoder *decoder, FLAC__StreamD
  ****************************/
 
 gboolean
-Flac_Header_Read_File_Info (const gchar *filename, ET_File_Info *ETFileInfo)
+flac_header_read_file_info (const gchar *filename,
+                            ET_File_Info *ETFileInfo,
+                            GError **error)
 {
     gint duration = 0;
     gulong filesize;
@@ -87,11 +89,15 @@ Flac_Header_Read_File_Info (const gchar *filename, ET_File_Info *ETFileInfo)
     file_info_struct tmp_file_info;
 
     g_return_val_if_fail (filename != NULL && ETFileInfo != NULL, FALSE);
+    g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
     /* Decoding FLAC file */
-    tmp_decoder = FLAC__stream_decoder_new();
+    tmp_decoder = FLAC__stream_decoder_new ();
+
     if (tmp_decoder == NULL)
     {
+        g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_NOMEM, "%s",
+                     g_strerror (ENOMEM));
         return FALSE;
     }
 
@@ -99,10 +105,24 @@ Flac_Header_Read_File_Info (const gchar *filename, ET_File_Info *ETFileInfo)
 
     FLAC__stream_decoder_set_md5_checking     (tmp_decoder, false);
     if(FLAC__stream_decoder_init_file(tmp_decoder, filename, write_callback_, metadata_callback_, 
error_callback_, &tmp_file_info) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
+    {
+        /* TODO: Set error message according to FLAC__StreamDecoderInitStatus.
+         */
+        g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, "%s",
+                     _("Error opening FLAC file"));
+        FLAC__stream_decoder_finish (tmp_decoder);
+        FLAC__stream_decoder_delete (tmp_decoder);
         return FALSE;
+    }
 
     if(!FLAC__stream_decoder_process_until_end_of_metadata(tmp_decoder))
     {
+        /* TODO: Set error message according to state fetched from
+         * FLAC__stream_decoder_get_state(). */
+        g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, "%s",
+                     _("Error opening FLAC file"));
+        FLAC__stream_decoder_finish (tmp_decoder);
+        FLAC__stream_decoder_delete (tmp_decoder);
         return FALSE;
     }
 
diff --git a/src/tags/flac_header.h b/src/tags/flac_header.h
index 2ecc680..b6872d4 100644
--- a/src/tags/flac_header.h
+++ b/src/tags/flac_header.h
@@ -24,7 +24,7 @@
 
 G_BEGIN_DECLS
 
-gboolean Flac_Header_Read_File_Info (const gchar *filename, ET_File_Info *ETFileInfo);
+gboolean flac_header_read_file_info (const gchar *filename, ET_File_Info *ETFileInfo, GError **error);
 EtFileHeaderFields * Flac_Header_Display_File_Info_To_UI (const gchar *filename, ET_File *ETFile);
 void et_flac_file_header_fields_free (EtFileHeaderFields *fields);
 
diff --git a/src/tags/flac_tag.c b/src/tags/flac_tag.c
index ec0ccb2..54c35fc 100644
--- a/src/tags/flac_tag.c
+++ b/src/tags/flac_tag.c
@@ -99,9 +99,12 @@ static gboolean Flac_Set_Tag (FLAC__StreamMetadata *vc_block, const gchar *tag_n
  *  - if field is found but contains no info (strlen(str)==0), we don't read it
  */
 gboolean
-Flac_Tag_Read_File_Tag (const gchar *filename, File_Tag *FileTag)
+flac_tag_read_file_tag (const gchar *filename,
+                        File_Tag *FileTag,
+                        GError **error)
 {
     FLAC__Metadata_SimpleIterator *iter;
+    const gchar *flac_error_msg;
     gchar *string = NULL;
     gchar *filename_utf8 = filename_to_display(filename);
     guint i;
@@ -109,8 +112,7 @@ Flac_Tag_Read_File_Tag (const gchar *filename, File_Tag *FileTag)
     //gint j = 1;
 
     g_return_val_if_fail (filename != NULL && FileTag != NULL, FALSE);
-
-    flac_error_msg = NULL;
+    g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
     // Initialize the iterator for the blocks
     iter = FLAC__metadata_simple_iterator_new();
@@ -129,9 +131,8 @@ Flac_Tag_Read_File_Tag (const gchar *filename, File_Tag *FileTag)
             FLAC__metadata_simple_iterator_delete(iter);
         }
 
-        Log_Print (LOG_ERROR, _("Error while opening file ‘%s’ as FLAC: %s"),
-                   filename_utf8, flac_error_msg);
-        g_free(filename_utf8);
+        g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
+                     _("Error while opening file: %s"), flac_error_msg);
         return FALSE;
     }
     
@@ -817,22 +818,25 @@ static gboolean Flac_Set_Tag (FLAC__StreamMetadata *vc_block, const gchar *tag_n
 /*
  * Write Flac tag, using the level 2 flac interface
  */
-gboolean Flac_Tag_Write_File_Tag (ET_File *ETFile)
+gboolean
+flac_tag_write_file_tag (ET_File *ETFile, GError **error)
 {
-    File_Tag *FileTag;
-    gchar *filename_utf8, *filename;
+    const File_Tag *FileTag;
+    const gchar *filename;
+    const gchar *filename_utf8;
     gchar *basename_utf8;
+    const gchar *flac_error_msg;
     FLAC__Metadata_Chain *chain;
     FLAC__Metadata_Iterator *iter;
     FLAC__StreamMetadata_VorbisComment_Entry vce_field_vendor_string; // To save vendor string
     gboolean vce_field_vendor_string_found = FALSE;
 
     g_return_val_if_fail (ETFile != NULL && ETFile->FileTag != NULL, FALSE);
+    g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
     FileTag       = (File_Tag *)ETFile->FileTag->data;
     filename      = ((File_Name *)ETFile->FileNameCur->data)->value;
     filename_utf8 = ((File_Name *)ETFile->FileNameCur->data)->value_utf8;
-    flac_error_msg = NULL;
 
     /* libFLAC is able to detect (and skip) ID3v2 tags by itself */
     
@@ -853,8 +857,9 @@ gboolean Flac_Tag_Write_File_Tag (ET_File *ETFile)
             FLAC__metadata_chain_delete(chain);
         }
         
-        Log_Print (LOG_ERROR, _("Error while opening file ‘%s’ as FLAC: %s"),
-                   filename_utf8, flac_error_msg);
+        g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
+                     _("Error while opening file ‘%s’ as FLAC: %s"),
+                     filename_utf8, flac_error_msg);
         return FALSE;
     }
     
@@ -864,8 +869,9 @@ gboolean Flac_Tag_Write_File_Tag (ET_File *ETFile)
     {
         flac_error_msg = 
FLAC__Metadata_ChainStatusString[FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR];
 
-        Log_Print (LOG_ERROR, _("Error while opening file ‘%s’ as FLAC: %s"),
-                   filename_utf8, flac_error_msg);
+        g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
+                     _("Error while opening file ‘%s’ as FLAC: %s"),
+                     filename_utf8, flac_error_msg);
         return FALSE;
     }
     
@@ -1120,10 +1126,12 @@ gboolean Flac_Tag_Write_File_Tag (ET_File *ETFile)
 
         FLAC__metadata_chain_delete(chain);
         
-        Log_Print (LOG_ERROR, _("Failed to write comments to file ‘%s’: %s"),
-                   filename_utf8, flac_error_msg);
+        g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
+                     _("Failed to write comments to file ‘%s’: %s"),
+                     filename_utf8, flac_error_msg);
         return FALSE;
-    }else
+    }
+    else
     {
         basename_utf8 = g_path_get_basename(filename_utf8);
         Log_Print (LOG_OK, _("Wrote tag of ‘%s’"), basename_utf8);
diff --git a/src/tags/flac_tag.h b/src/tags/flac_tag.h
index 9762aea..2ac8e57 100644
--- a/src/tags/flac_tag.h
+++ b/src/tags/flac_tag.h
@@ -1,43 +1,35 @@
-/* flac_tag.h - 2003/12/27 */
-/*
- *  EasyTAG - Tag editor for MP3 and Ogg Vorbis files
- *  Copyright (C) 2001-2003  Jerome Couderc <easytag gmail com>
- *  Copyright (C) 2003       Pavel Minayev <thalion front ru>
+/* EasyTAG - Tag editor for audo files
+ * Copyright (C) 2013  David King <amigadave amigadave com>
+ * Copyright (C) 2001-2003  Jerome Couderc <easytag gmail com>
+ * Copyright (C) 2003       Pavel Minayev <thalion front ru>
  *
- *  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 __FLAC_TAG_H__
-#define __FLAC_TAG_H__
-
+#ifndef ET_FLAC_TAG_H_
+#define ET_FLAC_TAG_H_
 
 #include <glib.h>
 #include "et_core.h"
 
-/***************
- * Declaration *
- ***************/
-const gchar *flac_error_msg;
-
+G_BEGIN_DECLS
 
-/**************
- * Prototypes *
- **************/
-gboolean Flac_Tag_Read_File_Tag  (const gchar *filename, File_Tag *FileTag);
-gboolean Flac_Tag_Write_File_Tag (ET_File *ETFile);
+gboolean flac_tag_read_file_tag (const gchar *filename, File_Tag *FileTag, GError **error);
+gboolean flac_tag_write_file_tag (ET_File *ETFile, GError **error);
 
+G_END_DECLS
 
-#endif /* __FLAC_TAG_H__ */
+#endif /* ET_FLAC_TAG_H_ */


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