1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
# -*- coding: utf-8 -*-
## Totem D-Bus plugin
## Copyright (C) 2009 Lucky <lucky1.data@gmail.com>
## Copyright (C) 2009 Philip Withnall <philip@tecnocode.co.uk>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin St, Fifth Floor,
## Boston, MA 02110-1301 USA.
##
## Sunday 13th May 2007: Bastien Nocera: Add exception clause.
## See license_change file for details.
import gettext
from gi.repository import GObject, Peas, Totem # pylint: disable=no-name-in-module
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
gettext.textdomain ("totem")
_ = gettext.gettext
class DbusService (GObject.Object, Peas.Activatable):
__gtype_name__ = 'DbusService'
object = GObject.property (type = GObject.Object)
def __init__ (self):
GObject.Object.__init__ (self)
self.root = None
def do_activate (self):
DBusGMainLoop (set_as_default = True)
name = dbus.service.BusName ('org.mpris.MediaPlayer2.totem',
bus = dbus.SessionBus ())
self.root = Root (name, self.object)
def do_deactivate (self):
# Ensure we don't leak our paths on the bus
self.root.disconnect ()
class Root (dbus.service.Object): # pylint: disable=R0904
def __init__ (self, name, totem):
dbus.service.Object.__init__ (self, name, '/org/mpris/MediaPlayer2')
self.totem = totem
self.null_metadata = {
'year' : '', 'tracknumber' : '', 'location' : '',
'title' : '', 'album' : '', 'time' : '', 'genre' : '',
'artist' : ''
}
self.current_metadata = self.null_metadata.copy ()
self.current_position = 0
totem.connect ('metadata-updated', self.__do_update_metadata)
totem.connect ('notify::playing', self.__do_notify_playing)
totem.connect ('notify::seekable', self.__do_notify_seekable)
totem.connect ('notify::current-mrl', self.__do_notify_current_mrl)
totem.connect ('notify::current-time', self.__do_notify_current_time)
def disconnect (self):
self.totem.disconnect_by_func (self.__do_notify_current_time)
self.totem.disconnect_by_func (self.__do_notify_current_mrl)
self.totem.disconnect_by_func (self.__do_notify_seekable)
self.totem.disconnect_by_func (self.__do_notify_playing)
self.totem.disconnect_by_func (self.__do_update_metadata)
self.__do_update_metadata (self.totem, '', '', '', 0)
self.remove_from_connection (None, None)
def __calculate_playback_status (self):
if self.totem.is_playing ():
return 'Playing'
if self.totem.is_paused ():
return 'Paused'
return 'Stopped'
def __calculate_metadata (self):
metadata = {
'mpris:trackid': dbus.String (self.totem.props.current_mrl,
variant_level = 1),
'mpris:length': dbus.Int64 (
self.totem.props.stream_length * 1000,
variant_level = 1),
}
if self.current_metadata['title'] != '':
metadata['xesam:title'] = dbus.String (
self.current_metadata['title'], variant_level = 1)
if self.current_metadata['artist'] != '':
metadata['xesam:artist'] = dbus.Array (
[ self.current_metadata['artist'] ], variant_level = 1)
if self.current_metadata['album'] != '':
metadata['xesam:album'] = dbus.String (
self.current_metadata['album'], variant_level = 1)
if self.current_metadata['tracknumber'] != '':
metadata['xesam:trackNumber'] = dbus.Int32 (
self.current_metadata['tracknumber'], variant_level = 1)
return metadata
def __do_update_metadata (self, _, artist, # pylint: disable=R0913
title, album, num):
self.current_metadata = self.null_metadata.copy ()
if title:
self.current_metadata['title'] = title
if artist:
self.current_metadata['artist'] = artist
if album:
self.current_metadata['album'] = album
if num:
self.current_metadata['tracknumber'] = num
self.PropertiesChanged ('org.mpris.MediaPlayer2.Player',
{ 'Metadata': self.__calculate_metadata () }, [])
def __do_notify_playing (self, _, prop): # pylint: disable=W0613
self.PropertiesChanged ('org.mpris.MediaPlayer2.Player',
{ 'PlaybackStatus': self.__calculate_playback_status () }, [])
def __do_notify_current_mrl (self, _, prop): # pylint: disable=W0613
self.PropertiesChanged ('org.mpris.MediaPlayer2.Player', {
'CanPlay': (self.totem.props.current_mrl is not None),
'CanPause': (self.totem.props.current_mrl is not None),
'CanSeek': (self.totem.props.current_mrl is not None and
self.totem.props.seekable),
'CanGoNext': self.totem.can_seek_next (),
'CanGoPrevious': self.totem.can_seek_previous (),
}, [])
def __do_notify_seekable (self, _, prop): # pylint: disable=W0613
self.PropertiesChanged ('org.mpris.MediaPlayer2.Player', {
'CanSeek': (self.totem.props.current_mrl is not None and
self.totem.props.seekable),
}, [])
def __do_notify_current_time (self, totem, _):
# Only notify of seeks if we've skipped more than 3 seconds
if abs (totem.props.current_time - self.current_position) > 3:
self.Seeked (totem.props.current_time * 1000)
self.current_position = totem.props.current_time
# org.freedesktop.DBus.Properties interface
@dbus.service.method (dbus_interface = dbus.PROPERTIES_IFACE,
in_signature = 'ss', # pylint: disable=C0103
out_signature = 'v')
def Get (self, interface_name, property_name): # pylint: disable=C0103
return self.GetAll (interface_name)[property_name]
@dbus.service.method (dbus_interface = dbus.PROPERTIES_IFACE,
in_signature = 's', # pylint: disable=C0103
out_signature = 'a{sv}')
def GetAll (self, interface_name): # pylint: disable=C0103
if interface_name == 'org.mpris.MediaPlayer2':
return {
'CanQuit': True,
'CanRaise': True,
'HasTrackList': False,
'Identity': 'Videos',
'DesktopEntry': self.totem.application_id,
'SupportedUriSchemes': self.totem.get_supported_uri_schemes (),
'SupportedMimeTypes': self.totem.get_supported_content_types (),
}
if interface_name == 'org.mpris.MediaPlayer2.Player':
# Loop status (we don't support Track)
if self.totem.remote_get_setting (Totem.RemoteSetting.REPEAT):
loop_status = 'Playlist'
else:
loop_status = 'None'
return {
'PlaybackStatus': self.__calculate_playback_status (),
'LoopStatus': loop_status, # TODO: Notifications
'Rate': 1.0,
'MinimumRate': 1.0,
'MaximumRate': 1.0,
'Metadata': self.__calculate_metadata (),
'Volume': self.totem.get_volume (), # TODO: Notifications
'Position': dbus.Int64(self.totem.props.current_time * 1000),
'CanGoNext': self.totem.can_seek_next (),
'CanGoPrevious': self.totem.can_seek_previous (),
'CanPlay': (self.totem.props.current_mrl is not None),
'CanPause': (self.totem.props.current_mrl is not None),
'CanSeek': (self.totem.props.current_mrl is not None and
self.totem.props.seekable),
'CanControl': True,
}
raise dbus.exceptions.DBusException (
'org.mpris.MediaPlayer2.UnknownInterface',
_('The MediaPlayer2 object does not implement the ‘%s’ interface')
% interface_name)
@dbus.service.method (dbus_interface = dbus.PROPERTIES_IFACE,
in_signature = 'ssv') # pylint: disable=C0103
def Set (self, interface_name, property_name, # pylint: disable=C0103
new_value):
if interface_name == 'org.mpris.MediaPlayer2':
raise dbus.exceptions.DBusException (
'org.mpris.MediaPlayer2.ReadOnlyProperty',
_('The property ‘%s’ is not writeable.'))
if interface_name == 'org.mpris.MediaPlayer2.Player':
if property_name == 'LoopStatus':
self.totem.remote_set_setting (
Totem.RemoteSetting.REPEAT, (new_value == 'Playlist'))
elif property_name == 'Rate':
# Ignore, since we don't support setting the rate
pass
elif property_name == 'Volume':
self.totem.set_volume (new_value)
raise dbus.exceptions.DBusException (
'org.mpris.MediaPlayer2.ReadOnlyProperty',
_('Unknown property ‘%s’ requested of a MediaPlayer 2 object')
% interface_name)
raise dbus.exceptions.DBusException (
'org.mpris.MediaPlayer2.UnknownInterface',
_('The MediaPlayer2 object does not implement the ‘%s’ interface')
% interface_name)
@dbus.service.signal (dbus_interface = dbus.PROPERTIES_IFACE,
signature = 'sa{sv}as') # pylint: disable=C0103
def PropertiesChanged (self, interface_name, # pylint: disable=C0103
changed_properties, invalidated_properties):
pass
# org.mpris.MediaPlayer2 interface
@dbus.service.method (dbus_interface = 'org.mpris.MediaPlayer2',
in_signature = '', # pylint: disable=C0103
out_signature = '')
def Raise (self): # pylint: disable=C0103
main_window = self.totem.get_main_window ()
main_window.present ()
@dbus.service.method (dbus_interface = 'org.mpris.MediaPlayer2',
in_signature = '', # pylint: disable=C0103
out_signature = '')
def Quit (self): # pylint: disable=C0103
self.totem.exit ()
# org.mpris.MediaPlayer2.Player interface
@dbus.service.method (dbus_interface = 'org.mpris.MediaPlayer2.Player',
in_signature = '', # pylint: disable=C0103
out_signature = '')
def Next (self): # pylint: disable=C0103
self.totem.seek_next ()
@dbus.service.method (dbus_interface = 'org.mpris.MediaPlayer2.Player',
in_signature = '', # pylint: disable=C0103
out_signature = '')
def Previous (self): # pylint: disable=C0103
self.totem.seek_previous ()
@dbus.service.method (dbus_interface = 'org.mpris.MediaPlayer2.Player',
in_signature = '', # pylint: disable=C0103
out_signature = '')
def Pause (self): # pylint: disable=C0103
self.totem.pause ()
@dbus.service.method (dbus_interface = 'org.mpris.MediaPlayer2.Player',
in_signature = '', # pylint: disable=C0103
out_signature = '')
def PlayPause (self): # pylint: disable=C0103
self.totem.play_pause ()
@dbus.service.method (dbus_interface = 'org.mpris.MediaPlayer2.Player',
in_signature = '', # pylint: disable=C0103
out_signature = '')
def Stop (self): # pylint: disable=C0103
self.totem.stop ()
@dbus.service.method (dbus_interface = 'org.mpris.MediaPlayer2.Player',
in_signature = '', # pylint: disable=C0103
out_signature = '')
def Play (self): # pylint: disable=C0103
# If playing or no track loaded: do nothing,
# else: start playing.
if self.totem.is_playing () or self.totem.props.current_mrl is None:
return
self.totem.play ()
@dbus.service.method (dbus_interface = 'org.mpris.MediaPlayer2.Player',
in_signature = 'x', # pylint: disable=C0103
out_signature = '')
def Seek (self, offset): # pylint: disable=C0103
self.totem.seek_relative (offset / 1000, False)
@dbus.service.method (dbus_interface = 'org.mpris.MediaPlayer2.Player',
in_signature = 'ox', # pylint: disable=C0103
out_signature = '')
def SetPosition (self, _, position): # pylint: disable=C0103
position = position / 1000
# Bail if the position is not in the permitted range
if position < 0 or position > self.totem.props.stream_length:
return
self.totem.seek_time (position, False)
@dbus.service.method (dbus_interface = 'org.mpris.MediaPlayer2.Player',
in_signature = 's', # pylint: disable=C0103
out_signature = '')
def OpenUri (self, uri): # pylint: disable=C0103
self.totem.add_to_playlist_and_play (uri)
@dbus.service.signal (dbus_interface = 'org.mpris.MediaPlayer2.Player',
signature = 'x') # pylint: disable=C0103
def Seeked (self, position): # pylint: disable=C0103
pass
|