view vixm/rb_dbus.py @ 43:d3ac3a46a294 pure

Abuse the Rhythmbox dbus messages to announce what is being played
author Josef "Jeff" Sipek <jeffpc@josefsipek.net>
date Mon, 06 Nov 2006 04:50:55 -0500
parents
children 8396cc3816ee
line wrap: on
line source

import time
from threading import Thread

import gobject
import dbus
import dbus.service
if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
	import dbus.glib

class PlayerObject(dbus.service.Object):
	def __init__(self, bus_name,
			object_path='/org/gnome/Rhythmbox/Player'):
		dbus.service.Object.__init__(self, bus_name, object_path)

	@dbus.service.signal('org.gnome.Rhythmbox.Player')
	def playingChanged(self, yesno):
		pass

	@dbus.service.signal('org.gnome.Rhythmbox.Player')
	def playingUriChanged(self, uri):
		pass
	
class RhythmboxObject(dbus.service.Object):
	def __init__(self, bus_name,
			object_path='/org/gnome/Rhythmbox'):
		dbus.service.Object.__init__(self, bus_name, object_path)

class ShellObject(dbus.service.Object):
	def __init__(self, bus_name,
			object_path='/org/gnome/Rhythmbox/Shell'):
		dbus.service.Object.__init__(self, bus_name, object_path)

		self.title	= ""
		self.artist	= ""

	@dbus.service.method('org.gnome.Rhythmbox.Shell')
	def getSongProperties(self, uri):
		return dbus.Dictionary({"title": dbus.Variant(self.title), \
			"artist": dbus.Variant(self.artist)},\
			    key_type=dbus.String, value_type=dbus.Variant)

	@dbus.service.signal('org.gnome.Rhythmbox.Shell')
	def visibilityChanged(self, yesno):
		pass

class RhythmboxDbusThread(Thread):
	def __init__(self, play):
		Thread.__init__(self)

		NAMESPACE = 'org.gnome.Rhythmbox'
		PLAYER_NS = NAMESPACE + ".Player"
		SHELL_NS  = NAMESPACE + ".Shell"

		session_bus = dbus.SessionBus()

		rb_bus = dbus.service.BusName(NAMESPACE, bus=session_bus)
		rb_object = RhythmboxObject(rb_bus)
		
		player_bus = dbus.service.BusName(PLAYER_NS, bus=session_bus)
		player_object = PlayerObject(player_bus)
		
		shell_bus = dbus.service.BusName(SHELL_NS, bus=session_bus)
		shell_object = ShellObject(shell_bus)
		
		self.__rb_obj = rb_object
		self.__player_obj = player_object
		self.__shell_obj = shell_object

		shell_object.visibilityChanged(True)
		player_object.playingChanged(False)

		self.__title  = ""
		self.__artist = ""
		self.__uri    = ""

		self.__main   = gobject.MainLoop()

		self.__play = play

	def notify(self, uri, title, artist):
		self.__uri    = uri
		self.__title  = title
		self.__artist = artist

	def run(self):
		while not self.__play.shutdown:
			if self.__title != self.__shell_obj.title:
				self.__shell_obj.title = self.__title
				self.__shell_obj.artist = self.__artist

				self.__player_obj.playingUriChanged(self.__uri)
				self.__player_obj.playingChanged(True)

			# if there is something to process, do so
			c = self.__main.get_context()
			if c.pending():
				c.iteration()

			time.sleep(0.2)