View Single Post
b666m's Avatar
Posts: 1,090 | Thanked: 476 times | Joined on Jan 2010 @ Ingolstadt, Germany
#63
NEW STUFF INCLUDED:
- only showing notification if conversation hasn't focus
- notification timeout is set to 3 seconds
- replacing new-line-characters with spaces
- if the length exceeds the maximum a "..." will be added at the end

THINGS THAT WILL NOT BE DONE:
- no buddy avatar in notification (can't convert the image to .png). the pidgin-icon is used
- clicking the notification only closes it - can't send any useful dbus-message (but if someone has an idea - just tell me ^^)

OPEN POINT LIST:
- msn: cut the formatting string from messages
- xmpp: cut the <body> </body> tag from messages

SCREENSHOT:


CURRENT CODE:
Code:
#!/usr/bin/env python

def cb (Notification=None, action=None, Data=None):
	pass

def my_func(account, sender, message, conversation, flags):

	if bus.pidginbus.PurpleConversationHasFocus(conversation) == 0:
	
		buddy = bus.pidginbus.PurpleFindBuddy(account,str(sender.split("@")[0]))
		name = bus.pidginbus.PurpleBuddyGetAlias(buddy)
		#icon = bus.pidginbus.PurpleBuddyGetIcon(buddy)
		#icon_path = bus.pidginbus.PurpleBuddyIconGetFullPath(icon)
		conv = str(conversation)
		
		msg = message.replace("\n"," ")
		msg = "\""+msg
		if len(msg) > 32:
			msg = msg[:32]+"...\""

		print name, "("+sender+") said \""+message+"\""
		#with icon at", icon_path, "in conv", conv

	    # it's only commented out for test-reasons because i only try it on ubuntu and console with print
        pynotify.init(os.path.splitext(os.path.basename(sys.argv[0]))[0])
		n = pynotify.Notification(name+" ("+sender+")",msg,"pidgin")
        # -------------------------
        # maybe the next two lines can be used for bringing the conversation window to the foreground 
        # when the notification is being clicked by the user
        # if that is not possible: comment them out or delete them (no use for them)
		n.set_hint_string("dbus-callback-default","im.pidgin.purple.PurpleService /im/pidgin/purple/PurpleObject im.pidgin.purple.PurpleInterface purple_conversation_present int32:"+conv)
		# n.set_hint_string("dbus-callback-default","com.nokia.osso_browser /com/nokia/osso_browser com.nokia.osso_browser open_new_window string:\"callto://666\"")
		n.add_action("default", "im", cb)
		n.set_timeout(3000)
        # or do i have to make an "add_action" for the notification?
        # maybe i can just put three NULL arguments there ^^
        # ------------------------
		n.show()

import os
import sys
import gobject, dbus
import pynotify

from dbus.mainloop.glib import DBusGMainLoop

dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
obj = bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
bus.pidginbus = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")

bus.add_signal_receiver(my_func,
                        dbus_interface="im.pidgin.purple.PurpleInterface",
                        signal_name="ReceivedImMsg")

loop = gobject.MainLoop()
loop.run()
attention: start pidgin first (or the script won't run)!
you can run this script in xterm via "python scriptname".
you maybe could autorun it via upstart if pidgin sends something when launching (just look for "event.d" or "upstart" here in the forum)
and maybe someone could wrap up a package for installing via packagemanager (:
 

The Following User Says Thank You to b666m For This Useful Post: