/soc/2015/nakulgulati/main: 84474d2d3cbd: hangouts: channel conn...
Nakul at rock.pidgin.im
Nakul at rock.pidgin.im
Thu Aug 20 20:45:39 EDT 2015
Changeset: 84474d2d3cbd9afbb7646efeda94f3318e4c7dbc
Author: Nakul Gulati
Date: 2015-08-21 06:14 +0530
Branch: hangouts
URL: https://hg.pidgin.im/soc/2015/nakulgulati/main/rev/84474d2d3cbd
Description:
hangouts: channel connect and subscribe signals
diffstat:
libpurple/protocols/hangouts/api.c | 55 +++++++++++++-----
libpurple/protocols/hangouts/channel.c | 99 ++++++++++++++++++++++++++-------
libpurple/protocols/hangouts/channel.h | 7 ++
3 files changed, 122 insertions(+), 39 deletions(-)
diffs (truncated from 356 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
@@ -93,14 +93,12 @@ struct _HangoutsApiHttpInfo
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
@@ -115,7 +113,7 @@ hangouts_api_dispose (GObject *obj)
purple_http_cookie_jar_unref (priv->cookie_jar);
- json_node_free(priv->chat_init_dict);
+ json_node_free (priv->chat_init_dict);
g_free (priv->cookies);
g_free (priv->access_token);
@@ -137,20 +135,20 @@ hangouts_api_class_init (HangoutsApiClas
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_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);
+ G_TYPE_FROM_CLASS(klass),
+ G_SIGNAL_ACTION, 0,
+ NULL, NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE,
+ NULL);
}
static void
@@ -163,6 +161,21 @@ hangouts_api_init (HangoutsApi *api)
api->priv = priv;
}
+static void
+hangouts_api_cb_connect (HangoutsChannel *channel, gpointer *data)
+{
+ if (!hangouts_channel_is_subscribed (channel))
+ {
+ hangouts_channel_subscribe (channel);
+ }
+}
+
+static void
+hangouts_api_cb_subscribe (HangoutsChannel *channel, gpointer *data)
+{
+ purple_debug_misc ("hangouts-api", "channel subscribed");
+}
+
HangoutsApi *
hangouts_api_new (PurpleConnection *gc)
{
@@ -175,6 +188,11 @@ hangouts_api_new (PurpleConnection *gc)
priv->cookies = g_new0(HangoutsApiCookies, 1);
priv->gc = gc;
+ g_signal_connect(priv->channel, "connect",
+ G_CALLBACK(hangouts_api_cb_connect), NULL);
+ g_signal_connect(priv->channel, "subscribe",
+ G_CALLBACK(hangouts_api_cb_subscribe), NULL);
+
return api;
}
@@ -362,6 +380,7 @@ hangouts_api_auth_with_code_cb (PurpleHt
"$['refresh_token']",
NULL);
+ hangouts_channel_set_access_token (priv->channel, priv->access_token);
purple_account_set_remember_password (acct, TRUE);
purple_account_set_password (acct, priv->refresh_token, NULL, NULL);
@@ -424,6 +443,7 @@ hangouts_api_oauth_refresh_token_cb (Pur
priv->access_token = hangouts_json_node_get_str (node,
"$['access_token']",
NULL);
+ hangouts_channel_set_access_token (priv->channel, priv->access_token);
g_signal_emit_by_name (api, "auth");
}
else
@@ -543,7 +563,8 @@ hangouts_api_get_chat_init_cb (PurpleHtt
priv->chat_init_dict = root;
g_signal_emit_by_name (api, "get-chat-init");
- g_hash_table_destroy(chat_init_tab);
+ g_hash_table_destroy (chat_init_tab);
+ hangouts_channel_get_gsid (priv->channel);
}
else
{
diff --git a/libpurple/protocols/hangouts/channel.c b/libpurple/protocols/hangouts/channel.c
--- a/libpurple/protocols/hangouts/channel.c
+++ b/libpurple/protocols/hangouts/channel.c
@@ -3,6 +3,8 @@
#include "connection.h"
#include "http.h"
+#include "http.c"
+#include "purple-socket.h"
#include "channel.h"
#include "api.h"
@@ -41,8 +43,14 @@ static void
hangouts_channel_class_init (HangoutsChannelClass *klass)
{
GObjectClass *g_class = G_OBJECT_CLASS(klass);
+ g_class->dispose = hangouts_channel_dispose;
g_type_class_add_private (g_class, sizeof(HangoutsChannelPrivate));
- g_class->dispose = hangouts_channel_dispose;
+
+ g_signal_new ("connect", G_TYPE_FROM_CLASS(klass), G_SIGNAL_ACTION, 0, NULL,
+ NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, NULL);
+
+ g_signal_new ("subscribe", G_TYPE_FROM_CLASS(klass), G_SIGNAL_ACTION, 0, NULL,
+ NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, NULL);
}
static void
@@ -70,6 +78,18 @@ hangouts_channel_new (PurpleConnection *
return channel;
}
+gboolean
+hangouts_channel_is_connected (HangoutsChannel *channel)
+{
+ return channel->priv->connected;
+}
+
+gboolean
+hangouts_channel_is_subscribed (HangoutsChannel *channel)
+{
+ return channel->priv->subscribed;
+}
+
void
hangouts_channel_set_access_token (HangoutsChannel *channel,
gchar *access_token)
@@ -86,10 +106,15 @@ static void
hangouts_channel_subscribe_cb (PurpleHttpConnection *http_conn,
PurpleHttpResponse *response, gpointer user_data)
{
- gchar *raw;
+ HangoutsChannel *channel;
- raw = purple_http_response_get_data (response, NULL);
- purple_debug_misc ("hangouts-channel-subscribe", "response: %s", raw);
+ channel = user_data;
+
+ if (purple_http_response_is_successful (response))
+ {
+ channel->priv->subscribed = TRUE;
+ g_signal_emit_by_name (channel, "subscribe");
+ }
}
void
@@ -99,11 +124,10 @@ hangouts_channel_subscribe (HangoutsChan
PurpleHttpRequest *request;
gchar *end_point;
gchar *params;
- guint64 *timestamp;
+ guint64 timestamp;
gchar *req0, *req1, *req2, *body;
timestamp = (unsigned) time (NULL);
- purple_debug_misc ("hangouts-channel-time", "timestamp: %u", timestamp);
req0 =
g_strdup_printf (
"{\"1\":{\"1\":{\"1\":{\"1\":3,\"2\":2}},\"2\":{\"1\":{\"1\":3,\"2\":2},\"2\":\"\",\"3\":\"JS\",\"4\":\"lcsclient\"},\"3\":%u,\"4\":0,\"5\":\"c1\"},\"2\":{}}",
@@ -118,8 +142,12 @@ hangouts_channel_subscribe (HangoutsChan
"{\"1\":{\"1\":{\"1\":{\"1\":3,\"2\":2}},\"2\":{\"1\":{\"1\":3,\"2\":2},\"2\":\"\",\"3\":\"JS\",\"4\":\"lcsclient\"},\"3\":%u,\"4\":%u,\"5\":\"c4\"},\"3\":{\"1\":{\"1\":\"hangout_invite\"}}}",
timestamp, timestamp);
- body = g_strdup_printf ("count=3&ofs=0&req0_p=%s&req1_p=%s&req2_p=%s", req0,
- req1, req2);
+ body = g_strdup_printf ("count=3"
+ "&ofs=0"
+ "&req0_p=%s"
+ "&req1_p=%s"
+ "&req2_p=%s",
+ req0, req1, req2);
priv = channel->priv;
end_point = "channel/bind";
@@ -140,24 +168,51 @@ hangouts_channel_subscribe (HangoutsChan
request, "Authorization",
g_strdup_printf ("Bearer %s", priv->access_token));
purple_http_request_header_add (request, "X-Origin",
- g_strdup ("https://talkgadget.google.com"));
+ HANGOUTS_CHANNEL_X_ORIGIN_URL);
purple_http_request_header_add (request, "X-Goog-AuthUser", g_strdup ("0"));
purple_http_request_header_add (request, "Content-Type",
"application/x-www-form-urlencoded");
purple_http_request_set_contents (request, g_strcompress (body), -1);
purple_http_request (priv->gc, request, hangouts_channel_subscribe_cb,
channel);
-
}
static void
hangouts_channel_long_poll_cb (PurpleHttpConnection *http_conn,
PurpleHttpResponse *response, gpointer user_data)
{
- gchar *raw;
+ HangoutsChannel *channel;
+ const gchar *raw;
- raw = purple_http_response_get_data (response, NULL);
- purple_debug_misc ("hangouts-channel-long-poll", "response: %s", raw);
+ channel = user_data;
+
+ if (purple_http_response_is_successful (response))
+ {
+ //TODO: fire event for disconnect
+ channel->priv->connected = FALSE;
+ channel->priv->subscribed = FALSE;
+ }
+}
+
+static void
+hangouts_channel_long_poll_watcher (PurpleHttpConnection *http_conn,
+ gboolean reading_state, int processed,
+ int total, gpointer user_data)
+{
+ HangoutsChannel *channel;
+ HangoutsChannelPrivate *priv;
+ gchar *chunk;
+
+ channel = user_data;
+ priv = channel->priv;
+
+ if (processed > 0 && !priv->connected)
+ {
+ priv->connected = TRUE;
+ g_signal_emit_by_name (channel, "connect");
+ }
+ //TODO: parse chunk
+ chunk = g_strdup (http_conn->response_buffer->str);
}
void
@@ -165,6 +220,7 @@ hangouts_channel_long_poll (HangoutsChan
{
HangoutsChannelPrivate *priv;
PurpleHttpRequest *request;
+ PurpleHttpConnection *hc;
gchar *end_point;
gchar *params;
@@ -189,8 +245,10 @@ hangouts_channel_long_poll (HangoutsChan
purple_http_request_header_add (
request, "Authorization",
g_strdup_printf ("Bearer %s", priv->access_token));
- purple_http_request (priv->gc, request, hangouts_channel_long_poll_cb,
- channel);
+ hc = purple_http_request (priv->gc, request, hangouts_channel_long_poll_cb,
+ channel);
+ purple_http_conn_set_progress_watcher (hc, hangouts_channel_long_poll_watcher,
+ channel, -1);
}
static void
@@ -202,17 +260,16 @@ hangouts_channel_get_gsid_cb (PurpleHttp
* [1,[{"gsid":"<gsid>"}]]
* ]
*/
- gchar *res_raw;
- gchar *gsid;
- gchar *sid;
More information about the Commits
mailing list