forked from fossfreedom/remember-the-rhythm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remember-the-rhythm.py
284 lines (234 loc) · 9.63 KB
/
remember-the-rhythm.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from gi.repository import GObject
from gi.repository import Peas
from gi.repository import RB
from gi.repository import Gio
from gi.repository import GLib
from gi.repository.GLib import Variant
from remember_prefs import RememberPreferences
import subprocess
GSETTINGS_KEY = "org.gnome.rhythmbox.plugins.remember-the-rhythm"
KEY_PLAYBACK_TIME = 'playback-time'
KEY_LOCATION = 'last-entry-location'
KEY_PLAYLIST = 'playlist'
KEY_BROWSER_VALUES = 'browser-values'
KEY_PLAY_STATE = 'play-state'
KEY_SOURCE = 'source'
KEY_STARTUP_STATE = 'startup-state'
class RememberTheRhythm(GObject.Object, Peas.Activatable):
__gtype_name = 'RememberTheRhythm'
object = GObject.property(type=GObject.Object)
first_run = False
def __init__(self):
GObject.Object.__init__(self)
self.settings = Gio.Settings.new(GSETTINGS_KEY)
self.location = self.settings.get_string(KEY_LOCATION)
self.playlist = self.settings.get_string(KEY_PLAYLIST)
self.playback_time = self.settings.get_uint(KEY_PLAYBACK_TIME)
self.browser_values_list = self.settings.get_value(KEY_BROWSER_VALUES)
self.play_state = self.settings.get_boolean(KEY_PLAY_STATE)
self.source = None
self.source_name = self.settings.get_string(KEY_SOURCE)
self.startup_state = self.settings.get_uint(KEY_STARTUP_STATE)
def do_activate(self):
print ("DEBUG-do_activate")
self.shell = self.object
self.library = self.shell.props.library_source
self.shell_player = self.shell.props.shell_player
self.playlist_manager = self.shell.props.playlist_manager
self.db = self.shell.props.db
self.backend_player = self.shell_player.props.player
def try_load(*args):
if len(self.playlist_manager.get_playlists()) == 0:
GLib.idle_add(self._load_complete)
else:
self._load_complete()
self.shell.props.db.connect('load-complete', try_load)
def do_deactivate(self):
self.first_run = True
def _connect_signals(self):
self.shell_player.connect('playing-changed', self.playing_changed)
self.shell_player.connect('playing-source-changed', self.playing_source_changed)
self.shell_player.connect('elapsed-changed', self.elapsed_changed)
def _load_complete(self, *args, **kwargs):
"""
called when load-complete signal is fired - this plays what was remembered
:param args:
:param kwargs:
:return:
"""
print("DEBUG - load_complete")
if not self.location:
self._scenario = 4
self.first_run = True
self._connect_signals()
return
entry = self.db.entry_lookup_by_location(self.location)
print (self.location)
if not entry:
self.first_run = True
self._connect_signals()
return
if self.playlist:
playlists = self.playlist_manager.get_playlists()
for playlist in playlists:
if playlist.props.name == self.playlist:
self.source = playlist
break
# now switch to the correct source to play the remembered entry
if not self.source:
print (self.location)
self.source = self.shell.guess_source_for_uri(self.location)
self.shell_player.set_playing_source(self.source)
#self.shell_player.set_selected_source(self.source)
# when dealing with playing we start a thread (so we don't block the UI
# each stage we wait a bit for stuff to start working
time = self.playback_time
def scenarios():
print ("scenario %d" % self._scenario)
if self._scenario == 1:
# always mute the sound - this helps with the pause scenario
# where we have to start playing first before pausing... but
# we dont want to here what is playing
#p = subprocess.Popen(['amixer set Master mute > /dev/null'], shell=True, stdout=subprocess.PIPE)
self._scenario += 1
return True
if self._scenario == 2:
# play the entry for the source chosen
print (entry)
print (self.source)
self.shell_player.play_entry(entry, self.source)
#self.shell_player.set_playing_time(time)
self._scenario += 1
return True
if self._scenario == 3:
# now pause if the preferences options calls for this.
print (self.play_state)
print (self.startup_state)
if (not self.play_state and self.startup_state == 1) or \
self.startup_state == 2:
print ("pausing")
self.shell_player.pause()
# note for radio streams rhythmbox doesnt pause - it can only stop
# so basically nothing we can do - just let the stream play
self._scenario += 1
return True
self._scenario = 4
# for the playing entry attempt to move to the remembered time
try:
self.shell_player.set_playing_time(time)
except:
# fallthrough ... some streams - radio - cannot seek
pass
# unmute and end the thread
#p = subprocess.Popen(['amixer set Master unmute > /dev/null'], shell=True, stdout=subprocess.PIPE)
return False
self._scenario = 1
GLib.timeout_add_seconds(1, scenarios)
self._connect_signals()
self.first_run = True
def playing_source_changed(self, player, source, data=None):
"""
called when user changes what is playing in a different source
:param player:
:param source:
:param data:
:return:
"""
print ("DEBUG-playing source changed")
if self._scenario != 4:
return
if source:
self.source = source
if self.source in self.playlist_manager.get_playlists():
self.settings.set_string('playlist', self.source.props.name)
self.settings.set_string('source', '')
else:
self.settings.set_string('playlist', '')
self.settings.set_string('source', self.source.props.name)
self.source_name = self.source.props.name
def playing_changed(self, player, playing, data=None):
"""
called when user changes what is actually playing
:param player:
:param playing:
:param data:
:return:
"""
def init_source():
print ("init source")
if self.source:
views = self.source.get_property_views()
for i, view in enumerate(views):
if i in self.browser_values_list:
value = self.browser_values_list[i]
if value:
view.set_selection(value)
self.shell.props.display_page_tree.select(self.source)
# self.shell_player.jump_to_current()
print("DEBUG-playing_changed")
if self._scenario != 4:
return
entry = self.shell_player.get_playing_entry()
if entry:
print ("found entry")
success, self.play_state = self.shell_player.get_playing()
self.location = entry.get_string(RB.RhythmDBPropType.LOCATION)
print (self.location)
else:
print ("not found entry")
self.play_state = False
self.location = ""
GLib.idle_add(self.save_rhythm, 0)
def elapsed_changed(self, player, entry, data=None):
"""
called when something is playing - remembers the time within a track
:param player:
:param entry:
:param data:
:return:
"""
if not self.first_run:
return
if self._scenario != 4:
try:
self.shell_player.set_playing_time(self.playback_time)
except:
pass
try:
if self.playback_time:
save_time = True
else:
save_time = False
if save_time and self.playback_time == self.shell_player.get_playing_time()[1]:
save_time = False
self.playback_time = self.shell_player.get_playing_time()[1]
GLib.idle_add(self.save_rhythm)
except:
pass
def save_rhythm(self, pb_time=None):
"""
This actually saves info into gsettings
:param pb_time:
:return:
"""
if self.location:
pb_time = pb_time is None and self.playback_time or pb_time is None
self.settings.set_uint(KEY_PLAYBACK_TIME, pb_time)
self.settings.set_string(KEY_LOCATION, self.location)
#print ("last location %s" % self.location)
self.settings.set_boolean(KEY_PLAY_STATE, self.play_state)
if self.source:
views = self.source.get_property_views()
browser_values_list = []
for view in views:
browser_values_list.append(view.get_selection())
self.browser_values_list = Variant('aas', browser_values_list)
self.settings.set_value(KEY_BROWSER_VALUES, self.browser_values_list)
def _import(self):
"""
dummy routine to stop pycharm from optimising out the preferences import
:return:
"""
RememberPreferences()