soylent r218 - trunk/libsoylent
- From: svenp svn gnome org
- To: svn-commits-list gnome org
- Subject: soylent r218 - trunk/libsoylent
- Date: Mon, 14 Jul 2008 17:42:42 +0000 (UTC)
Author: svenp
Date: Mon Jul 14 17:42:42 2008
New Revision: 218
URL: http://svn.gnome.org/viewvc/soylent?rev=218&view=rev
Log:
improved listing books
checking for book existance works
book creation works
implemented book opening
implemented book deletion
Modified:
trunk/libsoylent/sl-book.c
trunk/libsoylent/sl-book.h
Modified: trunk/libsoylent/sl-book.c
==============================================================================
--- trunk/libsoylent/sl-book.c (original)
+++ trunk/libsoylent/sl-book.c Mon Jul 14 17:42:42 2008
@@ -35,16 +35,16 @@
};
static GObjectClass *parent_class = NULL;
-static SlBook *sl_book_default = NULL;
static void sl_book_class_init (gpointer g_class, gpointer class_data);
static void sl_book_init (GTypeInstance *instance, gpointer g_class);
static void sl_book_dispose (GObject *object);
-static void sl_book_set_property (GObject *object, guint property_id,
+static void sl_book_set_property (SlBook *self, guint property_id,
const GValue *value, GParamSpec *pspec);
static void sl_book_get_property (GObject *object, guint property_id,
GValue *value, GParamSpec *pspec);
static SlBook *sl_book_new (EBook *ebook);
+static SlBook *sl_book_new_from_source (ESource *source, GError **error);
GType
sl_book_get_type (void)
@@ -77,234 +77,285 @@
return g_quark_from_static_string ("sl-book-error");
}
-void
-sl_book_setup (void)
+static void
+sl_book_class_init (gpointer g_class, gpointer class_data)
{
- sl_book_default = sl_book_new (NULL);
- sl_book_set_current (sl_book_default);
+ parent_class = g_type_class_peek (g_type_parent (SL_BOOK_TYPE));
+ g_assert (parent_class != NULL);
+
+ GObjectClass *obj_class = G_OBJECT_CLASS (g_class);
+ obj_class->dispose = sl_book_dispose;
+ obj_class->get_property = (void (*)(GObject *, guint, GValue *, GParamSpec *))
+ sl_book_get_property;
+ obj_class->set_property = (void (*)(GObject *, guint, const GValue *,
+ GParamSpec *)) sl_book_set_property;
+
+ GParamSpec *pspec = NULL;
+ pspec = g_param_spec_pointer ("ebook", "ebook", "EBook backend",
+ G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE);
+
+ g_object_class_install_property (obj_class, SL_BOOK_PROPERTY_EBOOK, pspec);
}
-void
-sl_book_set_current (SlBook *book)
+static void
+sl_book_init (GTypeInstance *instance, gpointer g_class)
{
- sl_book_current = book;
+ SlBook *self = SL_BOOK (instance);
+ SlBookPriv *priv = g_new (SlBookPriv, 1);
+ self->priv = priv;
+ self->priv->ebook = NULL;
+ self->priv->error = NULL;
+ self->disposed = FALSE;
+}
+
+static void
+sl_book_dispose (GObject *object)
+{
+ SlBook *self = SL_BOOK (object);
+ g_return_if_fail (!self->disposed);
+
+ g_error_free (self->priv->error);
+ g_object_unref (self->priv->ebook);
+ g_free (self->priv);
+ self->disposed = TRUE;
+
+ parent_class->dispose (object);
+}
+
+static void
+sl_book_set_property (SlBook *self, guint property_id, const GValue *value,
+ GParamSpec *pspec)
+{
+ switch (property_id)
+ {
+ case SL_BOOK_PROPERTY_EBOOK:
+ self->priv->ebook = g_object_ref (g_value_get_pointer (value));
+ break;
+ default:
+ g_assert_not_reached ();
+ break;
+ }
+}
+
+static void
+sl_book_get_property (GObject *object, guint property_id, GValue *value,
+ GParamSpec *pspec)
+{
+ g_warning("%s not implemented", __FUNCTION__);
+}
+
+gboolean
+sl_book_setup (GError **error)
+{
+ sl_book_default = sl_book_open_default (error);
+ if (sl_book_default == NULL)
+ {
+ return FALSE;
+ }
+ return TRUE;
}
GList *
sl_book_get_books (void)
{
- /* TODO: get all books: as objects? or as strings? */
+ GList *books = NULL;
+
GError *error = NULL;
ESourceList *source_tree = NULL;
-
- e_book_get_addressbooks (&source_tree, &error);
- if (error != NULL)
+ if (!e_book_get_addressbooks (&source_tree, &error))
{
- g_critical ("failed to list books");
+ g_critical ("failed get source tree: %s", error->message);
return NULL;
}
- ESourceGroup *default_group = NULL;
GSList *groups = e_source_list_peek_groups (source_tree);
for (; groups != NULL; groups = groups->next)
{
ESourceGroup *group = groups->data;
- if (!default_group)
- {
- default_group = group;
- }
- const char *group_name = e_source_group_peek_name (group);
GSList *sources = e_source_group_peek_sources (group);
+
+ /* debug
+ const char *group_name = e_source_group_peek_name (group); */
+
for (; sources != NULL; sources = sources->next)
{
ESource *source = sources->data;
const gchar *name = e_source_peek_name (source);
+ books = g_list_append (books, (gpointer) name);
+
+ /* debug
const gchar *reluri = e_source_peek_relative_uri(source);
const gchar *absuri = e_source_peek_absolute_uri(source);
- g_print (" * %s.%s [%s, %s]\n", group_name, name, reluri, absuri);
+ g_debug ("%s.%s [%s, %s]", group_name, name, reluri, absuri); */
}
}
-
- return NULL;
+
+ g_object_unref (source_tree);
+ return books;
}
-static gboolean
+gboolean
sl_book_exists (const gchar *name)
{
GError *error = NULL;
ESourceList *source_tree = NULL;
if (!e_book_get_addressbooks (&source_tree, &error))
{
- g_critical ("failed to get source tree: %s", error->message);
+ g_critical ("failed to get source-tree: %s", error->message);
+ return FALSE;
}
-
- /* TODO: check for address book */
-
- return FALSE;
+
+ ESource *source = sl_priv_util_get_source (source_tree, name);
+ g_object_unref (source_tree);
+ return (source != NULL);
}
SlBook *
sl_book_create (const gchar *name, GError **error)
{
- ESourceList *source_tree = NULL;
-
- g_return_val_if_fail (name != NULL, FALSE);
- g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+ g_return_val_if_fail (name != NULL, NULL);
+ g_return_val_if_fail (error == NULL || *error == NULL, NULL);
if (sl_book_exists (name))
{
g_set_error (error, SL_BOOK_ERROR, SL_BOOK_ERROR_ALREADY_EXISTS,
- "The book %s already exists.", (gchar *) name);
- return FALSE;
+ "The book \"%s\" already exists.", (gchar *) name);
+ return NULL;
}
+ ESourceList *source_tree = NULL;
if (!e_book_get_addressbooks (&source_tree, error))
{
- g_critical ("failed to get source tree");
- return FALSE;
+ return NULL;
}
ESourceGroup *default_group = sl_priv_util_source_tree_get_default_group
- (source_tree);
+ (source_tree);
ESource *source = e_source_new (name, "");
- e_source_set_group (source, default_group); /* workaround */
e_source_group_add_source (default_group, source, -1);
if (!e_source_list_sync (source_tree, error))
{
- g_critical ("failed to store source tree");
- return FALSE;
+ return NULL;
}
- EBook *ebook = e_book_new (source, error);
- if (!ebook)
- {
- g_critical ("failed to load ebook");
- return FALSE;
- }
- if (!e_book_open (ebook, FALSE, error))
+ SlBook *book = sl_book_new_from_source (source, error);
+ if (!book)
{
- g_critical ("failed to open ebook");
- return FALSE;
+ return NULL;
}
+
+ /* we don't need the source-tree anymore _after_ the book has been created */
+ g_object_unref (source_tree);
- //return sl_book_new (ebook);
- return NULL;
+ return book;
}
-static void
-sl_book_class_init (gpointer g_class, gpointer class_data)
+SlBook *sl_book_open (const gchar *name, GError **error)
{
- parent_class = g_type_class_peek (g_type_parent (SL_BOOK_TYPE));
- g_assert (parent_class != NULL);
+ ESourceList *source_tree = NULL;
+ if (!e_book_get_addressbooks (&source_tree, error))
+ {
+ return NULL;
+ }
+ ESource *source = sl_priv_util_get_source (source_tree, name);
+ if (source == NULL)
+ {
+ g_set_error (error, SL_BOOK_ERROR, SL_BOOK_ERROR_NOT_EXISTING,
+ "There is no book \"%s\".", (gchar *) name);
+ return NULL;
+ }
- GObjectClass *obj_class = G_OBJECT_CLASS (g_class);
- obj_class->dispose = sl_book_dispose;
- obj_class->get_property = sl_book_get_property;
- obj_class->set_property = sl_book_set_property;
+ return sl_book_new_from_source (source, error);
}
-static void
-sl_book_init (GTypeInstance *instance, gpointer g_class)
+SlBook *sl_book_open_default (GError **error)
{
- SlBook *self = SL_BOOK (instance);
- SlBookPriv *priv = g_new (SlBookPriv, 1);
- self->priv = priv;
- self->priv->error = NULL;
-
- /* TODO: ebook should be depending on sl_book_new call, but for now we just
- * use the default addressbook */
- self->priv->ebook = e_book_new_system_addressbook (&self->priv->error);
- if (self->priv->error)
- {
- g_critical ("failed to get system addressbook");
- return;
- }
- /* TODO: is it a good idea to open the book here? */
- e_book_open (self->priv->ebook, FALSE, &self->priv->error);
- if (self->priv->error)
+ EBook *ebook = e_book_new_system_addressbook (error);
+ if (ebook == NULL)
{
- g_critical ("failed to open addressbook");
- return;
+ return NULL;
+ }
+ if (!e_book_open (ebook, FALSE, error))
+ {
+ return NULL;
}
- self->disposed = FALSE;
+ return sl_book_new (ebook);
}
-static void
-sl_book_dispose (GObject *object)
+gboolean
+sl_book_delete (const gchar *name, GError **error)
{
- SlBook *self = SL_BOOK (object);
- g_return_if_fail (!self->disposed);
+ /* TODO: forbid to delete default addressbook */
- g_error_free (self->priv->error);
- g_object_unref (self->priv->ebook);
- g_free (self->priv);
- self->disposed = TRUE;
+ g_return_val_if_fail (name != NULL, FALSE);
+ g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
- parent_class->dispose (object);
-}
-
-static void
-sl_book_set_property (GObject *object, guint property_id, const GValue *value,
- GParamSpec *pspec)
-{
- g_warning("%s not implemented", __FUNCTION__);
+ ESourceList *source_tree = NULL;
+ if (!e_book_get_addressbooks (&source_tree, error))
+ {
+ return FALSE;
+ }
+
+ ESource *source = sl_priv_util_get_source (source_tree, name);
+ if (source == NULL)
+ {
+ g_set_error (error, SL_BOOK_ERROR, SL_BOOK_ERROR_NOT_EXISTING,
+ "There is no book \"%s\".", (gchar *) name);
+ return FALSE;
+ }
+
+ /* get ebook and delete its related data */
+ EBook *ebook = e_book_new (source, error);
+ if (ebook == NULL)
+ {
+ return FALSE;
+ }
+ gboolean removed = e_book_remove (ebook, error);
+ g_object_unref (ebook);
+ if (!removed)
+ {
+ return FALSE;
+ }
+
+ /* remove the source from the source-tree */
+ ESourceGroup *group = e_source_peek_group (source);
+ e_source_group_remove_source (group, source);
+ if (!e_source_list_sync (source_tree, error))
+ {
+ return FALSE;
+ }
+ g_object_unref (source_tree);
+
+ return TRUE;
}
-static void
-sl_book_get_property (GObject *object, guint property_id, GValue *value,
- GParamSpec *pspec)
+static SlBook *
+sl_book_new (EBook *ebook)
{
- g_warning("%s not implemented", __FUNCTION__);
-}
-
-void
-sl_book_delete (SlBook *book) {
- g_warning("%s not implemented", __FUNCTION__);
- /*
- AddressbookView *view = data;
- AddressbookViewPrivate *priv = view->priv;
- ESource *selected_source;
- EBook *book;
- GError *error = NULL;
- GtkWindow *toplevel;
-
- selected_source = e_source_selector_peek_primary_selection (E_SOURCE_SELECTOR (priv->selector));
- if (!selected_source)
- return;
-
- toplevel = (GtkWindow *)gtk_widget_get_toplevel(ep->target->widget);
-
- if (e_error_run(toplevel, "addressbook:ask-delete-addressbook", e_source_peek_name(selected_source)) != GTK_RESPONSE_YES)
- return;
-
- Remove local data
- book = e_book_new (selected_source, &error);
- if (book) {
- BookRemovedClosure *closure = g_new (BookRemovedClosure, 1);
-
- closure->toplevel = (GtkWidget *)toplevel;
- closure->view = view;
- closure->selected_source = selected_source;
-
- if (e_book_async_remove (book, book_removed, closure)) {
- e_error_run (toplevel, "addressbook:remove-addressbook", NULL);
- g_free (closure);
- g_object_unref (book);
- }
- }*/
+ g_return_val_if_fail (ebook != NULL && E_IS_BOOK (ebook), NULL);
+ SlBook *self = g_object_new (SL_BOOK_TYPE, "ebook", ebook, NULL);
+ return self;
}
static SlBook *
-sl_book_new (EBook *ebook)
+sl_book_new_from_source (ESource *source, GError **error)
{
- /* TODO: create new EBook with ESource, howto? */
- SlBook *self = g_object_new(SL_BOOK_TYPE, NULL);
- if (self->priv->error != NULL)
+ g_return_val_if_fail (source != NULL && E_IS_SOURCE (source), NULL);
+
+ EBook *ebook = e_book_new (source, error);
+ if (!ebook)
{
- g_object_unref (self);
- self = NULL;
+ return NULL;
}
+ if (!e_book_open (ebook, FALSE, error))
+ {
+ g_object_unref (ebook);
+ return NULL;
+ }
+
+ SlBook *self = sl_book_new (ebook);
+ g_object_unref (ebook);
return self;
}
Modified: trunk/libsoylent/sl-book.h
==============================================================================
--- trunk/libsoylent/sl-book.h (original)
+++ trunk/libsoylent/sl-book.h Mon Jul 14 17:42:42 2008
@@ -21,6 +21,11 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+/*
+ * TODO: architecture should be: SlStorage -> SlBook, SlStorageHandler; see
+ * SlEntity
+ */
+
#ifndef SL_BOOK_H
#define SL_BOOK_H
@@ -40,18 +45,27 @@
#define SL_BOOK_ERROR sl_book_error_quark ()
-#define SL_BOOK_STD sl_book_current;
+#define SL_BOOK_DEFAULT sl_book_default;
+
+typedef struct _SlBook SlBook;
+typedef struct _SlBookClass SlBookClass;
+typedef struct _SlBookPriv SlBookPriv;
+
+typedef enum _SlBookProperty SlBookProperty;
+typedef enum _SlBookError SlBookError;
+
+enum SlBookProperty
+{
+ SL_BOOK_PROPERTY_EBOOK = 1
+};
enum SlBookError
{
SL_BOOK_ERROR_ALREADY_EXISTS,
+ SL_BOOK_ERROR_NOT_EXISTING,
SL_BOOK_ERROR_FAILED
};
-typedef struct _SlBook SlBook;
-typedef struct _SlBookClass SlBookClass;
-typedef struct _SlBookPriv SlBookPriv;
-
struct _SlBook
{
GObject parent;
@@ -64,19 +78,19 @@
GObjectClass parent;
};
-SlBook *sl_book_current;
+SlBook *sl_book_default;
GType sl_book_get_type (void);
GQuark sl_book_error_quark (void);
-void sl_book_setup (void);
-void sl_book_set_current (SlBook *book);
+gboolean sl_book_setup (GError **error);
GList *sl_book_get_books (void);
+gboolean sl_book_exists (const gchar *name);
SlBook *sl_book_create (const gchar *name, GError **error);
-SlBook *sl_book_open (const gchar *name);
-SlBook *sl_book_open_default (void);
-void sl_book_delete (SlBook *book);
+SlBook *sl_book_open (const gchar *name, GError **error);
+SlBook *sl_book_open_default (GError **error);
+gboolean sl_book_delete (const gchar *name, GError **error);
SlEntity *sl_book_get_person(SlBook *self, gchar *attrname, gpointer value);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]