[PATCH 2/3] Refactor loading xml UI files

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: SZEDER Gábor
Date: Tuesday, February 3, 2009 - 6:08 am

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@ira.uka.de>
---

  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@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
git gtk+/GNOME gui application: gitg, Jesse van den Kieboom, (Sat Jan 31, 1:05 pm)
Re: [PATCH 1/2] fsck: HEAD is part of refs, Junio C Hamano, (Sat Jan 31, 5:53 pm)
Re: git gtk+/GNOME gui application: gitg, SZEDER , (Tue Feb 3, 6:08 am)
[PATCH 2/3] Refactor loading xml UI files, SZEDER Gábor, (Tue Feb 3, 6:08 am)
Re: git gtk+/GNOME gui application: gitg, Michael J Gruber, (Tue Feb 3, 9:58 am)
Re: git gtk+/GNOME gui application: gitg, Jakub Narebski, (Tue Feb 3, 10:00 am)
Re: git gtk+/GNOME gui application: gitg, Stefan Karpinski, (Tue Feb 3, 12:43 pm)
Re: git gtk+/GNOME gui application: gitg, Miles Bader, (Tue Feb 3, 8:38 pm)
Re: git gtk+/GNOME gui application: gitg, Felipe Contreras, (Wed Feb 4, 12:39 am)
Re: git gtk+/GNOME gui application: gitg, Jesse van den Kieboom, (Wed Feb 4, 4:48 am)
Re: git gtk+/GNOME gui application: gitg, Jesse van den Kieboom, (Wed Feb 4, 4:50 am)
Re: git gtk+/GNOME gui application: gitg, Michael J Gruber, (Wed Feb 4, 5:46 am)
Re: git gtk+/GNOME gui application: gitg, Felipe Contreras, (Wed Feb 4, 7:12 am)
Re: git gtk+/GNOME gui application: gitg, Jesse van den Kieboom, (Wed Feb 4, 7:18 am)
Re: git gtk+/GNOME gui application: gitg, Michael J Gruber, (Wed Feb 4, 7:29 am)
Re: git gtk+/GNOME gui application: gitg, Jesse van den Kieboom, (Wed Feb 4, 7:33 am)
Re: git gtk+/GNOME gui application: gitg, Michael J Gruber, (Wed Feb 4, 7:40 am)
Re: git gtk+/GNOME gui application: gitg, Jesse van den Kieboom, (Wed Feb 4, 7:46 am)
Re: git gtk+/GNOME gui application: gitg, Felipe Contreras, (Wed Feb 4, 8:00 am)
Re: git gtk+/GNOME gui application: gitg, Miles Bader, (Wed Feb 4, 10:46 pm)
Re: git gtk+/GNOME gui application: gitg, Jesse van den Kieboom, (Thu Feb 5, 1:06 pm)
Re: git gtk+/GNOME gui application: gitg, Miles Bader, (Fri Feb 6, 2:49 am)
Re: [PATCH 2/3] Refactor loading xml UI files, Jesse van den Kieboom, (Sat Feb 14, 2:37 pm)