soylent r269 - trunk/libsoylent



Author: svenp
Date: Mon Aug 11 21:40:45 2008
New Revision: 269
URL: http://svn.gnome.org/viewvc/soylent?rev=269&view=rev

Log:
added more attribute-types (readers, writers, to_strings)
added attribute / value cleanup mechanism

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

Modified: trunk/libsoylent/sl-attributes.c
==============================================================================
--- trunk/libsoylent/sl-attributes.c	(original)
+++ trunk/libsoylent/sl-attributes.c	Mon Aug 11 21:40:45 2008
@@ -22,6 +22,7 @@
  */
 
 #include "sl-attributes.h"
+#include "sl-priv-util.h"
 
 #include <stdlib.h>
 
@@ -59,23 +60,32 @@
   sl_install_attribute_mapper ("gint",
     SL_ATTRIBUTE_WRITER_FUNC (sl_attribute_writer_gint),
     SL_ATTRIBUTE_READER_FUNC (sl_attribute_reader_gint));
+  sl_install_attribute_mapper ("glist",
+    SL_ATTRIBUTE_WRITER_FUNC (sl_attribute_writer_glist),
+    SL_ATTRIBUTE_READER_FUNC (sl_attribute_reader_glist));
   sl_install_attribute_mapper ("gdate",
     SL_ATTRIBUTE_WRITER_FUNC (sl_attribute_writer_gdate),
     SL_ATTRIBUTE_READER_FUNC (sl_attribute_reader_gdate));
+  sl_install_attribute_mapper ("slname",
+    SL_ATTRIBUTE_WRITER_FUNC (sl_attribute_writer_slname),
+    SL_ATTRIBUTE_READER_FUNC (sl_attribute_reader_slname));
   sl_install_attribute_mapper ("sladdress",
     SL_ATTRIBUTE_WRITER_FUNC (sl_attribute_writer_sladdress),
     SL_ATTRIBUTE_READER_FUNC (sl_attribute_reader_sladdress));
   
   /* TODO: perhaps this should go to SlPerson? */
+  /* TODO: does g_free fit for slname, sladdress etc? what is with their strings? */
   
   sl_install_attribute (SL_ATTR_ID, "string", g_free,
     SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_string_to_string));
-  sl_install_attribute (SL_ATTR_NAME, "string", g_free,
+  sl_install_attribute (SL_ATTR_NAME, "slname", g_free,
+    SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_slname_to_string));
+  sl_install_attribute (SL_ATTR_FULL_NAME, "string", g_free,
     SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_string_to_string));
   sl_install_attribute (SL_ATTR_NICK, "string", g_free,
     SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_string_to_string));
-  sl_install_attribute (SL_ATTR_GROUP, "string", g_free,
-    SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_string_to_string));
+  sl_install_attribute (SL_ATTR_GROUP, "glist", (GDestroyNotify) g_list_free,
+    SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_glist_to_string));
   sl_install_attribute (SL_ATTR_ADDRESS, "sladdress", g_free,
     SL_ATTRIBUTE_TO_STRING_FUNC (sl_attribute_sladdress_to_string));
   sl_install_attribute (SL_ATTR_EMAIL, "string", g_free,
@@ -147,12 +157,31 @@
   return def;
 }
 
+void
+sl_attribute_cleanup (const gchar *attrname, gpointer value)
+{
+  SlAttributeDef *attrdef = g_hash_table_lookup (attr_defs, attrname);
+  GDestroyNotify cleanup = NULL;
+  if (attrdef == NULL)
+    {
+      cleanup = g_free;
+    }
+  else
+    {
+      cleanup = attrdef->cleanup;
+    }
+  if (cleanup != NULL)
+    {
+      cleanup (value);
+    }
+}
+
 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)
+  if (attrdef == NULL)
     {
       to_string = to_string_default;
     }
@@ -160,7 +189,14 @@
     {
       to_string = attrdef->to_string;
     }
-  return to_string (attrname, value);
+  if (to_string == NULL)
+    {
+      return "-";
+    }
+  else
+    {
+      return to_string (attrname, value);
+    }
 }
 
 /* TODO: this has to go to EntityEDS, something like
@@ -250,6 +286,18 @@
 }
 
 gchar *
+sl_attribute_writer_glist (const gchar *attrname, GList *list)
+{
+  return sl_priv_util_strjoin_list (",", list);
+}
+
+GList *
+sl_attribute_reader_glist (const gchar *attrname, gchar *string)
+{
+  return sl_priv_util_strsplit_list (string, ";", 0);
+}
+
+gchar *
 sl_attribute_writer_gdate (const gchar *attrname, GDate *date)
 {
   return sl_attribute_gdate_to_string (attrname, date);
@@ -267,6 +315,59 @@
   return g_date_new_dmy (day, month, year);
 }
 
+SlName *
+sl_attribute_reader_slname (const gchar *attrname, gchar *string)
+{
+  g_return_val_if_fail (attrname != NULL && string != NULL, NULL);
+  
+  /*"Family Name;Given Name;Additional Names,a;Honorific Prefixes,b;Honorific Suffixes,c"*/
+  /* TODO: error-tolerance */
+  gchar **fields = g_strsplit (string, ";", 0);
+  gint length = g_strv_length (fields);
+
+  SlName *name = sl_name_new ();
+  if (length > 0) {
+    name->family_name = fields[0];
+  }
+  if (length > 2) {    
+    name->names = sl_priv_util_strsplit_list (fields[2], ",", 0);
+  }
+  if (length > 1) {
+    name->names = g_list_prepend (name->names, fields[1]);
+  }
+  if (length > 3) {
+    name->honoric_prefixes = sl_priv_util_strsplit_list (fields[3], ",", 0);
+  }
+  if (length > 4) {
+    name->honoric_suffixes = sl_priv_util_strsplit_list (fields[4], ",", 0);
+  }
+  g_free (fields);
+  return name;
+}
+
+gchar *
+sl_attribute_writer_slname (const gchar *attrname, SlName *name)
+{
+  /*"Family Name;Given Name;Additional Names,a;Honorific Prefixes,b;Honorific Suffixes,c"*/
+  GString *gstr = g_string_new (NULL);
+  gchar *given_name = "";
+  gchar *additional_names = "";
+  if (g_list_length (name->names) > 0)
+    {
+      given_name = name->names->data;
+      if (g_list_length (name->names) > 1)
+        {
+          additional_names = sl_priv_util_strjoin_list (",", name->names->next);
+        }
+    }
+  gchar *honoric_prefixes = sl_priv_util_strjoin_list (",", name->honoric_prefixes);
+  gchar *honoric_suffixes = sl_priv_util_strjoin_list (",", name->honoric_prefixes);
+  g_string_printf (gstr, "%s;%s;%s;%s;%s", name->family_name, given_name, additional_names, honoric_prefixes, honoric_suffixes);
+  gchar *string = gstr->str;
+  g_string_free (gstr, FALSE);
+  return string;
+}
+
 gchar *
 sl_attribute_writer_sladdress (const gchar *attrname, SlAddress *address)
 {
@@ -284,9 +385,18 @@
 SlAddress *
 sl_attribute_reader_sladdress (const gchar *attrname, gchar *string)
 {
-  /* TODO: how to handle structs from eds? */
-  g_debug ("from eds: %s", string);
-  return NULL;
+  gchar **tokens = g_strsplit (string, ";", 0);
+  gint length = g_strv_length (tokens);
+  SlAddress *addr = sl_address_new ();
+  if (length > 0) addr->po_box = tokens[0];
+  if (length > 1) addr->addition = tokens[1];
+  if (length > 2) addr->street = tokens[2];
+  if (length > 3) addr->city = tokens[3];
+  if (length > 4) addr->region = tokens[4];
+  if (length > 5) addr->postal_code = tokens[5];
+  if (length > 6) addr->country = tokens[6];
+  g_free (tokens);
+  return addr;
 }
 
 #define STRING_TO_STRING_MAX_LEN 60
@@ -329,6 +439,23 @@
 }
 
 gchar *
+sl_attribute_glist_to_string (const gchar *attrname, GList *list)
+{
+  GString *gstr = g_string_new (NULL);
+  for (; list != NULL; list = list->next)
+    {
+      g_string_append_printf (gstr, "(%s)", (gchar *) list->data);
+      if (list->next != NULL)
+        {
+          g_string_append (gstr, "->");
+        }
+    }
+  gchar *string = gstr->str;
+  g_string_free (gstr, FALSE);
+  return string;
+}
+
+gchar *
 sl_attribute_gdate_to_string (const gchar *attrname, GDate *date)
 {
   GString *gstr = g_string_new (NULL);
@@ -340,8 +467,25 @@
 }
 
 gchar *
+sl_attribute_slname_to_string (const gchar *attrname, SlName *name)
+{
+  g_return_val_if_fail (attrname != NULL && name != NULL, NULL);
+
+  /* TODO: improve */
+  GString *gstr = g_string_new ("");
+  if (name->family_name != NULL)
+    {
+      g_string_printf (gstr, "%s", (gchar *) name->family_name);
+    }
+  gchar *string = gstr->str;
+  g_string_free (gstr, FALSE);
+  return string;
+}
+
+gchar *
 sl_attribute_sladdress_to_string (const gchar *attrname, SlAddress *address)
 {
+  g_return_val_if_fail (attrname != NULL && address != NULL, NULL);
   GString *gstr = g_string_new (NULL);
   gboolean first = TRUE;
   if (address->street != NULL)

Modified: trunk/libsoylent/sl-attributes.h
==============================================================================
--- trunk/libsoylent/sl-attributes.h	(original)
+++ trunk/libsoylent/sl-attributes.h	Mon Aug 11 21:40:45 2008
@@ -70,6 +70,7 @@
   SlAttributeToStringFunc to_string);
 
 gchar *sl_attribute_to_string (const gchar *attrname, gpointer value);
+void sl_attribute_cleanup (const gchar *attrname, gpointer value);
 
 gpointer sl_attribute_mapper_write (const gchar *attrname, gpointer value);
 gpointer sl_attribute_mapper_read (const gchar *attrname, gpointer value);
@@ -81,8 +82,12 @@
                                        gchar *string);
 gchar *sl_attribute_writer_gint (const gchar *attrname, GValue *value);
 GValue *sl_attribute_reader_gint (const gchar *attrname, gchar *value);
+gchar *sl_attribute_writer_glist (const gchar *attrname, GList *list);
+GList *sl_attribute_reader_glist (const gchar *attrname, gchar *string);
 gchar *sl_attribute_writer_gdate (const gchar *attrname, GDate *date);
 GDate *sl_attribute_reader_gdate (const gchar *attrname, gchar *string);
+SlName *sl_attribute_reader_slname (const gchar *attrname, gchar *string);
+gchar *sl_attribute_writer_slname (const gchar *attrname, SlName *name);
 gchar *sl_attribute_writer_sladdress (const gchar *attrname,
                                       SlAddress *address);
 SlAddress *sl_attribute_reader_sladdress (const gchar *attrname, gchar *string);
@@ -91,7 +96,9 @@
 gchar *sl_attribute_string_to_string (const gchar *attrname, gchar *string);
 gchar *sl_attribute_bytes_to_string (const gchar *attrname, GByteArray *bytes);
 gchar *sl_attribute_gint_to_string (const gchar *attrname, GValue *value);
+gchar *sl_attribute_glist_to_string (const gchar *attrname, GList *list);
 gchar *sl_attribute_gdate_to_string (const gchar *attrname, GDate *date);
+gchar *sl_attribute_slname_to_string (const gchar *attrname, SlName *name);
 gchar *sl_attribute_sladdress_to_string (const gchar *attrname,
                                          SlAddress *address);
 



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