/soc/2015/nakulgulati/main: c18d42bec562: hangouts: refactor, ch...

Nakul at rock.pidgin.im Nakul at rock.pidgin.im
Tue Aug 18 20:33:01 EDT 2015


Changeset: c18d42bec562d1185d809330ef5597cb83524f9f
Author:	 Nakul Gulati
Date:	 2015-08-19 06:02 +0530
Branch:	 hangouts
URL: https://hg.pidgin.im/soc/2015/nakulgulati/main/rev/c18d42bec562

Description:

hangouts: refactor, chat-init signal

diffstat:

 libpurple/protocols/hangouts/api.c      |  195 ++++++++++++++++---------------
 libpurple/protocols/hangouts/api.h      |    7 +-
 libpurple/protocols/hangouts/hangouts.c |   28 ++++-
 libpurple/protocols/hangouts/json.c     |   18 ++
 libpurple/protocols/hangouts/json.h     |    3 +
 libpurple/protocols/hangouts/util.c     |    2 +-
 libpurple/protocols/hangouts/util.h     |    2 +-
 7 files changed, 152 insertions(+), 103 deletions(-)

diffs (truncated from 466 to 300 lines):

diff --git a/libpurple/protocols/hangouts/api.c b/libpurple/protocols/hangouts/api.c
--- a/libpurple/protocols/hangouts/api.c
+++ b/libpurple/protocols/hangouts/api.c
@@ -38,6 +38,16 @@ enum _HangoutsConversationOffTheRecordSt
   HANGOUTS_OFF_THE_RECORD = 1, HANGOUTS_ON_THE_RECORD = 2
 };
 
+enum HangoutsChatInitPathIndex
+{
+  HANGOUTS_CHAT_INIT_API_KEY = 0,
+  HANGOUTS_CHAT_INIT_EMAIL = 1,
+  HANGOUTS_CHAT_INIT_HEADER_DATE = 2,
+  HANGOUTS_CHAT_INIT_HEADER_ID = 3,
+  HANGOUTS_CHAT_INIT_HEADER_VERSION = 4,
+  HANGOUTS_CHAT_INIT_SYNC_TIMESTAMP = 5
+};
+
 struct _HangoutsApiCookies
 {
   gchar *APISID;
@@ -59,6 +69,8 @@ struct _HangoutsApiPrivate
 
   HangoutsApiCookies *cookies;
 
+  JsonNode *chat_init_dict;
+
   gchar *api_key;
   gchar *access_token;
   gchar *client_id;
@@ -66,9 +78,10 @@ struct _HangoutsApiPrivate
   gchar *header_date;
   gchar *header_version;
   gchar *header_id;
-  gchar *last_sync_timestamp;
   gchar *pvt_token;
   gchar *refresh_token;
+
+  guint sync_timestamp;
 };
 
 struct _HangoutsApiHttpInfo
@@ -78,6 +91,18 @@ struct _HangoutsApiHttpInfo
   gchar *method;
 };
 
+const gchar *chat_init_path[] =
+  {
+
+  "$.ds:7[0][2]",
+  "$.ds:34[0][2]",
+  "$.ds:2[0][4]",
+  "$.ds:2[0][6]",
+  "$.ds:4[0][7]",
+  "$.ds:21[0][1][4]"
+
+  };
+
 static void
 hangouts_api_dispose (GObject *obj)
 {
@@ -90,6 +115,8 @@ hangouts_api_dispose (GObject *obj)
 
   purple_http_cookie_jar_unref (priv->cookie_jar);
 
+  json_node_free(priv->chat_init_dict);
+
   g_free (priv->cookies);
   g_free (priv->access_token);
   g_free (priv->api_key);
@@ -98,7 +125,6 @@ hangouts_api_dispose (GObject *obj)
   g_free (priv->header_date);
   g_free (priv->header_id);
   g_free (priv->header_version);
-  g_free (priv->last_sync_timestamp);
   g_free (priv->pvt_token);
   g_free (priv->refresh_token);
 }
@@ -110,11 +136,21 @@ hangouts_api_class_init (HangoutsApiClas
   gobject_class->dispose = hangouts_api_dispose;
   g_type_class_add_private (klass, sizeof(HangoutsApiPrivate));
 
-  g_signal_new ("auth", G_TYPE_FROM_CLASS(klass), G_SIGNAL_ACTION, 0,
-  NULL,
-		NULL, g_cclosure_marshal_VOID__VOID,
-		G_TYPE_NONE,
-		NULL);
+  g_signal_new ("auth",
+  		G_TYPE_FROM_CLASS(klass),
+  		G_SIGNAL_ACTION, 0,
+  		NULL, NULL,
+  		g_cclosure_marshal_VOID__VOID,
+    		G_TYPE_NONE,
+    		NULL);
+
+  g_signal_new ("get-chat-init",
+		G_TYPE_FROM_CLASS(klass),
+		G_SIGNAL_ACTION, 0,
+		NULL, NULL,
+		g_cclosure_marshal_VOID__VOID,
+  		G_TYPE_NONE,
+  		NULL);
 }
 
 static void
@@ -151,6 +187,16 @@ hangouts_api_set_refresh_token (Hangouts
   priv->refresh_token = g_strdup (refresh_token);
 }
 
+PurpleConnection *
+hangouts_api_get_connection (HangoutsApi *api)
+{
+  PurpleConnection *gc;
+
+  gc = api->priv->gc;
+
+  return gc;
+}
+
 void
 hangouts_api_parse_header (HangoutsApiCookies *cookies, GList *headers)
 {
@@ -228,7 +274,7 @@ hangouts_api_fetch_login_cookies (Hangou
   PurpleAccount *acct;
   PurpleHttpRequest *request;
   HangoutsApiPrivate *priv;
-  gchar *username, *password, *url, *request_body;
+  gchar *username, *password, *request_body;
 
   priv = api->priv;
   gc = priv->gc;
@@ -273,7 +319,6 @@ hangouts_api_get_galx_cookie_cb (PurpleH
 void
 hangouts_api_get_galx_cookie (HangoutsApi *api)
 {
-  gchar *url;
   PurpleHttpRequest *request;
   PurpleHttpCookieJar *cookieJar;
   HangoutsApiPrivate *priv;
@@ -422,25 +467,24 @@ hangouts_api_get_private_token_cb (Purpl
 				   gpointer user_data)
 {
   HangoutsApi *api;
-  gchar *raw_res;
-  gchar *start, *end, *pvt;
+  JsonNode *node;
+  const gchar *raw_response;
 
   api = user_data;
-  raw_res = purple_http_response_get_data (response, NULL);
+  raw_response = purple_http_response_get_data (response, NULL);
+  node = hangouts_json_node_new (raw_response, -1, FALSE);
 
-  purple_debug_misc ("hangouts_prpl", "res: %s", raw_res);
+  if (purple_http_response_is_successful (response))
+    {
+      api->priv->pvt_token = hangouts_json_node_get_str (node, "$[1]", NULL);
+      hangouts_api_get_chat_init (api);
+    }
+  else
+    {
+      //TODO: Handle pvt token request error
+    }
 
-  start = strchr (raw_res, ',');
-  start += 2;
-  end = strchr (start, ',');
-  end--;
-
-  api->priv->pvt_token = g_strndup (start, end - start);
-
-  purple_debug_misc ("hangouts_prpl", "pvt: %s", api->priv->pvt_token);
-
-  hangouts_api_get_chat_init (api);
-
+  json_node_free (node);
 }
 
 void
@@ -472,17 +516,40 @@ hangouts_api_get_chat_init_cb (PurpleHtt
   GHashTable *chat_init_tab;
   JsonNode *root;
   HangoutsApi *api;
+  HangoutsApiPrivate *priv;
 
   raw_res = purple_http_response_get_data (response, NULL);
   api = user_data;
+  priv = api->priv;
 
-  chat_init_tab = hangouts_util_parse (raw_res);
-  root = hangouts_util_construct_json_dictionary (chat_init_tab);
+  if (purple_http_response_is_successful (response))
+    {
+      chat_init_tab = hangouts_util_parse (raw_res);
+      root = hangouts_util_construct_json_dictionary (chat_init_tab);
 
-  hangouts_api_set_chat_init_data (api, root);
+      priv->api_key = hangouts_json_node_get_str (
+	  root, chat_init_path[HANGOUTS_CHAT_INIT_API_KEY], NULL);
+      priv->email = hangouts_json_node_get_str (
+	  root, chat_init_path[HANGOUTS_CHAT_INIT_EMAIL], NULL);
+      priv->header_date = hangouts_json_node_get_str (
+	  root, chat_init_path[HANGOUTS_CHAT_INIT_HEADER_DATE], NULL);
+      priv->header_id = hangouts_json_node_get_str (
+	  root, chat_init_path[HANGOUTS_CHAT_INIT_HEADER_ID], NULL);
+      priv->header_version = hangouts_json_node_get_str (
+	  root, chat_init_path[HANGOUTS_CHAT_INIT_HEADER_VERSION], NULL);
+      priv->sync_timestamp = hangouts_json_node_get_int (
+	  root, chat_init_path[HANGOUTS_CHAT_INIT_SYNC_TIMESTAMP], NULL);
 
-  g_hash_table_destroy (chat_init_tab);
-  json_node_free (root);
+      priv->chat_init_dict = root;
+
+      g_signal_emit_by_name (api, "get-chat-init");
+      g_hash_table_destroy(chat_init_tab);
+    }
+  else
+    {
+      //TODO: handle error chat init data
+    }
+
 }
 
 void
@@ -497,7 +564,8 @@ hangouts_api_get_chat_init (HangoutsApi 
   gc = priv->gc;
 
   url_with_param = g_strconcat (HANGOUTS_API_CHAT_INIT_URL, "?",
-				HANGOUTS_API_CHAT_INIT_PARAMS(priv->pvt_token));
+				HANGOUTS_API_CHAT_INIT_PARAMS(priv->pvt_token),
+				NULL);
   request = purple_http_request_new (url_with_param);
 
   purple_http_request_set_method (request, "GET");
@@ -509,71 +577,6 @@ hangouts_api_get_chat_init (HangoutsApi 
 
 }
 
-void
-hangouts_api_set_chat_init_data (HangoutsApi *api, JsonNode *dict)
-{
-  /*
-   * api_key -> $.ds:7[0][2]
-   * email -> $.ds:34[0][2]
-   * header_date -> $.ds:2[0][4]
-   * header_version -> $.ds:2[0][6]
-   * header_id -> $.ds:4[0][7]
-   * sync_timestamp -> $.ds:21[0][1][4]
-   */
-
-  JsonPath *path;
-  JsonArray *j_array;
-  JsonNode *node;
-  gchar *value;
-  HangoutsApiPrivate *priv;
-
-  priv = api->priv;
-  path = json_path_new ();
-
-  json_path_compile (path, "$.ds:7[0][2]", NULL);
-  node = json_path_match (path, dict);
-
-  j_array = json_node_dup_array (node);
-  value = json_array_get_string_element (j_array, 0);
-  priv->api_key = value;
-
-  json_path_compile (path, "$.ds:34[0][2]", NULL);
-  node = json_path_match (path, dict);
-
-  j_array = json_node_dup_array (node);
-  value = json_array_get_string_element (j_array, 0);
-  priv->email = value;
-
-  json_path_compile (path, "$.ds:2[0][4]", NULL);
-  node = json_path_match (path, dict);
-
-  j_array = json_node_dup_array (node);
-  value = json_array_get_string_element (j_array, 0);
-  priv->header_date = value;
-
-  json_path_compile (path, "$.ds:2[0][6]", NULL);
-  node = json_path_match (path, dict);
-
-  j_array = json_node_dup_array (node);
-  value = json_array_get_string_element (j_array, 0);
-  priv->header_version = value;
-
-  json_path_compile (path, "$.ds:4[0][7]", NULL);
-  node = json_path_match (path, dict);
-
-  j_array = json_node_dup_array (node);
-  value = json_array_get_string_element (j_array, 0);
-  priv->header_id = value;
-
-  json_path_compile (path, "$.ds:21[0][1][4]", NULL);
-  node = json_path_match (path, dict);
-
-  j_array = json_node_dup_array (node);
-  value = json_array_get_string_element (j_array, 0);
-  priv->last_sync_timestamp = value;



More information about the Commits mailing list