soylent r239 - trunk/libsoylent



Author: svenp
Date: Thu Jul 24 13:29:57 2008
New Revision: 239
URL: http://svn.gnome.org/viewvc/soylent?rev=239&view=rev

Log:
implemented attribute-handler mechanism
added some default attributer writers / readers
entities can now be stored
attributes can be added to entities
attribute creation and basic modification implemented

Modified:
   trunk/libsoylent/sl-entity.c
   trunk/libsoylent/sl-entity.h

Modified: trunk/libsoylent/sl-entity.c
==============================================================================
--- trunk/libsoylent/sl-entity.c	(original)
+++ trunk/libsoylent/sl-entity.c	Thu Jul 24 13:29:57 2008
@@ -31,24 +31,30 @@
 struct _SlEntityPriv
 {
   SlEntityHandler *entity_handler;
+  EBook *ebook;
   EContact *econtact;
   GList *attributes;
+  GHashTable *attribute_table;
 };
 
 struct _SlAttributePriv
 {
   SlEntity *entity;
-  gchar *name;
+  const gchar *name;
   GList *values;
+  EVCardAttribute *eattr;
 };
 
 struct _SlAttributeHandler
 {
   SlAttributeHandlerType type;
-  gpointer (*writer)(const gchar *attrname, gpointer value);
-  gpointer (*reader)(const gchar *attrname, gpointer value);
+  SlAttributeWriterFunc writer;
+  SlAttributeReaderFunc reader;
 };
 
+static GHashTable *attribute_handlers = NULL;
+static SlAttributeHandler *attrhandler_default = NULL;
+
 static GObjectClass *parent_class = NULL;
 
 static GObjectClass *attribute_parent_class = NULL;
@@ -120,17 +126,95 @@
 }
 
 void
-sl_entity_install_attribute_handler (const gchar *attrname,
-  SlAttributeHandlerType type, gpointer (*writer)(const gchar *attrname,
-   gpointer value), gpointer (*reader)(const gchar *attrname, gpointer value))
+sl_attribute_handler_init (void)
+{
+  attribute_handlers = g_hash_table_new_full (NULL, NULL, NULL, g_free);
+  attrhandler_default = g_new (SlAttributeHandler, 1);
+  attrhandler_default->type = SL_ATTRIBUTE_HANDLER_TYPE_STRING;
+  attrhandler_default->writer = (SlAttributeWriterFunc) sl_attribute_writer_string;
+  attrhandler_default->reader = (SlAttributeReaderFunc) sl_attribute_reader_string;
+}
+
+void
+sl_attribute_handler_cleanup (void)
 {
-  g_warning ("%s not implemented", __FUNCTION__);
+  g_hash_table_remove_all (attribute_handlers);
+  g_hash_table_unref (attribute_handlers);
+  g_free (attrhandler_default);
 }
 
 void
+sl_entity_install_attribute_handler (const gchar *attrname, 
+  SlAttributeHandlerType type, SlAttributeWriterFunc writer,
+  SlAttributeReaderFunc reader)
+{
+  SlAttributeHandler *attrhandler = g_new (SlAttributeHandler, 1);
+  attrhandler->type = type;
+  attrhandler->writer = writer;
+  attrhandler->reader = reader;
+  
+  /* if the key already exists the old handler will automatically be freed by
+   * GHashTable */
+  g_hash_table_insert (attribute_handlers, (gpointer) attrname, attrhandler);
+}
+
+gboolean
 sl_entity_remove_attribute_handler (const gchar *attrname)
 {
-  g_warning ("%s not implemented", __FUNCTION__);
+  return g_hash_table_remove (attribute_handlers, attrname);
+}
+
+gpointer
+sl_attribute_handler_write (const gchar *attrname, gpointer value, 
+                            SlAttributeHandlerType *type)
+{
+  SlAttributeHandler *attrhandler = g_hash_table_lookup (attribute_handlers,
+                                                         attrname);
+  if (attrhandler == NULL)
+    {
+      attrhandler = attrhandler_default;
+    }
+  *type = attrhandler->type;
+  return attrhandler->writer (attrname, value);
+}
+
+gpointer
+sl_attribute_handler_read (const gchar *attrname, gpointer value, 
+                           SlAttributeHandlerType *type)
+{
+  SlAttributeHandler *attrhandler = g_hash_table_lookup (attribute_handlers,
+                                                         attrname);
+  if (attrhandler == NULL)
+    {
+      attrhandler = attrhandler_default;
+    }
+  *type = attrhandler->type;
+  return attrhandler->reader (attrname, value);
+}
+
+gchar *sl_attribute_writer_string (const gchar *attrname, gchar *value)
+{
+  return value;
+}
+
+gchar *sl_attribute_reader_string (const gchar *attrname, gchar *value)
+{
+  return value;
+}
+
+gchar *sl_attribute_writer_int (const gchar *attrname, GValue *value)
+{
+  GString *str = g_string_new (NULL);
+  g_string_printf (str, "%d", g_value_get_int (value));
+  return str->str;
+}
+
+GValue *sl_attribute_reader_int (const gchar *attrname, gchar *value)
+{
+  GValue *gvalue = g_new (GValue, 1);
+  g_value_init (gvalue, G_TYPE_INT);
+  g_value_set_int (gvalue, strtol(value, NULL, 10));
+  return gvalue;
 }
 
 static void
@@ -200,7 +284,10 @@
 void
 sl_entity_constr (SlEntity *self)
 {
+  self->priv->ebook = NULL;
   self->priv->econtact = e_contact_new ();
+  self->priv->attribute_table = g_hash_table_new (NULL, NULL);
+  self->priv->attributes = NULL;
 }
 
 void
@@ -209,12 +296,48 @@
   self->priv->econtact = g_object_ref (econtact);
 }
 
+void
+sl_entity_set_ebook (SlEntity *self, EBook *ebook)
+{
+  self->priv->ebook = ebook;
+}
+
+EBook *
+sl_entity_get_ebook (SlEntity *self)
+{
+  return self->priv->ebook;
+}
+
 EContact
 *sl_entity_get_econtact (SlEntity *self)
 {
   return self->priv->econtact;
 }
 
+gboolean
+sl_entity_commit (SlEntity *self, GError **error)
+{
+  /* TODO: if ebook not set return error */
+  return e_book_commit_contact (self->priv->ebook, self->priv->econtact, error);
+}
+
+void
+sl_entity_add_attribute (SlEntity *self, SlAttribute *attr)
+{
+  /* TODO: what if attribute already exists? add values?*/
+  self->priv->attributes = g_list_append (self->priv->attributes, g_object_ref (attr));
+  g_hash_table_insert (self->priv->attribute_table, (const gpointer) attr->priv->name, attr);
+  attr->priv->entity = self;
+  
+  e_vcard_add_attribute (E_VCARD (self->priv->econtact), attr->priv->eattr);
+}
+
+void
+sl_entity_remove_attribute (SlEntity *self, SlAttribute *attr)
+{
+  g_warning("%s not implemented", __FUNCTION__);
+}
+
 /* ABSTRACT
 SlEntity *
 sl_entity_new (SlEntityHandler *entity_handler)
@@ -363,11 +486,20 @@
   g_warning("%s not implemented", __FUNCTION__);
 }
 
+void
+sl_attribute_constr (SlAttribute *self, const gchar *name)
+{
+  self->priv->entity = NULL;
+  self->priv->name = name;
+  self->priv->values = NULL;
+  self->priv->eattr = e_vcard_attribute_new (NULL, name);
+}
+
 SlAttribute *
 sl_attribute_new (const gchar *name)
 {
-  /* TODO: add constr-param name and values */
   SlAttribute *self = g_object_new (SL_ATTRIBUTE_TYPE, NULL);
+  sl_attribute_constr (self, name);
   return self;
 }
 
@@ -382,22 +514,43 @@
   return NULL;
 }
 
-void sl_attribute_remove (SlAttribute *self)
+void sl_attribute_add (SlAttribute *self, gpointer value)
 {
-  g_warning("%s not implemented", __FUNCTION__);
+  SlAttributeHandlerType type = 0;
+  gpointer result = sl_attribute_handler_write (self->priv->name, value, &type);
+  
+  self->priv->values = g_list_append (self->priv->values, value);
+  e_vcard_attribute_add_value (self->priv->eattr, result);
 }
 
-void sl_attribute_set_at (SlAttribute *self, gint index, gpointer value)
+gboolean sl_attribute_set_at (SlAttribute *self, gint index, gpointer value)
 {
-  /*SlEntityHandler *entity_handler = sl_entity_get_handler (self->priv->entity);
-   *sl_entity_handler_set_at (entity_handler, self->name, index, value);
-   * TODO: behind the scenes in EDS-handler:
-   * GList *values = vcard_attribute_get_values (attrname);
-   * for (... look for value
-   * modify value in list
-   * if direct list manipulation is not possible: set_at not possible at the
-   * moment */
-  g_warning("%s not implemented", __FUNCTION__);
+  SlAttributeHandlerType type = 0;
+  gpointer result = sl_attribute_handler_write (self->priv->name, value, &type);
+  
+  GList *evalues = e_vcard_attribute_get_values (self->priv->eattr);
+  GList *evalue_nth = g_list_nth (evalues, index);
+  if (evalue_nth == NULL)
+    {
+      return FALSE;
+    }
+  gchar *evalue = evalue_nth->data;
+  g_free (evalue);
+  
+  switch (type)
+    {
+      case SL_ATTRIBUTE_HANDLER_TYPE_STRING:
+        evalue_nth->data = result;
+        break;
+      case SL_ATTRIBUTE_HANDLER_TYPE_BIN:
+        /* TODO: encode in base64 */
+        evalue_nth->data = result;
+        break;
+      default:
+        g_assert_not_reached ();
+    }
+
+  return TRUE;
 }
 
 gpointer sl_attribute_get_at (SlAttribute *self, gint index)
@@ -411,18 +564,18 @@
   g_warning("%s not implemented", __FUNCTION__);
 }
 
-void sl_attribute_set_values (SlAttribute *self, GList *values)
+void sl_attribute_set_all (SlAttribute *self, GList *values)
 {
   g_warning("%s not implemented", __FUNCTION__);
 }
 
-GList *sl_attribute_get_values (SlAttribute *self)
+GList *sl_attribute_get_all (SlAttribute *self)
 {
   g_warning("%s not implemented", __FUNCTION__);
   return NULL;
 }
 
-void sl_attribute_remove_values (SlAttribute *self)
+void sl_attribute_remove_all (SlAttribute *self)
 {
   g_warning("%s not implemented", __FUNCTION__);
 }

Modified: trunk/libsoylent/sl-entity.h
==============================================================================
--- trunk/libsoylent/sl-entity.h	(original)
+++ trunk/libsoylent/sl-entity.h	Thu Jul 24 13:29:57 2008
@@ -71,6 +71,11 @@
 
 typedef enum _SlAttributeHandlerType SlAttributeHandlerType;
 
+typedef gpointer (*SlAttributeWriterFunc)(const gchar *attrname,
+                                          const gpointer value);
+typedef gpointer (*SlAttributeReaderFunc)(const gchar *attrname,
+                                          const gpointer value);
+
 enum _SlEntityProperty
 {
   SL_ENTITY_PROPERTY_ENTITY_HANDLER = 1
@@ -109,16 +114,34 @@
 GType sl_entity_get_type (void);
 GType sl_attribute_get_type (void);
 
-void sl_entity_install_attribute_handler (const gchar *attrname,
-  SlAttributeHandlerType type, gpointer (*writer)(const gchar *attrname,
-  gpointer value), gpointer (*reader)(const gchar *attrname, gpointer value));
-void sl_entity_remove_attribute_handler (const gchar *attrname);
+void sl_attribute_handler_init (void);
+void sl_attribute_handler_cleanup (void);
+void sl_entity_install_attribute_handler (const gchar *attrname, 
+  SlAttributeHandlerType type, SlAttributeWriterFunc writer,
+  SlAttributeReaderFunc reader);
+gboolean sl_entity_remove_attribute_handler (const gchar *attrname);
+gpointer sl_attribute_handler_write (const gchar *attrname, gpointer value, 
+                                     SlAttributeHandlerType *type);
+gpointer sl_attribute_handler_read (const gchar *attrname, gpointer value, 
+                                    SlAttributeHandlerType *type);
+
+gchar *sl_attribute_writer_string (const gchar *attrname, gchar *value);
+gchar *sl_attribute_reader_string (const gchar *attrname, gchar *value);
+gchar *sl_attribute_writer_int (const gchar *attrname, GValue *value);
+GValue *sl_attribute_reader_int (const gchar *attrname, gchar *value);
 
 /*SlEntity *sl_entity_new (SlEntityHandler *entity_handler);*/
 /*SlEntityHandler *sl_entity_get_handler (SlEntity *self);*/
 void sl_entity_constr (SlEntity *self);
 void sl_entity_constr_with_econtact (SlEntity *self, EContact *econtact);
+void sl_entity_set_ebook (SlEntity *self, EBook *ebook);
+EBook *sl_entity_get_ebook (SlEntity *self);
 EContact *sl_entity_get_econtact (SlEntity *self);
+
+gboolean sl_entity_commit (SlEntity *self, GError **error);
+
+void sl_entity_add_attribute (SlEntity *self, SlAttribute *attr);
+void sl_entity_remove_attribute (SlEntity *self, SlAttribute *attr);
 SlAttribute *sl_entity_get_attribute (SlEntity *self, gchar *attrname);
 GList *sl_entity_get_attributes (SlEntity *self);
 
@@ -135,16 +158,17 @@
 /* TODO: perhaps gboolean instead of void to indicate that an attrname doesn't
  * exist */
 
+void sl_attribute_constr (SlAttribute *self, const gchar *name);
 SlAttribute *sl_attribute_new (const gchar *name);
 void sl_attribute_set (SlAttribute *self, gpointer value);
 gpointer sl_attribute_get (SlAttribute *self);
-void sl_attribute_remove (SlAttribute *self);
-void sl_attribute_set_at (SlAttribute *self, gint index, gpointer value);
+gboolean sl_attribute_set_at (SlAttribute *self, gint index, gpointer value);
 gpointer sl_attribute_get_at (SlAttribute *self, gint index);
 void sl_attribute_remove_at (SlAttribute *self, gint index);
-void sl_attribute_set_values (SlAttribute *self, GList *values);
-GList *sl_attribute_get_values (SlAttribute *self);
-void sl_attribute_remove_values (SlAttribute *self);
+void sl_attribute_add (SlAttribute *self, gpointer value);
+void sl_attribute_set_all (SlAttribute *self, GList *values);
+GList *sl_attribute_get_all (SlAttribute *self);
+void sl_attribute_remove_all (SlAttribute *self);
 const gchar *sl_attribute_get_name (SlAttribute *self);
 
 #endif



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