[libsocialweb] Add Sina service



commit 40f620b3331d485baf5f204ad57b4b93d74596a2
Author: Gary Ching-Pang Lin <chingpang gmail com>
Date:   Thu Jan 13 17:06:43 2011 +0800

    Add Sina service

 configure.ac                   |    2 +
 services/Makefile.am           |    4 +
 services/sina/Makefile.am      |   23 ++
 services/sina/module.c         |   35 +++
 services/sina/sina-item-view.c |  584 ++++++++++++++++++++++++++++++++++++++++
 services/sina/sina-item-view.h |   60 ++++
 services/sina/sina.c           |  540 +++++++++++++++++++++++++++++++++++++
 services/sina/sina.h           |   61 +++++
 services/sina/sina.keys.in     |    5 +
 services/sina/sina.png         |  Bin 0 -> 2816 bytes
 10 files changed, 1314 insertions(+), 0 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 567912c..0d07b24 100644
--- a/configure.ac
+++ b/configure.ac
@@ -133,6 +133,7 @@ SOCIALWEB_ENABLE_SERVICE(SmugMug, smugmug, SMUGMUG)
 SOCIALWEB_ENABLE_SERVICE(Twitter, twitter, TWITTER)
 SOCIALWEB_ENABLE_SERVICE(Vimeo, vimeo, VIMEO)
 SOCIALWEB_ENABLE_SERVICE(Youtube, youtube, YOUTUBE)
+SOCIALWEB_ENABLE_SERVICE(Sina, sina, SINA)
 
 servicesdir='${libdir}'/libsocialweb/services
 AC_SUBST(servicesdir)
@@ -192,6 +193,7 @@ AC_OUTPUT([
         services/twitter/Makefile
         services/vimeo/Makefile
         services/youtube/Makefile
+        services/sina/Makefile
         tests/Makefile
         tools/Makefile
         examples/Makefile
diff --git a/services/Makefile.am b/services/Makefile.am
index 9471ae1..acfd04b 100644
--- a/services/Makefile.am
+++ b/services/Makefile.am
@@ -16,6 +16,10 @@ if WITH_PLURK
 SUBDIRS += plurk
 endif
 
+if WITH_SINA
+SUBDIRS += sina
+endif
+
 if WITH_SMUGMUG
 SUBDIRS += smugmug
 endif
diff --git a/services/sina/Makefile.am b/services/sina/Makefile.am
new file mode 100644
index 0000000..f4d2701
--- /dev/null
+++ b/services/sina/Makefile.am
@@ -0,0 +1,23 @@
+services_LTLIBRARIES = libsina.la
+libsina_la_SOURCES = module.c \
+		     sina.c \
+		     sina.h \
+		     sina-item-view.h \
+		     sina-item-view.c
+libsina_la_CFLAGS = -I$(top_srcdir) \
+		    $(REST_CFLAGS) \
+		    $(DBUS_GLIB_CFLAGS) \
+		    -DG_LOG_DOMAIN=\"Sina\"
+libsina_la_LIBADD = $(top_builddir)/libsocialweb/libsocialweb.la \
+		    $(top_builddir)/libsocialweb-keystore/libsocialweb-keystore.la \
+		    $(top_builddir)/libsocialweb-keyfob/libsocialweb-keyfob.la \
+		    $(REST_LIBS) \
+		    $(DBUS_GLIB_LIBS)
+libsina_la_LDFLAGS = -module -avoid-version
+
+dist_servicesdata_DATA = sina.png
+
+servicesdata_DATA = sina.keys
+CLEANFILES = sina.keys
+EXTRA_DIST = sina.keys.in
+ INTLTOOL_SOCIALWEB_KEYS@
diff --git a/services/sina/module.c b/services/sina/module.c
new file mode 100644
index 0000000..3456f06
--- /dev/null
+++ b/services/sina/module.c
@@ -0,0 +1,35 @@
+/*
+ * libsocialweb Sina service support
+ *
+ * Copyright (C) 2010 Novell, Inc.
+ *
+ * Author: Gary Ching-Pang Lin <glin novell com>
+ *
+ * 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,
+ * version 2.1, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <libsocialweb/sw-module.h>
+#include "sina.h"
+
+const gchar *
+sw_module_get_name (void)
+{
+  return "sina";
+}
+
+const GType
+sw_module_get_type (void)
+{
+  return SW_TYPE_SERVICE_SINA;
+}
diff --git a/services/sina/sina-item-view.c b/services/sina/sina-item-view.c
new file mode 100644
index 0000000..2bc3365
--- /dev/null
+++ b/services/sina/sina-item-view.c
@@ -0,0 +1,584 @@
+/*
+ * libsocialweb Sina service support
+ *
+ * Copyright (C) 2010 Novell, Inc.
+ *
+ * Author: Gary Ching-Pang Lin <glin novell com>
+ *
+ * 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,
+ * version 2.1, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <config.h>
+
+#include <time.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <rest/rest-proxy.h>
+#include <rest/rest-xml-parser.h>
+#include <libsoup/soup.h>
+
+#include <libsocialweb/sw-utils.h>
+#include <libsocialweb/sw-debug.h>
+#include <libsocialweb/sw-item.h>
+#include <libsocialweb/sw-cache.h>
+
+#include "sina-item-view.h"
+
+G_DEFINE_TYPE (SwSinaItemView,
+               sw_sina_item_view,
+               SW_TYPE_ITEM_VIEW)
+
+#define GET_PRIVATE(o) \
+  (G_TYPE_INSTANCE_GET_PRIVATE ((o), SW_TYPE_SINA_ITEM_VIEW, SwSinaItemViewPrivate))
+
+typedef struct _SwSinaItemViewPrivate SwSinaItemViewPrivate;
+
+struct _SwSinaItemViewPrivate {
+  RestProxy *proxy;
+  guint timeout_id;
+  GHashTable *params;
+  gchar *query;
+};
+
+enum
+{
+  PROP_0,
+  PROP_PROXY,
+  PROP_PARAMS,
+  PROP_QUERY
+};
+
+#define UPDATE_TIMEOUT 5 * 60
+
+static void _service_item_hidden_cb (SwService   *service,
+                                     const gchar *uid,
+                                     SwItemView  *item_view);
+static void _service_user_changed_cb (SwService  *service,
+                                      SwItemView *item_view);
+static void _service_capabilities_changed_cb (SwService    *service,
+                                              const gchar **caps,
+                                              SwItemView   *item_view);
+
+static void
+sw_sina_item_view_get_property (GObject    *object,
+                                guint       property_id,
+                                GValue     *value,
+                                GParamSpec *pspec)
+{
+  SwSinaItemViewPrivate *priv = GET_PRIVATE (object);
+
+  switch (property_id) {
+    case PROP_PROXY:
+      g_value_set_object (value, priv->proxy);
+      break;
+    case PROP_PARAMS:
+      g_value_set_boxed (value, priv->params);
+      break;
+    case PROP_QUERY:
+      g_value_set_string (value, priv->query);
+      break;
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+  }
+}
+
+static void
+sw_sina_item_view_set_property (GObject      *object,
+                                guint         property_id,
+                                const GValue *value,
+                                GParamSpec   *pspec)
+{
+  SwSinaItemViewPrivate *priv = GET_PRIVATE (object);
+
+  switch (property_id) {
+    case PROP_PROXY:
+      if (priv->proxy)
+      {
+        g_object_unref (priv->proxy);
+      }
+      priv->proxy = g_value_dup_object (value);
+      break;
+    case PROP_PARAMS:
+      priv->params = g_value_dup_boxed (value);
+      break;
+    case PROP_QUERY:
+      priv->query = g_value_dup_string (value);
+      break;
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+  }
+}
+
+static void
+sw_sina_item_view_dispose (GObject *object)
+{
+  SwItemView *item_view = SW_ITEM_VIEW (object);
+  SwSinaItemViewPrivate *priv = GET_PRIVATE (object);
+
+  if (priv->proxy) {
+    g_object_unref (priv->proxy);
+    priv->proxy = NULL;
+  }
+
+  if (priv->timeout_id) {
+    g_source_remove (priv->timeout_id);
+    priv->timeout_id = 0;
+  }
+
+  g_signal_handlers_disconnect_by_func (sw_item_view_get_service (item_view),
+                                        _service_item_hidden_cb,
+                                        item_view);
+  g_signal_handlers_disconnect_by_func (sw_item_view_get_service (item_view),
+                                        _service_user_changed_cb,
+                                        item_view);
+  g_signal_handlers_disconnect_by_func (sw_item_view_get_service (item_view),
+                                        _service_capabilities_changed_cb,
+                                        item_view);
+
+  G_OBJECT_CLASS (sw_sina_item_view_parent_class)->dispose (object);
+}
+
+static void
+sw_sina_item_view_finalize (GObject *object)
+{
+  SwSinaItemViewPrivate *priv = GET_PRIVATE (object);
+
+  /* free private variables */
+  g_free (priv->query);
+  g_hash_table_unref (priv->params);
+
+  G_OBJECT_CLASS (sw_sina_item_view_parent_class)->finalize (object);
+}
+
+static RestXmlNode *
+xml_node_from_call (RestProxyCall *call,
+                    const char    *name)
+{
+  static RestXmlParser *parser = NULL;
+  RestXmlNode *root;
+
+  if (call == NULL)
+    return NULL;
+
+  if (parser == NULL)
+    parser = rest_xml_parser_new ();
+
+  if (!SOUP_STATUS_IS_SUCCESSFUL (rest_proxy_call_get_status_code (call))) {
+    g_message ("Error from %s: %s (%d)",
+               name,
+               rest_proxy_call_get_status_message (call),
+               rest_proxy_call_get_status_code (call));
+    return NULL;
+  }
+
+  root = rest_xml_parser_parse_from_data (parser,
+                                          rest_proxy_call_get_payload (call),
+                                          rest_proxy_call_get_payload_length (call));
+
+  if (root == NULL) {
+    g_message ("Error from %s: %s",
+               name,
+               rest_proxy_call_get_payload (call));
+    return NULL;
+  }
+
+  return root;
+}
+
+/*
+ * For a given parent @node, get the child node called @name and return a copy
+ * of the content, or NULL. If the content is the empty string, NULL is
+ * returned.
+ */
+static char *
+xml_get_child_node_value (RestXmlNode *node,
+                          const char  *name)
+{
+  RestXmlNode *subnode;
+
+  g_assert (node);
+  g_assert (name);
+
+  subnode = rest_xml_node_find (node, name);
+  if (!subnode)
+    return NULL;
+
+  if (subnode->content && subnode->content[0])
+    return g_strdup (subnode->content);
+  else
+    return NULL;
+}
+
+static char*
+make_date (const char *s)
+{
+  struct tm tm = {0};
+  strptime (s, "%A %h %d %T %z %Y", &tm);
+  return sw_time_t_to_string (mktime (&tm));
+}
+
+static void
+_populate_set_from_node (SwService   *service,
+                         SwSet       *set,
+                         RestXmlNode *root)
+{
+  RestXmlNode *node;
+
+  if (!root)
+    return;
+
+  node = rest_xml_node_find (root, "status");
+  while (node) {
+    SwItem *item;
+    RestXmlNode *user;
+    char *id, *date, *uid, *url;
+
+    item = sw_item_new ();
+    sw_item_set_service (item, service);
+
+    user = rest_xml_node_find (node, "user");
+
+    id = g_strconcat ("sina-",
+                      xml_get_child_node_value (node, "id"),
+                      NULL);
+    sw_item_take (item, "id", id);
+
+    date = xml_get_child_node_value (node, "created_at");
+    sw_item_take (item, "date", make_date (date));
+    g_free (date);
+
+    sw_item_take (item,
+                  "author",
+                  xml_get_child_node_value (user, "screen_name"));
+
+    url = xml_get_child_node_value (user, "profile_image_url");
+    sw_item_request_image_fetch (item, FALSE, "authoricon", url);
+    g_free (url);
+
+    sw_item_take (item,
+                  "content",
+                  xml_get_child_node_value (node, "text"));
+
+    uid = xml_get_child_node_value (user, "id");
+    url = g_strconcat ("http://t.sina.com.cn/";, uid, NULL);
+    sw_item_take (item, "url", url);
+    g_free (uid);
+
+    if (!sw_service_is_uid_banned (service, sw_item_get (item, "id"))) {
+      sw_set_add (set, G_OBJECT (item));
+    }
+    g_object_unref (item);
+
+    /* Next node */
+    node = node->next;
+  }
+}
+
+static void _get_user_status_updates (SwSinaItemView *item_view, SwSet *set);
+
+static void
+_got_user_status_cb (RestProxyCall *call,
+                     const GError  *error,
+                     GObject       *weak_object,
+                     gpointer       userdata)
+{
+  SwSinaItemView *item_view = SW_SINA_ITEM_VIEW (weak_object);
+  SwSinaItemViewPrivate *priv = GET_PRIVATE (item_view);
+  SwSet *set = (SwSet *)userdata;
+  RestXmlNode *root;
+  SwService *service;
+
+  if (error) {
+    g_message ("Error: %s", error->message);
+    return;
+  }
+
+  service = sw_item_view_get_service (SW_ITEM_VIEW (item_view));
+
+  root = xml_node_from_call (call, "Sina");
+  _populate_set_from_node (service, set, root);
+  rest_xml_node_unref (root);
+
+  g_object_unref (call);
+
+  sw_item_view_set_from_set (SW_ITEM_VIEW (item_view), set);
+
+  /* Save the results of this set to the cache */
+  sw_cache_save (service,
+                 priv->query,
+                 priv->params,
+                 set);
+
+  sw_set_unref (set);
+}
+
+static void
+_got_friends_status_cb (RestProxyCall *call,
+                        const GError  *error,
+                        GObject       *weak_object,
+                        gpointer       userdata)
+{
+  SwSinaItemView *item_view = SW_SINA_ITEM_VIEW (weak_object);
+  SwSet *set = (SwSet *)userdata;
+  RestXmlNode *root;
+  SwService *service;
+
+  if (error) {
+    g_message ("Error: %s", error->message);
+    return;
+  }
+
+  service = sw_item_view_get_service (SW_ITEM_VIEW (item_view));
+
+  root = xml_node_from_call (call, "sina");
+  _populate_set_from_node (service, set, root);
+  rest_xml_node_unref (root);
+
+  g_object_unref (call);
+
+  _get_user_status_updates (item_view, set);
+}
+
+static void
+_get_user_status_updates (SwSinaItemView *item_view,
+                          SwSet          *set)
+{
+  SwSinaItemViewPrivate *priv = GET_PRIVATE (item_view);
+  RestProxyCall *call;
+
+  call = rest_proxy_new_call (priv->proxy);
+  rest_proxy_call_set_function (call, "statuses/user_timeline.xml");
+  rest_proxy_call_add_params(call,
+                             "count", "10",
+                             NULL);
+  rest_proxy_call_async (call, _got_user_status_cb, (GObject*)item_view, set, NULL);
+}
+
+static void
+_get_friends_status_updates (SwSinaItemView *item_view,
+                             SwSet          *set)
+{
+  SwSinaItemViewPrivate *priv = GET_PRIVATE (item_view);
+  RestProxyCall *call;
+
+  call = rest_proxy_new_call (priv->proxy);
+  rest_proxy_call_set_function (call, "statuses/friends_timeline.xml");
+  rest_proxy_call_add_params(call,
+                             "count", "10",
+                             NULL);
+  rest_proxy_call_async (call, _got_friends_status_cb, (GObject*)item_view, set, NULL);
+}
+
+static void
+_get_status_updates (SwSinaItemView *item_view)
+{
+  SwSinaItemViewPrivate *priv = GET_PRIVATE (item_view);
+  SwSet *set;
+
+  set = sw_item_set_new ();
+
+  if (g_str_equal (priv->query, "own"))
+    _get_user_status_updates (item_view, set);
+  else if (g_str_equal (priv->query, "feed"))
+    _get_friends_status_updates (item_view, set);
+  else
+    g_error (G_STRLOC ": Unexpected query '%s'", priv->query);
+}
+
+static gboolean
+_update_timeout_cb (gpointer data)
+{
+  SwSinaItemView *item_view = SW_SINA_ITEM_VIEW (data);
+
+  _get_status_updates (item_view);
+
+  return TRUE;
+}
+
+static void
+_load_from_cache (SwSinaItemView *item_view)
+{
+  SwSinaItemViewPrivate *priv = GET_PRIVATE (item_view);
+  SwSet *set;
+
+  set = sw_cache_load (sw_item_view_get_service (SW_ITEM_VIEW (item_view)),
+                       priv->query,
+                       priv->params);
+
+  if (set)
+  {
+    sw_item_view_set_from_set (SW_ITEM_VIEW (item_view),
+                               set);
+    sw_set_unref (set);
+  }
+}
+
+static void
+sina_item_view_start (SwItemView *item_view)
+{
+  SwSinaItemViewPrivate *priv = GET_PRIVATE (item_view);
+
+  if (priv->timeout_id)
+  {
+    g_warning (G_STRLOC ": View already started.");
+  } else {
+    priv->timeout_id = g_timeout_add_seconds (UPDATE_TIMEOUT,
+                                              (GSourceFunc)_update_timeout_cb,
+                                              item_view);
+    _load_from_cache ((SwSinaItemView *)item_view);
+    _get_status_updates ((SwSinaItemView *)item_view);
+  }
+}
+
+static void
+sina_item_view_stop (SwItemView *item_view)
+{
+  SwSinaItemViewPrivate *priv = GET_PRIVATE (item_view);
+
+  if (!priv->timeout_id)
+  {
+    g_warning (G_STRLOC ": View not running");
+  } else {
+    g_source_remove (priv->timeout_id);
+    priv->timeout_id = 0;
+  }
+}
+
+static void
+sina_item_view_refresh (SwItemView *item_view)
+{
+  _get_status_updates ((SwSinaItemView *)item_view);
+}
+
+static void
+_service_item_hidden_cb (SwService   *service,
+                         const gchar *uid,
+                         SwItemView  *item_view)
+{
+  sw_item_view_remove_by_uid (item_view, uid);
+}
+
+static void
+_service_user_changed_cb (SwService  *service,
+                          SwItemView *item_view)
+{
+  SwSet *set;
+
+  /* We need to empty the set */
+  set = sw_item_set_new ();
+  sw_item_view_set_from_set (SW_ITEM_VIEW (item_view),
+                             set);
+  sw_set_unref (set);
+
+  /* And drop the cache */
+  sw_cache_drop_all (service);
+}
+
+static void
+_service_capabilities_changed_cb (SwService    *service,
+                                  const gchar **caps,
+                                  SwItemView   *item_view)
+{
+  SwSinaItemViewPrivate *priv = GET_PRIVATE ((SwSinaItemView*) item_view);
+
+  if (sw_service_has_cap (caps, CREDENTIALS_VALID))
+  {
+    sina_item_view_refresh (item_view);
+
+    if (!priv->timeout_id)
+    {
+      priv->timeout_id = g_timeout_add_seconds (UPDATE_TIMEOUT,
+                                                (GSourceFunc)_update_timeout_cb,
+                                                item_view);
+    }
+  } else {
+    if (priv->timeout_id)
+    {
+      g_source_remove (priv->timeout_id);
+      priv->timeout_id = 0;
+    }
+  }
+}
+
+static void
+sw_sina_item_view_constructed (GObject *object)
+{
+  SwItemView *item_view = SW_ITEM_VIEW (object);
+
+  g_signal_connect (sw_item_view_get_service (item_view),
+                    "item-hidden",
+                    (GCallback)_service_item_hidden_cb,
+                    item_view);
+
+  g_signal_connect (sw_item_view_get_service (item_view),
+                    "user-changed",
+                    (GCallback)_service_user_changed_cb,
+                    item_view);
+
+  g_signal_connect (sw_item_view_get_service (item_view),
+                    "capabilities-changed",
+                    (GCallback)_service_capabilities_changed_cb,
+                    item_view);
+
+  if (G_OBJECT_CLASS (sw_sina_item_view_parent_class)->constructed)
+    G_OBJECT_CLASS (sw_sina_item_view_parent_class)->constructed (object);
+}
+
+static void
+sw_sina_item_view_class_init (SwSinaItemViewClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  SwItemViewClass *item_view_class = SW_ITEM_VIEW_CLASS (klass);
+  GParamSpec *pspec;
+
+  g_type_class_add_private (klass, sizeof (SwSinaItemViewPrivate));
+
+  object_class->get_property = sw_sina_item_view_get_property;
+  object_class->set_property = sw_sina_item_view_set_property;
+  object_class->dispose = sw_sina_item_view_dispose;
+  object_class->finalize = sw_sina_item_view_finalize;
+  object_class->constructed = sw_sina_item_view_constructed;
+
+  item_view_class->start = sina_item_view_start;
+  item_view_class->stop = sina_item_view_stop;
+  item_view_class->refresh = sina_item_view_refresh;
+
+  pspec = g_param_spec_object ("proxy",
+                               "proxy",
+                               "proxy",
+                               REST_TYPE_PROXY,
+                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
+  g_object_class_install_property (object_class, PROP_PROXY, pspec);
+
+  pspec = g_param_spec_string ("query",
+                               "query",
+                               "query",
+                               NULL,
+                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
+  g_object_class_install_property (object_class, PROP_QUERY, pspec);
+
+  pspec = g_param_spec_boxed ("params",
+                              "params",
+                              "params",
+                              G_TYPE_HASH_TABLE,
+                              G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
+  g_object_class_install_property (object_class, PROP_PARAMS, pspec);
+}
+
+static void
+sw_sina_item_view_init (SwSinaItemView *self)
+{
+  /* Initialize private variables */
+}
diff --git a/services/sina/sina-item-view.h b/services/sina/sina-item-view.h
new file mode 100644
index 0000000..aa57009
--- /dev/null
+++ b/services/sina/sina-item-view.h
@@ -0,0 +1,60 @@
+/*
+ * libsocialweb Sina service support
+ *
+ * Copyright (C) 2010 Novell, Inc.
+ *
+ * Author: Gary Ching-Pang Lin <glin novell com>
+ *
+ * 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,
+ * version 2.1, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _SW_SINA_ITEM_VIEW
+#define _SW_SINA_ITEM_VIEW
+
+#include <glib-object.h>
+#include <libsocialweb/sw-item-view.h>
+
+G_BEGIN_DECLS
+
+#define SW_TYPE_SINA_ITEM_VIEW sw_sina_item_view_get_type()
+
+#define SW_SINA_ITEM_VIEW(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST ((obj), SW_TYPE_SINA_ITEM_VIEW, SwSinaItemView))
+
+#define SW_SINA_ITEM_VIEW_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST ((klass), SW_TYPE_SINA_ITEM_VIEW, SwSinaItemViewClass))
+
+#define SW_IS_SINA_ITEM_VIEW(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SW_TYPE_SINA_ITEM_VIEW))
+
+#define SW_IS_SINA_ITEM_VIEW_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_TYPE ((klass), SW_TYPE_SINA_ITEM_VIEW))
+
+#define SW_SINA_ITEM_VIEW_GET_CLASS(obj) \
+  (G_TYPE_INSTANCE_GET_CLASS ((obj), SW_TYPE_SINA_ITEM_VIEW, SwSinaItemViewClass))
+
+typedef struct {
+  SwItemView parent;
+} SwSinaItemView;
+
+typedef struct {
+  SwItemViewClass parent_class;
+} SwSinaItemViewClass;
+
+GType sw_sina_item_view_get_type (void);
+
+G_END_DECLS
+
+#endif /* _SW_SINA_ITEM_VIEW */
+
diff --git a/services/sina/sina.c b/services/sina/sina.c
new file mode 100644
index 0000000..3a93a82
--- /dev/null
+++ b/services/sina/sina.c
@@ -0,0 +1,540 @@
+/*
+ * libsocialweb Sina service support
+ *
+ * Copyright (C) 2010 Novell, Inc.
+ *
+ * Author: Gary Ching-Pang Lin <glin novell com>
+ *
+ * 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,
+ * version 2.1, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <config.h>
+
+#include <time.h>
+#include <stdlib.h>
+#include <string.h>
+#include <glib/gi18n.h>
+#include <dbus/dbus-glib-lowlevel.h>
+
+#include <libsocialweb/sw-item.h>
+#include <libsocialweb/sw-set.h>
+#include <libsocialweb/sw-online.h>
+#include <libsocialweb/sw-utils.h>
+#include <libsocialweb/sw-web.h>
+#include <libsocialweb/sw-debug.h>
+#include <libsocialweb/sw-client-monitor.h>
+#include <libsocialweb-keyfob/sw-keyfob.h>
+#include <libsocialweb-keystore/sw-keystore.h>
+
+#include <rest/oauth-proxy.h>
+#include <rest/oauth-proxy-call.h>
+#include <rest/rest-xml-parser.h>
+#include <libsoup/soup.h>
+
+#include <interfaces/sw-query-ginterface.h>
+#include <interfaces/sw-avatar-ginterface.h>
+#include <interfaces/sw-status-update-ginterface.h>
+
+#include "sina.h"
+#include "sina-item-view.h"
+
+static void initable_iface_init (gpointer g_iface, gpointer iface_data);
+static void query_iface_init (gpointer g_iface, gpointer iface_data);
+static void avatar_iface_init (gpointer g_iface, gpointer iface_data);
+static void status_update_iface_init (gpointer g_iface, gpointer iface_data);
+
+G_DEFINE_TYPE_WITH_CODE (SwServiceSina,
+                         sw_service_sina,
+                         SW_TYPE_SERVICE,
+                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
+                                                initable_iface_init)
+                         G_IMPLEMENT_INTERFACE (SW_TYPE_QUERY_IFACE,
+                                                query_iface_init)
+                         G_IMPLEMENT_INTERFACE (SW_TYPE_AVATAR_IFACE,
+                                                avatar_iface_init)
+                         G_IMPLEMENT_INTERFACE (SW_TYPE_STATUS_UPDATE_IFACE,
+                                                status_update_iface_init));
+
+#define GET_PRIVATE(o) \
+  (G_TYPE_INSTANCE_GET_PRIVATE ((o), SW_TYPE_SERVICE_SINA, SwServiceSinaPrivate))
+
+struct _SwServiceSinaPrivate {
+  gboolean inited;
+  RestProxy *proxy;
+  char *user_id;
+  char *image_url;
+};
+
+static void online_notify (gboolean online, gpointer user_data);
+static void credentials_updated (SwService *service);
+
+static gboolean
+account_is_configured ()
+{
+  RestProxy *proxy;
+  gboolean configured = FALSE;
+  const char *key = NULL, *secret = NULL;
+
+  sw_keystore_get_key_secret ("sina", &key, &secret);
+  proxy = oauth_proxy_new (key, secret, "http://api.t.sina.com.cn/";, FALSE);
+
+  configured = sw_keyfob_oauth_sync ((OAuthProxy *)proxy);
+
+  g_object_unref (proxy);
+
+  return configured;
+}
+
+static const char **
+get_static_caps (SwService *service)
+{
+  static const char * caps[] = {
+    CAN_VERIFY_CREDENTIALS,
+    HAS_UPDATE_STATUS_IFACE,
+    HAS_AVATAR_IFACE,
+    HAS_BANISHABLE_IFACE,
+    HAS_QUERY_IFACE,
+
+    /* deprecated */
+    CAN_UPDATE_STATUS,
+    CAN_REQUEST_AVATAR,
+    NULL
+  };
+
+  return caps;
+}
+
+static const char **
+get_dynamic_caps (SwService *service)
+{
+  SwServiceSinaPrivate *priv = SW_SERVICE_SINA (service)->priv;
+  gboolean configured = FALSE;
+  static const char *no_caps[] = { NULL };
+  static const char *configured_caps[] = {
+    IS_CONFIGURED,
+    NULL
+  };
+  static const char *full_caps[] = {
+    IS_CONFIGURED,
+    CREDENTIALS_VALID,
+    CAN_UPDATE_STATUS,
+    CAN_REQUEST_AVATAR,
+    NULL
+  };
+
+  if (priv->user_id)
+    return full_caps;
+
+  configured = account_is_configured ();
+
+  if (configured)
+    return configured_caps;
+  else
+    return no_caps;
+}
+
+static RestXmlNode *
+xml_node_from_call (RestProxyCall *call,
+                    const char    *name)
+{
+  static RestXmlParser *parser = NULL;
+  RestXmlNode *root;
+
+  if (call == NULL)
+    return NULL;
+
+  if (parser == NULL)
+    parser = rest_xml_parser_new ();
+
+  if (!SOUP_STATUS_IS_SUCCESSFUL (rest_proxy_call_get_status_code (call))) {
+    g_message ("Error from %s: %s (%d)",
+               name,
+               rest_proxy_call_get_status_message (call),
+               rest_proxy_call_get_status_code (call));
+    return NULL;
+  }
+
+  root = rest_xml_parser_parse_from_data (parser,
+                                          rest_proxy_call_get_payload (call),
+                                          rest_proxy_call_get_payload_length (call));
+
+  if (root == NULL) {
+    g_message ("Error from %s: %s",
+               name,
+               rest_proxy_call_get_payload (call));
+    return NULL;
+  }
+
+  return root;
+}
+
+/*
+ * For a given parent @node, get the child node called @name and return a copy
+ * of the content, or NULL. If the content is the empty string, NULL is
+ * returned.
+ */
+static char *
+xml_get_child_node_value (RestXmlNode *node,
+                          const char  *name)
+{
+  RestXmlNode *subnode;
+
+  g_assert (node);
+  g_assert (name);
+
+  subnode = rest_xml_node_find (node, name);
+  if (!subnode)
+    return NULL;
+
+  if (subnode->content && subnode->content[0])
+    return g_strdup (subnode->content);
+  else
+    return NULL;
+}
+
+static void got_tokens_cb (RestProxy *proxy, gboolean authorised, gpointer user_data);
+
+static void
+got_user_cb (RestProxyCall *call,
+             const GError  *error,
+             GObject       *weak_object,
+             gpointer       userdata)
+{
+  SwService *service = SW_SERVICE (weak_object);
+  SwServiceSina *sina = SW_SERVICE_SINA (service);
+  SwServiceSinaPrivate *priv = GET_PRIVATE (sina);
+
+  RestXmlNode *root;
+
+  if (error) {
+    g_message ("Error: %s", error->message);
+    return;
+  }
+
+  root = xml_node_from_call (call, "Sina");
+  if (!root)
+    return;
+
+  priv->user_id = xml_get_child_node_value (root, "id");
+  priv->image_url = xml_get_child_node_value (root, "profile_image_url");
+
+  rest_xml_node_unref (root);
+
+  sw_service_emit_capabilities_changed (service, get_dynamic_caps (service));
+}
+
+static void
+got_tokens_cb (RestProxy *proxy, gboolean authorised, gpointer user_data)
+{
+  SwServiceSina *sina = SW_SERVICE_SINA (user_data);
+  SwServiceSinaPrivate *priv = GET_PRIVATE (sina);
+  RestProxyCall *call;
+
+  if (authorised) {
+    call = rest_proxy_new_call (priv->proxy);
+    rest_proxy_call_set_function (call, "account/verify_credentials.xml");
+    rest_proxy_call_async (call, got_user_cb, (GObject*)sina, NULL, NULL);
+  }
+}
+
+static void
+online_notify (gboolean online, gpointer user_data)
+{
+  SwServiceSina *sina = SW_SERVICE_SINA (user_data);
+  SwServiceSinaPrivate *priv = GET_PRIVATE (sina);
+
+  if (online) {
+    sw_keyfob_oauth ((OAuthProxy *)priv->proxy, got_tokens_cb, sina);
+  } else {
+    g_free (priv->user_id);
+    priv->user_id = NULL;
+
+    g_free (priv->image_url);
+    priv->image_url = NULL;
+
+    sw_service_emit_capabilities_changed ((SwService *)sina,
+                                          get_dynamic_caps ((SwService *)sina));
+  }
+}
+
+static void
+refresh_credentials (SwServiceSina *sina)
+{
+  online_notify (FALSE, (SwService *)sina);
+  online_notify (TRUE, (SwService *)sina);
+}
+
+static void
+credentials_updated (SwService *service)
+{
+  refresh_credentials (SW_SERVICE_SINA (service));
+
+  sw_service_emit_user_changed (service);
+  sw_service_emit_capabilities_changed (service, get_dynamic_caps (service));
+}
+
+static const char *
+sw_service_sina_get_name (SwService *service)
+{
+  return "sina";
+}
+
+static void
+sw_service_sina_dispose (GObject *object)
+{
+  SwServiceSinaPrivate *priv = SW_SERVICE_SINA (object)->priv;
+
+  sw_online_remove_notify (online_notify, object);
+
+  /* unref private variables */
+  if (priv->proxy) {
+    g_object_unref (priv->proxy);
+    priv->proxy = NULL;
+  }
+
+  G_OBJECT_CLASS (sw_service_sina_parent_class)->dispose (object);
+}
+
+static void
+sw_service_sina_finalize (GObject *object)
+{
+  SwServiceSinaPrivate *priv = SW_SERVICE_SINA (object)->priv;
+
+  g_free (priv->user_id);
+  g_free (priv->image_url);
+
+  G_OBJECT_CLASS (sw_service_sina_parent_class)->finalize (object);
+}
+
+static void
+sw_service_sina_class_init (SwServiceSinaClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  SwServiceClass *service_class = SW_SERVICE_CLASS (klass);
+
+  g_type_class_add_private (klass, sizeof (SwServiceSinaPrivate));
+
+  object_class->dispose = sw_service_sina_dispose;
+  object_class->finalize = sw_service_sina_finalize;
+
+  service_class->get_name = sw_service_sina_get_name;
+  service_class->get_static_caps = get_static_caps;
+  service_class->get_dynamic_caps = get_dynamic_caps;
+  service_class->credentials_updated = credentials_updated;
+}
+
+static void
+sw_service_sina_init (SwServiceSina *self)
+{
+  self->priv = GET_PRIVATE (self);
+  self->priv->inited = FALSE;
+}
+
+/* Initable interface */
+
+static gboolean
+sw_service_sina_initable (GInitable    *initable,
+                          GCancellable *cancellable,
+                          GError      **error)
+{
+  /* Initialize the service and return TRUE if everything is OK.
+     Otherwise, return FALSE */
+  SwServiceSina *sina = SW_SERVICE_SINA (initable);
+  SwServiceSinaPrivate *priv = GET_PRIVATE (sina);
+  const char *key = NULL, *secret = NULL;
+
+  if (priv->inited)
+    return TRUE;
+
+  sw_keystore_get_key_secret ("sina", &key, &secret);
+  if (key == NULL || secret == NULL) {
+    g_set_error_literal (error,
+                         SW_SERVICE_ERROR,
+                         SW_SERVICE_ERROR_NO_KEYS,
+                         "No API key configured");
+    return FALSE;
+  }
+  priv->proxy = oauth_proxy_new (key, secret, "http://api.t.sina.com.cn/";, FALSE);
+
+  sw_online_add_notify (online_notify, sina);
+
+  refresh_credentials (sina);
+
+  priv->inited = TRUE;
+
+  return TRUE;
+}
+
+static void
+initable_iface_init (gpointer g_iface, gpointer iface_data)
+{
+  GInitableIface *klass = (GInitableIface *)g_iface;
+
+  klass->init = sw_service_sina_initable;
+}
+
+/* Query interface */
+
+static const gchar *valid_queries[] = {"feed",
+                                       "own"};
+
+static gboolean
+_check_query_validity (const gchar *query)
+{
+  gint i = 0;
+
+  for (i = 0; i < G_N_ELEMENTS(valid_queries); i++)
+  {
+    if (g_str_equal (query, valid_queries[i]))
+      return TRUE;
+  }
+
+  return FALSE;
+}
+
+static void
+_sina_query_open_view (SwQueryIface          *self,
+                       const gchar           *query,
+                       GHashTable            *params,
+                       DBusGMethodInvocation *context)
+{
+  SwServiceSinaPrivate *priv = GET_PRIVATE (self);
+  SwItemView *item_view;
+  const gchar *object_path;
+
+  if (!_check_query_validity (query))
+  {
+    dbus_g_method_return_error (context,
+                                g_error_new (SW_SERVICE_ERROR,
+                                             SW_SERVICE_ERROR_INVALID_QUERY,
+                                             "Query '%s' is invalid",
+                                             query));
+    return;
+  }
+
+  item_view = g_object_new (SW_TYPE_SINA_ITEM_VIEW,
+                            "proxy", priv->proxy,
+                            "service", self,
+                            "query", query,
+                            "params", params,
+                            NULL);
+
+  object_path = sw_item_view_get_object_path (item_view);
+  /* Ensure the object gets disposed when the client goes away */
+  sw_client_monitor_add (dbus_g_method_get_sender (context),
+                         (GObject *)item_view);
+  sw_query_iface_return_from_open_view (context,
+                                        object_path);
+}
+
+static void
+query_iface_init (gpointer g_iface,
+                  gpointer iface_data)
+{
+  SwQueryIfaceClass *klass = (SwQueryIfaceClass*)g_iface;
+
+  sw_query_iface_implement_open_view (klass,
+                                      _sina_query_open_view);
+}
+
+/* Avatar interface */
+static void
+_requested_avatar_downloaded_cb (const gchar *uri,
+                                 gchar       *local_path,
+                                 gpointer     userdata)
+{
+  SwService *service = SW_SERVICE (userdata);
+
+  sw_avatar_iface_emit_avatar_retrieved (service, local_path);
+  g_free (local_path);
+}
+
+static void
+_sina_avatar_request_avatar (SwAvatarIface         *self,
+                             DBusGMethodInvocation *context)
+{
+  SwServiceSinaPrivate *priv = GET_PRIVATE (self);
+
+  if (priv->image_url) {
+    sw_web_download_image_async (priv->image_url,
+                                 _requested_avatar_downloaded_cb,
+                                 self);
+  }
+
+  sw_avatar_iface_return_from_request_avatar (context);
+}
+
+static void
+avatar_iface_init (gpointer g_iface,
+                   gpointer iface_data)
+{
+  SwAvatarIfaceClass *klass = (SwAvatarIfaceClass*)g_iface;
+
+  sw_avatar_iface_implement_request_avatar (klass,
+                                            _sina_avatar_request_avatar);
+}
+
+/* Status Update interface */
+static void
+_update_status_cb (RestProxyCall *call,
+                   const GError  *error,
+                   GObject       *weak_object,
+                   gpointer       userdata)
+{
+  if (error)
+  {
+    g_critical (G_STRLOC ": Error updating status: %s",
+                error->message);
+    sw_status_update_iface_emit_status_updated (weak_object, FALSE);
+  } else {
+    SW_DEBUG (TWITTER, G_STRLOC ": Status updated.");
+    sw_status_update_iface_emit_status_updated (weak_object, TRUE);
+  }
+}
+
+static void
+_sina_status_update_update_status (SwStatusUpdateIface   *self,
+                                   const gchar           *msg,
+                                   GHashTable            *fields,
+                                   DBusGMethodInvocation *context)
+{
+  SwServiceSina *sina = SW_SERVICE_SINA (self);
+  SwServiceSinaPrivate *priv = GET_PRIVATE (sina);
+  RestProxyCall *call;
+
+  if (!priv->user_id)
+    return;
+
+  call = rest_proxy_new_call (priv->proxy);
+  rest_proxy_call_set_method (call, "POST");
+  rest_proxy_call_set_function (call, "statuses/update.xml");
+
+  rest_proxy_call_add_params (call,
+                              "status", msg,
+                              NULL);
+
+  rest_proxy_call_async (call, _update_status_cb, (GObject *)self, NULL, NULL);
+  sw_status_update_iface_return_from_update_status (context);
+}
+
+static void
+status_update_iface_init (gpointer g_iface,
+                          gpointer iface_data)
+{
+  SwStatusUpdateIfaceClass *klass = (SwStatusUpdateIfaceClass*)g_iface;
+
+  sw_status_update_iface_implement_update_status (klass,
+                                                  _sina_status_update_update_status);
+}
+
diff --git a/services/sina/sina.h b/services/sina/sina.h
new file mode 100644
index 0000000..701d5da
--- /dev/null
+++ b/services/sina/sina.h
@@ -0,0 +1,61 @@
+/*
+ * libsocialweb Sina service support
+ *
+ * Copyright (C) 2010 Novell, Inc.
+ *
+ * Author: Gary Ching-Pang Lin <glin novell com>
+ *
+ * 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,
+ * version 2.1, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _SW_SERVICE_SINA
+#define _SW_SERVICE_SINA
+
+#include <libsocialweb/sw-service.h>
+
+G_BEGIN_DECLS
+
+#define SW_TYPE_SERVICE_SINA sw_service_sina_get_type()
+
+#define SW_SERVICE_SINA(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST ((obj), SW_TYPE_SERVICE_SINA, SwServiceSina))
+
+#define SW_SERVICE_SINA_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST ((klass), SW_TYPE_SERVICE_SINA, SwServiceSinaClass))
+
+#define SW_IS_SERVICE_SINA(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SW_TYPE_SERVICE_SINA))
+
+#define SW_IS_SERVICE_SINA_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_TYPE ((klass), SW_TYPE_SERVICE_SINA))
+
+#define SW_SERVICE_SINA_GET_CLASS(obj) \
+  (G_TYPE_INSTANCE_GET_CLASS ((obj), SW_TYPE_SERVICE_SINA, SwServiceSinaClass))
+
+typedef struct _SwServiceSinaPrivate SwServiceSinaPrivate;
+
+typedef struct {
+  SwService parent;
+  SwServiceSinaPrivate *priv;
+} SwServiceSina;
+
+typedef struct {
+  SwServiceClass parent_class;
+} SwServiceSinaClass;
+
+GType sw_service_sina_get_type (void);
+
+G_END_DECLS
+
+#endif /* _SW_SERVICE_SINA */
diff --git a/services/sina/sina.keys.in b/services/sina/sina.keys.in
new file mode 100644
index 0000000..d68830d
--- /dev/null
+++ b/services/sina/sina.keys.in
@@ -0,0 +1,5 @@
+[LibSocialWebService]
+_Name=Sina
+_Description=Sina microblogging
+Link=http://t.sina.com.cn
+AuthType=sina
diff --git a/services/sina/sina.png b/services/sina/sina.png
new file mode 100644
index 0000000..f674bb6
Binary files /dev/null and b/services/sina/sina.png differ



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