File 0008-Make-notifications-work-in-chat-rooms.patch of Package pidgin-libnotify

From 2fdb74f5c60e40f240307ba54d47ea495d43de94 Mon Sep 17 00:00:00 2001
From: Vincent Untz <vuntz@gnome.org>
Date: Mon, 17 Sep 2012 17:44:03 +0200
Subject: [PATCH 08/10] Make notifications work in chat rooms

This is a bit ugly, as we don't necessarily have a buddy for chat
rooms...
---
 src/pidgin-libnotify.c | 115 ++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 84 insertions(+), 31 deletions(-)

diff --git a/src/pidgin-libnotify.c b/src/pidgin-libnotify.c
index ec37607..1e09780 100644
--- a/src/pidgin-libnotify.c
+++ b/src/pidgin-libnotify.c
@@ -187,6 +187,17 @@ pixbuf_from_buddy_icon (PurpleBuddyIcon *buddy_icon)
 }
 
 static void
+notify_deleting_conversation_cb (PurpleConversation *conv,
+				 gpointer data)
+{
+    NotifyNotification *notification;
+
+    notification = g_hash_table_lookup (buddy_hash, conv);
+    if (notification != NULL)
+	notify_notification_close (notification, NULL);
+}
+
+static void
 action_cb (NotifyNotification *notification,
 		   gchar *action, gpointer user_data)
 {
@@ -197,19 +208,21 @@ action_cb (NotifyNotification *notification,
 					"notification: 0x%lx, action: '%s'", (unsigned long)notification, action);
 
 	buddy = (PurpleBuddy *)g_object_get_data (G_OBJECT(notification), "buddy");
-
-	if (!buddy) {
-		purple_debug_warning (PLUGIN_ID, "Got no buddy!");
+	conv = (PurpleConversation *)g_object_get_data (G_OBJECT(notification), "conv");
+
+	if (buddy) {
+		conv = purple_find_conversation_with_account (PURPLE_CONV_TYPE_ANY, buddy->name, buddy->account);
+
+		if (!conv) {
+			conv = purple_conversation_new (PURPLE_CONV_TYPE_IM,
+										  buddy->account,
+										  buddy->name);
+		}
+	} else if (!conv) {
+		purple_debug_warning (PLUGIN_ID, "Got no buddy nor conv!");
 		return;
 	}
 
-	conv = purple_find_conversation_with_account (PURPLE_CONV_TYPE_ANY, buddy->name, buddy->account);
-
-	if (!conv) {
-		conv = purple_conversation_new (PURPLE_CONV_TYPE_IM,
-									  buddy->account,
-									  buddy->name);
-	}
 	conv->ui_ops->present (conv);
 
 	notify_notification_close (notification, NULL);
@@ -219,12 +232,16 @@ static gboolean
 closed_cb (NotifyNotification *notification)
 {
 	PurpleContact *contact;
+	PurpleConversation *conv = NULL;
 
 	purple_debug_info (PLUGIN_ID, "closed_cb(), notification: 0x%lx\n", (unsigned long)notification);
 
 	contact = (PurpleContact *)g_object_get_data (G_OBJECT(notification), "contact");
+	conv = (PurpleConversation *)g_object_get_data (G_OBJECT(notification), "conv");
 	if (contact)
 		g_hash_table_remove (buddy_hash, contact);
+	else if (conv)
+		g_hash_table_remove (buddy_hash, conv);
 
 	g_object_unref (G_OBJECT(notification));
 
@@ -274,23 +291,27 @@ should_notify_unavailable (PurpleAccount *account)
 static void
 notify (const gchar *title,
 		const gchar *body,
-		PurpleBuddy *buddy)
+		PurpleBuddy *buddy,
+		PurpleConversation *conv)
 {
 	NotifyNotification *notification = NULL;
 	GdkPixbuf *icon;
 	PurpleBuddyIcon *buddy_icon;
 	gchar *tr_body;
 	PurpleContact *contact;
-	PurpleConversation *conv = NULL;
 
-	contact = purple_buddy_get_contact (buddy);
+	if (buddy)
+		contact = purple_buddy_get_contact (buddy);
+	else
+		contact = NULL;
 
 	if (body)
 		tr_body = truncate_escape_string (body, 60);
 	else
 		tr_body = NULL;
 
-	conv = purple_find_conversation_with_account (PURPLE_CONV_TYPE_ANY, buddy->name, buddy->account);
+	if (!conv && buddy)
+		conv = purple_find_conversation_with_account (PURPLE_CONV_TYPE_ANY, buddy->name, buddy->account);
 
 	if (conv && conv->ui_ops && conv->ui_ops->has_focus) {
 	    if (conv->ui_ops->has_focus(conv) == TRUE) {
@@ -299,7 +320,12 @@ notify (const gchar *title,
 	    }
 	}
 
-	notification = g_hash_table_lookup (buddy_hash, contact);
+	if (contact)
+		notification = g_hash_table_lookup (buddy_hash, contact);
+	else if (conv)
+		notification = g_hash_table_lookup (buddy_hash, conv);
+	else
+		notification = NULL;
 
 	if (notification != NULL) {
 		notify_notification_update (notification, title, tr_body, NULL);
@@ -309,7 +335,7 @@ notify (const gchar *title,
 
 		purple_debug_info (PLUGIN_ID, "notify(), update: "
 						 "title: '%s', body: '%s', buddy: '%s'\n",
-						 title, tr_body, best_name (buddy));
+						 title, tr_body, buddy ? best_name (buddy) : "");
 
 		g_free (tr_body);
 		return;
@@ -321,17 +347,27 @@ notify (const gchar *title,
 #endif
 	purple_debug_info (PLUGIN_ID, "notify(), new: "
 					 "title: '%s', body: '%s', buddy: '%s'\n",
-					 title, tr_body, best_name (buddy));
+					 title, tr_body, buddy ? best_name (buddy) : "");
 
 	g_free (tr_body);
 
-	buddy_icon = purple_buddy_get_icon (buddy);
+	if (buddy)
+		buddy_icon = purple_buddy_get_icon (buddy);
+	else
+		buddy_icon = NULL;
+
 	if (buddy_icon) {
 		icon = pixbuf_from_buddy_icon (buddy_icon);
 		purple_debug_info (PLUGIN_ID, "notify(), has a buddy icon.\n");
-	} else {
+	} else if (buddy) {
 		icon = pidgin_create_prpl_icon (buddy->account, 1);
 		purple_debug_info (PLUGIN_ID, "notify(), has a prpl icon.\n");
+	} else if (conv) {
+		icon = pidgin_create_prpl_icon (conv->account, 1);
+		purple_debug_info (PLUGIN_ID, "notify(), has a prpl icon.\n");
+	} else {
+		icon = NULL;
+		purple_debug_info (PLUGIN_ID, "notify(), has no icon.\n");
 	}
 
 	if (icon) {
@@ -341,9 +377,13 @@ notify (const gchar *title,
 		purple_debug_warning (PLUGIN_ID, "notify(), couldn't find any icon!\n");
 	}
 
-	g_hash_table_insert (buddy_hash, contact, notification);
+	if (contact)
+		g_hash_table_insert (buddy_hash, contact, notification);
+	else if (conv)
+		g_hash_table_insert (buddy_hash, conv, notification);
 
 	g_object_set_data (G_OBJECT(notification), "contact", contact);
+	g_object_set_data (G_OBJECT(notification), "conv", conv);
 	g_object_set_data (G_OBJECT(notification), "buddy", buddy);
 
 	g_signal_connect (notification, "closed", G_CALLBACK(closed_cb), NULL);
@@ -385,7 +425,7 @@ notify_buddy_signon_cb (PurpleBuddy *buddy,
 
 	title = g_strdup_printf (_("%s signed on"), tr_name);
 
-	notify (title, NULL, buddy);
+	notify (title, NULL, buddy, NULL);
 
 	g_free (tr_name);
 	g_free (title);
@@ -417,7 +457,7 @@ notify_buddy_signoff_cb (PurpleBuddy *buddy,
 
 	title = g_strdup_printf (_("%s signed off"), tr_name);
 
-	notify (title, NULL, buddy);
+	notify (title, NULL, buddy, NULL);
 
 	g_free (tr_name);
 	g_free (title);
@@ -425,6 +465,7 @@ notify_buddy_signoff_cb (PurpleBuddy *buddy,
 
 static void
 notify_msg_sent (PurpleAccount *account,
+				 PurpleConversation *conv,
 				 const gchar *sender,
 				 const gchar *message)
 {
@@ -432,28 +473,32 @@ notify_msg_sent (PurpleAccount *account,
 	gchar *title, *body, *tr_name;
 	gboolean blocked;
 
-	buddy = purple_find_buddy (account, sender);
-	if (!buddy)
-		return;
-
 	blocked = purple_prefs_get_bool ("/plugins/gtk/libnotify/blocked");
 	if (blocked && !purple_privacy_check(account, sender))
 		return;
 
-	tr_name = truncate_escape_string (best_name (buddy), 25);
+	buddy = purple_find_buddy (account, sender);
+	if (buddy)
+		tr_name = truncate_escape_string (best_name (buddy), 25);
+	else if (conv) {
+		char *name = g_strdup_printf (_("%s (%s)"), sender, purple_conversation_get_name (conv));
+		tr_name = truncate_escape_string (name, 25);
+		g_free (name);
+	} else
+		tr_name = truncate_escape_string (sender, 25);
 
 	if (purple_prefs_get_bool("/plugins/gtk/libnotify/newmsgtxt")) {
 		title = g_strdup_printf (_("%s says:"), tr_name);
 		body = purple_markup_strip_html (message);
 
-		notify (title, body, buddy);
+		notify (title, body, buddy, conv);
 
 		g_free (title);
 	} else {
 		title = _("new message received");
 		body = g_strdup_printf (_("from %s"), tr_name);
 
-		notify (title, body, buddy);
+		notify (title, body, buddy, conv);
 	}
 	g_free (tr_name);
 	g_free (body);
@@ -488,7 +533,7 @@ notify_new_message_cb (PurpleAccount *account,
 	if (!should_notify_unavailable (account))
 		return;
 
-	notify_msg_sent (account, sender, message);
+	notify_msg_sent (account, conv, sender, message);
 }
 
 static void
@@ -507,7 +552,7 @@ notify_chat_nick (PurpleAccount *account,
 	if (!g_strstr_len (message, strlen(message), nick))
 		return;
 
-	notify_msg_sent (account, sender, message);
+	notify_msg_sent (account, conv, sender, message);
 }
 
 static gboolean
@@ -538,6 +583,10 @@ plugin_load (PurplePlugin *plugin)
 	purple_signal_connect (conv_handle, "received-chat-msg", plugin,
 						PURPLE_CALLBACK(notify_chat_nick), NULL);
 
+	purple_signal_connect (conv_handle, "deleting-conversation", plugin,
+						PURPLE_CALLBACK(notify_deleting_conversation_cb), NULL);
+
+
 	/* used just to not display the flood of guifications we'd get */
 	purple_signal_connect (conn_handle, "signed-on", plugin,
 						PURPLE_CALLBACK(event_connection_throttle), NULL);
@@ -566,6 +615,10 @@ plugin_unload (PurplePlugin *plugin)
 	purple_signal_disconnect (conv_handle, "received-chat-msg", plugin,
 							PURPLE_CALLBACK(notify_chat_nick));
 
+	purple_signal_disconnect (conv_handle, "deleting-conversation", plugin,
+							PURPLE_CALLBACK(notify_deleting_conversation_cb));
+
+
 	purple_signal_disconnect (conn_handle, "signed-on", plugin,
 							PURPLE_CALLBACK(event_connection_throttle));
 
-- 
1.8.4.5

openSUSE Build Service is sponsored by