soylent r260 - trunk/libsoylent



Author: svenp
Date: Fri Aug  1 10:22:02 2008
New Revision: 260
URL: http://svn.gnome.org/viewvc/soylent?rev=260&view=rev

Log:
added system to print attribute runtime-types in human readable form (to_string)
added to_string functions for various types
added attribute-mappers for various types
well-known attributes are setup when loading libsoylent

Modified:
   trunk/libsoylent/sl-attributes.c
   trunk/libsoylent/sl-attributes.h
   trunk/libsoylent/soylent.c

Modified: trunk/libsoylent/sl-attributes.c
==============================================================================
--- trunk/libsoylent/sl-attributes.c	(original)
+++ trunk/libsoylent/sl-attributes.c	Fri Aug  1 10:22:02 2008
@@ -41,7 +41,7 @@
 static SlAttributeMapper *attrmapper_byte_byte = NULL;
 
 void
-sl_attribute_handler_init (void)
+sl_attributes_init (void)
 {
   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);
@@ -58,10 +58,53 @@
   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));
+  sl_install_attribute_mapper ("gdate-string", SL_ATTRIBUTE_SYS_TYPE_STRING,
+    SL_ATTRIBUTE_WRITER_FUNC (sl_attribute_writer_gdate_string),
+    SL_ATTRIBUTE_READER_FUNC (sl_attribute_reader_gdate_string));
+  sl_install_attribute_mapper ("sladdress-string", SL_ATTRIBUTE_SYS_TYPE_STRING,
+    SL_ATTRIBUTE_WRITER_FUNC (sl_attribute_writer_sladdress_string),
+    SL_ATTRIBUTE_READER_FUNC (sl_attribute_reader_sladdress_string));
+  
+  /* TODO: perhaps this should go to SlPerson? */
+  
+  sl_install_attribute (SL_ATTR_ID, "string-string", g_free,
+    SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_string_to_string));
+  sl_install_attribute (SL_ATTR_NAME, "string-string", g_free,
+    SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_string_to_string));
+  sl_install_attribute (SL_ATTR_NICK, "string-string", g_free,
+    SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_string_to_string));
+  sl_install_attribute (SL_ATTR_GROUP, "string-string", g_free,
+    SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_string_to_string));
+  sl_install_attribute (SL_ATTR_ADDRESS, "sladdress-string", g_free,
+    SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_sladdress_to_string));
+  sl_install_attribute (SL_ATTR_EMAIL, "string-string", g_free,
+    SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_string_to_string));
+  sl_install_attribute (SL_ATTR_TELEPHONE, "string-string", g_free,
+    SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_string_to_string));
+  sl_install_attribute (SL_ATTR_BIRTHDAY, "gdate-string", (GDestroyNotify) g_date_free,
+    SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_gdate_to_string));
+  sl_install_attribute (SL_ATTR_URL, "string-string", g_free,
+    SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_string_to_string));
+  sl_install_attribute (SL_ATTR_BLOG, "string-string", g_free,
+    SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_string_to_string));
+  sl_install_attribute (SL_ATTR_CALENDAR_URL, "string-string", g_free,
+    SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_string_to_string));
+  sl_install_attribute (SL_ATTR_FREE_BUSY_URL, "string-string", g_free,
+    SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_string_to_string));
+  sl_install_attribute (SL_ATTR_NOTE, "string-string", g_free,
+    SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_string_to_string));
+  sl_install_attribute (SL_ATTR_PHOTO, "byte-byte", (GDestroyNotify) g_byte_array_free,
+    SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_byte_to_string));
+  sl_install_attribute (SL_ATTR_ICON, "byte-byte", (GDestroyNotify) g_byte_array_free,
+    SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_byte_to_string));
+  sl_install_attribute (SL_ATTR_JOB_ROLE, "string-string", g_free,
+    SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_string_to_string));
+  sl_install_attribute (SL_ATTR_JOB_TITLE, "string-string", g_free,
+    SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_string_to_string));
 }
 
 void
-sl_attribute_handler_cleanup (void)
+sl_attributes_cleanup (void)
 {
   /* TODO */
   /*g_hash_table_remove_all (attribute_handlers);
@@ -88,7 +131,7 @@
 
 const SlAttributeDef *
 sl_install_attribute (const gchar *attrname, const gchar *attr_mapper_name,
-                      GDestroyNotify cleanup)
+                      GDestroyNotify cleanup, SlAttributeToStringFunc to_string)
 {
   SlAttributeMapper *attrmapper = g_hash_table_lookup (attr_mappers, attr_mapper_name);
   g_return_val_if_fail (attrmapper != NULL, NULL);
@@ -96,6 +139,7 @@
   SlAttributeDef *def = g_new (SlAttributeDef, 1);
   def->mapper = attrmapper;
   def->cleanup = cleanup;
+  def->to_string = to_string;
   
   /* if the key already exists the old handler will automatically be freed by
    * GHashTable */
@@ -104,6 +148,31 @@
   return def;
 }
 
+gchar *
+sl_attribute_to_string (const gchar *attrname, gpointer value)
+{
+  SlAttributeDef *attrdef = g_hash_table_lookup (attr_defs, attrname);
+  SlAttributeToStringFunc to_string = NULL;
+  if (attrdef == NULL || attrdef->to_string == NULL)
+    {
+      to_string = sl_attribute_default_to_string;
+    }
+  else
+    {
+      to_string = attrdef->to_string;
+    }
+  return to_string (attrname, value);
+}
+
+/* TODO: this has to go to EntityEDS, something like
+ * attrmapper = get_attribute_mapper (attrname);
+ * if (attrmapper == NULL) {
+ *    find out what systype the current thing is stored
+ *    systype = (string if unknown, string or bytes if known)
+ *    get_default_attribute_mapper (systype);
+ * }
+ * if (systype == BYTES) encode base64
+ * now store in EDS */
 gpointer
 sl_attribute_mapper_write (const gchar *attrname, gpointer value, 
                            SlAttributeSysType *systype)
@@ -127,6 +196,7 @@
 sl_attribute_mapper_read (const gchar *attrname, gpointer value,
                           SlAttributeSysType systype)
 {
+  g_debug ("mapper: reading %s", attrname);
   SlAttributeMapper *mapper = NULL;
   SlAttributeDef *attrdef = g_hash_table_lookup (attr_defs, attrname);
   if (attrdef != NULL)
@@ -193,3 +263,141 @@
   g_value_set_int (gvalue, strtol(value, NULL, 10));
   return gvalue;
 }
+
+gchar *
+sl_attribute_writer_gdate_string (const gchar *attrname, GDate *date)
+{
+  return sl_attribute_gdate_to_string (attrname, date);
+}
+
+GDate *
+sl_attribute_reader_gdate_string (const gchar *attrname, gchar *string)
+{
+  g_debug ("eds raw bday: %s", string);
+  /* TODO: more error handling */
+  gchar **tokens = g_strsplit (string, "-", 0);
+  gint year = strtol (tokens[0], NULL, 10);
+  gint month = strtol (tokens[1], NULL, 10);
+  gint day = strtol (tokens[2], NULL, 10);
+  g_strfreev (tokens);
+  return g_date_new_dmy (day, month, year);
+}
+
+gchar *
+sl_attribute_writer_sladdress_string (const gchar *attrname, SlAddress *address)
+{
+  /* to the post office box; the extended address; the street
+   address; the locality (e.g., city); the region (e.g., state or
+   province); the postal code; the country name */
+  GString *str = g_string_new (NULL);
+  g_string_printf (str, "%s;%s;%s;%s;%s;%s;%s", address->po_box, address->addition, address->street, address->city, address->region, address->postal_code, address->country);
+  gchar *string = str->str;
+  g_string_free (str, FALSE);
+  return string;
+}
+
+SlAddress *
+sl_attribute_reader_sladdress_string (const gchar *attrname, gchar *string)
+{
+  g_debug ("from eds: %s", string);
+  return NULL;
+}
+
+gchar *
+sl_attribute_default_to_string (const gchar *attrname, gpointer value)
+{
+  /*GString *gstr = g_string_new (NULL);
+  g_string_printf (gstr, "&0x%.8x", (guint) value);
+  gchar *str = gstr->str;
+  g_string_free (gstr, FALSE);
+  return str;*/
+  return sl_attribute_string_to_string (attrname, value);
+}
+
+gchar *
+sl_attribute_string_to_string (const gchar *attrname, gchar *string)
+{
+  return g_strdup (string);
+}
+
+gchar *
+sl_attribute_byte_to_string (const gchar *attrname, GByteArray *bytes)
+{
+  GString *gstr = g_string_new (NULL);
+  g_string_printf (gstr, "GByteArray [&0x%.8x, length: %db]", (guint) bytes->data, bytes->len);
+  gchar *str = gstr->str;
+  g_string_free (gstr, FALSE);
+  return str;
+}
+
+gchar *
+sl_attribute_gint_to_string (const gchar *attrname, GValue *value)
+{
+  GString *gstr = g_string_new (NULL);
+  g_string_printf (gstr, "%d", g_value_get_int (value));
+  gchar *str = gstr->str;
+  g_string_free (gstr, FALSE);
+  return str;
+}
+
+gchar *
+sl_attribute_gdate_to_string (const gchar *attrname, GDate *date)
+{
+  GString *gstr = g_string_new (NULL);
+  g_string_printf (gstr, "%.4d-%.2d-%.2d", g_date_get_year (date),
+                   g_date_get_month (date), g_date_get_day (date));
+  gchar *str = gstr->str;
+  g_string_free (gstr, FALSE);
+  return str;
+}
+
+gchar *
+sl_attribute_sladdress_to_string (const gchar *attrname, SlAddress *address)
+{
+  GString *gstr = g_string_new (NULL);
+  gboolean first = TRUE;
+  if (address->street != NULL)
+    {
+      g_string_append (gstr, address->street);
+      first = FALSE;
+    }
+  if (address->addition != NULL)
+    {
+      if (!first) g_string_append (gstr, ", ");
+      g_string_append (gstr, address->addition);
+      first = FALSE;
+    }
+  if (address->postal_code != NULL)
+    {
+      if (!first) g_string_append (gstr, ", ");
+      g_string_append (gstr, address->postal_code);
+      first = FALSE;
+    }
+  if (address->city != NULL)
+    {
+      if (!first && address->postal_code == NULL) g_string_append (gstr, ", ");
+      g_string_append (gstr, address->city);
+      first = FALSE;
+    }
+  if (address->region != NULL)
+    {
+      if (!first) g_string_append (gstr, ", ");
+      g_string_append (gstr, address->region);
+      first = FALSE;
+    }
+  if (address->country != NULL)
+    {
+      if (!first) g_string_append (gstr, ", ");
+      g_string_append (gstr, address->country);
+      first = FALSE;
+    }
+  if (address->po_box != NULL)
+    {
+      if (!first) g_string_append (gstr, "; ");
+      g_string_append (gstr, address->po_box);
+      first = FALSE;
+    }
+  gchar *str = gstr->str;
+  g_string_free (gstr, FALSE);
+  return str;
+}

Modified: trunk/libsoylent/sl-attributes.h
==============================================================================
--- trunk/libsoylent/sl-attributes.h	(original)
+++ trunk/libsoylent/sl-attributes.h	Fri Aug  1 10:22:02 2008
@@ -24,17 +24,24 @@
 #ifndef SL_ATTRIBUTES_H
 #define SL_ATTRIBUTES_H
 
+#include "sl-person.h"
+
 #include <glib.h>
 #include <glib-object.h>
 
 #define SL_ATTRIBUTE_WRITER_FUNC(func)  ((SlAttributeWriterFunc) (func))
 #define SL_ATTRIBUTE_READER_FUNC(func)  ((SlAttributeReaderFunc) (func))
 
+#define SL_ATTRIBUTE_TO_STRING_FUNC(func) ((SlAttributeToStringFunc) (func))
+
 typedef gpointer (*SlAttributeWriterFunc)(const gchar *attrname,
                                           gpointer value);
 typedef gpointer (*SlAttributeReaderFunc)(const gchar *attrname,
                                           gpointer value);
 
+typedef gchar * (*SlAttributeToStringFunc)(const gchar *attrname,
+                                           gpointer value);
+
 typedef enum _SlAttributeSysType SlAttributeSysType;
 
 typedef struct _SlAttributeDef    SlAttributeDef;
@@ -57,16 +64,20 @@
 {
   SlAttributeMapper *mapper;
   GDestroyNotify cleanup;
+  SlAttributeToStringFunc to_string;
 };
 
-void sl_attribute_handler_init (void);
-void sl_attribute_handler_cleanup (void);
+void sl_attributes_init (void);
+void sl_attributes_cleanup (void);
 
 const SlAttributeMapper *sl_install_attribute_mapper (const gchar *mappername,
   SlAttributeSysType systype, SlAttributeWriterFunc writer,
   SlAttributeReaderFunc reader);
 const SlAttributeDef *sl_install_attribute (const gchar *attrname,
-  const gchar *attr_mapper_name, GDestroyNotify cleanup);
+  const gchar *attr_mapper_name, GDestroyNotify cleanup,
+  SlAttributeToStringFunc to_string);
+
+gchar *sl_attribute_to_string (const gchar *attrname, gpointer value);
 
 gpointer sl_attribute_mapper_write (const gchar *attrname, gpointer value,
                                     SlAttributeSysType *systype);
@@ -81,5 +92,18 @@
                                            GByteArray *value);
 gchar *sl_attribute_writer_gint_string (const gchar *attrname, GValue *value);
 GValue *sl_attribute_reader_gint_string (const gchar *attrname, gchar *value);
+gchar *sl_attribute_writer_gdate_string (const gchar *attrname, GDate *date);
+GDate *sl_attribute_reader_gdate_string (const gchar *attrname, gchar *string);
+gchar *sl_attribute_writer_sladdress_string (const gchar *attrname,
+                                             SlAddress *address);
+SlAddress *sl_attribute_reader_sladdress_string (const gchar *attrname,
+                                                 gchar *string);
+
+gchar *sl_attribute_default_to_string (const gchar *attrname, gpointer value);
+gchar *sl_attribute_string_to_string (const gchar *attrname, gchar *string);
+gchar *sl_attribute_byte_to_string (const gchar *attrname, GByteArray *bytes);
+gchar *sl_attribute_gint_to_string (const gchar *attrname, GValue *value);
+gchar *sl_attribute_gdate_to_string (const gchar *attrname, GDate *date);
+gchar *sl_attribute_sladdress_to_string (const gchar *attrname, SlAddress *address);
 
 #endif

Modified: trunk/libsoylent/soylent.c
==============================================================================
--- trunk/libsoylent/soylent.c	(original)
+++ trunk/libsoylent/soylent.c	Fri Aug  1 10:22:02 2008
@@ -30,12 +30,12 @@
 sl_init (GError **error)
 {
 	g_type_init ();
-  sl_attribute_handler_init ();
+  sl_attributes_init ();
 	return sl_book_setup (error);
 }
 
 void
 sl_cleanup (void)
 {
-  sl_attribute_handler_cleanup ();
+  sl_attributes_cleanup ();
 }



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