[gnac/devel] Code refactoring



commit d1aec0da7c64780c7a9ee4c5bcf26b99eb5444ea
Author: BenoÃt Dupasquier <bdupasqu src gnome org>
Date:   Wed Nov 16 15:41:24 2011 +0000

    Code refactoring

 src/gnac-playlist.c                     |    3 +-
 src/profiles/gnac-profiles-default.c    |    4 +-
 src/profiles/gnac-profiles-manager.c    |  221 +++++++++++++++++--------------
 src/profiles/gnac-profiles-properties.c |   12 +-
 src/profiles/gnac-profiles-properties.h |    3 +-
 src/profiles/gnac-profiles.c            |    2 +-
 6 files changed, 134 insertions(+), 111 deletions(-)
---
diff --git a/src/gnac-playlist.c b/src/gnac-playlist.c
index b33d1e8..31d389a 100755
--- a/src/gnac-playlist.c
+++ b/src/gnac-playlist.c
@@ -71,7 +71,8 @@ gnac_playlist_resolve_uri_and_add(GFile       *parent,
 
 
 static gchar *
-gnac_playlist_read_file(GFile *file) {
+gnac_playlist_read_file(GFile *file)
+{
   gsize  size;
   gchar *contents;
 
diff --git a/src/profiles/gnac-profiles-default.c b/src/profiles/gnac-profiles-default.c
index 5539981..4ea6027 100755
--- a/src/profiles/gnac-profiles-default.c
+++ b/src/profiles/gnac-profiles-default.c
@@ -100,8 +100,8 @@ gnac_profiles_default_init(BasicFormatInfo *bfi)
   bfi->pipeline_multiplexers = g_strconcat("", NULL);
   GList *multiplexers_list = gnac_profiles_xml_engine_get_list_values(bfi->doc,
       "//process[ id='multiplexer']/value");
-  if (multiplexers_list != NULL) {
-    bfi->pipeline_multiplexers= gnac_profiles_utils_add_pipes(
+  if (multiplexers_list) {
+    bfi->pipeline_multiplexers = gnac_profiles_utils_add_pipes(
         bfi->pipeline_multiplexers, multiplexers_list);
     g_list_free(multiplexers_list);
   }
diff --git a/src/profiles/gnac-profiles-manager.c b/src/profiles/gnac-profiles-manager.c
index 998c538..a013792 100644
--- a/src/profiles/gnac-profiles-manager.c
+++ b/src/profiles/gnac-profiles-manager.c
@@ -870,16 +870,117 @@ gnac_profiles_mgr_on_drag_data_received(GtkWidget        *widget,
 }
 
 
+static gchar *
+gnac_profiles_mgr_get_file_name(GFile *file,
+                                guint  target_type)
+{
+  gchar     *name;
+  GError    *error = NULL;
+  GFileInfo *file_info;
+
+  file_info = g_file_query_info(file,
+      G_FILE_ATTRIBUTE_STANDARD_NAME ","
+      G_FILE_ATTRIBUTE_STANDARD_TYPE,
+      G_FILE_QUERY_INFO_NONE, NULL, &error);
+  if (error) {
+    libgnac_warning("%s", error->message);
+    g_clear_error(&error);
+    return NULL;
+  }
+
+  switch (target_type)
+  {
+    case TARGET_URI:
+      if (g_file_info_get_file_type(file_info) == G_FILE_TYPE_REGULAR) {
+        name = g_strdup(g_file_info_get_name(file_info));
+      }
+      break;
+
+    case TARGET_PLAIN:
+      if (g_file_info_get_file_type(file_info) == G_FILE_TYPE_UNKNOWN) {
+        name = g_file_get_basename(file);
+      }
+      break;
+  }
+
+  g_object_unref(file_info);
+
+  return name;
+}
+
+
+static void
+gnac_profiles_mgr_display_import_status(guint profiles_ok,
+                                        guint profiles_error)
+{
+  gchar *oks = NULL;
+  gchar *errors = NULL;
+
+  if (profiles_error > 0) {
+    errors = g_strdup_printf(ngettext("%d file failed to be imported",
+        "%d files failed to be imported", profiles_error), profiles_error);
+  }
+
+  if (profiles_ok > 0) {
+    oks = g_strdup_printf(ngettext("%d file successfully imported",
+        "%d files successfully imported", profiles_ok), profiles_ok);
+  }
+
+  gdk_threads_enter();
+  gnac_profiles_mgr_set_window_sensitive(TRUE);
+  gnac_profiles_mgr_show_import_progressbar(FALSE);
+  gnac_profiles_mgr_display_status_message(oks, errors);
+  gdk_threads_leave();
+
+  g_free(oks);
+  g_free(errors);
+}
+
+
+static gboolean
+gnac_profiles_mgr_copy_file(GFile       *src_file,
+                            const gchar *dest_name)
+{
+  gboolean  success;
+  gchar    *path;
+  GError   *error = NULL;
+  GFile    *dest_file;
+  CopyData  copy_data;
+
+  path = g_build_filename(g_get_tmp_dir(), dest_name, NULL);
+  dest_file = g_file_new_for_path(path);
+  copy_data.path = path;
+  copy_data.name = dest_name;
+  copy_data.error = &error;
+  copy_data.toggle = FALSE;
+
+  gdk_threads_enter();
+  gnac_profiles_mgr_set_progress_bar_fraction(0.0);
+  gdk_threads_leave();
+
+  success = g_file_copy(src_file, dest_file, G_FILE_COPY_NONE, NULL,
+      (GFileProgressCallback) gnac_profiles_mgr_on_drag_profile_copied,
+      &copy_data, &error);
+  if (error) {
+    libgnac_warning("%s", error->message);
+    g_clear_error(&error);
+  }
+
+  g_free(path);
+  g_object_unref(dest_file);
+
+  return success;
+}
+
+
 static void
 gnac_profiles_mgr_copy_and_load_files(gpointer data)
 {
   gchar  **uris;
   gchar   *uri;
-  GError  *error = NULL;
   guint    profiles_ok = 0;
   guint    profiles_error = 0;
   gint     index = 0;
-  const gchar    *name;
   ThreadCopyData *tcopy_data;
 
   tcopy_data = (ThreadCopyData*) data;
@@ -893,115 +994,37 @@ gnac_profiles_mgr_copy_and_load_files(gpointer data)
   gdk_threads_leave();
   
   while (uri) {
-    GFile     *file;
-    GFileInfo *file_info;
+    gchar *name;
+    GFile *file;
 
     file = g_file_new_for_uri(uri);
-    file_info = g_file_query_info(file,
-        G_FILE_ATTRIBUTE_STANDARD_NAME ","
-        G_FILE_ATTRIBUTE_STANDARD_TYPE,
-        G_FILE_QUERY_INFO_NONE, NULL, &error);
-    if (error) {
-      libgnac_warning("%s", error->message);
-      ++profiles_error;
-      g_clear_error(&error);
-    } else {
-      gboolean ok = FALSE;
-
-      switch (tcopy_data->info)
-      {
-        case TARGET_URI:
-          if (g_file_info_get_file_type(file_info) == G_FILE_TYPE_REGULAR) {
-            name = g_file_info_get_name(file_info);
-            if (name) ok = TRUE;
-          }
-          break;
-
-        case TARGET_PLAIN:
-          if (g_file_info_get_file_type(file_info) == G_FILE_TYPE_UNKNOWN) {
-            name = g_file_get_basename(file);
-            if (name) ok = TRUE;
-          }
-          break;
-
-        default:
-          return;
-      }
-
-      if (ok) {
-        if (gnac_profiles_properties_saved_profiles_contain_name(name, FALSE))
-        {
-          libgnac_warning(
-             _("Impossible to load file \"%s\": "
-               "a profile with the same name already exists."), uri);
+    name = gnac_profiles_mgr_get_file_name(file, tcopy_data->info);
+    if (!name) {
+      libgnac_warning(
+          _("Impossible to import file \"%s\". File type not supported."),
+          uri);
+      profiles_error++;
+    } else if (gnac_profiles_properties_saved_profiles_contain_name(name)) {
+      libgnac_warning(
+          _("Impossible to load file \"%s\": "
+            "a profile with the same name already exists."), uri);
           ++profiles_error;
-        } else {
-          gchar    *path;
-          GFile    *dest;
-          CopyData  copy_data;
-
-          path = g_build_filename(g_get_tmp_dir(), name, NULL);
-          dest = g_file_new_for_path(path);
-          copy_data.path = path;
-          copy_data.name = name;
-          copy_data.error = &error;
-          copy_data.toggle = FALSE;
-
-          gdk_threads_enter();
-          gnac_profiles_mgr_set_progress_bar_fraction(0.0);
-          gdk_threads_leave(); 
-          
-          g_file_copy(file, dest, G_FILE_COPY_NONE, NULL, 
-              (GFileProgressCallback) gnac_profiles_mgr_on_drag_profile_copied,
-              &copy_data, &error);
-          
-          if (error) {
-            libgnac_warning("%s", error->message);
-            g_clear_error(&error);
-            ++profiles_error;
-          } else {
-            ++profiles_ok;
-          }
-
-          g_free(path);
-          g_object_unref(dest);
-        }
-      } else {
-        libgnac_warning(
-            _("Impossible to import file \"%s\". File type not supported."),
-            uri);
-        ++profiles_error;
-      }
+    } else if (gnac_profiles_mgr_copy_file(file, name)) {
+      profiles_ok++;
+    } else {
+      profiles_error++;
     }
 
+    g_free(name);
+
     ++index;
     uri = uris[index];
 
     g_object_unref(file);
-    g_object_unref(file_info);
   }
 
-  gchar *oks = NULL;
-  gchar *errors = NULL;
-  
-  if (profiles_error > 0) {
-    errors = g_strdup_printf(ngettext("%d file failed to be imported", 
-        "%d files failed to be imported", profiles_error), profiles_error);
-  }
+  gnac_profiles_mgr_display_import_status(profiles_ok, profiles_error);
 
-  if (profiles_ok > 0) {
-    oks = g_strdup_printf(ngettext("%d file successfully imported",
-        "%d files successfully imported", profiles_ok), profiles_ok);
-  }
-
-  gdk_threads_enter();
-  gnac_profiles_mgr_set_window_sensitive(TRUE);
-  gnac_profiles_mgr_show_import_progressbar(FALSE);
-  gnac_profiles_mgr_display_status_message(oks,errors);
-  gdk_threads_leave();
-
-  g_free(oks);
-  g_free(errors);
   g_strfreev(tcopy_data->uris);
   g_free(tcopy_data);
 }
diff --git a/src/profiles/gnac-profiles-properties.c b/src/profiles/gnac-profiles-properties.c
index 2a7d1aa..c54070f 100644
--- a/src/profiles/gnac-profiles-properties.c
+++ b/src/profiles/gnac-profiles-properties.c
@@ -178,7 +178,8 @@ gnac_profiles_properties_set_parent(GtkWindow *parent)
 
 
 static void
-gnac_profiles_properties_init_format(void) {
+gnac_profiles_properties_init_format(void)
+{
   GtkWidget    *widget;
   GtkWidget    *format_combo_box;
   GtkWidget    *hbox_properties;
@@ -499,7 +500,7 @@ gnac_profiles_properties_on_save(GtkWidget *widget,
   GtkWidget           *window;
   GError              *error = NULL;
 
-  StandardCallBack     call_back = (StandardCallBack) data;
+  StandardCallBack call_back = (StandardCallBack) data;
 
   window = gnac_profiles_properties_get_widget("window1");
   
@@ -560,7 +561,7 @@ gnac_profiles_properties_is_valid_profile_name(const gchar *name)
   }
 
   if (!gnac_utils_str_equal(current_profile_name, name)
-      && gnac_profiles_properties_saved_profiles_contain_name(name, TRUE))
+      && gnac_profiles_properties_saved_profiles_contain_name(name))
   {
       gnac_profiles_properties_display_status_message(
           _("This name is already used by another profile."));
@@ -695,14 +696,13 @@ gnac_profiles_properties_load_profile_from_file(const gchar  *path,
 
 
 gboolean
-gnac_profiles_properties_saved_profiles_contain_name(const gchar *name,
-                                                     gboolean     add_ext)
+gnac_profiles_properties_saved_profiles_contain_name(const gchar *name)
 {
   gboolean  exists;
   gchar    *path;
   GFile    *file;
   
-  if (!add_ext) {
+  if (g_str_has_suffix(name, ".xml")) {
     path = g_strconcat(GNAC_SAVED_PROFILES_URL(name), NULL);
   } else {
     path = g_strconcat(GNAC_SAVED_PROFILES_URL_WITH_EXT(name), NULL);
diff --git a/src/profiles/gnac-profiles-properties.h b/src/profiles/gnac-profiles-properties.h
index 915d5e5..e4e2b59 100644
--- a/src/profiles/gnac-profiles-properties.h
+++ b/src/profiles/gnac-profiles-properties.h
@@ -56,8 +56,7 @@ void
 gnac_profiles_properties_destroy(void);
 
 gboolean
-gnac_profiles_properties_saved_profiles_contain_name(const gchar *name,
-                                                     gboolean     add_ext);
+gnac_profiles_properties_saved_profiles_contain_name(const gchar *name);
 
 void
 gnac_profiles_properties_save_profile(gpointer profile);
diff --git a/src/profiles/gnac-profiles.c b/src/profiles/gnac-profiles.c
index 4b7bbd3..874a861 100755
--- a/src/profiles/gnac-profiles.c
+++ b/src/profiles/gnac-profiles.c
@@ -81,7 +81,7 @@ gnac_profiles_destroy(void)
 GtkWidget *
 gnac_profiles_get_widget(GtkWidget *parent)
 {
-  if (combo_profile == NULL) {
+  if (!combo_profile) {
     gnac_profiles_init();
   }
 



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