-
Notifications
You must be signed in to change notification settings - Fork 5
/
RhythmboxFullscreen.py
316 lines (255 loc) · 12 KB
/
RhythmboxFullscreen.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
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
# -*- Mode: python; coding: utf-8; tab-width: 4; indent-tabs-mode: nil; -*-
#
# Copyright (C) 2013 - Benjamin Bach <benjamin@overtag.dk>
#
# 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
import mimetypes
from os import path, listdir
from gi.repository import GObject # @UnresolvedImport
from gi.repository import Peas # @UnresolvedImport
from gi.repository import RB # @UnresolvedImport
from gi.repository import GdkPixbuf # @UnresolvedImport
from fullscreen_rb3compat import url2pathname
import FullscreenWindow
from fullscreen_rb3compat import ActionGroup
from fullscreen_rb3compat import ApplicationShell
from RhythmboxFullscreenPrefs import Preferences
ui_str = \
"""<ui>
<menubar name="MenuBar">
<menu name="ViewMenu" action="View">
<placeholder name="ViewMenuModePlaceholder">
<menuitem name="ViewMenuToggleFullscreen" action="ToggleFullscreen"/>
</placeholder>
</menu>
</menubar>
</ui>"""
def find_plugin_file(filename):
"""Since there were a couple of unresolved issues with rb.find_plugin_file,
we use our own little utility function"""
root_dir = path.abspath(path.split(__file__)[0])
path_to_file = path.join(root_dir, filename)
if path.exists(path_to_file):
return path_to_file
return None
class FullscreenTrack():
def __init__(self, artist=None, title=None, album=None, duration=0.0, entry=None):
self.artist = artist
self.title = title
self.album = album
self.duration = duration
self.entry = entry
def __eq__(self, obj):
# Do not compare RB entry object because it changes
return (
self.artist == obj.artist and
self.title == obj.title and
self.album == obj.album and
self.duration == obj.duration
)
class FullscreenView(GObject.Object, Peas.Activatable):
__gtype_name = 'FullscreenPlugin'
object = GObject.property(type=GObject.Object) # @ReservedAssignment
# Scales the prefetched album art for later use
ALBUM_ART_W = 800
ALBUM_ART_H = 800
def __init__(self):
super(FullscreenView, self).__init__()
def do_activate(self):
shell = self.object
data = {}
self.shell = shell
self.entries = None
self.action_group = ActionGroup(self.shell, 'FullscreenPluginActions')
action = self.action_group.add_action(func=self.show_fullscreen,
action_name='ToggleFullscreen', label='Full Screen',
action_type='app', accel="F12")
self._appshell = ApplicationShell(self.shell)
self._appshell.insert_action_group(self.action_group)
self._appshell.add_app_menuitems(ui_str, 'FullscreenPluginActions', 'view')
w = self.shell.props.window
s = w.get_screen()
# Using the screen of the Window, the monitor it's on can be identified
m = s.get_monitor_at_window(s.get_active_window())
# Then get the geometry of that monitor
monitor = s.get_monitor_geometry(m)
# This is an example output
print("Height: %s, Width: %s" % (monitor.height, monitor.width))
if monitor.height < monitor.width:
self.ALBUM_ART_H = monitor.height
self.ALBUM_ART_W = monitor.height
else:
self.ALBUM_ART_H = monitor.width
self.ALBUM_ART_W = monitor.width
def do_deactivate(self):
shell = self.object
self._appshell.cleanup()
def show_fullscreen(self, *args):
self.window = FullscreenWindow.FullscreenWindow(plugin=self)
# Receive notification of song changes
self.player = self.shell.props.shell_player
self.player.connect("playing-source-changed", self.reload_playlist)
self.player.connect("playing-song-changed", self.on_playing_song_changed)
self.player.connect("playing-changed", self.reload_play_pause)
# TODO: This signal is not fired - which should we listen for?
# We should use the cover_db,
# but what are its signals??
cover_db = RB.ExtDB(name='album-art')
self.player.connect("playing-song-property-changed", self.notify_metadata)
cover_db.connect("added", self.notify_cover_art_change)
# Load current state
self.reload_playlist(self.player, self.player.get_playing_entry())
def playpause(self):
# Argument 'True' is unused
# (see http://developer.gnome.org/rhythmbox/2.98/RBShellPlayer.html#rb-shell-player-playpause)
self.player.playpause(True)
def play_entry(self, index):
if len(self.tracks) > index:
self.player.play_entry(self.tracks[index].entry,
self.shell.get_property("library-source"))
def reload_play_pause(self, player, playing):
if not self.window.track_widgets:
return
if playing:
try:
elapsed = player.get_playing_time()
except:
elapsed = (True, 0.0)
self.window.track_widgets[self.window.current_track].paused = False
self.window.track_widgets[self.window.current_track].start_progress_bar(elapsed)
self.window.current_info = "Now playing..."
self.window.track_infos[0] = FullscreenWindow.FullscreenWindow.INFO_STATUS_PAUSE
else:
self.window.track_widgets[self.window.current_track].paused = True
self.window.current_info = FullscreenWindow.FullscreenWindow.INFO_STATUS_IDLE
self.window.track_infos[0] = FullscreenWindow.FullscreenWindow.INFO_STATUS_PLAY
def get_entries(self, player, entry, cnt):
"""Gets the next and previous entries to be played from both active source and queue
Next entries: Everything from source and queue
Previous entries: Everything just from the source
Uses each source's query-model.
player = player to use
entry = entry to start from (as a kind of offset)
cnt = number of entries to return
"""
if not entry:
return []
entries = [entry]
def get_entries(property_name, backwards):
queue = player.get_property(property_name)
if queue:
querymodel = queue.get_property("query-model")
if not backwards:
l = querymodel.get_next_from_entry(entry)
while l and len(entries) <= cnt:
entries.append(l)
l = querymodel.get_next_from_entry(l)
else:
l = querymodel.get_previous_from_entry(entry)
while l and len(entries) <= cnt:
entries.insert(0, l)
l = querymodel.get_previous_from_entry(l)
get_entries("queue-source", False)
get_entries("source", True)
get_entries("source", False)
return entries
def get_track_info(self, entry):
artist = entry.get_string(RB.RhythmDBPropType.ARTIST) # .replace('&', '&')
album = entry.get_string(RB.RhythmDBPropType.ALBUM) # .replace('&', '&')
title = entry.get_string(RB.RhythmDBPropType.TITLE) # .replace('&', '&')
duration = entry.get_ulong(RB.RhythmDBPropType.DURATION)
track = FullscreenTrack(
artist=artist,
album=album,
title=title,
duration=duration,
entry=entry
)
return track
def notify_metadata(self, player, uri, prop, *args, **kwargs):
"""Subscribe to metadata changes from database"""
self.set_cover_art(player.get_playing_entry())
def notify_cover_art_change(self, *args):
self.set_cover_art(self.shell.props.shell_player.get_playing_entry())
def set_cover_art(self, entry):
if entry:
self.window.set_artwork(self.get_cover(entry))
def get_cover(self, entry):
if entry:
# Try to find an album cover in the folder of the currently playing track
cover_dir = path.dirname(url2pathname(entry.get_playback_uri()).replace('file://', ''))
# TODO: use os.walk()
if path.isdir(cover_dir):
for f in listdir(cover_dir):
file_name = path.join(cover_dir, f)
mt = mimetypes.guess_type(file_name)[0]
if mt and mt.startswith('image/'):
if True in [x in path.splitext(f)[0].lower() for x in
['cover', 'album', 'albumart', 'folder', 'front']]:
return GdkPixbuf.Pixbuf.new_from_file_at_size(file_name, self.ALBUM_ART_W, self.ALBUM_ART_H)
# Otherwise use what's found by the album art plugin
key = entry.create_ext_db_key(RB.RhythmDBPropType.ALBUM)
cover_db = RB.ExtDB(name='album-art')
art_location = cover_db.lookup(key)
if art_location and not isinstance(art_location, str):
# RB 3.2 returns a tuple (path, key)
art_location = art_location[0]
if art_location and path.exists(art_location):
return GdkPixbuf.Pixbuf.new_from_file_at_size(art_location, self.ALBUM_ART_W, self.ALBUM_ART_H)
def reload_playlist(self, player, entry):
entry = player.get_playing_entry()
if not entry:
# When there is no entry set for reload playlist, then what's happening?
# Is everything fine and totally inactive?
return
# Set cover art
self.set_cover_art(entry)
self.entries = self.get_entries(player, entry, 100)
self.tracks = []
for e in self.entries:
self.tracks.append(self.get_track_info(e))
current_track_index = self.entries.index(entry)
self.window.set_tracks(self.tracks, current_track=current_track_index)
self.set_active_track_properties(player, entry, current_track_index)
def set_active_track_properties(self, player, entry, current_track_index):
try:
elapsed = player.get_playing_time()
except:
elapsed = (True, 0.0)
if player.get_playing():
self.window.track_widgets[current_track_index].start_progress_bar(elapsed)
self.window.current_info = "Now playing..." # TODO
else:
self.window.track_widgets[current_track_index].set_elapsed(elapsed)
self.window.current_info = FullscreenWindow.FullscreenWindow.INFO_STATUS_IDLE
self.window.show_info()
def on_playing_song_changed(self, player, entry):
if not self.entries:
return self.reload_playlist(player, entry)
entry = player.get_playing_entry()
if not entry:
# When there is no entry set for reload playlist, then what's happening?
# Is everything fine and totally inactive?
return
try:
current_track_index = self.tracks.index(self.get_track_info(entry))
except ValueError:
return self.reload_playlist(player, entry)
self.window.change_playing_track(current_track_index)
self.set_active_track_properties(player, entry, current_track_index)
def _pycharm_optimize(self):
# dummy procedure required for pycharm to prevent removal of
# preferences during optimization
x = Preferences()