-
Notifications
You must be signed in to change notification settings - Fork 2
/
TelegramSource.py
400 lines (336 loc) · 14.2 KB
/
TelegramSource.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# rhythmbox-telegram
# Copyright (C) 2023-2024 Andrey Izman <izmanw@gmail.com>
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
import rb
from gi.repository import RB
from gi.repository import GObject, Gtk, Gio, Gdk, GLib
from gi.repository.Gio import ThemedIcon
from common import to_location, get_location_data, empty_cb, SingletonMeta
from common import file_uri, get_entry_state, set_entry_state
from TelegramLoader import PlaylistLoader
from TelegramStorage import TgAudio
import gettext
gettext.install('rhythmbox', RB.locale_dir())
state_icons = {
TgAudio.STATE_DEFAULT : 'tg-state-download-symbolic',
TgAudio.STATE_ERROR : 'tg-state-error',
TgAudio.STATE_IN_LIBRARY : 'tg-state-library-symbolic',
TgAudio.STATE_HIDDEN : 'tg-state-visibility-off-symbolic',
TgAudio.STATE_DOWNLOADED : None,
}
class StateColumn:
_icon_cache = {}
def __init__(self, source):
self._pulse = 0
self._models = {}
self.timeout_id = None
column = Gtk.TreeViewColumn()
pixbuf_renderer = Gtk.CellRendererPixbuf()
spinner_renderer = Gtk.CellRendererSpinner()
column.add_attribute(spinner_renderer, "active", 1)
column.add_attribute(spinner_renderer, "pulse", 1)
column.set_title(" ")
column.set_cell_data_func(pixbuf_renderer, self.model_data_func, "pixbuf") # noqa
column.set_cell_data_func(spinner_renderer, self.model_data_func, "spinner") # noqa
column.pack_start(spinner_renderer, expand=True)
column.pack_start(pixbuf_renderer, expand=True)
column.set_expand(False)
column.set_resizable(False)
column.set_sizing(Gtk.TreeViewColumnSizing.FIXED)
column.set_fixed_width(36)
entry_view = source.get_entry_view()
self.entry_view = entry_view
entry_view.append_column_custom(column, ' ', "tg-state", empty_cb, None, None)
visible_columns = entry_view.get_property("visible-columns")
visible_columns.append('tg-state')
entry_view.set_property("visible-columns", visible_columns)
def activate(self):
if not self.timeout_id:
self.timeout_id = GLib.timeout_add(100, self.spinner_pulse)
def deactivate(self):
if self.timeout_id:
GLib.source_remove(self.timeout_id)
self.timeout_id = None
def spinner_pulse(self):
self._pulse = 0 if self._pulse == 999999 else self._pulse + 1
for idx in self._models.keys():
model, iter = self._models[idx]
if model and iter:
model.emit("row_changed", model.get_path(iter), iter)
else:
del self._models[idx]
return True
def model_data_func(self, column, cell, model, iter, cell_type): # noqa
entry = model.get_value(iter, 0)
idx = model.get_value(iter, 1)
# print('==== XXXX %s' % entry.get_ulong(RB.RhythmDBPropType.MTIME))
state = get_entry_state(entry)
is_spinner = cell_type == 'spinner'
if state == TgAudio.STATE_LOADING:
cell.props.visible = is_spinner
if is_spinner:
self._models[idx] = [model, iter]
cell.props.active = True
cell.props.pulse = self._pulse
else:
cell.props.visible = not is_spinner
if is_spinner:
if idx in self._models:
del self._models[idx]
cell.props.active = False
else:
if state in StateColumn._icon_cache:
gicon = StateColumn._icon_cache[state]
else:
icon_name = state_icons[state] if state in state_icons else state_icons[TgAudio.STATE_DEFAULT]
gicon = Gio.ThemedIcon.new(icon_name) if icon_name is not None else None
StateColumn._icon_cache[state] = gicon
cell.props.gicon = gicon
class DownloadBar(metaclass=SingletonMeta):
def __init__(self, plugin):
self.plugin = plugin
self.source = None
self.active = False
self.info = {}
plugin.connect('update_download_info', self.update_download_info)
def deactivate(self, source):
self.active = False
def activate(self, source):
self.source = source
if source.bar_ui is None:
entry_view = source.get_entry_view()
builder = Gtk.Builder()
builder.add_from_file(rb.find_plugin_file(source.plugin, "ui/status.ui"))
status_box = builder.get_object('status_box')
source.bar_ui = {
"box": status_box,
"counter": builder.get_object('counter_label'),
"filename": builder.get_object('filename_label'),
"progress": builder.get_object('progress_bar'),
}
entry_view.pack_end(status_box, False, False, 0)
status_box.show_all()
status_box.props.visible = False
self.active = True
self._update_ui()
def _update_ui(self):
if not self.active:
return
if self.source:
is_visible = self.info.get('active', False)
self.source.bar_ui["box"].props.visible = is_visible
if is_visible:
self.source.bar_ui["counter"].set_text(_('File %s of %s') % (self.info.get('index', 1), self.info.get('total', 1)))
self.source.bar_ui["filename"].set_text(self.info.get('filename', ''))
self.source.bar_ui["progress"].set_fraction(self.info.get('fraction', 0.0))
def update_download_info(self, plugin, info):
self.info = info
self._update_ui()
class TelegramSource(RB.BrowserSource):
def __init__(self):
self.is_activated = False
RB.BrowserSource.__init__(self)
self.app = Gio.Application.get_default()
self.initialised = False
self.shell = None
self.db = None
self.entry_type = None
self.loader = None
self.plugin = None
self.chat_id = None
self.chat_title = None
self.bar = None
self.bar_ui = None
self.entry_updated_id = None
self.suppress_is_hidden = None
self.loaded_entries = []
def setup(self, plugin, chat_id, chat_title):
self.initialised = False
shell = self.props.shell
self.shell = shell
self.db = shell.props.db
self.set_property("query-model", RB.RhythmDBQueryModel.new_empty(self.db))
self.entry_type = self.props.entry_type
self.plugin = plugin
self.chat_id = chat_id
self.chat_title = chat_title
self.loader = None
self.get_entry_view().append_column(rb.RB.EntryViewColumn.RATING, True)
self.state_column = StateColumn(self) # noqa
self.loaded_entries = []
self.activate()
app = self.shell.props.application
self.set_property("playlist-menu", app.get_shared_menu("playlist-page-menu"))
def activate(self):
self.entry_updated_id = self.db.connect('entry-changed', self.on_entry_changed)
self.props.entry_type.activate()
def deactivate(self):
self.db.disconnect(self.entry_updated_id)
self.props.entry_type.deactivate()
def on_entry_changed(self, db, entry, changes):
if self.entry_type != entry.get_entry_type():
return
for change in changes:
if change.prop == RB.RhythmDBPropType.PLAY_COUNT:
play_count = entry.get_ulong(RB.RhythmDBPropType.PLAY_COUNT)
audio = self.plugin.storage.get_entry_audio(entry)
audio.save({"play_count": play_count})
elif change.prop == RB.RhythmDBPropType.RATING:
rating = entry.get_double(RB.RhythmDBPropType.RATING)
audio = self.plugin.storage.get_entry_audio(entry)
audio.save({"rating": round(rating)})
def hide_thyself(self):
self.deactivate()
self.set_property('visibility', False)
def show_thyself(self):
self.activate()
self.set_property('visibility', True)
def do_deselected(self):
self.bar.deactivate(self)
self.state_column.deactivate()
if self.loader is not None:
self.loader.stop()
def do_selected(self):
self.plugin.source = self
self.state_column.activate()
self.get_entry_view().set_sorting_order("Location", Gtk.SortType.DESCENDING)
self.bar = DownloadBar(self.plugin)
self.bar.activate(self)
if not self.initialised:
self.initialised = True
visibility = self.plugin.settings['audio-visibility']
if visibility == 'visible':
self.suppress_is_hidden = 1
elif visibility == 'hidden':
self.suppress_is_hidden = 0
GLib.idle_add(self.add_entries, visibility)
# self.add_entries()
self.loader = PlaylistLoader(self.chat_id, self.add_entry)
self.loader.start()
def add_entries(self, visibility):
self.plugin.storage.load_entries(self.chat_id, self.add_entry, visibility)
def add_entry(self, audio):
if audio.id not in self.loaded_entries:
if audio.is_hidden == self.suppress_is_hidden:
return
self.loaded_entries.append(audio.id)
location = to_location(self.plugin.api.hash, audio.created_at, self.chat_id, audio.message_id)
entry = self.db.entry_lookup_by_location(location)
if not entry:
entry = RB.RhythmDBEntry.new(self.db, self.entry_type, location)
audio.update_entry(entry, self.db)
def do_copy(self):
tg_entries = self.get_entry_view().get_selected_entries()
if len(tg_entries) == 0:
return None
sort_audio = []
albums = {}
num_format = '%0' + str(len(str(len(tg_entries))) + 2) + 'i'
key_format = '%s|%s|%s' % (num_format, num_format, num_format)
for idx, tg_entry in enumerate(tg_entries):
audio = self.plugin.storage.get_entry_audio(tg_entry)
if audio.is_moved:
album = f'{audio.artist}|{audio.album}|{audio.get_year()}'
albums_keys = albums.keys()
if album not in albums_keys:
albums[album] = len(albums_keys) + 1
sort_key = key_format % (albums[album], audio.track_number, idx)
sort_audio.append([sort_key, audio])
if len(sort_audio) == 0:
return None
sort_audio.sort(key=lambda d: d[0])
entry_type = self.db.entry_type_get_by_name("song")
song_entries = []
for data in sort_audio:
audio = data[1]
uri = file_uri(audio.local_path)
entry = self.db.entry_lookup_by_location(uri)
if not entry:
entry = RB.RhythmDBEntry.new(self.db, entry_type, uri)
audio.update_entry(entry, self.db, commit=False, state=False)
self.db.entry_set(entry, RB.RhythmDBPropType.COMMENT, f'Downloaded from {self.chat_title}')
self.db.commit()
song_entries.append(entry)
return song_entries
def do_can_delete(self):
return True
def do_can_copy(self):
return False
def do_can_paste(self):
return False
def do_can_pause(self):
return True
def do_can_add_to_queue(self):
return True
def do_can_move_to_trash(self):
return False
def browse_action(self):
screen = self.props.shell.props.window.get_screen()
entries = self.get_entry_view().get_selected_entries()
if len(entries) == 0:
return
entry = entries[0]
location = entry.get_string(RB.RhythmDBPropType.LOCATION)
chat_id, message_id = get_location_data(location)
audio = TgAudio({"chat_id": chat_id, "message_id": message_id})
url = audio.get_link()
Gtk.show_uri(screen, url, Gdk.CURRENT_TIME)
def file_manager_action(self):
entries = self.get_entry_view().get_selected_entries()
if len(entries) == 0:
return
app_info = Gio.AppInfo.get_default_for_type('inode/directory', True)
if not app_info:
return
for entry in entries:
audio = self.plugin.storage.get_entry_audio(entry)
if audio.is_file_exists():
app_info.launch_uris([file_uri(audio.local_path)], None)
return
def download_action(self):
entries = self.get_entry_view().get_selected_entries()
if len(entries) == 0:
return
downloader = self.plugin.downloader
downloader.setup()
downloader.add_entries(entries)
downloader.start()
def hide_action(self):
entries = self.get_entry_view().get_selected_entries()
if len(entries) == 0:
return
commit = False
for entry in entries:
audio = self.plugin.storage.get_entry_audio(entry)
if not audio.is_hidden:
audio.save({"is_hidden": True})
set_entry_state(self.db, entry, audio.get_state())
commit = True
if commit:
self.db.commit()
def unhide_action(self):
entries = self.get_entry_view().get_selected_entries()
if len(entries) == 0:
return
commit = False
for entry in entries:
audio = self.plugin.storage.get_entry_audio(entry)
if audio.is_hidden:
audio.save({"is_hidden": False})
set_entry_state(self.db, entry, audio.get_state())
commit = True
if commit:
self.db.commit()
GObject.type_register(TelegramSource)