soylent r258 - trunk/libsoylent



Author: svenp
Date: Thu Jul 31 14:20:08 2008
New Revision: 258
URL: http://svn.gnome.org/viewvc/soylent?rev=258&view=rev

Log:
improved attribute-handler (is now attribute-definition and attribute-mapper)
implemented reading bytes from EDS

Modified:
   trunk/libsoylent/sl-attribute-eds.c
   trunk/libsoylent/sl-attributes.c
   trunk/libsoylent/sl-attributes.h

Modified: trunk/libsoylent/sl-attribute-eds.c
==============================================================================
--- trunk/libsoylent/sl-attribute-eds.c	(original)
+++ trunk/libsoylent/sl-attribute-eds.c	Thu Jul 31 14:20:08 2008
@@ -229,7 +229,28 @@
   for (; evalues != NULL; evalues = evalues->next)
     {
       evalue = evalues->data;
-      gpointer value = sl_attribute_handler_read (self->priv->name, evalue, NULL);
+      gpointer value = NULL;
+      
+      /* TODO: install attribute-definition if attribute is unknown? would
+       * solve the problem that reading is done in bytes while reading is still
+       * done with strings */
+      
+      GList* encodings = e_vcard_attribute_get_param (eattr, EVC_ENCODING);
+      if (encodings != NULL)
+        {
+          g_debug ("now I'm decoding some base64 stuff!");
+          /* TODO: handle other encodings */
+          GByteArray *bytes = g_byte_array_new ();
+          bytes->data = g_base64_decode (evalue, &bytes->len);
+          value = sl_attribute_mapper_read (self->priv->name, bytes, SL_ATTRIBUTE_SYS_TYPE_BYTE);
+          /* TODO: Free bytes here. For the moment the bytes are just passed
+           * through the mapper, so they are still needed later */
+        }
+      else
+        {
+          value = sl_attribute_mapper_read (self->priv->name, evalue, SL_ATTRIBUTE_SYS_TYPE_STRING);
+        }
+      
       self->priv->values = g_list_append (self->priv->values, value);
     }
 }
@@ -300,8 +321,8 @@
 
 void sl_attribute_add (SlAttribute *self, gpointer value)
 {
-  SlAttributeHandlerType type = 0;
-  gpointer evalue = sl_attribute_handler_write (self->priv->name, value, &type);
+  SlAttributeSysType systype = 0;
+  gpointer evalue = sl_attribute_mapper_write (self->priv->name, value, &systype);
   
   /* TODO: encode value if binary */
   self->priv->values = g_list_append (self->priv->values, value);
@@ -310,8 +331,8 @@
 
 gboolean sl_attribute_set_at (SlAttribute *self, gint index, gpointer value)
 {
-  SlAttributeHandlerType type = 0;
-  gpointer result = sl_attribute_handler_write (self->priv->name, value, &type);
+  SlAttributeSysType systype = 0;
+  gpointer result = sl_attribute_mapper_write (self->priv->name, value, &systype);
   
   GList *evalues = e_vcard_attribute_get_values (self->priv->eattr);
   GList *evalue_nth = g_list_nth (evalues, index);
@@ -326,12 +347,12 @@
   /* TODO: user has to free value, I don't know how */
   
   value_nth->data = value;
-  switch (type)
+  switch (systype)
     {
-      case SL_ATTRIBUTE_HANDLER_TYPE_STRING:
+      case SL_ATTRIBUTE_SYS_TYPE_STRING:
         evalue_nth->data = result;
         break;
-      case SL_ATTRIBUTE_HANDLER_TYPE_BIN:
+      case SL_ATTRIBUTE_SYS_TYPE_BYTE:
         /* TODO: encode in base64 */
         g_warning ("binary encoding not implemented yet");
         evalue_nth->data = result;

Modified: trunk/libsoylent/sl-attributes.c
==============================================================================
--- trunk/libsoylent/sl-attributes.c	(original)
+++ trunk/libsoylent/sl-attributes.c	Thu Jul 31 14:20:08 2008
@@ -34,104 +34,159 @@
  * An attribute-handler is responsible for reading and writing attributes (i.e.
  * converting them from their runtime-types to a string / binary form and back).
  */
-struct _SlAttributeHandler
-{
-  SlAttributeHandlerType type;
-  SlAttributeWriterFunc writer;
-  SlAttributeReaderFunc reader;
-};
 
-static GHashTable *attribute_handlers = NULL;
-static SlAttributeHandler *attrhandler_default = NULL;
+static GHashTable *attr_defs = NULL;
+static GHashTable *attr_mappers = NULL;
+static SlAttributeMapper *attrmapper_string_string = NULL;
+static SlAttributeMapper *attrmapper_byte_byte = NULL;
 
 void
 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;
+  attr_defs = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_free);
+  attr_mappers = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
+  
+  attrmapper_string_string = (SlAttributeMapper *) sl_install_attribute_mapper (
+    "string-string", SL_ATTRIBUTE_SYS_TYPE_STRING,
+    SL_ATTRIBUTE_WRITER_FUNC (sl_attribute_writer_string_string),
+    SL_ATTRIBUTE_READER_FUNC (sl_attribute_reader_string_string));
+  attrmapper_byte_byte = (SlAttributeMapper *) sl_install_attribute_mapper (
+    "byte-byte", SL_ATTRIBUTE_SYS_TYPE_STRING,
+    SL_ATTRIBUTE_WRITER_FUNC (sl_attribute_writer_byte_byte),
+    SL_ATTRIBUTE_READER_FUNC (sl_attribute_reader_byte_byte));
+  
+  sl_install_attribute_mapper ("gint-string", SL_ATTRIBUTE_SYS_TYPE_STRING,
+    SL_ATTRIBUTE_WRITER_FUNC (sl_attribute_writer_gint_string),
+    SL_ATTRIBUTE_READER_FUNC (sl_attribute_reader_gint_string));
 }
 
 void
 sl_attribute_handler_cleanup (void)
 {
-  g_hash_table_remove_all (attribute_handlers);
+  /* TODO */
+  /*g_hash_table_remove_all (attribute_handlers);
   g_hash_table_unref (attribute_handlers);
-  g_free (attrhandler_default);
+  g_free (attrhandler_default);*/
 }
 
-void
-sl_entity_install_attribute_handler (const gchar *attrname, 
-  SlAttributeHandlerType type, SlAttributeWriterFunc writer,
+const SlAttributeMapper *
+sl_install_attribute_mapper (const gchar *mappername,
+  SlAttributeSysType systype, SlAttributeWriterFunc writer,
   SlAttributeReaderFunc reader)
 {
-  SlAttributeHandler *attrhandler = g_new (SlAttributeHandler, 1);
-  attrhandler->type = type;
-  attrhandler->writer = writer;
-  attrhandler->reader = reader;
+  SlAttributeMapper *mapper = g_new (SlAttributeMapper, 1);
+  mapper->systype = systype;
+  mapper->writer = writer;
+  mapper->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);
+  g_hash_table_insert (attr_mappers, (gpointer) mappername, mapper);
+  
+  return mapper;
 }
 
-gboolean
-sl_entity_remove_attribute_handler (const gchar *attrname)
+const SlAttributeDef *
+sl_install_attribute (const gchar *attrname, const gchar *attr_mapper_name,
+                      GDestroyNotify cleanup)
 {
-  return g_hash_table_remove (attribute_handlers, attrname);
+  SlAttributeMapper *attrmapper = g_hash_table_lookup (attr_mappers, attr_mapper_name);
+  g_return_val_if_fail (attrmapper != NULL, NULL);
+  
+  SlAttributeDef *def = g_new (SlAttributeDef, 1);
+  def->mapper = attrmapper;
+  def->cleanup = cleanup;
+  
+  /* if the key already exists the old handler will automatically be freed by
+   * GHashTable */
+  g_hash_table_insert (attr_defs, (gpointer) attrname, def);
+  
+  return def;
 }
 
 gpointer
-sl_attribute_handler_write (const gchar *attrname, gpointer value, 
-                            SlAttributeHandlerType *type)
+sl_attribute_mapper_write (const gchar *attrname, gpointer value, 
+                           SlAttributeSysType *systype)
 {
-  SlAttributeHandler *attrhandler = g_hash_table_lookup (attribute_handlers,
-                                                         attrname);
-  if (attrhandler == NULL)
+  SlAttributeMapper *mapper = NULL;
+  SlAttributeDef *attrdef = g_hash_table_lookup (attr_defs, attrname);
+  if (attrdef != NULL)
+    {
+      mapper = attrdef->mapper;
+    }
+  else
     {
-      attrhandler = attrhandler_default;
+      mapper = attrmapper_string_string;
     }
-  *type = attrhandler->type;
-  return attrhandler->writer (attrname, value);
+  
+  *systype = mapper->systype;
+  return mapper->writer (attrname, value);
 }
 
 gpointer
-sl_attribute_handler_read (const gchar *attrname, gpointer value, 
-                           SlAttributeHandlerType *type)
+sl_attribute_mapper_read (const gchar *attrname, gpointer value,
+                          SlAttributeSysType systype)
 {
-  SlAttributeHandler *attrhandler = g_hash_table_lookup (attribute_handlers,
-                                                         attrname);
-  if (attrhandler == NULL)
+  SlAttributeMapper *mapper = NULL;
+  SlAttributeDef *attrdef = g_hash_table_lookup (attr_defs, attrname);
+  if (attrdef != NULL)
     {
-      attrhandler = attrhandler_default;
+      mapper = attrdef->mapper;
     }
-  if (type != NULL)
+  else
     {
-      *type = attrhandler->type;
+      switch (systype)
+        {
+          case SL_ATTRIBUTE_SYS_TYPE_STRING:
+            mapper = attrmapper_string_string;
+            break;
+          case SL_ATTRIBUTE_SYS_TYPE_BYTE:
+            mapper = attrmapper_byte_byte;
+            break;
+          default:
+            g_assert_not_reached ();
+        }
     }
-  return attrhandler->reader (attrname, value);
+
+  return mapper->reader (attrname, value);
+}
+
+gchar *
+sl_attribute_writer_string_string (const gchar *attrname, gchar *value)
+{
+  /* TODO: strdup? */
+  return value;
+}
+
+gchar *
+sl_attribute_reader_string_string (const gchar *attrname, gchar *value)
+{
+  return value;
 }
 
-gchar *sl_attribute_writer_string (const gchar *attrname, gchar *value)
+GByteArray *
+sl_attribute_writer_byte_byte (const gchar *attrname, GByteArray *value)
 {
+  /* TODO: copy? */
   return value;
 }
 
-gchar *sl_attribute_reader_string (const gchar *attrname, gchar *value)
+GByteArray *
+sl_attribute_reader_byte_byte (const gchar *attrname, GByteArray *value)
 {
   return value;
 }
 
-gchar *sl_attribute_writer_int (const gchar *attrname, GValue *value)
+gchar *
+sl_attribute_writer_gint_string (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 *
+sl_attribute_reader_gint_string (const gchar *attrname, gchar *value)
 {
   GValue *gvalue = g_new (GValue, 1);
   g_value_init (gvalue, G_TYPE_INT);

Modified: trunk/libsoylent/sl-attributes.h
==============================================================================
--- trunk/libsoylent/sl-attributes.h	(original)
+++ trunk/libsoylent/sl-attributes.h	Thu Jul 31 14:20:08 2008
@@ -30,35 +30,56 @@
 #define SL_ATTRIBUTE_WRITER_FUNC(func)  ((SlAttributeWriterFunc) (func))
 #define SL_ATTRIBUTE_READER_FUNC(func)  ((SlAttributeReaderFunc) (func))
 
-typedef struct _SlAttributeHandler SlAttributeHandler;
-
-typedef enum _SlAttributeHandlerType SlAttributeHandlerType;
-
 typedef gpointer (*SlAttributeWriterFunc)(const gchar *attrname,
-                                          const gpointer value);
+                                          gpointer value);
 typedef gpointer (*SlAttributeReaderFunc)(const gchar *attrname,
-                                          const gpointer value);
+                                          gpointer value);
+
+typedef enum _SlAttributeSysType SlAttributeSysType;
+
+typedef struct _SlAttributeDef    SlAttributeDef;
+typedef struct _SlAttributeMapper SlAttributeMapper;
+
+enum _SlAttributeSysType
+{
+  SL_ATTRIBUTE_SYS_TYPE_STRING = 1,
+  SL_ATTRIBUTE_SYS_TYPE_BYTE
+};
+
+struct _SlAttributeMapper
+{
+  SlAttributeSysType systype;
+  SlAttributeWriterFunc writer;
+  SlAttributeReaderFunc reader;
+};
 
-enum _SlAttributeHandlerType
+struct _SlAttributeDef
 {
-  SL_ATTRIBUTE_HANDLER_TYPE_STRING,
-  SL_ATTRIBUTE_HANDLER_TYPE_BIN
+  SlAttributeMapper *mapper;
+  GDestroyNotify cleanup;
 };
 
 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,
+
+const SlAttributeMapper *sl_install_attribute_mapper (const gchar *mappername,
+  SlAttributeSysType systype, 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);
+const SlAttributeDef *sl_install_attribute (const gchar *attrname,
+  const gchar *attr_mapper_name, GDestroyNotify cleanup);
+
+gpointer sl_attribute_mapper_write (const gchar *attrname, gpointer value,
+                                    SlAttributeSysType *systype);
+gpointer sl_attribute_mapper_read (const gchar *attrname, gpointer value,
+                                   SlAttributeSysType systype);
+
+gchar *sl_attribute_writer_string_string (const gchar *attrname, gchar *value);
+gchar *sl_attribute_reader_string_string (const gchar *attrname, gchar *value);
+GByteArray *sl_attribute_writer_byte_byte (const gchar *attrname,
+                                           GByteArray *value);
+GByteArray *sl_attribute_reader_byte_byte (const gchar *attrname,
+                                           GByteArray *value);
+gchar *sl_attribute_writer_gint_string (const gchar *attrname, GValue *value);
+GValue *sl_attribute_reader_gint_string (const gchar *attrname, gchar *value);
 
 #endif



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