-
Notifications
You must be signed in to change notification settings - Fork 2
/
PrefsConnectPage.py
251 lines (217 loc) · 9.61 KB
/
PrefsConnectPage.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
# rhythmbox-telegram
# Copyright (C) 2023-2024 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 gi
gi.require_version('Gtk', '3.0')
# import json
import re
from gi.repository import Gtk
from telegram.client import AuthorizationState
from DialogCode import DialogCode
from TelegramApi import TelegramApi, TelegramAuthError, TelegramAuthStateError
from PrefsPage import PrefsPage
from common import show_error
def safe_cast(val, to_type, default=None):
try:
return to_type(val)
except (ValueError, TypeError):
return default
class PrefsConnectPage(PrefsPage):
name = _('Connect')
main_box = 'connect_vbox'
ui_file = 'ui/prefs/connect.ui'
loading = None
spinner = None
api = None
def _create_widget(self):
# settings_box = self.ui.get_object('connect_vbox')
logo = self.ui.get_object("logo")
api_id_entry = self.ui.get_object("api_id_entry")
api_hash_entry = self.ui.get_object("api_hash_entry")
phone_entry = self.ui.get_object("phone_number_entry")
connect_btn = self.ui.get_object("connect_btn")
details_box = self.ui.get_object('details_box')
helpbox_wrap = self.ui.get_object('helpbox_wrap')
helpbox = self.ui.get_object('helpbox')
def update_connect(connected=None):
print('update_connect %s ' % connected)
self.on_change("connected", connected)
if connected is not None:
self.connected = connected
else:
connected = self.connected
enabled = not self.loading and not connected
upd_spinner()
self.prefs.page2.box.set_sensitive(connected)
self.prefs.page3.box.set_sensitive(connected)
self.prefs.page4.box.set_sensitive(connected)
details_box.set_sensitive(enabled)
helpbox.set_sensitive(enabled)
connect_btn.set_sensitive(not self.loading)
btn_label = _('Connect') if not connected else _('Disconnect')
if self.loading:
btn_label = _('Disconnecting...') if not connected else _('Connecting...')
connect_btn.set_label(btn_label)
# @TODO enable/disable prefs pages
# channel_box.set_sensitive(not self.loading and not enabled)
# if enabled and not self.loading:
# if self.removed_help:
# self.removed_help = False
# settings_box.pack_start(helpbox, True, True, 0)
# elif not self.removed_help:
# self.removed_help = True
# settings_box.remove(helpbox)
return connected
def fill_account_details():
print('fill_account_details')
# helpbox.set_size_request(450, -1)
logo.set_size_request(500, -1)
(api_id, api_hash, phone_number, connected) = self.prefs.account.get_secure()
if connected:
print('==emit.channels-reload')
self.prefs.emit('channels-reload')
# selected = json.loads(account().settings['channels'])
# search_list_box.set_selected(selected)
api_id_entry.set_text(api_id or "")
api_hash_entry.set_text(api_hash or "")
phone_entry.set_text(phone_number or "")
update_connect(connected)
if connected:
self.loading = True
connect_api()
def account_details_changed(entry, event):
print('account_details_changed')
api_id = re.sub("\D", "", api_id_entry.get_text())
api_hash = api_hash_entry.get_text().strip()
phone_number = re.sub("(?!(^\+)|\d).", "", phone_entry.get_text())
api_id_entry.set_text(api_id)
api_hash_entry.set_text(api_hash)
phone_entry.set_text(phone_number)
self.prefs.account.update(api_id, api_hash, phone_number)
self.clear_errors()
def upd_spinner():
# @TODO fixme
print('upd_spinner')
return
if self.loading:
if self.spinner is None:
helpbox_wrap.set_property('height_request', 80)
self.spinner = Gtk.Spinner()
helpbox_wrap.pack_start(self.spinner, True, True, 0)
helpbox_wrap.remove(helpbox)
self.spinner.show()
self.spinner.start()
elif self.loading is not None:
helpbox_wrap.set_property('height_request', -1)
if self.spinner:
self.spinner.stop()
helpbox_wrap.remove(self.spinner)
self.spinner = None
if self.connected:
helpbox_wrap.pack_start(helpbox, True, True, 0)
elif not self.connected:
self.loading = False
helpbox_wrap.set_property('height_request', 40)
self.spinner = Gtk.Spinner()
helpbox_wrap.pack_start(self.spinner, True, True, 0)
helpbox_wrap.remove(helpbox)
self.spinner.show()
def connect_btn_clicked(event):
print('connect_btn_clicked')
self.loading = True
if update_connect(not self.connected):
connect_api()
else:
disconnect_api()
def set_state(state):
print('set_state %s' % state)
self.loading = False
update_connect(state)
self.prefs.account.set_connected(state)
if state:
print('emit.channels-fetch')
self.prefs.emit('channels-fetch')
# self.loading = True
# upd_spinner()
#
# def _set_chats(chats):
# search_list_box.clear_list()
# search_list_box.set_items(list(chats.values()))
# self.loading = False
# upd_spinner()
#
# self.api.get_chats_idle(_set_chats)
return state
def validate(api_id, api_hash, phone_number):
print('validate')
errors = []
if not api_id:
self.set_error(api_id_entry)
errors.append(_('API Id is required'))
if not api_hash:
self.set_error(api_hash_entry)
errors.append(_('API Hash is required'))
if safe_cast(api_id, int) is None:
self.set_error(api_id_entry)
errors.append(_('API Id must be integer'))
if not phone_number:
self.set_error(phone_entry)
errors.append(_('The phone number is required'))
if not re.search('^\+?\d{10,14}$', phone_number):
self.set_error(phone_entry)
errors.append(_('The phone number is invalid'))
if errors:
show_error(_('Validation error'), errors[0], parent=self.box)
return False
return True
def connect_api(code=None):
print('connect_api')
(api_id, api_hash, phone_number, connected) = self.prefs.account.get_secure()
if validate(api_id, api_hash, phone_number):
self.api = TelegramApi.api(api_id, api_hash, phone_number)
self.prefs.api = self.api
try:
self.api.login(code)
except TelegramAuthStateError as e:
if self.api.state == AuthorizationState.WAIT_CODE:
def unable_to_login():
show_error(_("Unable to login Telegram"), _('Login code is required'), parent=self.box)
set_state(False)
DialogCode(self, connect_api, unable_to_login)
return
else:
show_error(_("Unable to login Telegram"), e, parent=self.box)
except TelegramAuthError as e:
err = self.api.get_error()
show_error(_("Unable to login Telegram"), err if err else e, parent=self.box)
except RuntimeError as e:
show_error(_("Unable to login Telegram"), e, parent=self.box)
set_state(self.api.state == AuthorizationState.READY)
else:
set_state(False)
def disconnect_api():
print('disconnect_api')
print('emit.channels-clear')
self.prefs.emit('channels-clear')
# search_list_box.reset()
self.api.reset_chats()
# account().settings.set_string('channels', '[]')
set_state(False)
self.ui.connect_signals({"connect_btn_clicked_cb": connect_btn_clicked})
api_id_entry.connect("focus-out-event", account_details_changed)
api_hash_entry.connect("focus-out-event", account_details_changed)
phone_entry.connect("focus-out-event", account_details_changed)
fill_account_details()
upd_spinner()