#!/usr/bin/env python
#+---------------------------------------------+
#| Pidgin Banshee Status
#+---------------------------------------------+
#| Written by Alec Hussey
#| Copyright (C) 2008 MadSoft
#| Website: www.madsoft.org
#| License: GNU General Public License v3
#+---------------------------------------------+

import dbus, gobject
from dbus.mainloop.glib import DBusGMainLoop

class BansheeStatusPlugin:
	def __init__(self):
		bus = dbus.SessionBus()
		bus.add_signal_receiver(self.bansheeStateChanged,
		                        dbus_interface="org.bansheeproject.Banshee.PlayerEngine",
		                        signal_name="StateChanged")
		
		self.pidgin = bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
		self.pidgin = dbus.Interface(self.pidgin, "im.pidgin.purple.PurpleInterface")
		self.banshee = bus.get_object("org.bansheeproject.Banshee", "/org/bansheeproject/Banshee/PlayerEngine")
	
	def __resetPidginStatusMessage(self, message):
		# Set new status message for the currently playing song
		status = self.pidgin.PurpleSavedstatusGetCurrent()
		self.pidgin.PurpleSavedstatusSetMessage(status, "Banshee: " + message)
		self.pidgin.PurpleSavedstatusActivate(status)
	
	def bansheeStateChanged(self, state):
		# Loop through all active accounts
		for account in self.pidgin.PurpleAccountsGetAllActive():
			# Get currently playing song from banshee
			currentTrack = self.banshee.GetCurrentTrack()
			
			# Reset pidgin status message with current track information
			if state == "playing":
				self.__resetPidginStatusMessage(currentTrack['artist'] + " - " + currentTrack['name'])
			elif state == "paused":
				self.__resetPidginStatusMessage("Paused")

if __name__ == "__main__":
	# Setup dbus glib mainloop
	dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
	
	# Create new instance of our plugin class
	BansheeStatusPlugin()
	
	# Get main loop and run it
	gobject.MainLoop().run()
