diff libpurple/ft.c @ 29807:d0730ad1c7df

propagate from branch 'im.pidgin.pidgin' (head cd4ab1e30725817663f30bcb4d6cbe22879b271f) to branch 'im.pidgin.cpw.malu.ft_thumbnails' (head f009319a480177e111738b76e2b2efd7da2b1dfc)
author Marcus Lundblad <ml@update.uu.se>
date Mon, 26 Oct 2009 22:16:32 +0000
parents 56b67321b7c6
children b0f1698c52f8
line wrap: on
line diff
--- a/libpurple/ft.c	Mon Oct 26 08:27:40 2009 +0000
+++ b/libpurple/ft.c	Mon Oct 26 22:16:32 2009 +0000
@@ -136,6 +136,7 @@
 	g_free(xfer->local_filename);
 
 	g_hash_table_remove(xfers_data, xfer);
+	g_free(xfer->thumbnail_data);
 
 	PURPLE_DBUS_UNREGISTER_POINTER(xfer);
 	xfers = g_list_remove(xfers, xfer);
@@ -211,11 +212,14 @@
 	}
 }
 
-void purple_xfer_conversation_write(PurpleXfer *xfer, char *message, gboolean is_error)
+static void 
+purple_xfer_conversation_write_internal(PurpleXfer *xfer,
+	const char *message, gboolean is_error, gboolean print_thumbnail)
 {
 	PurpleConversation *conv = NULL;
 	PurpleMessageFlags flags = PURPLE_MESSAGE_SYSTEM;
 	char *escaped;
+	const gpointer *thumbnail_data = purple_xfer_get_thumbnail_data(xfer);
 
 	g_return_if_fail(xfer != NULL);
 	g_return_if_fail(message != NULL);
@@ -231,10 +235,40 @@
 	if (is_error)
 		flags = PURPLE_MESSAGE_ERROR;
 
-	purple_conversation_write(conv, NULL, escaped, flags, time(NULL));
+	if (print_thumbnail && thumbnail_data) {
+		gchar *message_with_img;
+		gsize size = purple_xfer_get_thumbnail_size(xfer);
+		gpointer data = g_memdup(thumbnail_data, size); 
+		int id = purple_imgstore_add_with_id(data, size, NULL);
+
+		message_with_img = 
+			g_strdup_printf("<img id='%d'/> %s", id, escaped);
+		purple_conversation_write(conv, NULL, message_with_img, flags, 
+			time(NULL));
+		purple_imgstore_unref_by_id(id);
+		g_free(message_with_img);
+	} else {
+		purple_conversation_write(conv, NULL, escaped, flags, time(NULL));
+	}
 	g_free(escaped);
 }
 
+void
+purple_xfer_conversation_write(PurpleXfer *xfer, gchar *message,
+	gboolean is_error)
+{
+	purple_xfer_conversation_write_internal(xfer, message, is_error, FALSE);
+}
+
+/* maybe this one should be exported puplically? */
+static void
+purple_xfer_conversation_write_with_thumbnail(PurpleXfer *xfer,
+	const gchar *message)
+{
+	purple_xfer_conversation_write_internal(xfer, message, FALSE, TRUE);
+}
+
+
 static void purple_xfer_show_file_error(PurpleXfer *xfer, const char *filename)
 {
 	int err = errno;
@@ -398,13 +432,20 @@
 			serv_got_im(purple_account_get_connection(xfer->account),
 								 xfer->who, xfer->message, 0, time(NULL));
 
-		purple_request_accept_cancel(xfer, NULL, buf, NULL,
-								  PURPLE_DEFAULT_ACTION_NONE,
-								  xfer->account, xfer->who, NULL,
-								  xfer,
-								  G_CALLBACK(purple_xfer_choose_file),
-								  G_CALLBACK(cancel_recv_cb));
-
+		if (purple_xfer_get_thumbnail_data(xfer)) {
+			purple_request_accept_cancel_with_icon(xfer, NULL, buf, NULL,
+				PURPLE_DEFAULT_ACTION_NONE, xfer->account, xfer->who, NULL,
+				purple_xfer_get_thumbnail_data(xfer),
+				purple_xfer_get_thumbnail_size(xfer), xfer, 
+				G_CALLBACK(purple_xfer_choose_file),
+				G_CALLBACK(cancel_recv_cb));
+		} else {
+			purple_request_accept_cancel(xfer, NULL, buf, NULL,
+				PURPLE_DEFAULT_ACTION_NONE, xfer->account, xfer->who, NULL,
+				xfer, G_CALLBACK(purple_xfer_choose_file),
+				G_CALLBACK(cancel_recv_cb));
+		}
+			
 		g_free(buf);
 	} else
 		purple_xfer_choose_file(xfer);
@@ -472,10 +513,12 @@
 		{
 			gchar* message = NULL;
 			PurpleBuddy *buddy = purple_find_buddy(xfer->account, xfer->who);
+
 			message = g_strdup_printf(_("%s is offering to send file %s"),
 				buddy ? purple_buddy_get_alias(buddy) : xfer->who, purple_xfer_get_filename(xfer));
-			purple_xfer_conversation_write(xfer, message, FALSE);
+			purple_xfer_conversation_write_with_thumbnail(xfer, message);
 			g_free(message);
+
 			/* Ask for a filename to save to if it's not already given by a plugin */
 			if (xfer->local_filename == NULL)
 				purple_xfer_ask_recv(xfer);
@@ -1459,6 +1502,35 @@
 		ui_ops->update_progress(xfer, purple_xfer_get_progress(xfer));
 }
 
+const void *
+purple_xfer_get_thumbnail_data(const PurpleXfer *xfer)
+{
+	return xfer->thumbnail_data;
+}
+
+gsize
+purple_xfer_get_thumbnail_size(const PurpleXfer *xfer)
+{
+	return xfer->thumbnail_size;
+}
+
+void
+purple_xfer_set_thumbnail(PurpleXfer *xfer, gconstpointer thumbnail,
+	gsize size)
+{
+	if (thumbnail && size > 0) {
+		xfer->thumbnail_data = g_memdup(thumbnail, size);
+		xfer->thumbnail_size = size;
+	}
+}
+
+void
+purple_xfer_prepare_thumbnail(PurpleXfer *xfer)
+{
+	if (xfer->ui_ops->add_thumbnail) {
+		xfer->ui_ops->add_thumbnail(xfer);
+	}
+}
 
 /**************************************************************************
  * File Transfer Subsystem API