[Goodies-commits] r5623 - xfbib/branches/gobject/src
David Gustafsson
tssj at xfce.org
Tue Oct 14 22:26:15 CEST 2008
Author: tssj
Date: 2008-10-14 20:26:15 +0000 (Tue, 14 Oct 2008)
New Revision: 5623
Added:
xfbib/branches/gobject/src/xfbib-bibtex-entry.c
xfbib/branches/gobject/src/xfbib-bibtex-entry.h
xfbib/branches/gobject/src/xfbib-bibtex-value.c
xfbib/branches/gobject/src/xfbib-bibtex-value.h
xfbib/branches/gobject/src/xfbib-bibtex.c
xfbib/branches/gobject/src/xfbib-bibtex.h
Removed:
xfbib/branches/gobject/src/xfbib-elements.c
xfbib/branches/gobject/src/xfbib-elements.h
xfbib/branches/gobject/src/xfbib-entry.c
xfbib/branches/gobject/src/xfbib-entry.h
xfbib/branches/gobject/src/xfbib-value.c
xfbib/branches/gobject/src/xfbib-value.h
Modified:
xfbib/branches/gobject/src/main.c
xfbib/branches/gobject/src/xfbib-entry-edit-dialog.c
xfbib/branches/gobject/src/xfbib-list-store.c
xfbib/branches/gobject/src/xfbib-list-store.h
xfbib/branches/gobject/src/xfbib-menu-bar.c
xfbib/branches/gobject/src/xfbib-tree-view.c
Log:
Renamed parser to make the names a bit more obvious. Also
started adding functionality to save edited entries.
Modified: xfbib/branches/gobject/src/main.c
===================================================================
--- xfbib/branches/gobject/src/main.c 2008-10-14 20:23:17 UTC (rev 5622)
+++ xfbib/branches/gobject/src/main.c 2008-10-14 20:26:15 UTC (rev 5623)
@@ -28,7 +28,7 @@
#include "xfbib-state.h"
#include "xfbib-window.h"
-gboolean
+gboolean
cb_window_destroy_event (GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
g_fprintf(stderr, "destroy-event\n");
Copied: xfbib/branches/gobject/src/xfbib-bibtex-entry.c (from rev 5611, xfbib/branches/gobject/src/xfbib-entry.c)
===================================================================
--- xfbib/branches/gobject/src/xfbib-bibtex-entry.c (rev 0)
+++ xfbib/branches/gobject/src/xfbib-bibtex-entry.c 2008-10-14 20:26:15 UTC (rev 5623)
@@ -0,0 +1,195 @@
+/*
+ * Copyright (c) 2008 Jesper Karlsson & David Gustafsson
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#include <gtk/gtk.h>
+#include <string.h>
+#include <libxfce4util/libxfce4util.h>
+
+#include "xfbib-entry.h"
+#include "xfbib-bibtex-field.h"
+#include "xfbib-strbuf.h"
+
+struct _XfbibBibtexEntry
+{
+ GObject parent;
+ XfbibStrbuf *bibtype;
+ XfbibStrbuf *key;
+ GList *fields;
+};
+
+typedef struct _XfbibBibtexEntryClass
+{
+ GObjectClass parent;
+} XfbibBibtexEntryClass;
+
+static void xfbib_bibtex_entry_class_init(XfbibBibtexEntryClass *klass);
+
+static void xfbib_bibtex_entry_init(XfbibBibtexEntry *instance);
+static void xfbib_bibtex_entry_finalize(GObject *obj);
+
+G_DEFINE_TYPE(XfbibBibtexEntry, xfbib_bibtex_entry, G_TYPE_OBJECT)
+
+static void
+xfbib_bibtex_entry_class_init(XfbibBibtexEntryClass *klass)
+{
+ GObjectClass *object_class = (GObjectClass *)klass;
+ object_class->finalize = xfbib_bibtex_entry_finalize;
+}
+
+static void
+xfbib_bibtex_entry_init(XfbibBibtexEntry *instance)
+{
+ instance->key = xfbib_strbuf_new();
+ instance->bibtype = xfbib_strbuf_new();
+ instance->fields = NULL;
+}
+
+static void
+xfbib_bibtex_entry_finalize(GObject *obj)
+{
+ XfbibBibtexEntry *entry = XFBIB_BIBTEX_ENTRY(obj);
+ g_object_unref(entry->key);
+ g_object_unref(entry->bibtype);
+ g_list_foreach (entry->fields, (GFunc) g_object_unref, NULL);
+ g_list_free (entry->fields);
+ G_OBJECT_CLASS(xfbib_bibtex_entry_parent_class)->finalize(obj);
+}
+
+XfbibBibtexEntry *
+xfbib_bibtex_entry_new()
+{
+ XfbibBibtexEntry *entry;
+ entry = g_object_new(XFBIB_TYPE_BIBTEX_ENTRY, NULL);
+ return entry;
+}
+
+gboolean
+xfbib_bibtex_entry_parse(XfbibBibtexEntry *entry, const gchar *str)
+{
+ gchar *tmp;
+ XfbibStrbuf *buf;
+ XfbibBibtexField *field;
+ gint i, brackets, quotes;
+
+ DBG("str = %s", str);
+
+ /* Dont copy the last bracket */
+ tmp = g_strndup(str, strlen(str) - 1);
+
+ /* Parse entry type */
+ for (i = 0; i < strlen(tmp) && tmp[i] != '{' && tmp[i] != '('; i++)
+ xfbib_strbuf_append_char(entry->bibtype, tmp[i]);
+
+ if (i == strlen(tmp)) {
+ return FALSE;
+ }
+
+ /* Parse the entry key */
+ for (i++; tmp[i] != '\0' && tmp[i] != ','; i++)
+ xfbib_strbuf_append_char(entry->key, tmp[i]);
+
+ buf = xfbib_strbuf_new();
+
+ for (i++, brackets = 0, quotes = 0; i < strlen(tmp); i++) {
+ if (tmp[i] == ',' && brackets == 0 && quotes%2 == 0) {
+ field = xfbib_bibtex_field_new();
+ if (!xfbib_bibtex_field_parse(field, xfbib_strbuf_get_str(buf)))
+ return FALSE;
+ entry->fields = g_list_append(entry->fields, field);
+ xfbib_strbuf_wipe(buf);
+ continue;
+ } else if (tmp[i] == '{' || tmp[i] == '(') {
+ brackets++;
+ } else if (tmp[i] == '}' || tmp[i] == ')') {
+ brackets--;
+ } else if (tmp[i] == '"') {
+ quotes++;
+ }
+ 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;
+ }
+
+ if (i < xfbib_strbuf_len(buf)) {
+ field = xfbib_bibtex_field_new();
+ if (!xfbib_bibtex_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);
+
+ return TRUE;
+}
+
+XfbibBibtexField *
+xfbib_bibtex_entry_get_field_by_column(XfbibBibtexEntry *entry, gint column)
+{
+ XfbibBibtexField *field;
+ gint i;
+ g_return_val_if_fail(XFBIB_IS_BIBTEX_ENTRY(entry), NULL);
+
+ for (i = 0; i < g_list_length(entry->fields); i++) {
+ field = g_list_nth_data(entry->fields, i);
+ if (xfbib_bibtex_field_is_column(field, column)) {
+ return field;
+ }
+ }
+
+ return NULL;
+}
+
+XfbibStrbuf *
+xfbib_bibtex_entry_get_bibtype(XfbibBibtexEntry *entry)
+{
+ return entry->bibtype;
+}
+
+XfbibStrbuf *
+xfbib_bibtex_entry_get_key(XfbibBibtexEntry *entry)
+{
+ return entry->key;
+}
+
+void
+xfbib_bibtex_entry_set_key(XfbibBibtexEntry *entry, const gchar *key)
+{
+ xfbib_strbuf_append(entry->key, key);
+}
+
+void
+xfbib_bibtex_entry_set_bibtype(XfbibBibtexEntry *entry, const gchar *bibtype)
+{
+ xfbib_strbuf_append(entry->bibtype, bibtype);
+}
+
+void
+xfbib_bibtex_entry_set_fields(XfbibBibtexEntry *entry, GList *fields)
+{
+ entry->fields = fields;
+}
+
Property changes on: xfbib/branches/gobject/src/xfbib-bibtex-entry.c
___________________________________________________________________
Name: svn:mergeinfo
+
Copied: xfbib/branches/gobject/src/xfbib-bibtex-entry.h (from rev 5611, xfbib/branches/gobject/src/xfbib-entry.h)
===================================================================
--- xfbib/branches/gobject/src/xfbib-bibtex-entry.h (rev 0)
+++ xfbib/branches/gobject/src/xfbib-bibtex-entry.h 2008-10-14 20:26:15 UTC (rev 5623)
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2008 Jesper Karlsson & David Gustafsson
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __XFBIB_BIBTEX_ENTRY_H
+#define __XFBIB_BIBTEX_ENTRY_H
+
+#include <glib-object.h>
+#include "xfbib-bibtex-field.h"
+#include "xfbib-strbuf.h"
+
+#define XFBIB_TYPE_BIBTEX_ENTRY (xfbib_bibtex_entry_get_type())
+#define XFBIB_BIBTEX_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), XFBIB_TYPE_BIBTEX_ENTRY, XfbibBibtexEntry))
+#define XFBIB_IS_BIBTEX_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), XFBIB_TYPE_BIBTEX_ENTRY))
+#define XFBIB_BIBTEX_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), XFBIB_TYPE_BIBTEX_ENTRY, XfbibBibtexEntryClass))
+#define XFBIB_IS_BIBTEX_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), XFBIB_TYPE_BIBTEX_ENTRY))
+#define XFBIB_BIBTEX_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), XFBIB_TYPE_BIBTEX_ENTRY, XfbibBibtexEntryClass))
+
+G_BEGIN_DECLS
+
+typedef struct _XfbibBibtexEntry XfbibBibtexEntry;
+
+GType xfbib_bibtex_entry_get_type() G_GNUC_CONST;
+
+XfbibBibtexEntry *xfbib_bibtex_entry_new();
+gboolean xfbib_bibtex_entry_parse(XfbibBibtexEntry *, const gchar *);
+XfbibBibtexField *xfbib_bibtex_entry_get_field_by_column(XfbibBibtexEntry *, gint);
+XfbibStrbuf *xfbib_bibtex_entry_get_bibtype(XfbibBibtexEntry *);
+XfbibStrbuf *xfbib_bibtex_entry_get_key(XfbibBibtexEntry *);
+void xfbib_bibtex_entry_set_key(XfbibBibtexEntry *, const gchar *);
+void xfbib_bibtex_entry_set_bibtype(XfbibBibtexEntry *, const gchar *);
+void xfbib_bibtex_entry_set_fields(XfbibBibtexEntry *, GList *);
+G_END_DECLS
+
+#endif //__XFBIB_BIBTEX_ENTRY_H
+
Property changes on: xfbib/branches/gobject/src/xfbib-bibtex-entry.h
___________________________________________________________________
Name: svn:mergeinfo
+
Copied: xfbib/branches/gobject/src/xfbib-bibtex-value.c (from rev 5611, xfbib/branches/gobject/src/xfbib-value.c)
===================================================================
--- xfbib/branches/gobject/src/xfbib-bibtex-value.c (rev 0)
+++ xfbib/branches/gobject/src/xfbib-bibtex-value.c 2008-10-14 20:26:15 UTC (rev 5623)
@@ -0,0 +1,208 @@
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#include <gtk/gtk.h>
+#include <string.h>
+
+#include "xfbib-value.h"
+#include "xfbib-strbuf.h"
+#include "xfbib-bibtex-string.h"
+#include "xfbib-integer.h"
+
+
+struct _XfbibBibtexValue
+{
+ GObject parent;
+ GList *list;
+};
+
+typedef struct _XfbibBibtexValueClass
+{
+ GObjectClass parent;
+} XfbibBibtexValueClass;
+
+static void xfbib_bibtex_value_class_init(XfbibBibtexValueClass *klass);
+
+static void xfbib_bibtex_value_init(XfbibBibtexValue *instance);
+static void xfbib_bibtex_value_finalize(GObject *obj);
+
+G_DEFINE_TYPE(XfbibBibtexValue, xfbib_bibtex_value, G_TYPE_OBJECT)
+
+static void
+xfbib_bibtex_value_class_init(XfbibBibtexValueClass *klass)
+{
+ GObjectClass *object_class = (GObjectClass *)klass;
+ object_class->finalize = xfbib_bibtex_value_finalize;
+}
+
+static void
+xfbib_bibtex_value_init(XfbibBibtexValue *instance)
+{
+ instance->list = NULL;
+}
+
+static void
+xfbib_bibtex_value_finalize(GObject *obj)
+{
+ G_OBJECT_CLASS(xfbib_bibtex_value_parent_class)->finalize(obj);
+}
+
+XfbibBibtexValue *
+xfbib_bibtex_value_new()
+{
+ XfbibBibtexValue *value;
+ value = g_object_new(XFBIB_TYPE_BIBTEX_VALUE, NULL);
+
+ return value;
+}
+
+gboolean
+xfbib_bibtex_value_parse(XfbibBibtexValue *value, const gchar *str)
+{
+ gint i, j, len, braces;
+ GList *list;
+ gchar **split = NULL;
+ GObject *obj;
+
+ /* TODO: Free all elements of the list */
+ value->list = NULL;
+
+ split = g_strsplit(str, "#", 0);
+
+ for (i = 0; split[i] != NULL; i++) {
+ /*
+ * Check the type of the string to parse.
+ *
+ * If the string starts with " or { it is a string,
+ * if the string starts with a alphabetic character
+ * it is a variable or if it contains all numbers it
+ * is an integer.
+ */
+ g_strstrip(split[i]);
+ if (split[i][0] == '"' || split[i][0] == '{') {
+ /* String */
+
+ len = strlen(split[i]);
+
+ if (split[i][0] == '"' && split[i][len - 1] != '"')
+ return FALSE;
+ else if (split[i][0] == '{' && split[i][len - 1] != '}')
+ return FALSE;
+
+ if (split[i][0] == '{' && strchr(split[i], '@') != NULL) {
+ /* Braced string contains @ */
+ g_warning("Braced string contains @: %s\n", split[i]);
+ return FALSE;
+ }
+
+ braces = 0;
+ len = strlen(split[i]) - 1;
+ for (j = 1; j < len; j++) {
+ /* Check if the braces are balanced and quotes are inside braces*/
+ if (split[i][j] == '{')
+ braces++;
+ else if (split[i][j] == '}')
+ braces--;
+ else if (split[i][0] == '"' && split[i][j] == '"' && braces == 0) {
+ g_warning("Quote not inside braces in string: %s\n", split[i]);
+ return FALSE;
+ }
+ }
+
+ if (braces != 0) {
+ /* Braces was not balanced */
+ g_warning("Braces not balanced in field string: %s\n", split[i]);
+ return FALSE;
+ }
+
+ obj = G_OBJECT(xfbib_strbuf_new());
+ xfbib_strbuf_append(XFBIB_STRBUF(obj), split[i]);
+ value->list = g_list_prepend(value->list, obj);
+ } else if(g_ascii_isalpha(split[i][0])) {
+ /* Variable */
+
+ if (strstr(split[i], "\"") != NULL) {
+ g_warning("Variable contains double quote\n");
+ return FALSE;
+ }
+
+ obj = G_OBJECT(xfbib_bibtex_string_new());
+ xfbib_bibtex_string_set_variable(XFBIB_BIBTEX_STRING(obj), split[i]);
+ value->list = g_list_prepend(value->list, obj);
+ } else {
+ len = strlen(split[i]);
+ for (j = 0; j < len; j++) {
+ if (!g_ascii_isdigit(split[i][j])) {
+ /* Character was not a number between 0 and 9 */
+ g_warning("TODO: Add a character in number warning\n");
+ return FALSE;
+ }
+ }
+ /* Integer */
+ obj = G_OBJECT(xfbib_integer_new());
+ xfbib_integer_set(XFBIB_INTEGER(obj), split[i]);
+ value->list = g_list_prepend(value->list, obj);
+ }
+ }
+
+ g_strfreev(split);
+
+
+ value->list = g_list_reverse(value->list);
+ return TRUE;
+}
+
+const gchar *
+xfbib_bibtex_value_get_str(XfbibBibtexValue *value)
+{
+ GObject *obj;
+ if (g_list_length(value->list) == 1) {
+ obj = g_list_nth_data(value->list, 0);
+ if (XFBIB_IS_BIBTEX_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";
+ }
+}
+
+void
+xfbib_bibtex_value_set_str(XfbibBibtexValue *value, const gchar *str)
+{
+ XfbibStrbuf *strbuf;
+ GList *list;
+
+ strbuf = xfbib_strbuf_new();
+ xfbib_strbuf_append(strbuf, str);
+ list = g_list_append(list, G_OBJECT(strbuf));
+ if(value->list != NULL) {
+ /* TODO: Free the list */
+ }
+ value->list = list;
+}
+
+void
+xfbib_bibtex_value_set_int(XfbibBibtexValue *value)
+{
+ /* TODO */
+}
+
+void
+xfbib_bibtex_value_set_string(XfbibBibtexValue *value)
+{
+ /* TODO */
+}
+
+void
+xfbib_bibtex_value_set_comstr(XfbibBibtexValue *value)
+{
+ /* TODO */
+}
+
Property changes on: xfbib/branches/gobject/src/xfbib-bibtex-value.c
___________________________________________________________________
Name: svn:mergeinfo
+
Copied: xfbib/branches/gobject/src/xfbib-bibtex-value.h (from rev 5611, xfbib/branches/gobject/src/xfbib-value.h)
===================================================================
--- xfbib/branches/gobject/src/xfbib-bibtex-value.h (rev 0)
+++ xfbib/branches/gobject/src/xfbib-bibtex-value.h 2008-10-14 20:26:15 UTC (rev 5623)
@@ -0,0 +1,27 @@
+#ifndef __XFBIB_BIBTEX_VALUE_H
+#define __XFBIB_BIBTEX_VALUE_H
+
+#include <glib-object.h>
+
+#define XFBIB_TYPE_BIBTEX_VALUE (xfbib_bibtex_value_get_type())
+#define XFBIB_BIBTEX_VALUE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), XFBIB_TYPE_BIBTEX_VALUE, XfbibBibtexValue))
+#define XFBIB_IS_BIBTEX_VALUE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), XFBIB_TYPE_BIBTEX_VALUE))
+#define XFBIB_BIBTEX_VALUE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), XFBIB_TYPE_BIBTEX_VALUE, XfbibBibtexValueClass))
+#define XFBIB_IS_BIBTEX_VALUE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), XFBIB_TYPE_BIBTEX_VALUE))
+#define XFBIB_BIBTEX_VALUE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), XFBIB_TYPE_BIBTEX_VALUE, XfbibBibtexValueClass))
+
+G_BEGIN_DECLS
+
+typedef struct _XfbibBibtexValue XfbibBibtexValue;
+
+GType xfbib_bibtex_value_get_type() G_GNUC_CONST;
+
+XfbibBibtexValue *xfbib_bibtex_value_new();
+gboolean xfbib_bibtex_value_parse(XfbibBibtexValue *, const gchar *);
+const gchar *xfbib_bibtex_value_get_str(XfbibBibtexValue *);
+void xfbib_bibtex_value_set_str(XfbibBibtexValue *, const gchar *);
+
+G_END_DECLS
+
+#endif //__XFBIB_BIBTEX_VALUE_H
+
Property changes on: xfbib/branches/gobject/src/xfbib-bibtex-value.h
___________________________________________________________________
Name: svn:mergeinfo
+
Copied: xfbib/branches/gobject/src/xfbib-bibtex.c (from rev 5611, xfbib/branches/gobject/src/xfbib-elements.c)
===================================================================
--- xfbib/branches/gobject/src/xfbib-bibtex.c (rev 0)
+++ xfbib/branches/gobject/src/xfbib-bibtex.c 2008-10-14 20:26:15 UTC (rev 5623)
@@ -0,0 +1,308 @@
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#include <string.h>
+#include <gtk/gtk.h>
+
+#include "xfbib-elements.h"
+#include "xfbib-entry.h"
+#include "xfbib-strbuf.h"
+#include "xfbib-bibtex-comment.h"
+#include "xfbib-bibtex-preamble.h"
+#include "xfbib-bibtex-string.h"
+
+
+#define CURLY_BRACKET 0
+#define ROUND_BRACKET 1
+
+struct _XfbibBibtex
+{
+ GObject parent;
+ GList *list;
+ gint n_entries;
+};
+
+typedef struct _XfbibBibtexClass
+{
+ GObjectClass parent;
+} XfbibBibtexClass;
+
+static void xfbib_bibtex_class_init(XfbibBibtexClass *klass);
+
+static void xfbib_bibtex_init(XfbibBibtex *instance);
+static void xfbib_bibtex_finalize(GObject *obj);
+
+G_DEFINE_TYPE(XfbibBibtex, xfbib_bibtex, G_TYPE_OBJECT)
+
+static void
+xfbib_bibtex_class_init(XfbibBibtexClass *klass)
+{
+ GObjectClass *object_class = (GObjectClass *)klass;
+ object_class->finalize = xfbib_bibtex_finalize;
+}
+
+static void
+xfbib_bibtex_init(XfbibBibtex *instance)
+{
+ instance->list = NULL;
+ instance->n_entries = 0;
+}
+
+static void
+xfbib_bibtex_finalize(GObject *obj)
+{
+ G_OBJECT_CLASS(xfbib_bibtex_parent_class)->finalize(obj);
+}
+
+XfbibBibtex *
+xfbib_bibtex_new()
+{
+ XfbibBibtex *elements;
+ elements = g_object_new(XFBIB_TYPE_BIBTEX, NULL);
+
+ return elements;
+}
+
+
+static gboolean
+parse_balanced_brackets(XfbibStrbuf *buf, const gchar *str, gint *i)
+{
+ gint brackets;
+ gchar bracket_type;
+
+ while (str[(*i)] != '{' && str[(*i)] != '(') {
+ xfbib_strbuf_append_char(buf, str[(*i)]);
+ (*i)++;
+ }
+
+ if (str[(*i)] == '{') {
+ bracket_type = CURLY_BRACKET;
+ } else if (str[(*i)] == '(') {
+ bracket_type = ROUND_BRACKET;
+ } else {
+ g_warning("Error, on line %d(TODO)\n", __LINE__);
+ return FALSE;
+ }
+
+ xfbib_strbuf_append_char(buf, str[(*i)]);
+ (*i)++;
+
+ for (brackets = 1; brackets > 0; (*i)++) {
+ if (bracket_type == CURLY_BRACKET) {
+ if (str[(*i)] == '{')
+ brackets++;
+ else if(str[(*i)] == '}')
+ brackets--;
+ } else if (bracket_type == ROUND_BRACKET) {
+ if (str[(*i)] == '(')
+ brackets++;
+ else if(str[(*i)] == ')')
+ brackets--;
+ } else {
+ g_warning("Error, could not parse wrong entry type (TODO)\n");
+ return FALSE;;
+ /* Strange */
+ }
+ xfbib_strbuf_append_char(buf, str[(*i)]);
+ }
+ return TRUE;
+}
+
+
+gboolean
+xfbib_bibtex_parse(XfbibBibtex *elements, const gchar *str)
+{
+ gint i;
+ GRegex *regex;
+ GObject *obj;
+ XfbibStrbuf *buf;
+
+ buf = xfbib_strbuf_new();
+
+ /* Compile the regex for the entry types */
+ regex = g_regex_new("^@(article|book|booklet|conference|inbook|incollection|inproceedings|manual|"
+ "mastersthesis|misc|phdthesis|proceedings|techreport|unpublished)({|\\s|[(])",
+ G_REGEX_CASELESS, 0, NULL);
+
+ for (i = 0; i < strlen(str); i++) {
+ if (str[i] == '@') {
+ if (xfbib_strbuf_len(buf) > 0) {
+ /* The comment ended put it in the list */
+ obj = G_OBJECT(xfbib_bibtex_comment_new(xfbib_strbuf_get_str(buf)));
+ elements->list = g_list_append(elements->list, obj);
+ xfbib_strbuf_wipe(buf);
+ }
+ if (g_ascii_strncasecmp(&str[i], "@comment", 8) == 0) {
+ /* Comment */
+ for (; str[i] != '\n'; i++)
+ xfbib_strbuf_append_char(buf, str[i]);
+
+ /* Dont forget the newline character */
+ xfbib_strbuf_append_char(buf, str[i]);
+
+ obj = G_OBJECT(xfbib_bibtex_comment_new(xfbib_strbuf_get_str(buf)));
+ elements->list = g_list_append(elements->list, obj);
+ xfbib_strbuf_wipe(buf);
+ } else if (g_ascii_strncasecmp(&str[i], "@preamble", 9) == 0) {
+ /* Preamble */
+ if (parse_balanced_brackets(buf, str, &i)) {
+ obj = G_OBJECT(xfbib_bibtex_preamble_new());
+ if (xfbib_bibtex_preamble_parse(XFBIB_BIBTEX_PREAMBLE(obj), xfbib_strbuf_get_str(buf)))
+ elements->list = g_list_append(elements->list, obj);
+ }
+ xfbib_strbuf_wipe(buf);
+ } else if (g_ascii_strncasecmp(&str[i], "@string", 7) == 0) {
+ /* String */
+ if (parse_balanced_brackets(buf, str, &i)) {
+ obj = G_OBJECT(xfbib_bibtex_string_new());
+ if (xfbib_bibtex_string_parse(XFBIB_BIBTEX_STRING(obj), xfbib_strbuf_get_str(buf)))
+ elements->list = g_list_append(elements->list, obj);
+ }
+ xfbib_strbuf_wipe(buf);
+ } else if (g_regex_match (regex, &str[i], 0, NULL)) {
+ /* Entry */
+ if (parse_balanced_brackets(buf, str, &i)) {
+ obj = G_OBJECT(xfbib_entry_new());
+ if (xfbib_entry_parse(XFBIB_ENTRY(obj), xfbib_strbuf_get_str(buf))) {
+ elements->n_entries++;
+ elements->list = g_list_append(elements->list, obj);
+ }
+ }
+ xfbib_strbuf_wipe(buf);
+ } else {
+ /* Error */
+ g_warning("Error, on line %d(TODO)\n", __LINE__);
+ while (str[++i] != '@')
+ continue;
+ }
+ } else {
+ xfbib_strbuf_append_char(buf, str[i]);
+ }
+ }
+ g_regex_unref(regex);
+ return TRUE;
+}
+
+
+GList *
+xfbib_bibtex_get_list(XfbibBibtex *elements)
+{
+ g_return_val_if_fail(XFBIB_IS_BIBTEX(elements), NULL);
+ return elements->list;
+}
+
+gint
+xfbib_bibtex_get_size(XfbibBibtex *elements)
+{
+ g_return_val_if_fail(XFBIB_IS_BIBTEX(elements), 0);
+ return g_list_length(elements->list);
+}
+
+
+gint
+xfbib_bibtex_get_n_entries(XfbibBibtex *elements)
+{
+ g_return_val_if_fail(XFBIB_IS_BIBTEX(elements), 0);
+ return elements->n_entries;
+}
+
+
+XfbibEntry *
+xfbib_bibtex_get_nth_entry(XfbibBibtex *elements, gint n)
+{
+ GObject *obj;
+ gint i, len, found;
+ g_return_val_if_fail(XFBIB_IS_BIBTEX(elements)
+ && n < elements->n_entries, NULL);
+
+ len = g_list_length(elements->list);
+ for (i = 0, found = -1; i < len; i++) {
+ obj = g_list_nth_data(elements->list, i);
+ if (XFBIB_IS_ENTRY(obj)) {
+ if (++found == n)
+ return XFBIB_ENTRY(obj);
+ }
+ }
+ return NULL;
+}
+
+
+gint
+xfbib_bibtex_get_entry_position(XfbibBibtex *elements, XfbibEntry *entry)
+{
+ GObject *obj;
+ gint i, len, n;
+ g_return_val_if_fail(XFBIB_IS_BIBTEX(elements), -1);
+
+ len = g_list_length(elements->list);
+ for (i = 0, n = -1; i < len; i++) {
+ obj = g_list_nth_data(elements->list, i);
+ if (XFBIB_IS_ENTRY(obj)) {
+ n++;
+ if (XFBIB_ENTRY(obj) == entry)
+ return n;
+ }
+ }
+ return -1;
+}
+
+XfbibEntry *
+xfbib_bibtex_get_next_entry(XfbibBibtex *elements, XfbibEntry *entry)
+{
+ GObject *obj;
+ gint i, len;
+ g_return_val_if_fail(XFBIB_IS_BIBTEX(elements), NULL);
+
+ len = g_list_length(elements->list);
+ for (i = 0; i < len; i++) {
+ obj = g_list_nth_data(elements->list, i);
+ if (obj == G_OBJECT(entry)) {
+ for (i++; i < len; i++) {
+ obj = g_list_nth_data(elements->list, i);
+ if (XFBIB_IS_ENTRY(obj))
+ return XFBIB_ENTRY(obj);
+ }
+ }
+ }
+ return NULL;
+}
+
+
+void
+xfbib_bibtex_remove(XfbibBibtex *elements, GObject *data)
+{
+ GObject *obj;
+ gint i, len;
+ g_return_if_fail(XFBIB_IS_BIBTEX(elements));
+
+ len = g_list_length(elements->list);
+ for (i = 0; i < len; i++) {
+ if ((obj = g_list_nth_data(elements->list, i)) == data) {
+ elements->list = g_list_remove(elements->list, obj);
+ if(XFBIB_IS_ENTRY(obj))
+ elements->n_entries--;
+ g_object_unref(obj);
+ }
+ }
+}
+
+void
+xfbib_bibtex_remove_all(XfbibBibtex *elements)
+{
+ g_return_if_fail(XFBIB_IS_BIBTEX(elements));
+ g_list_foreach (elements->list, (GFunc) g_object_unref, NULL);
+ g_list_free (elements->list);
+ elements->list = NULL;
+ elements->n_entries = 0;
+}
+
+void
+xfbib_bibtex_add_entry(XfbibBibtex *elements, XfbibEntry *entry)
+{
+ g_return_if_fail(XFBIB_IS_BIBTEX(elements));
+ g_return_if_fail(XFBIB_IS_ENTRY(entry));
+
+ elements->list = g_list_append(elements->list, G_OBJECT(entry));
+ elements->n_entries++;
+}
+
Property changes on: xfbib/branches/gobject/src/xfbib-bibtex.c
___________________________________________________________________
Name: svn:mergeinfo
+
Copied: xfbib/branches/gobject/src/xfbib-bibtex.h (from rev 5611, xfbib/branches/gobject/src/xfbib-elements.h)
===================================================================
--- xfbib/branches/gobject/src/xfbib-bibtex.h (rev 0)
+++ xfbib/branches/gobject/src/xfbib-bibtex.h 2008-10-14 20:26:15 UTC (rev 5623)
@@ -0,0 +1,35 @@
+#ifndef __XFBIB_BIBTEX_H
+#define __XFBIB_BIBTEX_H
+
+#include <glib-object.h>
+#include <xfbib-entry.h>
+
+#define XFBIB_TYPE_BIBTEX (xfbib_bibtex_get_type())
+#define XFBIB_BIBTEX(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), XFBIB_TYPE_BIBTEX, XfbibBibtex))
+#define XFBIB_IS_BIBTEX(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), XFBIB_TYPE_BIBTEX))
+#define XFBIB_BIBTEX_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), XFBIB_TYPE_BIBTEX, XfbibBibtexClass))
+#define XFBIB_IS_BIBTEX_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), XFBIB_TYPE_BIBTEX))
+#define XFBIB_BIBTEX_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), XFBIB_TYPE_BIBTEX, XfbibBibtexClass))
+
+G_BEGIN_DECLS
+
+typedef struct _XfbibBibtex XfbibBibtex;
+
+GType xfbib_bibtex_get_type() G_GNUC_CONST;
+
+XfbibBibtex *xfbib_bibtex_new();
+gboolean xfbib_bibtex_parse(XfbibBibtex *, const gchar *);
+GList *xfbib_bibtex_get_list(XfbibBibtex *);
+gint xfbib_bibtex_get_size(XfbibBibtex *);
+gint xfbib_bibtex_get_n_entries(XfbibBibtex *);
+XfbibEntry *xfbib_bibtex_get_nth_entry(XfbibBibtex *, gint);
+gint xfbib_bibtex_get_entry_position(XfbibBibtex *, XfbibEntry *);
+XfbibEntry *xfbib_bibtex_get_next_entry(XfbibBibtex *, XfbibEntry *);
+void xfbib_bibtex_remove(XfbibBibtex *, GObject *);
+void xfbib_bibtex_remove_all(XfbibBibtex *);
+void xfbib_bibtex_add_entry(XfbibBibtex *, XfbibEntry *);
+
+G_END_DECLS
+
+#endif //__XFBIB_BIBTEX_H
+
Property changes on: xfbib/branches/gobject/src/xfbib-bibtex.h
___________________________________________________________________
Name: svn:mergeinfo
+
Deleted: xfbib/branches/gobject/src/xfbib-elements.c
Deleted: xfbib/branches/gobject/src/xfbib-elements.h
Modified: xfbib/branches/gobject/src/xfbib-entry-edit-dialog.c
===================================================================
--- xfbib/branches/gobject/src/xfbib-entry-edit-dialog.c 2008-10-14 20:23:17 UTC (rev 5622)
+++ xfbib/branches/gobject/src/xfbib-entry-edit-dialog.c 2008-10-14 20:26:15 UTC (rev 5623)
@@ -384,7 +384,7 @@
{
XfbibEntry *entry;
XfbibBibtexField *field;
- GList *field_list;
+ GList *field_list = NULL;
XfbibValue *value;
int n;
Deleted: xfbib/branches/gobject/src/xfbib-entry.c
Deleted: xfbib/branches/gobject/src/xfbib-entry.h
Modified: xfbib/branches/gobject/src/xfbib-list-store.c
===================================================================
--- xfbib/branches/gobject/src/xfbib-list-store.c 2008-10-14 20:23:17 UTC (rev 5622)
+++ xfbib/branches/gobject/src/xfbib-list-store.c 2008-10-14 20:26:15 UTC (rev 5623)
@@ -524,7 +524,6 @@
GtkTreeIter iter;
g_return_if_fail(XFBIB_IS_LIST_STORE(list_store));
-
elements = xfbib_state_get_bibtex_elements(list_store->state);
while (xfbib_elements_get_n_entries(elements) > 0) {
@@ -566,3 +565,19 @@
return XFBIB_ENTRY(iter->user_data);
}
+
+void
+xfbib_list_store_add_entry(XfbibListStore *list_store, XfbibEntry *entry)
+{
+ XfbibElements *elements;
+ gint n;
+ g_return_if_fail(XFBIB_IS_LIST_STORE(list_store));
+ g_return_if_fail(XFBIB_ENTRY(entry));
+
+ elements = xfbib_state_get_bibtex_elements(list_store->state);
+
+ xfbib_elements_add_entry(elements, entry);
+
+ n = xfbib_elements_get_n_entries(elements);
+ xfbib_list_store_row_inserted(list_store, n-1);
+}
Modified: xfbib/branches/gobject/src/xfbib-list-store.h
===================================================================
--- xfbib/branches/gobject/src/xfbib-list-store.h 2008-10-14 20:23:17 UTC (rev 5622)
+++ xfbib/branches/gobject/src/xfbib-list-store.h 2008-10-14 20:26:15 UTC (rev 5623)
@@ -41,8 +41,8 @@
void xfbib_list_store_clear(XfbibListStore *);
void xfbib_list_store_row_inserted(XfbibListStore *, gint);
XfbibEntry *xfbib_list_store_get_entry(XfbibListStore *, GtkTreeIter *);
+void xfbib_list_store_add_entry(XfbibListStore *, XfbibEntry *);
-
G_END_DECLS
#endif //__XFBIB_LIST_STORE_H
Modified: xfbib/branches/gobject/src/xfbib-menu-bar.c
===================================================================
--- xfbib/branches/gobject/src/xfbib-menu-bar.c 2008-10-14 20:23:17 UTC (rev 5622)
+++ xfbib/branches/gobject/src/xfbib-menu-bar.c 2008-10-14 20:26:15 UTC (rev 5623)
@@ -102,12 +102,21 @@
static void
cb_entry_add_activate (GtkMenuItem *menuitem, gpointer user_data)
{
+ XfbibState *state;
+ XfbibEntry *new_entry;
+ GtkTreeModel *model;
GtkWidget *dialog;
g_printf("Entry/Add\n");
+
+ state = xfbib_state_new();
+ model = gtk_tree_view_get_model(GTK_TREE_VIEW(xfbib_window_get_tree_view(
+ XFBIB_WINDOW(xfbib_state_get_window(state)))));
+
dialog = xfbib_entry_edit_dialog_new(NULL);
if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK) {
- /* TODO: Save the entry */
+ new_entry = xfbib_entry_edit_dialog_get_entry(XFBIB_ENTRY_EDIT_DIALOG(dialog));
+ xfbib_list_store_add_entry(XFBIB_LIST_STORE(model), new_entry);
}
gtk_widget_destroy(dialog);
}
@@ -138,12 +147,11 @@
cb_entry_edit_activate (GtkMenuItem *menuitem, gpointer user_data)
{
XfbibState *state;
- XfbibEntry *entry;
+ XfbibEntry *entry, *new_entry;
GtkWidget *tree_view, *dialog;
GtkTreeSelection *selection;
GtkTreeModel *model;
GtkTreeIter iter;
- GtkTreePath *path;
g_printf("Entry/Edit\n");
state = xfbib_state_new();
@@ -152,15 +160,13 @@
/* This will only work in single or browse selection mode! */
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
- /*
- * Get the XfbibEntry pointer and pass it to the
- * entry edit dialog
- */
- entry = xfbib_list_store_get_entry(model, &iter);
-
+ entry = xfbib_list_store_get_entry(XFBIB_LIST_STORE(model), &iter);
+
dialog = xfbib_entry_edit_dialog_new(entry);
if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK) {
/* TODO: Save the entry */
+ new_entry = xfbib_entry_edit_dialog_get_entry(XFBIB_ENTRY_EDIT_DIALOG(dialog));
+ g_printf("TODO: Edit an existing entry\n");
}
gtk_widget_destroy(dialog);
} else {
Modified: xfbib/branches/gobject/src/xfbib-tree-view.c
===================================================================
--- xfbib/branches/gobject/src/xfbib-tree-view.c 2008-10-14 20:23:17 UTC (rev 5622)
+++ xfbib/branches/gobject/src/xfbib-tree-view.c 2008-10-14 20:26:15 UTC (rev 5623)
@@ -27,6 +27,7 @@
#include "xfbib-elements.h"
#include "xfbib-state.h"
#include "xfbib-entry.h"
+#include "xfbib-entry-edit-dialog.h"
static struct {
gchar *label;
@@ -80,25 +81,57 @@
void
cb_row_activated (GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data)
{
- XfbibEntry *entry;
+ XfbibState *state;
+ XfbibEntry *entry, *new_entry;
GtkWidget *dialog;
GtkTreeModel *model;
GtkTreeIter iter;
+ state = xfbib_state_new();
model = gtk_tree_view_get_model(tree_view);
if (gtk_tree_model_get_iter(model, &iter, path)) {
- entry = xfbib_list_store_get_entry(model, &iter);
+ entry = xfbib_list_store_get_entry(XFBIB_LIST_STORE(model), &iter);
dialog = xfbib_entry_edit_dialog_new(entry);
if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK) {
- /* TODO: Save the entry */
+ /* TODO: Save the entry to an existing entry */
+ new_entry = xfbib_entry_edit_dialog_get_entry(XFBIB_ENTRY_EDIT_DIALOG(dialog));
+ g_printf("TODO: Edit an existing entry\n");
}
gtk_widget_destroy(dialog);
- g_print ("Double-clicked on a row with stuff");
}
}
+gboolean
+cb_button_press (GtkWidget *tree_view, GdkEventButton *event, gpointer data)
+{
+ XfbibEntry *new_entry;
+ GtkTreeModel *model;
+ GtkWidget *dialog;
+ if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
+ /* single click with the right mouse button */
+ /* TODO: Add a context menu here */
+ } else if (event->type == GDK_2BUTTON_PRESS && event->button == 1) {
+ /* double click with the left mouse button */
+
+ model = gtk_tree_view_get_model(GTK_TREE_VIEW(tree_view));
+
+ dialog = xfbib_entry_edit_dialog_new(NULL);
+
+ if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK) {
+ new_entry = xfbib_entry_edit_dialog_get_entry(XFBIB_ENTRY_EDIT_DIALOG(dialog));
+ xfbib_list_store_add_entry(XFBIB_LIST_STORE(model), new_entry);
+ }
+ gtk_widget_destroy(dialog);
+
+ } else {
+ return FALSE;
+ }
+ return TRUE;
+}
+
+
GType
xfbib_tree_view_get_type (void)
{
@@ -134,14 +167,15 @@
static void
xfbib_tree_view_init(XfbibTreeView *instance)
{
- XfbibState *state;
GtkTreeViewColumn *column;
GtkCellRenderer *renderer;
GtkTreeModel *model;
gint i;
- /* Connect the double-click to a callback function */
+ /* Connect the double-click on a row to a callback function */
g_signal_connect(GTK_TREE_VIEW(instance), "row-activated", (GCallback) cb_row_activated, NULL);
+ /* Connect the double-click NOT on a row to a callback function */
+ g_signal_connect(GTK_WIDGET(instance), "button-press-event", (GCallback) cb_button_press, NULL);
for (i = 0; i < XFBIB_FIELD_N_FIELDS; i++) {
@@ -190,8 +224,6 @@
gtk_tree_view_column_add_attribute(column, renderer, "text", i);
}
- state = xfbib_state_new();
-
model = GTK_TREE_MODEL(xfbib_list_store_new());
gtk_tree_view_set_model(GTK_TREE_VIEW(instance), model);
g_object_unref(model); /* destroy model automatically with view */
Deleted: xfbib/branches/gobject/src/xfbib-value.c
Deleted: xfbib/branches/gobject/src/xfbib-value.h
More information about the Goodies-commits
mailing list