/cpw/tomkiewicz/gg11: ed78b0fc180e: Gadu-Gadu: file transfer cod...

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Sun Nov 4 11:47:23 EST 2012


Changeset: ed78b0fc180e60e649a597b7b89e6d35cc8d9481
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2012-11-04 17:47 +0100
Branch:	 default
URL: http://hg.pidgin.im/cpw/tomkiewicz/gg11/rev/ed78b0fc180e

Description:

Gadu-Gadu: file transfer code tidying, first stage of receiving a file

diffstat:

 libpurple/protocols/gg/edisc.c          |  403 ++++++++++++++++++++++---------
 libpurple/protocols/gg/edisc.h          |    9 +-
 libpurple/protocols/gg/gg.c             |    2 +-
 libpurple/protocols/gg/libgadu-events.c |    2 +-
 libpurple/protocols/gg/utils.c          |    8 +
 libpurple/protocols/gg/utils.h          |    8 +
 6 files changed, 307 insertions(+), 125 deletions(-)

diffs (truncated from 693 to 300 lines):

diff --git a/libpurple/protocols/gg/edisc.c b/libpurple/protocols/gg/edisc.c
--- a/libpurple/protocols/gg/edisc.c
+++ b/libpurple/protocols/gg/edisc.c
@@ -49,12 +49,31 @@ struct _ggp_edisc_xfer
 typedef void (*ggp_ggdrive_auth_cb)(PurpleConnection *gc, gboolean success,
 	gpointer user_data);
 
+/* Setting up. */
+static inline ggp_edisc_session_data *
+ggp_edisc_get_sdata(PurpleConnection *gc);
+
+/* Misc. */
+static void ggp_edisc_set_defaults(PurpleHttpRequest *req);
+static int ggp_edisc_parse_error(const gchar *data);
+
+/* General xfer functions. */
+static void ggp_edisc_xfer_free(PurpleXfer *xfer);
+static void ggp_edisc_xfer_error(PurpleXfer *xfer, const gchar *msg);
+static void ggp_edisc_xfer_cancel(PurpleXfer *xfer);
+
+/* Sending a file. */
+void ggp_edisc_xfer_send_ticket_changed(PurpleConnection *gc, PurpleXfer *xfer,
+	gboolean is_allowed);
+
+/* Receiving a file. */
+static void ggp_edisc_xfer_recv_ticket_changed(PurpleConnection *gc,
+	const gchar *ticket);
+
+/* Authentication. */
 static void ggp_ggdrive_auth(PurpleConnection *gc, ggp_ggdrive_auth_cb cb,
 	gpointer user_data);
 
-static void ggp_edisc_xfer_free(PurpleXfer *xfer);
-static void ggp_edisc_set_defaults(PurpleHttpRequest *req);
-
 /*******************************************************************************
  * Setting up.
  ******************************************************************************/
@@ -117,20 +136,28 @@ static void ggp_edisc_set_defaults(Purpl
 		"application/x-www-form-urlencoded; charset=UTF-8");
 }
 
+static int ggp_edisc_parse_error(const gchar *data)
+{
+	JsonParser *parser;
+	JsonObject *result;
+	int error_id;
+
+	parser = ggp_json_parse(data);
+	result = json_node_get_object(json_parser_get_root(parser));
+	result = json_object_get_object_member(result, "result");
+	error_id = json_object_get_int_member(result, "appStatus");
+	purple_debug_info("gg", "edisc error: %s (%d)\n",
+		json_object_get_string_member(result, "errorMsg"),
+		error_id);
+	g_object_unref(parser);
+
+	return error_id;
+}
+
 /*******************************************************************************
- * Sending a file.
+ * General xfer functions.
  ******************************************************************************/
 
-static void ggp_edisc_xfer_init(PurpleXfer *xfer);
-static void ggp_edisc_xfer_init_authenticated(PurpleConnection *gc,
-	gboolean success, gpointer _xfer);
-static void ggp_edisc_xfer_init_ticket_sent(PurpleHttpConnection *hc,
-	PurpleHttpResponse *response, gpointer _xfer);
-static void ggp_edisc_xfer_sent(PurpleHttpConnection *hc,
-	PurpleHttpResponse *response, gpointer _xfer);
-
-static void ggp_edisc_xfer_error(PurpleXfer *xfer, const gchar *msg);
-
 static void ggp_edisc_xfer_free(PurpleXfer *xfer)
 {
 	ggp_edisc_session_data *sdata;
@@ -167,48 +194,96 @@ static void ggp_edisc_xfer_error(PurpleX
 	purple_xfer_end(xfer);
 }
 
-static int ggp_edisc_parse_error(const gchar *data)
+void ggp_edisc_xfer_ticket_changed(PurpleConnection *gc, const char *data)
 {
+	ggp_edisc_session_data *sdata = ggp_edisc_get_sdata(gc);
+	PurpleXfer *xfer;
 	JsonParser *parser;
-	JsonObject *result;
-	int error_id;
+	JsonObject *ticket;
+	const gchar *ticket_id, *ack_status;
+	gboolean is_allowed, is_rejected;
 
 	parser = ggp_json_parse(data);
-	result = json_node_get_object(json_parser_get_root(parser));
-	result = json_object_get_object_member(result, "result");
-	error_id = json_object_get_int_member(result, "appStatus");
-	purple_debug_info("gg", "edisc error: %s (%d)\n",
-		json_object_get_string_member(result, "errorMsg"),
-		error_id);
+	ticket = json_node_get_object(json_parser_get_root(parser));
+	ticket_id = json_object_get_string_member(ticket, "id");
+	ack_status = json_object_get_string_member(ticket, "ack_status");
+
+	xfer = g_hash_table_lookup(sdata->xfers_initialized, ticket_id);
+	if (xfer == NULL) {
+		purple_debug_misc("gg", "ggp_edisc_event_ticket_changed: "
+			"ticket %s not found, updating it...\n",
+			purple_debug_is_unsafe() ? ticket_id : "");
+		ggp_edisc_xfer_recv_ticket_changed(gc, ticket_id);
+		g_object_unref(parser);
+		return;
+	}
+
+	is_allowed = is_rejected = FALSE;
+	if (g_strcmp0("unknown", ack_status) == 0) {
+		/* do nothing */
+	} else if (g_strcmp0("rejected", ack_status) == 0)
+		is_rejected = TRUE;
+	else if (g_strcmp0("allowed", ack_status) == 0)
+		is_allowed = TRUE;
+	else {
+		purple_debug_warning("gg", "ggp_edisc_event_ticket_changed: "
+			"unknown ack_status=%s\n", ack_status);
+		g_object_unref(parser);
+		return;
+	}
+
 	g_object_unref(parser);
 
-	return error_id;
+	if (is_allowed || is_rejected)
+		ggp_edisc_xfer_send_ticket_changed(gc, xfer, is_allowed);
+
 }
 
-gboolean ggp_edisc_xfer_can_receive_file(PurpleConnection *gc, const char *who)
+static void ggp_edisc_xfer_cancel(PurpleXfer *xfer)
+{
+	g_return_if_fail(xfer != NULL);
+
+	ggp_edisc_xfer_free(xfer);
+}
+
+/*******************************************************************************
+ * Sending a file.
+ ******************************************************************************/
+
+static void ggp_edisc_xfer_send_init(PurpleXfer *xfer);
+static void ggp_edisc_xfer_send_init_authenticated(PurpleConnection *gc,
+	gboolean success, gpointer _xfer);
+static void ggp_edisc_xfer_send_init_ticket_created(PurpleHttpConnection *hc,
+	PurpleHttpResponse *response, gpointer _xfer);
+static void ggp_edisc_xfer_send_reader(PurpleHttpConnection *hc,
+	gchar *buffer, size_t offset, size_t length, gpointer _xfer,
+	PurpleHttpContentReaderCb cb);
+static void ggp_edisc_xfer_send_progress_watcher(PurpleHttpConnection *hc,
+	gboolean reading_state, int processed, int total, gpointer _xfer);
+static void ggp_edisc_xfer_send_start(PurpleXfer *xfer);
+static void ggp_edisc_xfer_send_done(PurpleHttpConnection *hc,
+	PurpleHttpResponse *response, gpointer _xfer);
+
+gboolean ggp_edisc_xfer_can_receive_file(PurpleConnection *gc,
+	const char *who)
 {
 	return TRUE; /* TODO: only online, buddies (?) */
 }
 
-static void ggp_edisc_xfer_init(PurpleXfer *xfer)
+static void ggp_edisc_xfer_send_init(PurpleXfer *xfer)
 {
 	ggp_edisc_xfer *edisc_xfer = purple_xfer_get_protocol_data(xfer);
 
-	if (purple_xfer_get_type(xfer) != PURPLE_XFER_SEND) {
-		purple_debug_error("gg", "ggp_edisc_xfer_init: "
-			"Not yet implemented\n");
-		return;
-	}
-
 	purple_xfer_set_status(xfer, PURPLE_XFER_STATUS_NOT_STARTED);
 
 	edisc_xfer->filename = g_strdup(purple_xfer_get_filename(xfer));
 	g_strcanon(edisc_xfer->filename, GGP_EDISC_FNAME_ALLOWED, '_');
 
-	ggp_ggdrive_auth(edisc_xfer->gc, ggp_edisc_xfer_init_authenticated, xfer);
+	ggp_ggdrive_auth(edisc_xfer->gc, ggp_edisc_xfer_send_init_authenticated,
+		xfer);
 }
 
-static void ggp_edisc_xfer_init_authenticated(PurpleConnection *gc,
+static void ggp_edisc_xfer_send_init_authenticated(PurpleConnection *gc,
 	gboolean success, gpointer _xfer)
 {
 	ggp_edisc_session_data *sdata = ggp_edisc_get_sdata(gc);
@@ -246,11 +321,11 @@ static void ggp_edisc_xfer_init_authenti
 	g_free(data);
 
 	edisc_xfer->hc = purple_http_request(gc, req,
-		ggp_edisc_xfer_init_ticket_sent, xfer);
+		ggp_edisc_xfer_send_init_ticket_created, xfer);
 	purple_http_request_unref(req);
 }
 
-static void ggp_edisc_xfer_init_ticket_sent(PurpleHttpConnection *hc,
+static void ggp_edisc_xfer_send_init_ticket_created(PurpleHttpConnection *hc,
 	PurpleHttpResponse *response, gpointer _xfer)
 {
 	ggp_edisc_session_data *sdata = ggp_edisc_get_sdata(
@@ -269,12 +344,14 @@ static void ggp_edisc_xfer_init_ticket_s
 	if (!purple_http_response_is_successfull(response)) {
 		int error_id = ggp_edisc_parse_error(data);
 		if (error_id == 206) /* recipient not logged in */
-			ggp_edisc_xfer_error(xfer, _("Recipient not logged in"));
+			ggp_edisc_xfer_error(xfer,
+				_("Recipient not logged in"));
 		else if (error_id == 207) /* bad sender recipient relation */
 			ggp_edisc_xfer_error(xfer, _("Recipient didn't added "
 				"you to his buddy list"));
 		else
-			ggp_edisc_xfer_error(xfer, _("Cannot offer sending a file"));
+			ggp_edisc_xfer_error(xfer,
+				_("Cannot offer sending a file"));
 		return;
 	}
 
@@ -289,66 +366,30 @@ static void ggp_edisc_xfer_init_ticket_s
 
 	if (edisc_xfer->ticket_id == NULL) {
 		purple_debug_error("gg",
-			"ggp_edisc_xfer_init_ticket_sent: "
+			"ggp_edisc_xfer_send_init_ticket_created: "
 			"couldn't get ticket id\n");
 		return;
 	}
 
-	purple_debug_info("gg", "ggp_edisc_xfer_init_ticket_sent: "
+	purple_debug_info("gg", "ggp_edisc_xfer_send_init_ticket_created: "
 		"ticket \"%s\" created\n", edisc_xfer->ticket_id);
 
 	g_hash_table_insert(sdata->xfers_initialized,
 		edisc_xfer->ticket_id, xfer);
 }
 
-void ggp_edisc_event_send_ticket_changed(PurpleConnection *gc, const char *data)
+void ggp_edisc_xfer_send_ticket_changed(PurpleConnection *gc, PurpleXfer *xfer,
+	gboolean is_allowed)
 {
-	ggp_edisc_session_data *sdata = ggp_edisc_get_sdata(gc);
-	PurpleXfer *xfer;
-	ggp_edisc_xfer *edisc_xfer;
-	JsonParser *parser;
-	JsonObject *ticket;
-	const gchar *ticket_id, *ack_status;
-	gboolean is_allowed;
-
-	parser = ggp_json_parse(data);
-	ticket = json_node_get_object(json_parser_get_root(parser));
-	ticket_id = json_object_get_string_member(ticket, "id");
-	ack_status = json_object_get_string_member(ticket, "ack_status");
-
-	if (g_strcmp0("unknown", ack_status) == 0) {
-		g_object_unref(parser);
-		return;
-	} else if (g_strcmp0("rejected", ack_status) == 0)
-		is_allowed = FALSE;
-	else if (g_strcmp0("allowed", ack_status) == 0)
-		is_allowed = TRUE;
-	else {
-		purple_debug_warning("gg", "ggp_edisc_event_send_ticket_changed: "
-			"unknown ack_status=%s\n", ack_status);
-		g_object_unref(parser);
-		return;
-	}
-
-	xfer = g_hash_table_lookup(sdata->xfers_initialized, ticket_id);
-	if (xfer == NULL) {
-		purple_debug_warning("gg", "ggp_edisc_event_send_ticket_changed: "
-			"ticket %s not found\n", ticket_id);
-		g_object_unref(parser);
-		return;
-	}
-
-	g_object_unref(parser);
-
-	edisc_xfer = purple_xfer_get_protocol_data(xfer);
+	ggp_edisc_xfer *edisc_xfer = purple_xfer_get_protocol_data(xfer);
 	if (!edisc_xfer) {
-		purple_debug_fatal("gg", "ggp_edisc_event_send_ticket_changed: "
+		purple_debug_fatal("gg", "ggp_edisc_event_ticket_changed: "
 			"transfer %p already free'd\n", xfer);
 		return;
 	}
 
 	if (!is_allowed) {
-		purple_debug_info("gg", "ggp_edisc_event_send_ticket_changed: "
+		purple_debug_info("gg", "ggp_edisc_event_ticket_changed: "
 			"transfer %p rejected\n", xfer);
 		purple_xfer_cancel_remote(xfer);
 		ggp_edisc_xfer_free(xfer);



More information about the Commits mailing list