fixed seeking with buttons when paused
[panucci.git] / src / panucci / dbusinterface.py
blob5af8d7d75d0159016c2331f1ccaf87d3f39ddd94
1 # -*- coding: utf-8 -*-
3 # This file is part of Panucci.
4 # Copyright (c) 2008-2011 The Panucci Project
6 # Panucci is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # Panucci is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Panucci. If not, see <http://www.gnu.org/licenses/>.
19 from __future__ import absolute_import
21 import logging
22 import dbus
23 import dbus.service
25 session_bus = dbus.SessionBus()
27 class panucciInterface(dbus.service.Object):
28 """ Panucci's d-bus interface """
29 HEADSET_NAME = 'org.freedesktop.Hal'
30 HEADSET_PATH = '/org/freedesktop/Hal/devices/platform_retu_headset_logicaldev_input'
31 HEADSET_INTF = 'org.freedesktop.Hal.Device'
33 def __init__(self, bus_name, path='/panucciInterface'):
34 self.__log = logging.getLogger('panucci.dbusinterface.panucciInterface')
35 dbus.service.Object.__init__(self, object_path=path, bus_name=bus_name)
37 self.player = None
38 self.gui = None
39 self.headset_device = None
41 try:
42 bus = dbus.SystemBus()
43 headset = bus.get_object(self.HEADSET_NAME, self.HEADSET_PATH)
44 self.headset_device = dbus.Interface(headset, self.HEADSET_INTF)
45 except Exception, e:
46 self.__log.debug('Could not find headset object (on Maemo)')
48 def register_player(self, player):
49 self.__log.debug('Registered player.')
50 self.player = player
52 def register_gui(self, gui):
53 self.__log.debug('Registered GUI.')
54 self.gui = gui
56 def start_service_by_name_noblock(
57 self, service_name, reply_handler=None, error_handler=None ):
58 # it's dbus.SessionBus.start_service_by_name except it doesn't block
60 return session_bus.call_async(
61 dbus.BUS_DAEMON_NAME, dbus.BUS_DAEMON_PATH, dbus.BUS_DAEMON_IFACE,
62 'StartServiceByName', 'su', ( service_name, 0 ), None, None )
64 @dbus.service.method('org.panucci.panucciInterface')
65 def play(self):
66 self.__log.debug('play() called')
67 if self.player is not None: self.player.play()
69 @dbus.service.method('org.panucci.panucciInterface')
70 def pause(self):
71 self.__log.debug('pause() called')
72 if self.player is not None: self.player.pause()
74 @dbus.service.method('org.panucci.panucciInterface')
75 def stop(self):
76 self.__log.debug('stop() called')
77 if self.player is not None: self.player.stop()
79 @dbus.service.method('org.panucci.panucciInterface')
80 def playPause(self):
81 self.__log.debug('playPause() called')
82 if self.player is not None: self.player.play_pause_toggle()
84 @dbus.service.method('org.panucci.panucciInterface')
85 def playNext(self):
86 self.__log.debug('playNext() called')
87 if self.player is not None: self.player.play_next()
89 @dbus.service.method('org.panucci.panucciInterface')
90 def playPrev(self):
91 self.__log.debug('playPrev() called')
92 if self.player is not None: self.player.play_prev()
94 @dbus.service.method('org.panucci.panucciInterface')
95 def backShort(self):
96 self.__log.debug('backShort() called')
97 seek_amount = self.gui.config.getint("options", "seek_short")
98 if self.player is not None: self.player.do_seek(from_current=-seek_amount*10**9)
100 @dbus.service.method('org.panucci.panucciInterface')
101 def forwardShort(self):
102 self.__log.debug('forwardShort() called')
103 seek_amount = self.gui.config.getint("options", "seek_short")
104 if self.player is not None: self.player.do_seek(from_current=seek_amount*10**9)
106 @dbus.service.method('org.panucci.panucciInterface')
107 def backLong(self):
108 self.__log.debug('backLong() called')
109 seek_amount = self.gui.config.getint("options", "seek_long")
110 if self.player is not None: self.player.do_seek(from_current=-seek_amount*10**9)
112 @dbus.service.method('org.panucci.panucciInterface')
113 def forwardLong(self):
114 self.__log.debug('forwardLong() called')
115 seek_amount = self.gui.config.getint("options", "seek_long")
116 if self.player is not None: self.player.do_seek(from_current=seek_amount*10**9)
118 @dbus.service.method('org.panucci.panucciInterface', in_signature='s')
119 def play_file(self, filepath):
120 self.__log.debug('play_file() called with: ' + filepath)
121 if self.player is not None:
122 self.player.playlist.load(filepath)
123 self.player.play()
125 @dbus.service.method('org.panucci.panucciInterface', in_signature='su')
126 def playback_from(self, uri, seconds):
127 """Playback an URI from a position (in seconds)
128 This sets the current file to "uri" and tries
129 to start playback from the position given by
130 the "seconds" parameter.
133 self.__log.debug('%s playback_from %d' % (uri, seconds))
134 if self.player is not None:
135 self.show_main_window()
137 new_file = (self.player.playlist.current_filepath != uri)
139 # I've diabled this for now as I've no possibility to debug it. /xerxes2
140 #if self.gui is not None:
141 # self.gui.set_progress_indicator(new_file)
143 if new_file:
144 self.player.playlist.load(uri)
145 self.player.playlist.last()
146 self.player.playlist.set_seek_to(seconds)
147 elif not self.player._is_playing:
148 self.player.play()
149 #else:
150 # self.player.do_seek(from_beginning=(10**9)*seconds)
152 @dbus.service.method('org.panucci.panucciInterface', in_signature='s')
153 def queue_file(self, filepath):
154 self.__log.debug('queue_file() called with: ' + filepath)
155 if self.player is not None: self.player.playlist.append(filepath)
157 @dbus.service.method('org.panucci.panucciInterface', in_signature='su')
158 def insert_file(self, pos, filepath):
159 self.__log.debug('insert_file() called')
160 if self.player is not None: self.player.playlist.insert(pos, filepath)
162 @dbus.service.method('org.panucci.panucciInterface', in_signature='sb')
163 def load_directory(self, directory, append):
164 self.__log.debug('load_directory() called')
165 if self.player is not None: self.player.playlist.load_directory(
166 directory, append )
168 @dbus.service.method('org.panucci.panucciInterface')
169 def show_main_window(self):
170 self.__log.debug('show_main_window() called')
171 if self.gui is not None: self.gui.show_main_window()
173 # Signals for gPodder's media player integration
174 @dbus.service.signal(dbus_interface='org.gpodder.player', signature='us')
175 def PlaybackStarted(self, position, file_uri):
176 pass
178 @dbus.service.signal(dbus_interface='org.gpodder.player', signature='uuus')
179 def PlaybackStopped(self, start_position, end_position, total_time, \
180 file_uri):
181 pass
183 @dbus.service.signal(dbus_interface='org.gpodder.player', signature='uussb')
184 def ChapterAdded(self, start_position, end_position, file_uri, name, \
185 advertising):
186 pass
188 @dbus.service.signal(dbus_interface='org.gpodder.player', signature='uus')
189 def ChapterRemoved(self, start_position, end_position, file_uri):
190 pass
192 interface = panucciInterface(
193 dbus.service.BusName('org.panucci.panucciInterface', session_bus) )