[gnac/devel] Code refactoring



commit 82dad8fe3ec681b7b92dfa458c78c1e4ef43c672
Author: BenoÃt Dupasquier <bdupasqu src gnome org>
Date:   Sat Oct 22 19:29:55 2011 +0100

    Code refactoring

 libgnac/libgnac-converter.c |   84 ++----------------------------------------
 libgnac/libgnac-converter.h |   18 +++++++++
 libgnac/libgnac-error.c     |   79 ++++++++++++++++++++++++++++++++++++++++
 libgnac/libgnac-error.h     |    5 +++
 libgnac/libgnac-output.c    |   18 +++++++++
 libgnac/libgnac-output.h    |    4 ++
 6 files changed, 128 insertions(+), 80 deletions(-)
---
diff --git a/libgnac/libgnac-converter.c b/libgnac/libgnac-converter.c
index 80d014e..e217122 100644
--- a/libgnac/libgnac-converter.c
+++ b/libgnac/libgnac-converter.c
@@ -37,23 +37,6 @@
 #include "libgnac-profile.h"
 
 
-enum 
-{
-  OVERWRITE,
-  PROGRESS,
-  FILE_ADDED,
-  FILE_REMOVED,
-  FILE_STARTED,
-  FILE_COMPLETED,
-  FILES_CLEARED,
-  STARTED,
-  STOPPED,
-  COMPLETION,
-  PLUGIN_INSTALL,
-  ERROR,
-  LAST_SIGNAL
-};
-
 enum
 {
   PROP_0, 
@@ -69,7 +52,7 @@ enum
   PROP_NB_FILES
 };
 
-static guint signals[LAST_SIGNAL] = { 0 };
+guint signals[LAST_SIGNAL] = { 0 };
 
 struct LibgnacConverterPrivate
 {
@@ -945,7 +928,6 @@ libgnac_converter_error_sink(LibgnacMediaItem *item,
   }
 
   if (err) {
-  /* If folder existed but something went wrong */
     if (!g_error_matches(err, G_IO_ERROR, G_IO_ERROR_EXISTS)) {
       libgnac_debug("Unable to create directory");
       g_signal_emit(item->parent, signals[ERROR], 0, uri, 
@@ -956,71 +938,13 @@ libgnac_converter_error_sink(LibgnacMediaItem *item,
     g_clear_error(&err);
   }
 
-  /* The destination file already exists */
-  gchar *dst_path;
-  dst_path = g_file_get_path(item->destination);
-  if (g_file_test(dst_path, G_FILE_TEST_EXISTS))
-  {
-    gchar *src_path;
-    src_path = g_file_get_path(item->source);
-    if (g_str_equal(dst_path, src_path)) {
-      const gchar *msg = _("Source and destination files are identical");
-      libgnac_debug(msg);
-      g_signal_emit(item->parent, signals[ERROR], 0, uri, msg, NULL);
-      g_free(dst_path);
-      g_free(src_path);
-      return FALSE;
-    }
-    g_free(src_path);
-
-    libgnac_debug("Destination file already exists");
-
-    gboolean overwrite = FALSE;
-
-    g_signal_emit(item->parent, signals[OVERWRITE], 0, 
-                    item->destination, &overwrite);
-
-    if (overwrite)
-    {
-      GError *err = NULL;
-
-      libgnac_debug("Overwrite file");
-      g_file_delete(item->destination, NULL, &err);
-      if (err) {
-        libgnac_warning("Unable to overwrite file %s: %s", uri, err->message);
-        g_clear_error(&err);
-      }
-      else
-      {
-        return TRUE;
-      }
-    }
-  /* Just send error signal */
-    else 
-    {
-      gchar  *msg;
-      GError *err = NULL;
-
-      libgnac_debug("Do not owerwrite file");
-      msg = g_strdup_printf(_("Destination file %s already exists"), uri);
-      g_set_error_literal(&err, LIBGNAC_ERROR, 
-          LIBGNAC_ERROR_FILE_EXISTS, msg);
-      g_signal_emit(item->parent, signals[ERROR], 0, uri, msg, err);
-      g_clear_error(&err);
-      g_free(msg);
-    }
-  } 
-  /* Access error */
-  else
-  {
-    libgnac_debug("Other error happend");
+  if (!libgnac_error_handle_dest_file_already_exists(item, message, uri)) {
     g_signal_emit(item->parent, signals[ERROR], 0, uri, 
                       _("Unable to access destination file"), error);
+    return FALSE;
   }
 
-  g_free(dst_path);
-
-  return FALSE;
+  return TRUE;
 }
 
 static gboolean
diff --git a/libgnac/libgnac-converter.h b/libgnac/libgnac-converter.h
index 9b1f4e7..c19c605 100644
--- a/libgnac/libgnac-converter.h
+++ b/libgnac/libgnac-converter.h
@@ -39,6 +39,24 @@
 
 #define PROGRESS_TIMEOUT 250
 
+enum {
+  OVERWRITE,
+  PROGRESS,
+  FILE_ADDED,
+  FILE_REMOVED,
+  FILE_STARTED,
+  FILE_COMPLETED,
+  FILES_CLEARED,
+  STARTED,
+  STOPPED,
+  COMPLETION,
+  PLUGIN_INSTALL,
+  ERROR,
+  LAST_SIGNAL
+};
+
+extern guint signals[LAST_SIGNAL];
+
 G_BEGIN_DECLS
 
 #define LIBGNAC_TYPE_CONVERTER (libgnac_converter_get_type())
diff --git a/libgnac/libgnac-error.c b/libgnac/libgnac-error.c
index ddac65e..f8f0605 100644
--- a/libgnac/libgnac-error.c
+++ b/libgnac/libgnac-error.c
@@ -23,6 +23,12 @@
  * Boston, MA  02110-1301  USA
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <glib/gi18n.h>
+
 #include "libgnac-converter.h"
 #include "libgnac-debug.h"
 #include "libgnac-error.h"
@@ -58,3 +64,76 @@ libgnac_error_handle_missing_plugin(LibgnacMediaItem *item,
     }
   }
 }
+
+
+static gboolean
+libgnac_error_handle_overwrite(LibgnacMediaItem *item,
+                               const gchar      *uri)
+{
+  libgnac_debug("Destination file already exists");
+
+  gboolean overwrite = FALSE;
+
+  g_signal_emit(item->parent, signals[OVERWRITE], 0,
+      item->destination, &overwrite);
+  if (overwrite) {
+    overwrite = libgnac_output_overwrite_dest_file(item, uri);
+  }
+
+  return overwrite;
+}
+
+
+static void
+libgnac_error_emit_dest_file_exists_error(LibgnacMediaItem *item,
+                                          const gchar      *uri)
+{
+  gchar  *msg;
+  GError *err = NULL;
+
+  libgnac_debug("Do not owerwrite file");
+
+  msg = g_strdup_printf(_("Destination file %s already exists"), uri);
+  g_set_error_literal(&err, LIBGNAC_ERROR,
+      LIBGNAC_ERROR_FILE_EXISTS, msg);
+  g_signal_emit(item->parent, signals[ERROR], 0, uri, msg, err);
+  g_clear_error(&err);
+  g_free(msg);
+}
+
+
+gboolean
+libgnac_error_handle_dest_file_already_exists(LibgnacMediaItem *item,
+                                              GstMessage       *message,
+                                              gchar            *uri)
+{
+  gchar *dst_path;
+
+  dst_path = g_file_get_path(item->destination);
+  if (!g_file_test(dst_path, G_FILE_TEST_EXISTS)) {
+    g_free(dst_path);
+    return FALSE;
+  }
+
+  gchar *src_path;
+  src_path = g_file_get_path(item->source);
+  if (g_str_equal(dst_path, src_path)) {
+    const gchar *msg = _("Source and destination files are identical");
+    libgnac_debug(msg);
+    g_signal_emit(item->parent, signals[ERROR], 0, uri, msg, NULL);
+    g_free(dst_path);
+    g_free(src_path);
+    return FALSE;
+  }
+  g_free(src_path);
+
+  if (!libgnac_error_handle_overwrite(item, uri)) {
+    libgnac_error_emit_dest_file_exists_error(item, uri);
+    g_free(dst_path);
+    return FALSE;
+  }
+
+  g_free(dst_path);
+
+  return TRUE;
+}
diff --git a/libgnac/libgnac-error.h b/libgnac/libgnac-error.h
index ce0c1c9..cc8929a 100644
--- a/libgnac/libgnac-error.h
+++ b/libgnac/libgnac-error.h
@@ -51,6 +51,11 @@ void
 libgnac_error_handle_missing_plugin(LibgnacMediaItem *item,
                                     const GError     *error);
 
+gboolean
+libgnac_error_handle_dest_file_already_exists(LibgnacMediaItem *item,
+                                              GstMessage       *message,
+                                              gchar            *uri);
+
 G_END_DECLS
 
 #endif /* __LIBGNAC_ERROR_H__ */
diff --git a/libgnac/libgnac-output.c b/libgnac/libgnac-output.c
index 8a96d34..a504043 100644
--- a/libgnac/libgnac-output.c
+++ b/libgnac/libgnac-output.c
@@ -470,3 +470,21 @@ libgnac_output_get_preview_from_pattern(const gchar *pattern,
   
   return sanitized;
 }
+
+
+gboolean
+libgnac_output_overwrite_dest_file(LibgnacMediaItem *item,
+                                   const gchar      *uri)
+{
+  GError *err = NULL;
+
+  libgnac_debug("Overwrite file");
+  g_file_delete(item->destination, NULL, &err);
+  if (err) {
+    libgnac_warning("Unable to overwrite file %s: %s", uri, err->message);
+    g_clear_error(&err);
+    return FALSE;
+  }
+
+  return TRUE;
+}
diff --git a/libgnac/libgnac-output.h b/libgnac/libgnac-output.h
index 950d959..3010fd2 100644
--- a/libgnac/libgnac-output.h
+++ b/libgnac/libgnac-output.h
@@ -102,6 +102,10 @@ gchar *
 libgnac_output_get_preview_from_pattern(const gchar *pattern,
                                         gboolean     strip_special);
 
+gboolean
+libgnac_output_overwrite_dest_file(LibgnacMediaItem *item,
+                                   const gchar      *uri);
+
 G_END_DECLS
 
 #endif /* __LIBGNAC_OUTPUT_H__ */



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