[PATCH 2/3] Refactor loading xml UI files

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

 



For loading the xml UI files 'gitg-ui.xml' and 'gitg-menus.xml' the
same function call and error handling were performed twice.

This patch introduces a helper function to remove this code
duplication.

Signed-off-by: SZEDER Gábor <szeder@xxxxxxxxxx>
---

  Not sure about putting the new function into gitg-utils.c;  maybe it
  would be better in gitg.c...

 gitg/gitg-utils.c  |   16 ++++++++++++++++
 gitg/gitg-utils.h  |    2 ++
 gitg/gitg-window.c |   10 ++--------
 gitg/gitg.c        |    9 +--------
 4 files changed, 21 insertions(+), 16 deletions(-)

diff --git a/gitg/gitg-utils.c b/gitg/gitg-utils.c
index 5fb6772..cfce323 100644
--- a/gitg/gitg-utils.c
+++ b/gitg/gitg-utils.c
@@ -1,3 +1,4 @@
+#include <stdlib.h>
 #include <string.h>
 #include <glib.h>
 
@@ -417,3 +418,18 @@ gitg_utils_menu_position_under_tree_view (GtkMenu  *menu,
 							tree);
 	}
 }
+
+void
+gitg_builder_add_from_file(GtkBuilder *builder, const gchar *filename)
+{
+	GError *error = NULL;
+	gchar full_filename[PATH_MAX];
+
+	g_snprintf(full_filename, PATH_MAX, "%s%c%s", GITG_UI_DIR, G_DIR_SEPARATOR, filename);
+	if (!gtk_builder_add_from_file(builder, full_filename, &error))
+	{
+		g_critical("Could not open UI file: %s (%s)", full_filename, error->message);
+		g_error_free(error);
+		exit(1);
+	}
+}
diff --git a/gitg/gitg-utils.h b/gitg/gitg-utils.h
index 867fff7..46f8010 100644
--- a/gitg/gitg-utils.h
+++ b/gitg/gitg-utils.h
@@ -36,4 +36,6 @@ gint gitg_utils_sort_names(gchar const *s1, gchar const *s2);
 void gitg_utils_menu_position_under_widget(GtkMenu *menu, gint *x, gint *y,	gboolean *push_in, gpointer user_data);
 void gitg_utils_menu_position_under_tree_view(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer user_data);
 					   
+void gitg_builder_add_from_file(GtkBuilder *builder, const gchar *filename);
+
 #endif /* __GITG_UTILS_H__ */
diff --git a/gitg/gitg-window.c b/gitg/gitg-window.c
index 8abdb9a..6f583d9 100644
--- a/gitg/gitg-window.c
+++ b/gitg/gitg-window.c
@@ -171,14 +171,8 @@ build_search_entry(GitgWindow *window, GtkBuilder *builder)
 	
 	GtkBuilder *b = gtk_builder_new();
 	gtk_builder_set_translation_domain(b, GETTEXT_PACKAGE);
-	GError *error = NULL;
 
-	if (!gtk_builder_add_from_file(b, GITG_UI_DIR "/gitg-menus.xml", &error))
-	{
-		g_critical("Could not open UI file: %s (%s)", GITG_UI_DIR "/gitg-menus.xml", error->message);
-		g_error_free(error);
-		exit(1);
-	}
+	gitg_builder_add_from_file(b, "gitg-menus.xml");
 	
 	GtkUIManager *manager = GTK_UI_MANAGER(gtk_builder_get_object(b, "uiman"));
 	window->priv->search_popup = GTK_WIDGET(g_object_ref(gtk_ui_manager_get_widget(manager, "/ui/search_popup")));
diff --git a/gitg/gitg.c b/gitg/gitg.c
index 69c5b56..20b9b53 100644
--- a/gitg/gitg.c
+++ b/gitg/gitg.c
@@ -52,17 +52,10 @@ on_window_delete_event(GtkWidget *widget, GdkEvent *event, gpointer userdata)
 static GitgWindow *
 build_ui()
 {
-	GError *error = NULL;
-	
 	GtkBuilder *builder = gtk_builder_new();
 	gtk_builder_set_translation_domain(builder, GETTEXT_PACKAGE);
 	
-	if (!gtk_builder_add_from_file(builder, GITG_UI_DIR "/gitg-ui.xml", &error))
-	{
-		g_critical("Could not open UI file: %s (%s)", GITG_UI_DIR "/gitg-ui.xml", error->message);
-		g_error_free(error);
-		exit(1);
-	}
+	gitg_builder_add_from_file(builder, "gitg-ui.xml");
 	
 	GtkWidget *window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
 	gtk_widget_show_all(window);
-- 
1.6.1.2.362.g0f78

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux