forked from sunng87/Exaile-Soundmenu-Indicator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__init__.py
219 lines (191 loc) · 8.56 KB
/
__init__.py
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
# vim: ts=4:sw=4:et:
#
# Copyright (C) 2010 Sun Ning <[email protected]>
#
# 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#
# The developers of the Exaile media player hereby grant permission
# for non-GPL compatible GStreamer and Exaile plugins to be used and
# distributed together with GStreamer and Exaile. This permission is
# above and beyond the permissions granted by the GPL license by which
# Exaile is covered. If you modify this code, you may extend this
# exception to your version of the code, but you are not obligated to
# do so. If you do not wish to do so, delete this exception statement
# from your version.
#
import os
import fnmatch
try:
import indicate
except ImportError:
pass
import dbus
import gtk
from mpris2 import Mpris2Adapter
from mpris2 import ORG_MPRIS_MEDIAPLAYER2
from mpris2 import ORG_MPRIS_MEDIAPLAYER2_PLAYER
from mpris2 import ORG_MPRIS_MEDIAPLAYER2_TRACKLIST
from mpris2 import MICROSECOND
from xl import event, settings
from xl.player import PLAYER
MPRIS2 = None
_WINDOW_STATE_HANDLER = None
def enable(exaile):
if exaile.loading:
event.add_callback(_enable, "exaile_loaded")
else:
_enable(None, exaile, None)
def _enable(nothing, exaile, nothing2):
global MPRIS2
MPRIS2 = Mpris2Manager(exaile)
MPRIS2.acquire()
MPRIS2.register_events()
init_indicate()
event.add_callback(_clean_tmp, 'quit_application')
# _WINDOW_STATE_HANDLER = exaile.gui.main.window.connect("window_state_event", _destroy_window_and_tray, exaile)
_WINDOW_STATE_HANDLER = exaile.gui.main.window.connect("delete-event", _delete_event, exaile)
patch_tray_icon(exaile)
def patch_tray_icon(exaile):
### currently this function still not work
settings.set_option('gui/minimize_to_tray', False)
exaile.gui.main.controller.tray_icon = DummyTrayIcon()
settings.set_option('gui/use_tray', False)
class DummyTrayIcon(object):
def destroy(self):
pass
def disable(exaile):
global MPRIS2
MPRIS2.unregister_events()
MPRIS2.release()
event.remove_callback(_clean_tmp, 'quit_application')
if _WINDOW_STATE_HANDLER is not None:
exaile.gui.main.window.disconnect(_WINDOW_STATE_HANDLER)
def _delete_event(window, event, exaile):
""" window behavior on closing, according to sound menu spec:
https://wiki.ubuntu.com/SoundMenu
"""
if PLAYER.is_playing():
window.hide()
else:
exaile.gui.main.quit()
return True
def _destroy_window_and_tray(window, event, exaile):
if event.changed_mask & gtk.gdk.WINDOW_STATE_ICONIFIED:
window.hide()
window.deiconify()
def _clean_tmp(type, exaile, data):
tmpdir = os.path.expanduser('~/.cache/exaile/')
for tmp in os.listdir(tmpdir):
if fnmatch.fnmatch(tmp, 'exaile-soundmenu*'):
os.remove(os.path.join(tmpdir, tmp))
def init_indicate():
## for Maverick registration
try:
server = indicate.indicate_server_ref_default()
server.set_type('music.exaile')
server.set_desktop_file('/usr/share/applications/exaile.desktop')
server.show()
except:
pass
DBUS_OBJECT_NAME = 'org.mpris.MediaPlayer2.exaile'
class Mpris2Manager(object):
def __init__(self, exaile):
self.exaile = exaile
self.bus = None
def acquire(self):
if self.bus:
self.bus.get_bus().request_name(DBUS_OBJECT_NAME)
else:
self.bus = dbus.service.BusName(DBUS_OBJECT_NAME, bus=dbus.SessionBus())
self.adapter = Mpris2Adapter(self.exaile, self.bus)
### for Natty registration
self.adapter.populate(ORG_MPRIS_MEDIAPLAYER2, 'DesktopEntry')
self.adapter.populate(ORG_MPRIS_MEDIAPLAYER2_PLAYER,
'PlaybackStatus', 'Metadata', 'CanGoNext', 'CanGoPrevious',
'CanPause', 'CanPlay')
def register_events(self):
event.add_callback(self.on_playback_start, 'playback_track_start')
event.add_callback(self.on_playback_start, 'playback_player_start')
event.add_callback(self.on_playback_end, 'playback_track_end')
event.add_callback(self.on_playback_pause, 'playback_player_pause')
event.add_callback(self.on_playback_pause, 'playback_toggle_pause')
event.add_callback(self.on_tags_update, 'track_tags_changed')
event.add_callback(self.on_option_change, 'option_set')
# <= 0.3.2
event.add_callback(self.on_playlist_change, 'tracks_reordered')
# >= 0.3.3
event.add_callback(self.on_playlist_change, 'playlist_current_position_changed')
event.add_callback(self.on_shuffle_change, 'playlist_shuffle_mode_changed')
event.add_callback(self.on_repeat_change, 'playlist_repeat_mode_changed')
event.add_callback(self.on_seek, 'playback_seeked')
def release(self):
if self.adapter is not None:
self.adapter.remove_from_connection()
if self.bus is not None:
self.bus.get_bus().release_name(self.bus.get_name())
def unregister_events(self):
event.remove_callback(self.on_playback_start, 'playback_track_start')
event.remove_callback(self.on_playback_start, 'playback_player_start')
event.remove_callback(self.on_playback_end, 'playback_track_end')
event.remove_callback(self.on_playback_pause, 'playback_player_pause')
event.remove_callback(self.on_playback_pause, 'playback_toggle_pause')
event.remove_callback(self.on_tags_update, 'track_tags_changed')
event.remove_callback(self.on_option_change, 'option_set')
# <= 0.3.2
event.remove_callback(self.on_playlist_change, 'tracks_reordered')
# >= 0.3.3
event.remove_callback(self.on_playlist_change, 'playlist_current_position_changed')
event.remove_callback(self.on_shuffle_change, 'playlist_shuffle_mode_changed')
event.remove_callback(self.on_repeat_change, 'playlist_repeat_mode_changed')
event.remove_callback(self.on_seek, 'playback_seeked')
def on_playback_start(self, evt, exaile, data):
# When looping a playlist, this is called a bit too early, causing the
# PlaybackStatus property to contain "Paused"... Let's assume Exaile will
# always be playing after this event.
self.adapter.populate(ORG_MPRIS_MEDIAPLAYER2_PLAYER,
('PlaybackStatus', 'Playing'), 'Metadata', 'CanGoNext', 'CanGoPrevious',
'CanPause', 'CanPlay')
def on_playback_end(self, evt, exaile, data):
self.adapter.populate(ORG_MPRIS_MEDIAPLAYER2_PLAYER,
'PlaybackStatus', 'Metadata', 'CanGoNext', 'CanGoPrevious',
'CanPause', 'CanPlay')
def on_playback_pause(self, evt, exaile, data):
self.adapter.populate(ORG_MPRIS_MEDIAPLAYER2_PLAYER,
'PlaybackStatus', 'CanPause', 'CanPlay')
def on_tags_update(self, evt, track, data):
if track == PLAYER.current:
self.adapter.populate(ORG_MPRIS_MEDIAPLAYER2_PLAYER, 'Metadata')
def on_option_change(self, evt, settings_manager, data):
if data == 'playback/repeat':
# <= 0.3.2
self.adapter.populate(ORG_MPRIS_MEDIAPLAYER2_PLAYER,
'LoopStatus', 'CanGoNext', 'CanGoPrevious')
elif data == 'playback/shuffle':
# <= 0.3.2
self.adapter.populate(ORG_MPRIS_MEDIAPLAYER2_PLAYER, 'Shuffle')
elif data == 'player/volume':
self.adapter.populate(ORG_MPRIS_MEDIAPLAYER2_PLAYER, 'Volume')
def on_shuffle_change(self, evt, playlist, data):
# >= 0.3.3
self.adapter.populate(ORG_MPRIS_MEDIAPLAYER2_PLAYER, 'Shuffle')
def on_repeat_change(self, evt, playlist, data):
# >= 0.3.3
self.adapter.populate(ORG_MPRIS_MEDIAPLAYER2_PLAYER,
'LoopStatus', 'CanGoNext', 'CanGoPrevious')
def on_playlist_change(self, evt, playlist, unknown):
self.adapter.populate(ORG_MPRIS_MEDIAPLAYER2_PLAYER, 'CanGoNext', 'CanGoPrevious')
def on_seek(self, evt, player, pos):
self.adapter.Seeked(pos * MICROSECOND)