forked from owais/remember-the-rhythm
-
Notifications
You must be signed in to change notification settings - Fork 5
/
remember-the-rhythm.py
273 lines (229 loc) · 9.37 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
#!/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.shell_player = self.shell.props.shell_player
self.playlist_manager = self.shell.props.playlist_manager
self.db = self.shell.props.db
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
self.settings.set_string('playlist', self.playlist)
self.settings.set_string('source', self.source_name)
if self.location:
self.settings.set_string(KEY_LOCATION, self.location)
self.settings.set_uint(KEY_PLAYBACK_TIME, self.playback_time)
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 _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:
"""
self._scenario = 5
print("DEBUG - load_complete")
if not self.location:
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.startup_state == 5:
playlists = self.playlist_manager.get_playlists()
if playlists.__len__():
last = playlists[len(playlists)-1]
if (type(playlists[3]) == RB.StaticPlaylistSource):
self.source = last
else:
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)
# 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():
def init_source():
print("\x1b[150G init source")
if self.source:
views = self.source.get_property_views()
for i, view in enumerate(views):
if i < len(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)
print ("scenario %d" % self._scenario)
if self._scenario == 1:
init_source()
# If you need to select a playlist or other source and that's it
if self.startup_state in [4,5]:
self._scenario = 5
return False
# 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
ok,self.volume=self.shell_player.get_volume() # Я добавляю, чтобы звук не мешал
self.shell_player.set_volume(0)
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 += 1
if 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
self._scenario += 1
return True
# unmute and end the thread
self.shell_player.set_volume(self.volume)
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 != 5:
return
if source:
self.source = source
if self.source in self.playlist_manager.get_playlists():
self.playlist=self.source.props.name
self.source_name=''
else:
self.playlist=''
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:
"""
print("DEBUG-playing_changed")
if self._scenario != 5:
return
entry = self.shell_player.get_playing_entry()
if entry:
print ("found entry")
self.play_state = playing
self.location = entry.get_string(RB.RhythmDBPropType.LOCATION)
print (self.location)
else:
print ("not found entry")
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
return
try:
self.playback_time = self.shell_player.get_playing_time()[1]
except:
pass
def _import(self):
"""
dummy routine to stop pycharm from optimising out the preferences import
:return:
"""
RememberPreferences()