-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconflict_dialog.py
188 lines (158 loc) · 7.6 KB
/
conflict_dialog.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
# rhythmbox-telegram
# Copyright (C) 2023-2025 Andrey Izman <[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 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 os
import gi
import rb
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio, GLib
from typing import Callable
from storage import Audio
from common import get_audio_tags, format_time, pretty_file_size, get_file_size, file_uri, move_window_center
from common import CONFLICT_ACTION_RENAME, CONFLICT_ACTION_REPLACE, CONFLICT_ACTION_IGNORE
# def parse_stream_info(data):
# result = {}
# for line in data.strip().splitlines():
# line = line.strip()
# if '=' in line:
# key, value = line.split('=', 1)
# if key not in result:
# result[key] = value.strip()
# return result
class ConcurrentResolveError(Exception):
pass
class FileInfo:
@staticmethod
def from_file(file_path):
info = FileInfo()
info.file_path = file_path
info.meta_tags = get_audio_tags(file_path)
info.file_size = get_file_size(info.file_path )
return info
@staticmethod
def from_audio(audio):
info = FileInfo()
info.audio = audio
info.file_path = audio.local_path
info.meta_tags = audio.meta_tags
info.file_size = get_file_size(info.file_path)
return info
def __init__(self):
self.audio = None
self.file_path = None
self.file_size = 0
self.meta_tags = {}
# self.file_path = file_path
# self.meta_tags = get_audio_tags(file_path)
# self.stream_info = {}
# self._update
# try:
# args = ['ffprobe', '-v', 'quiet', '-show_streams', '-select_streams', 'a', file_path]
# result = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
# GLib.io_add_watch(result.stdout, GLib.IO_IN, self._subprocess_out_cb, result)
# except:
# pass
#
# def _subprocess_out_cb(self, source, condition, process):
# if condition == GLib.IO_IN:
# output = source.read()
# process.stdout.close()
# process.stderr.close()
# process.terminate()
# info = parse_stream_info(output)
# if info:
# self.stream_info = info
# self._update()
# return False
# return True
def browse_in_file_manager(self):
app_info = Gio.AppInfo.get_default_for_type('inode/directory', True)
if app_info:
app_info.launch_uris([file_uri(self.file_path)], None)
def set_small_label(widget, label):
widget.set_markup('<small>%s</small>' % label)
class ConflictDialog:
builder: Gtk.Builder
window: Gtk.Window | None = None
callback: Callable[[str, Audio, str], None] | None = None
new_file: FileInfo | None = None
old_file: FileInfo | None = None
def __init__(self, plugin, audio, filename, callback):
self._running = True
self.plugin = plugin
self.new_file = FileInfo.from_audio(audio)
self.old_file = FileInfo.from_file(filename)
self.builder = Gtk.Builder()
self.builder.add_from_file(rb.find_plugin_file(self.plugin, "ui/conflict-dialog.ui"))
self.window = self.builder.get_object('window')
signals = {
"browse_new_btn_clicked_cb" : self._browse_new_btn_clicked_cb,
"browse_old_btn_clicked_cb" : self._browse_old_btn_clicked_cb,
"rename_btn_clicked_cb": self._rename_btn_clicked_cb,
"replace_btn_clicked_cb": self._replace_btn_clicked_cb,
"skip_btn_clicked_cb": self._skip_btn_clicked_cb,
"on_window_destroy": self._on_window_destroy,
"destroy": self._on_window_destroy,
"delete-event": self._on_window_destroy,
}
self.builder.connect_signals(signals)
self.window.set_title(_('Telegram: Download File Conflict'))
self.callback = callback
self.update_ui()
self.window.set_default_size(500, 400)
self.window.set_resizable(False)
self.window.set_modal(True)
self.window.set_transient_for(self.plugin.shell.props.window)
self.window.set_position(Gtk.WindowPosition.CENTER_ON_PARENT)
self.window.show_all()
# if self.plugin.shell.props.visibility:
# GLib.timeout_add(400, move_window_center, self.window, self.plugin.shell.props.window)
def update_ui(self):
basename = os.path.basename(self.old_file.file_path)
self.builder.get_object('title').set_label(
'<span font_desc=\'14\' weight=\'bold\'>%s</span>' % (_('Replace file "%s"?') % basename))
set_small_label(self.builder.get_object('artist_new_lbl'), _('Artist:'))
set_small_label(self.builder.get_object('artist_new_val'), self.new_file.meta_tags.get('artist', 'Unknown'))
set_small_label(self.builder.get_object('title_new_lbl'), _('Title:'))
set_small_label(self.builder.get_object('title_new_val'), self.new_file.meta_tags.get('title', 'Unknown'))
set_small_label(self.builder.get_object('duration_new_lbl'), _('Duration:'))
set_small_label(self.builder.get_object('duration_new_val'), format_time(int(self.new_file.meta_tags.get('duration', 0))))
set_small_label(self.builder.get_object('filesize_new_lbl'), _('File size:'))
set_small_label(self.builder.get_object('filesize_new_val'), pretty_file_size(self.new_file.file_size))
set_small_label(self.builder.get_object('artist_old_lbl'), _('Artist:'))
set_small_label(self.builder.get_object('artist_old_val'), self.old_file.meta_tags.get('artist', 'Unknown'))
set_small_label(self.builder.get_object('title_old_lbl'), _('Title:'))
set_small_label(self.builder.get_object('title_old_val'), self.old_file.meta_tags.get('title', 'Unknown'))
set_small_label(self.builder.get_object('duration_old_lbl'), _('Duration:'))
set_small_label(self.builder.get_object('duration_old_val'), format_time(int(self.old_file.meta_tags.get('duration', 0))))
set_small_label(self.builder.get_object('filesize_old_lbl'), _('File size:'))
set_small_label(self.builder.get_object('filesize_old_val'), pretty_file_size(self.old_file.file_size))
def _callback_action(self, action):
if self._running:
self._running = False
self.window.close()
GLib.timeout_add(100, self.callback, action, self.new_file.audio, self.old_file.file_path)
def _browse_new_btn_clicked_cb(self, *args):
self.new_file.browse_in_file_manager()
def _browse_old_btn_clicked_cb(self, *args):
self.old_file.browse_in_file_manager()
def _rename_btn_clicked_cb(self, *args):
self._callback_action(CONFLICT_ACTION_RENAME)
def _replace_btn_clicked_cb(self, *args):
self._callback_action(CONFLICT_ACTION_REPLACE)
def _skip_btn_clicked_cb(self, *args):
self._callback_action(CONFLICT_ACTION_IGNORE)
def _on_window_destroy(self, *args):
self._callback_action(CONFLICT_ACTION_IGNORE)