Plugins: dcopResponder.py

File dcopResponder.py, 3.8 kB (added by Andy Caldwell, 3 months ago)

DCOP responder (KDE app integration)

Line 
1 # -*- coding: utf-8 -*-
2 # Copyright 2007 Andy Caldwell <andrew.caldwell at sjc.ox.ac.uk>
3 #
4 #
5 # This plugin allows KDE applications (initially designed for KMess) to
6 # communicate with QuodLibet through DCOP calls.  It currently responds to
7 # the following DCOP requests:
8 #
9 #   + artist()  - returns current artist
10 #   + album()   - returns current album
11 #   + title()   - returns current track title
12 #   + playing() - returns true if a track is currently playing and false
13 #                 if the current track is paused or stopped
14 #
15 # These functions can easily be added to.  Just add to the DCopServer's
16 # self.setMethods arguments and add the respective functions to its
17 # methods.  DCOP calls can take strings, integers or bools as arguments
18 # and can return strings, integers, bools or void.
19 #
20 # As it stands, the plugin is running all the time.  Even if it is
21 # disabled in the plugins menu.  This should really be changed...
22 #
23 # ------------------------------------------------------------------------
24 # This program is free software; you can redistribute it and/or modify
25 # it under the terms of the GNU General Public License version 2 as
26 # published by the Free Software Foundation
27
28
29 # pcop must be imported before pydcop (prevents segfault)
30 import pcop
31 import pydcop
32 from time import sleep
33 import threading
34 from player import playlist as player
35 from parse import XMLFromPattern
36 from plugins.events import EventPlugin
37
38 class DCopResponder(EventPlugin):
39     PLUGIN_ID = "DCopResponder"
40     PLUGIN_NAME = _("DCOP Responder")
41     PLUGIN_DESC = _("Reply to DCOP calls from external KDE apps.\n"
42                     "By Andy Caldwell")
43     PLUGIN_VERSION = "0.1"
44
45     def __init__(self):
46         self.appid = pydcop.registerAs('QuodLibet')
47         self.server = DCopServer()
48         self.firststart = True
49         if player.song is not None:
50             self.server.artist = player.song["artist"]
51             self.server.title = player.song["title"]
52             self.server.album = player.song["album"]
53             self.server.playing = False
54    
55     def enabled(self):
56         self.server.setEnabled(True)
57    
58     def disabled(self):
59         self.server.setEnabled(False)
60        
61     def plugin_on_song_started(self, song):
62         self.server.song = song
63         self.server.artist = song["artist"]
64         self.server.title = song["title"]
65         self.server.album = song["album"]
66         if self.firststart:
67             self.firststart = False
68         else:
69             self.server.playing = True
70
71     def plugin_on_song_ended(self, song, stopped):
72         if stopped:
73             self.server.playing = False
74
75     def plugin_on_paused(self):
76         self.server.playing = False
77
78     def plugin_on_unpaused(self):
79         self.server.playing = True
80
81 class DCopServer(pydcop.DCOPServerObject):
82     def __init__(self):
83         pydcop.DCOPServerObject.__init__(self,'quodlibet')
84         self.setMethods( [
85             ('QString artist()', self.getArtist),
86             ('QString album()', self.getAlbum),
87             ('QString title()', self.getTitle),
88             ('bool playing()', self.isPlaying)
89             ] )
90         self._enabled = False
91         self.playing = False
92         self.song = ""
93         self.artist = ""
94         self.title = ""
95         self.album = ""
96         self.DCopEventProcessor = DCopEventProcessLoop()
97         self.DCopEventProcessor.setDaemon(True)
98         self.DCopEventProcessor.start()
99        
100     def getArtist(self):
101         return str(self.artist)
102    
103     def getAlbum(self):
104         return str(self.album)
105    
106     def getTitle(self):
107         return str(self.title)
108    
109     def isPlaying(self):
110         return self.playing
111
112     def setEnabled(self, state):
113         self._enabled = state
114
115 class DCopEventProcessLoop(threading.Thread):
116     def run(self):
117         while 1:
118             sleep(0.01)
119             pydcop.processEvents()
120