[libsocialweb] cache: updates for SwContact and SwItem



commit cbf996bd72f1cc348ddfc94244ccb3f0bc4e64e5
Author: Alban Crequy <alban crequy collabora co uk>
Date:   Fri Mar 18 17:56:32 2011 +0000

    cache: updates for SwContact and SwItem

 libsocialweb/sw-cache.c                |  114 +++++++++++++++-----------------
 libsocialweb/sw-cache.h                |    5 +-
 libsocialweb/sw-contact.c              |   42 +++++++++++-
 libsocialweb/sw-item.c                 |   58 ++++++++++++++++-
 libsocialweb/sw-item.h                 |    2 +-
 services/facebook/facebook-item-view.c |    3 +-
 services/flickr/flickr-item-view.c     |    3 +-
 services/lastfm/lastfm-item-view.c     |    3 +-
 services/plurk/plurk-item-view.c       |    3 +-
 services/sina/sina-item-view.c         |    3 +-
 services/twitter/twitter-item-view.c   |    3 +-
 services/vimeo/vimeo-item-view.c       |    3 +-
 services/youtube/youtube-item-view.c   |    3 +-
 13 files changed, 172 insertions(+), 73 deletions(-)
---
diff --git a/libsocialweb/sw-cache.c b/libsocialweb/sw-cache.c
index db99133..8051e6c 100644
--- a/libsocialweb/sw-cache.c
+++ b/libsocialweb/sw-cache.c
@@ -1,6 +1,7 @@
 /*
  * libsocialweb - social data store
  * Copyright (C) 2008 - 2009 Intel Corporation.
+ * Copyright (C) 2011 Collabora Ltd.
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms and conditions of the GNU Lesser General Public License,
@@ -20,7 +21,9 @@
 #include <glib/gstdio.h>
 
 #include "sw-cache.h"
+#include "sw-cacheable.h"
 #include "sw-item.h"
+#include "sw-contact.h"
 #include "sw-utils.h"
 
 /*
@@ -75,7 +78,7 @@ get_cache_filename (SwService   *service,
 /*
  * Make an absolute path relative, for when saving it to the cache.
  */
-static char *
+char *
 make_relative_path (const char *key, const char *value)
 {
   if (g_str_equal (key, "authoricon") || g_str_equal (key, "thumbnail")) {
@@ -102,43 +105,15 @@ make_absolute_path (const char *key, const char *value)
 }
 
 /*
- * Create a new group in the keyfile based on the SwItem.
+ * Create a new group in the keyfile based on the SwCacheable.
  */
 static void
 set_keyfile_from_item (gpointer data, gpointer user_data)
 {
-  SwItem *item = data;
+  SwCacheable *cacheable = data;
   GKeyFile *keys = user_data;
-  const char *group, *key, *value;
-  GHashTableIter iter;
-
-  group = sw_item_get (item, "id");
-  if (group == NULL)
-    return;
-
-  /* Skip items that are not ready. Their properties will not be intact */
-  if (!sw_item_get_ready (item))
-    return;
-
-  /* Set a magic field saying that this item is cached */
-  g_key_file_set_string (keys, group, "cached", "1");
-
-  g_hash_table_iter_init (&iter, sw_item_peek_hash (item));
-  while (g_hash_table_iter_next (&iter, (gpointer)&key, (gpointer)&value)) {
-    char *new_value;
-    /*
-     * We make relative paths when saving so that the cache files are portable
-     * between users.  This normally doesn't happen but it's useful and the
-     * preloaded cache depends on this.
-     */
-    new_value = make_relative_path (key, value);
-    if (new_value) {
-      g_key_file_set_string (keys, group, key, new_value);
-      g_free (new_value);
-    } else {
-      g_key_file_set_string (keys, group, key, value);
-    }
-  }
+
+  sw_cacheable_save_into_cache (cacheable, keys);
 }
 
 /**
@@ -192,50 +167,68 @@ sw_cache_save (SwService   *service,
  * From @keyfile load the items in @group and create a new #Switem for
  * @service.
  */
-static SwItem *
+static SwCacheable *
 load_item_from_keyfile (SwService  *service,
                         GKeyFile   *keyfile,
                         const char *group)
 {
-  SwItem *item = NULL;
+  SwCacheable *cacheable = NULL;
   char **keys;
   gsize i, count;
 
   keys = g_key_file_get_keys (keyfile, group, &count, NULL);
   if (keys) {
-    item = sw_item_new ();
-    sw_item_set_service (item, service);
-    for (i = 0; i < count; i++) {
-      char *value, *new_value;
-
-      value = g_key_file_get_string (keyfile, group, keys[i], NULL);
-      /*
-       * Make the cached relative paths absolute so that the client doesn't have
-       * to know any internal details.
-       */
-      new_value = make_absolute_path (keys[i], value);
-
-      if (new_value) {
-        sw_item_take (item, keys[i], new_value);
-        g_free (value);
-      } else {
-        sw_item_take (item, keys[i], value);
+    const gchar *type = g_key_file_get_string (keyfile, group, "type",
+        NULL);
+
+    if (!type || g_str_equal (type, "item")) {
+      cacheable = SW_CACHEABLE (sw_item_new ());
+      sw_item_set_service (SW_ITEM (cacheable), service);
+      for (i = 0; i < count; i++) {
+        char *value, *new_value;
+
+        value = g_key_file_get_string (keyfile, group, keys[i], NULL);
+        /*
+         * Make the cached relative paths absolute so that the client doesn't have
+         * to know any internal details.
+         */
+        new_value = make_absolute_path (keys[i], value);
+
+        if (new_value) {
+          sw_item_take (SW_ITEM (cacheable), keys[i], new_value);
+          g_free (value);
+        } else {
+          sw_item_take (SW_ITEM (cacheable), keys[i], value);
+        }
+      }
+    } else if (g_str_equal (type, "contact")) {
+      cacheable = SW_CACHEABLE (sw_contact_new ());
+      sw_contact_set_service (SW_CONTACT (cacheable), service);
+      for (i = 0; i < count; i++) {
+        char **str_array;
+        int j;
+
+        str_array = g_key_file_get_string_list (keyfile, group, keys[i], NULL,
+            NULL);
+        for (j = 0 ; str_array && str_array[j] ; j++) {
+          sw_contact_put (SW_CONTACT (cacheable), keys[i], str_array[j]);
+        }
+        g_strfreev (str_array);
       }
     }
   }
 
   g_strfreev (keys);
 
-  if (sw_service_is_uid_banned (service,
-                                sw_item_get (item,
-                                             "id")))
+  if (cacheable && sw_service_is_uid_banned (service,
+        sw_cacheable_get_id (cacheable)))
   {
-    g_object_unref (item);
+    g_object_unref (cacheable);
 
     return NULL;
   }
 
-  return item;
+  return cacheable;
 }
 
 /**
@@ -251,7 +244,8 @@ load_item_from_keyfile (SwService  *service,
 SwSet *
 sw_cache_load (SwService   *service,
                const gchar *query,
-               GHashTable  *params)
+               GHashTable  *params,
+               SwSet* (*set_constr)())
 {
   char *filename;
   GKeyFile *keys;
@@ -273,10 +267,10 @@ sw_cache_load (SwService   *service,
     groups = g_key_file_get_groups (keys, &count);
 
     if (count) {
-      set = sw_item_set_new ();
+      set = set_constr ();
 
       for (i = 0; i < count; i++) {
-        SwItem *item;
+        SwCacheable *item;
 
         /* May be null if it's banned */
         item = load_item_from_keyfile (service, keys, groups[i]);
diff --git a/libsocialweb/sw-cache.h b/libsocialweb/sw-cache.h
index 83c24d3..fba7e2e 100644
--- a/libsocialweb/sw-cache.h
+++ b/libsocialweb/sw-cache.h
@@ -31,7 +31,8 @@ void sw_cache_save (SwService   *service,
 
 SwSet *sw_cache_load (SwService   *service,
                       const gchar *query,
-                      GHashTable  *params);
+                      GHashTable  *params,
+                      SwSet* (*set_constr)());
 
 void sw_cache_drop (SwService   *service,
                     const gchar *query,
@@ -39,6 +40,8 @@ void sw_cache_drop (SwService   *service,
 
 void sw_cache_drop_all (SwService *service);
 
+char *make_relative_path (const char *key, const char *value);
+
 G_END_DECLS
 
 #endif /* _SW_CACHE */
diff --git a/libsocialweb/sw-contact.c b/libsocialweb/sw-contact.c
index 061de25..993aa05 100644
--- a/libsocialweb/sw-contact.c
+++ b/libsocialweb/sw-contact.c
@@ -21,9 +21,13 @@
 #include <libsocialweb/sw-utils.h>
 #include <libsocialweb/sw-web.h>
 #include "sw-contact.h"
+#include "sw-cacheable.h"
 #include "sw-debug.h"
 
-G_DEFINE_TYPE (SwContact, sw_contact, G_TYPE_OBJECT)
+static void sw_contact_cacheable_init (SwCacheableInterface *iface,
+    gpointer user_data);
+G_DEFINE_TYPE_WITH_CODE (SwContact, sw_contact, G_TYPE_OBJECT,
+    G_IMPLEMENT_INTERFACE (SW_TYPE_CACHEABLE, sw_contact_cacheable_init))
 
 #define GET_PRIVATE(o) \
   (G_TYPE_INSTANCE_GET_PRIVATE ((o), SW_TYPE_CONTACT, SwContactPrivate))
@@ -491,3 +495,39 @@ sw_contact_equal (SwContact *a,
   return TRUE;
 }
 
+static const gchar *
+sw_contact_get_id (SwCacheable *cacheable)
+{
+  SwContact *self = SW_CONTACT (cacheable);
+  return sw_contact_get (self, "id");
+}
+
+static void
+sw_contact_save_into_cache (SwCacheable *cacheable, GKeyFile *keys,
+                            const gchar *group)
+{
+  SwContact *contact = SW_CONTACT (cacheable);
+  const char *key;
+  const gpointer value;
+  GHashTableIter iter;
+
+  /* Set a magic field saying that this contact is cached */
+  g_key_file_set_string (keys, group, "cached", "1");
+  g_key_file_set_string (keys, group, "type", "contact");
+
+  g_hash_table_iter_init (&iter, sw_contact_peek_hash (contact));
+  while (g_hash_table_iter_next (&iter, (gpointer)&key, &value)) {
+    g_key_file_set_string_list (keys, group, key, value,
+        g_strv_length (value));
+  }
+
+}
+
+static void
+sw_contact_cacheable_init (SwCacheableInterface *iface,
+                           gpointer user_data)
+{
+  iface->get_id = sw_contact_get_id;
+  iface->is_ready = sw_contact_get_ready;
+  iface->save_into_cache = sw_contact_save_into_cache;
+}
diff --git a/libsocialweb/sw-item.c b/libsocialweb/sw-item.c
index 8d25a79..97764c3 100644
--- a/libsocialweb/sw-item.c
+++ b/libsocialweb/sw-item.c
@@ -1,6 +1,7 @@
 /*
  * libsocialweb - social data store
  * Copyright (C) 2008 - 2009 Intel Corporation.
+ * Copyright (C) 2011 Collabora Ltd.
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms and conditions of the GNU Lesser General Public License,
@@ -16,12 +17,18 @@
  * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+#include <string.h>
 #include <libsocialweb/sw-utils.h>
 #include <libsocialweb/sw-web.h>
 #include "sw-item.h"
+#include "sw-cache.h"
+#include "sw-cacheable.h"
 #include "sw-debug.h"
 
-G_DEFINE_TYPE (SwItem, sw_item, G_TYPE_OBJECT)
+static void sw_item_cacheable_init (SwCacheableInterface *iface,
+    gpointer user_data);
+G_DEFINE_TYPE_WITH_CODE (SwItem, sw_item, G_TYPE_OBJECT,
+    G_IMPLEMENT_INTERFACE (SW_TYPE_CACHEABLE, sw_item_cacheable_init))
 
 #define GET_PRIVATE(o) \
   (G_TYPE_INSTANCE_GET_PRIVATE ((o), SW_TYPE_ITEM, SwItemPrivate))
@@ -178,7 +185,7 @@ sw_item_take (SwItem *item, const char *key, char *value)
 }
 
 const char *
-sw_item_get (SwItem *item, const char *key)
+sw_item_get (const SwItem *item, const char *key)
 {
   g_return_val_if_fail (SW_IS_ITEM (item), NULL);
   g_return_val_if_fail (key, NULL);
@@ -458,3 +465,50 @@ sw_item_equal (SwItem *a,
   return TRUE;
 }
 
+static const gchar *
+sw_item_get_id (SwCacheable *cacheable)
+{
+  SwItem *self = SW_ITEM (cacheable);
+  return sw_item_get (self, "id");
+}
+
+static void
+sw_item_save_into_cache (SwCacheable *cacheable, GKeyFile *keys,
+                         const gchar *group)
+{
+  SwItem *item = SW_ITEM (cacheable);
+  const char *key;
+  const gpointer value;
+  GHashTableIter iter;
+
+  /* Set a magic field saying that this item is cached */
+  g_key_file_set_string (keys, group, "cached", "1");
+  g_key_file_set_string (keys, group, "type", "item");
+
+  g_hash_table_iter_init (&iter, sw_item_peek_hash (item));
+  while (g_hash_table_iter_next (&iter, (gpointer)&key, &value)) {
+    char *new_value;
+    /*
+     * We make relative paths when saving so that the cache files are portable
+     * between users.  This normally doesn't happen but it's useful and the
+     * preloaded cache depends on this.
+     */
+    new_value = make_relative_path (key, value);
+    if (new_value) {
+      g_key_file_set_string (keys, group, key, new_value);
+      g_free (new_value);
+    } else {
+      g_key_file_set_string (keys, group, key, value);
+    }
+  }
+
+}
+
+static void
+sw_item_cacheable_init (SwCacheableInterface *iface,
+                           gpointer user_data)
+{
+  iface->get_id = sw_item_get_id;
+  iface->is_ready = sw_item_get_ready;
+  iface->save_into_cache = sw_item_save_into_cache;
+}
diff --git a/libsocialweb/sw-item.h b/libsocialweb/sw-item.h
index 434ff22..e3eccbb 100644
--- a/libsocialweb/sw-item.h
+++ b/libsocialweb/sw-item.h
@@ -76,7 +76,7 @@ void sw_item_request_image_fetch (SwItem      *item,
                                   const gchar *key,
                                   const gchar *url);
 
-const char * sw_item_get (SwItem *item, const char *key);
+const char * sw_item_get (const SwItem *item, const char *key);
 
 int sw_item_compare_date_older (SwItem *a, SwItem *b);
 
diff --git a/services/facebook/facebook-item-view.c b/services/facebook/facebook-item-view.c
index bd47354..1036caf 100644
--- a/services/facebook/facebook-item-view.c
+++ b/services/facebook/facebook-item-view.c
@@ -402,7 +402,8 @@ load_from_cache (SwItemView *self)
 
   set = sw_cache_load (sw_item_view_get_service (self),
                        priv->query,
-                       priv->params);
+                       priv->params,
+                       sw_item_set_new);
 
   if (set != NULL)
     {
diff --git a/services/flickr/flickr-item-view.c b/services/flickr/flickr-item-view.c
index b26e083..71a02cc 100644
--- a/services/flickr/flickr-item-view.c
+++ b/services/flickr/flickr-item-view.c
@@ -437,7 +437,8 @@ _load_from_cache (SwFlickrItemView *item_view)
 
   set = sw_cache_load (sw_item_view_get_service (SW_ITEM_VIEW (item_view)),
                        priv->query,
-                       priv->params);
+                       priv->params,
+                       sw_item_set_new);
 
   if (set)
   {
diff --git a/services/lastfm/lastfm-item-view.c b/services/lastfm/lastfm-item-view.c
index 18faab6..1195c08 100644
--- a/services/lastfm/lastfm-item-view.c
+++ b/services/lastfm/lastfm-item-view.c
@@ -583,7 +583,8 @@ _load_from_cache (SwLastfmItemView *item_view)
 
   set = sw_cache_load (sw_item_view_get_service (SW_ITEM_VIEW (item_view)),
                        priv->query,
-                       priv->params);
+                       priv->params,
+                       sw_item_set_new);
 
   if (set)
   {
diff --git a/services/plurk/plurk-item-view.c b/services/plurk/plurk-item-view.c
index 36b95bf..3150f0d 100644
--- a/services/plurk/plurk-item-view.c
+++ b/services/plurk/plurk-item-view.c
@@ -448,7 +448,8 @@ _load_from_cache (SwPlurkItemView *item_view)
 
   set = sw_cache_load (sw_item_view_get_service (SW_ITEM_VIEW (item_view)),
                        priv->query,
-                       priv->params);
+                       priv->params,
+                       sw_item_set_new);
 
   if (set)
   {
diff --git a/services/sina/sina-item-view.c b/services/sina/sina-item-view.c
index 2bc3365..29271a6 100644
--- a/services/sina/sina-item-view.c
+++ b/services/sina/sina-item-view.c
@@ -415,7 +415,8 @@ _load_from_cache (SwSinaItemView *item_view)
 
   set = sw_cache_load (sw_item_view_get_service (SW_ITEM_VIEW (item_view)),
                        priv->query,
-                       priv->params);
+                       priv->params,
+                       sw_item_set_new);
 
   if (set)
   {
diff --git a/services/twitter/twitter-item-view.c b/services/twitter/twitter-item-view.c
index 004d1a0..507102b 100644
--- a/services/twitter/twitter-item-view.c
+++ b/services/twitter/twitter-item-view.c
@@ -548,7 +548,8 @@ _load_from_cache (SwTwitterItemView *item_view)
 
   set = sw_cache_load (sw_item_view_get_service (SW_ITEM_VIEW (item_view)),
                        priv->query,
-                       priv->params);
+                       priv->params,
+                       sw_item_set_new);
 
   if (set)
   {
diff --git a/services/vimeo/vimeo-item-view.c b/services/vimeo/vimeo-item-view.c
index ca8c550..40c12a3 100644
--- a/services/vimeo/vimeo-item-view.c
+++ b/services/vimeo/vimeo-item-view.c
@@ -344,7 +344,8 @@ _load_from_cache (SwVimeoItemView *item_view)
 
   set = sw_cache_load (sw_item_view_get_service (SW_ITEM_VIEW (item_view)),
                        priv->query,
-                       priv->params);
+                       priv->params,
+                       sw_item_set_new);
 
   if (set)
   {
diff --git a/services/youtube/youtube-item-view.c b/services/youtube/youtube-item-view.c
index ebd90de..86f5645 100644
--- a/services/youtube/youtube-item-view.c
+++ b/services/youtube/youtube-item-view.c
@@ -463,7 +463,8 @@ _load_from_cache (SwYoutubeItemView *item_view)
 
   set = sw_cache_load (sw_item_view_get_service (SW_ITEM_VIEW (item_view)),
                        priv->query,
-                       priv->params);
+                       priv->params,
+                       sw_item_set_new);
 
   if (set)
   {



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