[Goodies-commits] r5428 - in xfbib/branches/gobject: . src

David Gustafsson tssj at xfce.org
Sun Sep 21 20:21:47 CEST 2008


Author: tssj
Date: 2008-09-21 18:21:47 +0000 (Sun, 21 Sep 2008)
New Revision: 5428

Modified:
   xfbib/branches/gobject/configure.ac.in
   xfbib/branches/gobject/src/Makefile.am
   xfbib/branches/gobject/src/xfbib-elements.c
   xfbib/branches/gobject/src/xfbib-elements.h
   xfbib/branches/gobject/src/xfbib-entry-edit-dialog.c
   xfbib/branches/gobject/src/xfbib-entry-edit-dialog.h
   xfbib/branches/gobject/src/xfbib-entry.c
   xfbib/branches/gobject/src/xfbib-file-io.c
   xfbib/branches/gobject/src/xfbib-menu-bar.c
   xfbib/branches/gobject/src/xfbib-strbuf.c
   xfbib/branches/gobject/src/xfbib-strbuf.h
   xfbib/branches/gobject/src/xfbib-value.c
   xfbib/branches/gobject/src/xfbib-window.c
   xfbib/branches/gobject/src/xfbib-window.h
Log:
Added file opening functionality.


Modified: xfbib/branches/gobject/configure.ac.in
===================================================================
--- xfbib/branches/gobject/configure.ac.in	2008-09-20 20:50:34 UTC (rev 5427)
+++ xfbib/branches/gobject/configure.ac.in	2008-09-21 18:21:47 UTC (rev 5428)
@@ -69,6 +69,7 @@
 XDT_CHECK_PACKAGE([GOBJECT], [gobject-2.0], [2.12.0])
 dnl XDT_CHECK_PACKAGE([XFCONF], [libxfconf-0], [0])
 XDT_CHECK_PACKAGE([LIBXFCEGUI4], [libxfcegui4-1.0], [4.4.0])
+XDT_CHECK_PACKAGE([LIBXFCE4UTIL], [libxfce4util-1.0], [4.4.0])
 
 dnl check for debugging support
 XDT_FEATURE_DEBUG

Modified: xfbib/branches/gobject/src/Makefile.am
===================================================================
--- xfbib/branches/gobject/src/Makefile.am	2008-09-20 20:50:34 UTC (rev 5427)
+++ xfbib/branches/gobject/src/Makefile.am	2008-09-21 18:21:47 UTC (rev 5428)
@@ -48,6 +48,7 @@
 	$(GTK_CFLAGS) \
 	$(GLIB_CFLAGS)  \
 	$(LIBXFCEGUI4_CFLAGS) \
+	$(LIBXFCE4UTIL_CFLAGS) \
 	-DDATADIR=\"$(datadir)\" \
 	-DSRCDIR=\"$(top_srcdir)\" \
 	-DLOCALEDIR=\"$(localedir)\"
@@ -55,4 +56,5 @@
 xfbib_LDADD = \
 	$(GTK_LIBS) \
 	$(GLIB_LIBS) \
-	$(LIBXFCEGUI4_LIBS)
+	$(LIBXFCEGUI4_LIBS) \
+	$(LIBXFCE4UTIL_LIBS)

Modified: xfbib/branches/gobject/src/xfbib-elements.c
===================================================================
--- xfbib/branches/gobject/src/xfbib-elements.c	2008-09-20 20:50:34 UTC (rev 5427)
+++ xfbib/branches/gobject/src/xfbib-elements.c	2008-09-21 18:21:47 UTC (rev 5428)
@@ -185,3 +185,10 @@
 	g_return_val_if_fail(XFBIB_IS_ELEMENTS(elements), NULL);
 	return elements->list;
 }
+
+gint
+xfbib_elements_get_size(XfbibElements *elements)
+{
+	g_return_val_if_fail(XFBIB_IS_ELEMENTS(elements), 0);
+	return g_list_length(elements->list);
+}

Modified: xfbib/branches/gobject/src/xfbib-elements.h
===================================================================
--- xfbib/branches/gobject/src/xfbib-elements.h	2008-09-20 20:50:34 UTC (rev 5427)
+++ xfbib/branches/gobject/src/xfbib-elements.h	2008-09-21 18:21:47 UTC (rev 5428)
@@ -19,6 +19,7 @@
 XfbibElements *xfbib_elements_new();
 gboolean xfbib_elements_parse(XfbibElements *, const gchar *);
 GList *xfbib_elements_get(XfbibElements *);
+gint xfbib_elements_get_size(XfbibElements *);
 G_END_DECLS
 
 #endif //__XFBIB_ELEMENTS_H

Modified: xfbib/branches/gobject/src/xfbib-entry-edit-dialog.c
===================================================================
--- xfbib/branches/gobject/src/xfbib-entry-edit-dialog.c	2008-09-20 20:50:34 UTC (rev 5427)
+++ xfbib/branches/gobject/src/xfbib-entry-edit-dialog.c	2008-09-21 18:21:47 UTC (rev 5428)
@@ -255,14 +255,14 @@
 }
 
 GtkWidget *
-xfbib_entry_edit_dialog_new(gchar *title)
+xfbib_entry_edit_dialog_new(XfbibEntry *entry)
 {
 	XfbibEntryEditDialog *entry_edit_dialog;
 	entry_edit_dialog = g_object_new(XFBIB_TYPE_ENTRY_EDIT_DIALOG,
 			"destroy-with-parent", ((GTK_DIALOG_MODAL & GTK_DIALOG_DESTROY_WITH_PARENT) != 0),
 			"has-separator", ((GTK_DIALOG_MODAL & GTK_DIALOG_NO_SEPARATOR) == 0),
 			"modal", ((GTK_DIALOG_MODAL & GTK_DIALOG_MODAL) != 0),
-			"title", title,
+			"title", (entry == NULL) ? "Add new entry" : "Edit existing entry",
 			NULL);
 	
 	return GTK_WIDGET(entry_edit_dialog);

Modified: xfbib/branches/gobject/src/xfbib-entry-edit-dialog.h
===================================================================
--- xfbib/branches/gobject/src/xfbib-entry-edit-dialog.h	2008-09-20 20:50:34 UTC (rev 5427)
+++ xfbib/branches/gobject/src/xfbib-entry-edit-dialog.h	2008-09-21 18:21:47 UTC (rev 5428)
@@ -21,6 +21,7 @@
 #define __XFBIB_ENTRY_EDIT_DIALOG_H
 
 #include <gtk/gtk.h>
+#include "xfbib-entry.h"
 
 #define XFBIB_TYPE_ENTRY_EDIT_DIALOG             (xfbib_entry_edit_dialog_get_type())
 #define XFBIB_ENTRY_EDIT_DIALOG(obj)             (G_TYPE_CHECK_INSTANCE_CAST((obj), XFBIB_TYPE_ENTRY_EDIT_DIALOG, XfbibEntryEditDialog))
@@ -35,7 +36,7 @@
 
 GType xfbib_entry_edit_dialog_get_type() G_GNUC_CONST;
 
-GtkWidget *xfbib_entry_edit_dialog_new(gchar *);
+GtkWidget *xfbib_entry_edit_dialog_new(XfbibEntry *);
 
 G_END_DECLS
 

Modified: xfbib/branches/gobject/src/xfbib-entry.c
===================================================================
--- xfbib/branches/gobject/src/xfbib-entry.c	2008-09-20 20:50:34 UTC (rev 5427)
+++ xfbib/branches/gobject/src/xfbib-entry.c	2008-09-21 18:21:47 UTC (rev 5428)
@@ -22,6 +22,7 @@
 #endif
 #include <gtk/gtk.h>
 #include <string.h>
+#include <libxfce4util/libxfce4util.h>
 
 #include "xfbib-entry.h"
 #include "xfbib-field.h"
@@ -83,6 +84,8 @@
 	XfbibStrbuf *buf;
 	XfbibField *field;
 	gint i, brackets, quotes;
+	
+	DBG("str = %s", str);
 
 	/* Dont copy the last bracket */
 	tmp = g_strndup(str, strlen(str) - 1);
@@ -102,7 +105,6 @@
 	buf = xfbib_strbuf_new();
 
 	for (i++, brackets = 0, quotes = 0; i < strlen(tmp); i++) {
-
 		if (tmp[i] == ',' && brackets == 0 && quotes%2 == 0) {
 			field = xfbib_field_new();
 			if (!xfbib_field_parse(field, xfbib_strbuf_get_str(buf)))
@@ -119,11 +121,19 @@
 		}
 		xfbib_strbuf_append_char(buf, tmp[i]);
 	}
+	
+	/* When the last row has a comma their will be nothing here to parse */
+	for (i = 0; i < xfbib_strbuf_len(buf); i++) {
+		if(!g_ascii_isspace(xfbib_strbuf_get_nth_char(buf, i)))
+			break;
+	}
 
-	field = xfbib_field_new();
-	if (!xfbib_field_parse(field, xfbib_strbuf_get_str(buf)))
-		return FALSE;
-	entry->fields = g_list_append(entry->fields, field);
+	if (i < xfbib_strbuf_len(buf)) {
+		field = xfbib_field_new();
+		if (!xfbib_field_parse(field, xfbib_strbuf_get_str(buf)))
+			return FALSE;
+		entry->fields = g_list_append(entry->fields, field);
+	}
 
 	g_object_unref(G_OBJECT(buf));
 	g_free(tmp);

Modified: xfbib/branches/gobject/src/xfbib-file-io.c
===================================================================
--- xfbib/branches/gobject/src/xfbib-file-io.c	2008-09-20 20:50:34 UTC (rev 5427)
+++ xfbib/branches/gobject/src/xfbib-file-io.c	2008-09-21 18:21:47 UTC (rev 5428)
@@ -27,6 +27,7 @@
 #include "xfbib-strbuf.h"
 #include "xfbib-elements.h"
 #include "xfbib-window.h"
+#include "xfbib-list-store.h"
 
 struct _XfbibFileIo
 {
@@ -76,6 +77,14 @@
 gboolean
 xfbib_file_io_open(XfbibFileIo *file_io, XfbibWindow *window)
 {
+	XfbibElements *elements;
+	GtkTreeModel *list_store;
+	gchar *contents, *name;
+	gsize length;
+	gint i;
+	GError *err = NULL;
+	GtkTreeIter iter;
+	GtkTreePath *path;
 	GtkWidget *dialog = gtk_file_chooser_dialog_new(_("Open BibTeX file"),
                                                     GTK_WINDOW(window),
                                                     GTK_FILE_CHOOSER_ACTION_OPEN,
@@ -85,19 +94,34 @@
 
 	if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
 	{
-		/* TODO: Check if the file is accessible or not */
-		xfbib_strbuf_wipe(file_io->filename);
-		xfbib_strbuf_append(file_io->filename, gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)));
-		
-		const gchar *file;
-		file = fopen(xfbib_strbuf_get_str(file_io->filename), "r");
+		name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
 
-		XfbibElements *elements;
-		xfbib_elements_parse(elements, file);
+		if (!g_file_get_contents(name, &contents, &length, &err)) {
+			xfce_err("An error occured when opening the file: %s\n", err->message);
+			g_error_free(err);
+		} else {
+			/* Looking good, now parse the contents of the file */
+			xfbib_strbuf_wipe(file_io->filename);
+			xfbib_strbuf_append(file_io->filename, name);
 
-		/* Add all the entries to the tree view*/
+			elements = xfbib_window_get_elements(window);
 
-		fclose(file);
+			if (xfbib_elements_parse(elements, contents)) {
+				for (i = 0; i < xfbib_elements_get_size(elements); i++) {
+					path = gtk_tree_path_new();
+					gtk_tree_path_append_index(path, i);
+					
+					list_store = gtk_tree_view_get_model(GTK_TREE_VIEW(xfbib_window_get_tree_view(window)));
+
+					gtk_tree_model_get_iter(list_store, &iter, path);
+
+					gtk_tree_model_row_inserted(list_store, path, &iter);
+
+					gtk_tree_path_free(path);
+				}
+			}
+
+		}
 	}
 	gtk_widget_destroy(dialog);
 	

Modified: xfbib/branches/gobject/src/xfbib-menu-bar.c
===================================================================
--- xfbib/branches/gobject/src/xfbib-menu-bar.c	2008-09-20 20:50:34 UTC (rev 5427)
+++ xfbib/branches/gobject/src/xfbib-menu-bar.c	2008-09-21 18:21:47 UTC (rev 5428)
@@ -103,7 +103,7 @@
 {
 	GtkWidget *dialog;
 	g_printf("Entry/Add\n");
-	dialog = xfbib_entry_edit_dialog_new("Add entry");
+	dialog = xfbib_entry_edit_dialog_new(NULL);
 	
 	if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK) {
 		/* TODO: Save the entry */

Modified: xfbib/branches/gobject/src/xfbib-strbuf.c
===================================================================
--- xfbib/branches/gobject/src/xfbib-strbuf.c	2008-09-20 20:50:34 UTC (rev 5427)
+++ xfbib/branches/gobject/src/xfbib-strbuf.c	2008-09-21 18:21:47 UTC (rev 5428)
@@ -120,3 +120,10 @@
 	g_return_val_if_fail(XFBIB_IS_STRBUF(strbuf), 0);
 	return strbuf->used;
 }
+
+gchar
+xfbib_strbuf_get_nth_char(XfbibStrbuf *strbuf, gint i)
+{
+	g_return_val_if_fail(XFBIB_IS_STRBUF(strbuf) && i < strbuf->used, '\0');
+	return strbuf->buf[i];
+}

Modified: xfbib/branches/gobject/src/xfbib-strbuf.h
===================================================================
--- xfbib/branches/gobject/src/xfbib-strbuf.h	2008-09-20 20:50:34 UTC (rev 5427)
+++ xfbib/branches/gobject/src/xfbib-strbuf.h	2008-09-21 18:21:47 UTC (rev 5428)
@@ -22,8 +22,8 @@
 void xfbib_strbuf_append_char(XfbibStrbuf *, const gchar);
 void xfbib_strbuf_wipe(XfbibStrbuf *);
 gint xfbib_strbuf_len(XfbibStrbuf *);
+gchar xfbib_strbuf_get_nth_char(XfbibStrbuf *, gint);
 
-
 G_END_DECLS
 
 #endif //__XFBIB_STRBUF_H

Modified: xfbib/branches/gobject/src/xfbib-value.c
===================================================================
--- xfbib/branches/gobject/src/xfbib-value.c	2008-09-20 20:50:34 UTC (rev 5427)
+++ xfbib/branches/gobject/src/xfbib-value.c	2008-09-21 18:21:47 UTC (rev 5428)
@@ -155,5 +155,20 @@
 const gchar *
 xfbib_value_get_str(XfbibValue *value)
 {
-	return "Value";
+	GObject *obj;
+	if (g_list_length(value->list) == 1) {
+		obj = g_list_nth_data(value->list, 0);
+		if (XFBIB_IS_STRING(obj)) {
+			/* Variable */
+			return "TODO: Variable";	
+		} else if (XFBIB_IS_STRBUF(obj)) {
+			/* Regular string */
+			return xfbib_strbuf_get_str(XFBIB_STRBUF(obj));
+		} else if (XFBIB_IS_INTEGER(obj)) {
+			/* Integer value */
+			return "TODO: Integer";	
+		}
+	} else {
+		return "Compund string";
+	}
 }

Modified: xfbib/branches/gobject/src/xfbib-window.c
===================================================================
--- xfbib/branches/gobject/src/xfbib-window.c	2008-09-20 20:50:34 UTC (rev 5427)
+++ xfbib/branches/gobject/src/xfbib-window.c	2008-09-21 18:21:47 UTC (rev 5428)
@@ -137,3 +137,16 @@
 	window->file_io = file_io;
 }
 
+XfbibElements *
+xfbib_window_get_elements(XfbibWindow *window)
+{
+	g_return_val_if_fail(XFBIB_IS_WINDOW(window), NULL);
+	return window->elements;
+}
+
+GtkWidget *
+xfbib_window_get_tree_view(XfbibWindow *window)
+{
+	g_return_val_if_fail(XFBIB_IS_WINDOW(window), NULL);
+	return window->tree_view;
+}

Modified: xfbib/branches/gobject/src/xfbib-window.h
===================================================================
--- xfbib/branches/gobject/src/xfbib-window.h	2008-09-20 20:50:34 UTC (rev 5427)
+++ xfbib/branches/gobject/src/xfbib-window.h	2008-09-21 18:21:47 UTC (rev 5428)
@@ -22,6 +22,7 @@
 
 #include <gtk/gtk.h>
 #include "xfbib-strbuf.h"
+#include "xfbib-elements.h"
 
 #define XFBIB_TYPE_WINDOW             (xfbib_window_get_type())
 #define XFBIB_WINDOW(obj)             (G_TYPE_CHECK_INSTANCE_CAST((obj), XFBIB_TYPE_WINDOW, XfbibWindow))
@@ -38,6 +39,8 @@
 
 GtkWidget *xfbib_window_new();
 void xfbib_window_set_title(XfbibWindow *, XfbibStrbuf *);
+XfbibElements *xfbib_window_get_elements(XfbibWindow *);
+GtkWidget *xfbib_window_get_tree_view(XfbibWindow *);
 
 G_END_DECLS
 




More information about the Goodies-commits mailing list