summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomasz Wasilczyk <tomkiewicz@cpw.pidgin.im>2012-06-30 19:56:46 +0200
committerTomasz Wasilczyk <tomkiewicz@cpw.pidgin.im>2012-06-30 19:56:46 +0200
commit7b9b55b30718b8e2f6e577669fbb1222006fe7c1 (patch)
tree06c396ef411f2a0b7a8ad5fa037b1917cf0b5c14
parent1b24d1c300dd043b9ca2fb5c262015c91495ba37 (diff)
downloadpidgin-7b9b55b30718b8e2f6e577669fbb1222006fe7c1.tar.gz
Gadu-Gadu: cancellable gg_http watcher
-rw-r--r--libpurple/protocols/gg/account.c83
-rw-r--r--libpurple/protocols/gg/libgaduw.c70
-rw-r--r--libpurple/protocols/gg/libgaduw.h11
3 files changed, 86 insertions, 78 deletions
diff --git a/libpurple/protocols/gg/account.c b/libpurple/protocols/gg/account.c
index 7e6dda23f9..089118b654 100644
--- a/libpurple/protocols/gg/account.c
+++ b/libpurple/protocols/gg/account.c
@@ -19,16 +19,10 @@ typedef struct
PurpleConnection *gc;
void *user_data;
- gboolean cancelled;
ggp_libgaduw_http_req *req;
- ggp_purplew_request_processing_handle *req_processing;
} ggp_account_token_reqdata;
-static void ggp_account_token_response(struct gg_http *h, gboolean success, void *_reqdata);
-
-static void ggp_account_token_finish(ggp_account_token_reqdata *reqdata, ggp_account_token *token);
-
-static void ggp_account_token_request_cancel(PurpleConnection *gc, void *_reqdata);
+static void ggp_account_token_response(struct gg_http *h, gboolean success, gboolean cancelled, gpointer _reqdata);
/******************************************************************************/
@@ -43,14 +37,16 @@ void ggp_account_token_request(PurpleConnection *gc, ggp_account_token_cb callba
{
struct gg_http *h;
ggp_account_token_reqdata *reqdata;
-
+
+ purple_debug_info("gg", "ggp_account_token_request: requesting token...\n");
+
if (!ggp_deprecated_setup_proxy(gc))
{
callback(gc, NULL, user_data);
return;
}
- h = gg_token(1);
+ h = gg_token(TRUE);
if (!h)
{
@@ -62,63 +58,42 @@ void ggp_account_token_request(PurpleConnection *gc, ggp_account_token_cb callba
reqdata->callback = callback;
reqdata->gc = gc;
reqdata->user_data = user_data;
- reqdata->cancelled = FALSE;
- reqdata->req_processing = ggp_purplew_request_processing(gc, NULL, reqdata, ggp_account_token_request_cancel);
- reqdata->req = ggp_libgaduw_http_watch(h, ggp_account_token_response, reqdata);
+ reqdata->req = ggp_libgaduw_http_watch(gc, h, ggp_account_token_response, reqdata, TRUE);
}
-static void ggp_account_token_request_cancel(PurpleConnection *gc, void *_reqdata)
-{
- ggp_account_token_reqdata *reqdata = _reqdata;
-
- purple_debug_info("gg", "ggp_account_token_request_cancel\n");
- reqdata->cancelled = TRUE;
- reqdata->req_processing = NULL;
- ggp_libgaduw_http_cancel(reqdata->req);
-}
-
-static void ggp_account_token_response(struct gg_http *h, gboolean success, void *_reqdata)
+static void ggp_account_token_response(struct gg_http *h, gboolean success,
+ gboolean cancelled, gpointer _reqdata)
{
ggp_account_token_reqdata *reqdata = _reqdata;
struct gg_token *token_info;
- ggp_account_token *token;
-
- if (!success)
- {
- gg_token_free(h);
- ggp_account_token_finish(reqdata, NULL);
- return;
- }
+ ggp_account_token *token = NULL;
- purple_debug_info("gg", "ggp_account_token_handler: got token\n");
+ g_assert(!(success && cancelled));
- token = g_new(ggp_account_token, 1);
-
- token_info = h->data;
- token->id = g_strdup(token_info->tokenid);
- token->size = h->body_size;
- token->data = g_memdup(h->body, token->size);
- gg_token_free(h);
+ if (cancelled)
+ purple_debug_info("gg", "ggp_account_token_handler: cancelled\n");
+ else if (success)
+ {
+ purple_debug_info("gg", "ggp_account_token_handler: got token\n");
- ggp_account_token_finish(reqdata, token);
-}
-
-static void ggp_account_token_finish(ggp_account_token_reqdata *reqdata, ggp_account_token *token)
-{
- g_assert(!reqdata->cancelled || !token); // cancelled => not token
+ token = g_new(ggp_account_token, 1);
- if (reqdata->req_processing)
- {
- ggp_purplew_request_processing_done(reqdata->req_processing);
- reqdata->req_processing = NULL;
+ token_info = h->data;
+ token->id = g_strdup(token_info->tokenid);
+ token->size = h->body_size;
+ token->data = g_memdup(h->body, token->size);
}
- reqdata->callback(reqdata->gc, token, reqdata->user_data);
-
- if (!token && !reqdata->cancelled)
- purple_notify_error(purple_connection_get_account(reqdata->gc),
+ else
+ {
+ purple_debug_error("gg", "ggp_account_token_handler: error\n");
+ purple_notify_error(
+ purple_connection_get_account(reqdata->gc),
_("Token Error"),
_("Unable to fetch the token.\n"), NULL);
-
+ }
+
+ gg_token_free(h);
+ reqdata->callback(reqdata->gc, token, reqdata->user_data);
g_free(reqdata);
}
diff --git a/libpurple/protocols/gg/libgaduw.c b/libpurple/protocols/gg/libgaduw.c
index b4184b6a17..07d1818d19 100644
--- a/libpurple/protocols/gg/libgaduw.c
+++ b/libpurple/protocols/gg/libgaduw.c
@@ -8,29 +8,48 @@
* HTTP requests.
******************************************************************************/
+static void ggp_libgaduw_http_processing_cancel(PurpleConnection *gc,
+ void *_req);
+
static void ggp_libgaduw_http_handler(gpointer _req, gint fd,
PurpleInputCondition cond);
-static void ggp_libgaduw_http_cb_call(ggp_libgaduw_http_req *req,
+static void ggp_libgaduw_http_finish(ggp_libgaduw_http_req *req,
gboolean success);
/******************************************************************************/
-ggp_libgaduw_http_req * ggp_libgaduw_http_watch(struct gg_http *h,
- ggp_libgaduw_http_cb cb, gpointer user_data)
+ggp_libgaduw_http_req * ggp_libgaduw_http_watch(PurpleConnection *gc,
+ struct gg_http *h, ggp_libgaduw_http_cb cb,
+ gpointer user_data, gboolean show_processing)
{
- ggp_libgaduw_http_req *req = g_new(ggp_libgaduw_http_req, 1);
- purple_debug_info("gg", "ggp_libgaduw_http_watch\n");
+ ggp_libgaduw_http_req *req;
+ purple_debug_misc("gg", "ggp_libgaduw_http_watch(h=%x, show_processing=%d)\n",
+ (unsigned int)h, show_processing);
+ req = g_new(ggp_libgaduw_http_req, 1);
req->user_data = user_data;
req->cb = cb;
+ req->cancelled = FALSE;
req->h = h;
- req->inpa = purple_input_add(h->fd, PURPLE_INPUT_READ,
- ggp_libgaduw_http_handler, req);
+ req->processing = NULL;
+ if (show_processing)
+ req->processing = ggp_purplew_request_processing(gc, NULL,
+ req, ggp_libgaduw_http_processing_cancel);
+ req->inpa = ggp_purplew_http_input_add(h, ggp_libgaduw_http_handler,
+ req);
return req;
}
+static void ggp_libgaduw_http_processing_cancel(PurpleConnection *gc,
+ void *_req)
+{
+ ggp_libgaduw_http_req *req = _req;
+ req->processing = NULL;
+ ggp_libgaduw_http_cancel(req);
+}
+
static void ggp_libgaduw_http_handler(gpointer _req, gint fd,
PurpleInputCondition cond)
{
@@ -40,13 +59,13 @@ static void ggp_libgaduw_http_handler(gpointer _req, gint fd,
{
purple_debug_error("gg", "ggp_libgaduw_http_handler: failed to "
"make http request: %d\n", req->h->error);
- ggp_libgaduw_http_cb_call(req, FALSE);
+ ggp_libgaduw_http_finish(req, FALSE);
return;
}
//TODO: verbose mode
- purple_debug_misc("gg", "ggp_libgaduw_http_handler: got fd update "
- "[check=%d, state=%d]\n", req->h->check, req->h->state);
+ //purple_debug_misc("gg", "ggp_libgaduw_http_handler: got fd update "
+ // "[check=%d, state=%d]\n", req->h->check, req->h->state);
if (req->h->state != GG_STATE_DONE)
{
@@ -60,24 +79,33 @@ static void ggp_libgaduw_http_handler(gpointer _req, gint fd,
{
purple_debug_error("gg", "ggp_libgaduw_http_handler: got empty "
"http response: %d\n", req->h->error);
- ggp_libgaduw_http_cb_call(req, FALSE);
+ ggp_libgaduw_http_finish(req, FALSE);
return;
}
- ggp_libgaduw_http_cb_call(req, TRUE);
+ ggp_libgaduw_http_finish(req, TRUE);
}
-static void ggp_libgaduw_http_cb_call(ggp_libgaduw_http_req *req,
- gboolean success)
+void ggp_libgaduw_http_cancel(ggp_libgaduw_http_req *req)
{
- purple_input_remove(req->inpa);
- req->cb(req->h, success, req->user_data);
- g_free(req);
+ purple_debug_misc("gg", "ggp_libgaduw_http_cancel\n");
+ req->cancelled = TRUE;
+ gg_http_stop(req->h);
+ ggp_libgaduw_http_finish(req, FALSE);
}
-void ggp_libgaduw_http_cancel(ggp_libgaduw_http_req *req)
+static void ggp_libgaduw_http_finish(ggp_libgaduw_http_req *req,
+ gboolean success)
{
- purple_debug_info("gg", "ggp_libgaduw_http_cancel\n");
- gg_http_stop(req->h);
- ggp_libgaduw_http_cb_call(req, FALSE);
+ purple_debug_misc("gg", "ggp_libgaduw_http_finish(h=%x, processing=%x):"
+ " success=%d\n", (unsigned int)req->h,
+ (unsigned int)req->processing, success);
+ if (req->processing)
+ {
+ ggp_purplew_request_processing_done(req->processing);
+ req->processing = NULL;
+ }
+ purple_input_remove(req->inpa);
+ req->cb(req->h, success, req->cancelled, req->user_data);
+ g_free(req);
}
diff --git a/libpurple/protocols/gg/libgaduw.h b/libpurple/protocols/gg/libgaduw.h
index e292ee6ec8..b63a1149c1 100644
--- a/libpurple/protocols/gg/libgaduw.h
+++ b/libpurple/protocols/gg/libgaduw.h
@@ -4,19 +4,24 @@
#include <internal.h>
#include <libgadu.h>
-typedef void (*ggp_libgaduw_http_cb)(struct gg_http *h, gboolean success, gpointer user_data);
+#include "purplew.h"
+
+typedef void (*ggp_libgaduw_http_cb)(struct gg_http *h, gboolean success,
+ gboolean cancelled, gpointer user_data);
typedef struct
{
gpointer user_data;
ggp_libgaduw_http_cb cb;
+ gboolean cancelled;
struct gg_http *h;
+ ggp_purplew_request_processing_handle *processing;
guint inpa;
} ggp_libgaduw_http_req;
-ggp_libgaduw_http_req * ggp_libgaduw_http_watch(struct gg_http *h,
- ggp_libgaduw_http_cb cb, gpointer user_data);
+ggp_libgaduw_http_req * ggp_libgaduw_http_watch(PurpleConnection *gc,
+ struct gg_http *h, ggp_libgaduw_http_cb cb, gpointer user_data, gboolean show_processing);
void ggp_libgaduw_http_cancel(ggp_libgaduw_http_req *req);