[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pidgin-commits
Subject:    /pidgin/main: b59120d66fdd: Refactor a painfully long Drag&Drop ...
From:       Jorge Villase?or <salinasv () pidgin ! im>
Date:       2016-01-23 19:06:34
Message-ID: hg.b59120d66fdd.1453575994.-874508059 () rock ! pidgin ! im
[Download RAW message or body]

Changeset: b59120d66fddba0391de34d6ce6540f82252495f
Author:	 Jorge Villase?or <salinasv@pidgin.im>
Date:	 2016-01-18 23:24 -0800
Branch:	 default
URL: https://hg.pidgin.im/pidgin/main/rev/b59120d66fdd

Description:

Refactor a painfully long Drag&Drop util function

This way it is easier to know what's going on, in preparation
of dropping our PurpleDesktopItem code.

diffstat:

 pidgin/gtkutils.c |  223 ++++++++++++++++++++++++++++-------------------------
 1 files changed, 117 insertions(+), 106 deletions(-)

diffs (254 lines):

diff --git a/pidgin/gtkutils.c b/pidgin/gtkutils.c
--- a/pidgin/gtkutils.c
+++ b/pidgin/gtkutils.c
@@ -1446,13 +1446,123 @@ static void dnd_set_icon_cancel_cb(_DndD
 	g_free(data);
 }
 
+static void
+pidgin_dnd_file_send_image(PurpleAccount *account, const gchar *who,
+		const gchar *filename)
+{
+	PurpleConnection *gc = purple_account_get_connection(account);
+	PurpleProtocol *protocol = NULL;
+	_DndData *data = g_malloc(sizeof(_DndData));
+	gboolean ft = FALSE, im = FALSE;
+
+	data->who = g_strdup(who);
+	data->filename = g_strdup(filename);
+	data->account = account;
+
+	if (gc)
+		protocol = purple_connection_get_protocol(gc);
+
+	if (!(purple_connection_get_flags(gc) & PURPLE_CONNECTION_FLAG_NO_IMAGES))
+		im = TRUE;
+
+	if (protocol && PURPLE_PROTOCOL_IMPLEMENTS(protocol, XFER_IFACE, can_receive))
+		ft = purple_protocol_xfer_iface_can_receive(protocol, gc, who);
+	else if (protocol && PURPLE_PROTOCOL_IMPLEMENTS(protocol, XFER_IFACE, send))
+		ft = TRUE;
+
+	if (im && ft) {
+		purple_request_choice(NULL, NULL,
+				_("You have dragged an image"),
+				_("You can send this image as a file "
+					"transfer, embed it into this message, "
+					"or use it as the buddy icon for this user."),
+				(gpointer)DND_FILE_TRANSFER, _("OK"),
+				(GCallback)dnd_image_ok_callback, _("Cancel"),
+				(GCallback)dnd_image_cancel_callback,
+				purple_request_cpar_from_account(account), data,
+				_("Set as buddy icon"), DND_BUDDY_ICON,
+				_("Send image file"), DND_FILE_TRANSFER,
+				_("Insert in message"), DND_IM_IMAGE,
+				NULL);
+	} else if (!(im || ft)) {
+		purple_request_yes_no(NULL, NULL, _("You have dragged an image"),
+				_("Would you like to set it as the buddy icon for this user?"),
+				PURPLE_DEFAULT_ACTION_NONE,
+				purple_request_cpar_from_account(account),
+				data, (GCallback)dnd_set_icon_ok_cb, (GCallback)dnd_set_icon_cancel_cb);
+	} else {
+		purple_request_choice(NULL, NULL,
+				_("You have dragged an image"),
+				(ft ? _("You can send this image as a file transfer, or use it as the buddy icon \
for this user.") : +				 _("You can insert this image into this message, or use it as \
the buddy icon for this user")), +				GINT_TO_POINTER(ft ? DND_FILE_TRANSFER : \
DND_IM_IMAGE), +				_("OK"), (GCallback)dnd_image_ok_callback,
+				_("Cancel"), (GCallback)dnd_image_cancel_callback,
+				purple_request_cpar_from_account(account),
+				data,
+				_("Set as buddy icon"), DND_BUDDY_ICON,
+				(ft ? _("Send image file") : _("Insert in message")), (ft ? DND_FILE_TRANSFER : \
DND_IM_IMAGE), +				NULL);
+	}
+
+}
+
+#ifndef _WIN32
+static void
+pidgin_dnd_file_send_desktop(PurpleAccount *account, const gchar *who,
+		PurpleDesktopItem *item)
+{
+	PurpleDesktopItemType dtype;
+	char key[64];
+	const char *itemname = NULL;
+
+	const char * const *langs;
+	langs = g_get_language_names();
+	if (langs[0]) {
+		g_snprintf(key, sizeof(key), "Name[%s]", langs[0]);
+		itemname = purple_desktop_item_get_string(item, key);
+	}
+
+	if (!itemname)
+		itemname = purple_desktop_item_get_string(item, "Name");
+
+	dtype = purple_desktop_item_get_entry_type(item);
+	switch (dtype) {
+		PurpleConversation *conv;
+		PidginConversation *gtkconv;
+
+		case PURPLE_DESKTOP_ITEM_TYPE_LINK:
+		conv = PURPLE_CONVERSATION(purple_im_conversation_new(account, who));
+		gtkconv =  PIDGIN_CONVERSATION(conv);
+		pidgin_webview_insert_link(PIDGIN_WEBVIEW(gtkconv->entry),
+				purple_desktop_item_get_string(item, "URL"),
+				itemname);
+		break;
+		default:
+		/* I don't know if we really want to do anything here.  Most of
+		 * the desktop item types are crap like "MIME Type" (I have no
+		 * clue how that would be a desktop item) and "Comment"...
+		 * nothing we can really send.  The only logical one is
+		 * "Application," but do we really want to send a binary and
+		 * nothing else? Probably not.  I'll just give an error and
+		 * return. */
+		/* The original patch sent the icon used by the launcher.  That's probably wrong \
*/ +		purple_notify_error(NULL, NULL, _("Cannot send launcher"),
+				_("You dragged a desktop launcher. Most "
+					"likely you wanted to send the target "
+					"of this launcher instead of this "
+					"launcher itself."), NULL);
+		break;
+	}
+}
+#endif /* _WIN32 */
+
 void
 pidgin_dnd_file_manage(GtkSelectionData *sd, PurpleAccount *account, const char \
*who)  {
 	GdkPixbuf *pb;
 	GList *files = purple_uri_list_extract_filenames((const gchar *) \
gtk_selection_data_get_data(sd));  PurpleConnection *gc = \
                purple_account_get_connection(account);
-	PurpleProtocol *protocol = NULL;
 #ifndef _WIN32
 	PurpleDesktopItem *item;
 #endif
@@ -1493,119 +1603,20 @@ pidgin_dnd_file_manage(GtkSelectionData 
 		/* Are we dealing with an image? */
 		pb = pidgin_pixbuf_new_from_file(filename);
 		if (pb) {
-			_DndData *data = g_malloc(sizeof(_DndData));
-			gboolean ft = FALSE, im = FALSE;
-
-			data->who = g_strdup(who);
-			data->filename = g_strdup(filename);
-			data->account = account;
-
-			if (gc)
-				protocol = purple_connection_get_protocol(gc);
-
-			if (!(purple_connection_get_flags(gc) & PURPLE_CONNECTION_FLAG_NO_IMAGES))
-				im = TRUE;
-
-			if (protocol && PURPLE_PROTOCOL_IMPLEMENTS(protocol, XFER_IFACE, can_receive))
-				ft = purple_protocol_xfer_iface_can_receive(protocol, gc, who);
-			else if (protocol && PURPLE_PROTOCOL_IMPLEMENTS(protocol, XFER_IFACE, send))
-				ft = TRUE;
-
-			if (im && ft) {
-				purple_request_choice(NULL, NULL,
-					_("You have dragged an image"),
-					_("You can send this image as a file "
-					"transfer, embed it into this message, "
-					"or use it as the buddy icon for this user."),
-					(gpointer)DND_FILE_TRANSFER, _("OK"),
-					(GCallback)dnd_image_ok_callback, _("Cancel"),
-					(GCallback)dnd_image_cancel_callback,
-					purple_request_cpar_from_account(account), data,
-					_("Set as buddy icon"), DND_BUDDY_ICON,
-					_("Send image file"), DND_FILE_TRANSFER,
-					_("Insert in message"), DND_IM_IMAGE,
-					NULL);
-			} else if (!(im || ft))
-				purple_request_yes_no(NULL, NULL, _("You have dragged an image"),
-							_("Would you like to set it as the buddy icon for this user?"),
-							PURPLE_DEFAULT_ACTION_NONE,
-							purple_request_cpar_from_account(account),
-							data, (GCallback)dnd_set_icon_ok_cb, (GCallback)dnd_set_icon_cancel_cb);
-			else
-				purple_request_choice(NULL, NULL,
-						    _("You have dragged an image"),
-						    (ft ? _("You can send this image as a file transfer, or use it as the \
                buddy icon for this user.") :
-						    _("You can insert this image into this message, or use it as the buddy \
                icon for this user")),
-						    GINT_TO_POINTER(ft ? DND_FILE_TRANSFER : DND_IM_IMAGE),
-							_("OK"), (GCallback)dnd_image_ok_callback,
-						    _("Cancel"), (GCallback)dnd_image_cancel_callback,
-							purple_request_cpar_from_account(account),
-							data,
-						    _("Set as buddy icon"), DND_BUDDY_ICON,
-						    (ft ? _("Send image file") : _("Insert in message")), (ft ? \
                DND_FILE_TRANSFER : DND_IM_IMAGE),
-							NULL);
+			pidgin_dnd_file_send_image(account, who, filename);
+
 			g_object_unref(G_OBJECT(pb));
 
-			g_free(basename);
-			while (files) {
-				g_free(files->data);
-				files = g_list_delete_link(files, files);
-			}
-			return;
+			continue;
 		}
 
 #ifndef _WIN32
 		/* Are we trying to send a .desktop file? */
 		else if (purple_str_has_suffix(basename, ".desktop") && (item = \
                purple_desktop_item_new_from_file(filename))) {
-			PurpleDesktopItemType dtype;
-			char key[64];
-			const char *itemname = NULL;
-
-			const char * const *langs;
-			langs = g_get_language_names();
-			if (langs[0]) {
-				g_snprintf(key, sizeof(key), "Name[%s]", langs[0]);
-				itemname = purple_desktop_item_get_string(item, key);
-			}
-
-			if (!itemname)
-				itemname = purple_desktop_item_get_string(item, "Name");
-
-			dtype = purple_desktop_item_get_entry_type(item);
-			switch (dtype) {
-				PurpleConversation *conv;
-				PidginConversation *gtkconv;
-
-			case PURPLE_DESKTOP_ITEM_TYPE_LINK:
-				conv = PURPLE_CONVERSATION(purple_im_conversation_new(account, who));
-				gtkconv =  PIDGIN_CONVERSATION(conv);
-				pidgin_webview_insert_link(PIDGIN_WEBVIEW(gtkconv->entry),
-				                        purple_desktop_item_get_string(item, "URL"),
-				                        itemname);
-				break;
-			default:
-				/* I don't know if we really want to do anything here.  Most of
-				 * the desktop item types are crap like "MIME Type" (I have no
-				 * clue how that would be a desktop item) and "Comment"...
-				 * nothing we can really send.  The only logical one is
-				 * "Application," but do we really want to send a binary and
-				 * nothing else? Probably not.  I'll just give an error and
-				 * return. */
-				/* The original patch sent the icon used by the launcher.  That's probably wrong \
                */
-				purple_notify_error(NULL, NULL, _("Cannot send launcher"),
-				                    _("You dragged a desktop launcher. Most "
-				                      "likely you wanted to send the target "
-				                      "of this launcher instead of this "
-				                      "launcher itself."), NULL);
-				break;
-			}
+			pidgin_dnd_file_send_desktop(account, who, item);
 			purple_desktop_item_unref(item);
-			g_free(basename);
-			while (files) {
-				g_free(files->data);
-				files = g_list_delete_link(files, files);
-			}
-			return;
+
+			continue;
 		}
 #endif /* _WIN32 */
 

_______________________________________________
Commits mailing list
Commits@pidgin.im
https://pidgin.im/cgi-bin/mailman/listinfo/commits


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic