[virt-tools-list] [virt-viewer v2 2/3] util: slightly reorganize _load_ui()

Fabiano Fidêncio fidencio at redhat.com
Thu Feb 25 13:03:39 UTC 2016


_load_ui() was hard to expand, in a readable way, when adding one more
directory to (try to) load the ui file from. So, it got split in a way
that adding new directories to (try to) load ui files from is more
readable.

Signed-off-by: Fabiano Fidêncio <fidencio at redhat.com>
---
 src/virt-viewer-util.c | 67 +++++++++++++++++++++++++++++++++-----------------
 1 file changed, 44 insertions(+), 23 deletions(-)

diff --git a/src/virt-viewer-util.c b/src/virt-viewer-util.c
index 8e60659..d6dbbe4 100644
--- a/src/virt-viewer-util.c
+++ b/src/virt-viewer-util.c
@@ -47,6 +47,48 @@ virt_viewer_error_quark(void)
   return g_quark_from_static_string ("virt-viewer-error-quark");
 }
 
+static gboolean
+virt_viewer_util_try_to_load_ui_from_system_data_dirs(GtkBuilder *builder,
+                                                      const gchar *name)
+{
+    const gchar * const * dirs = g_get_system_data_dirs();
+    g_return_val_if_fail(dirs != NULL, FALSE);
+    gboolean ret = FALSE;
+
+    while (!ret && dirs[0] != NULL) {
+        gchar *path = g_build_filename(dirs[0], PACKAGE, "ui", name, NULL);
+        ret = (gtk_builder_add_from_file(builder, path, NULL) != 0);
+        g_free(path);
+
+        dirs++;
+    }
+
+    return ret;
+}
+
+static gboolean
+virt_viewer_util_try_to_load_ui_from_non_system_data_dirs(GtkBuilder *builder,
+                                                          const gchar *name,
+                                                          GError **error)
+{
+    gchar *path = g_build_filename(PACKAGE_DATADIR, "ui", name, NULL);
+    GError *local_error = NULL;
+    gboolean ret = (gtk_builder_add_from_file(builder, path, &local_error) != 0);
+
+    if (g_error_matches(local_error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) {
+        g_propagate_error(error, local_error);
+        local_error = NULL;
+    }
+
+    if (local_error != NULL) {
+        g_warning("Failed to add ui file '%s': %s", path, local_error->message);
+        g_clear_error(&local_error);
+    }
+    g_free(path);
+
+    return ret;
+}
+
 GtkBuilder *virt_viewer_util_load_ui(const char *name)
 {
     struct stat sb;
@@ -57,30 +99,9 @@ GtkBuilder *virt_viewer_util_load_ui(const char *name)
     if (stat(name, &sb) >= 0) {
         gtk_builder_add_from_file(builder, name, &error);
     } else {
-        gchar *path = g_build_filename(PACKAGE_DATADIR, "ui", name, NULL);
-        gboolean success = (gtk_builder_add_from_file(builder, path, &error) != 0);
-        if (!g_error_matches(error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) {
-            g_warning("Failed to add ui file '%s': %s", path, error->message);
-            g_clear_error(&error);
-        }
-        g_free(path);
-
-        if (!success) {
-            const gchar * const * dirs = g_get_system_data_dirs();
-            g_return_val_if_fail(dirs != NULL, NULL);
-
-            while (dirs[0] != NULL) {
-                path = g_build_filename(dirs[0], PACKAGE, "ui", name, NULL);
-                if (gtk_builder_add_from_file(builder, path, NULL) != 0) {
-                    g_free(path);
-                    break;
-                }
-                g_free(path);
-                dirs++;
-            }
-            if (dirs[0] == NULL)
+        if (!virt_viewer_util_try_to_load_ui_from_non_system_data_dirs(builder, name, &error))
+            if (!virt_viewer_util_try_to_load_ui_from_system_data_dirs(builder, name))
                 goto failed;
-        }
     }
 
     if (error) {
-- 
2.5.0




More information about the virt-tools-list mailing list