-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprefs_connect.py
221 lines (183 loc) · 8.41 KB
/
prefs_connect.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
# 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 gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import re, sys, os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "lib"))
from telegram.client import AuthorizationState
from auth_dialog import AuthDialog
from account import KEY_CONNECTED
from telegram_client import TelegramApi, TelegramAuthError, TelegramAuthStateError
from prefs_base import PrefsPageBase
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(PrefsPageBase):
name = _('Connect')
main_box = 'connect_vbox'
ui_file = 'ui/prefs/connect.ui'
loading = None
spinner = None
api = None
def _create_widget(self):
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")
connect_status = self.ui.get_object("connect_status")
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):
self.on_change(KEY_CONNECTED, connected)
if connected is not None:
self.connected = connected
else:
connected = self.connected
not_connected_sensitive = not self.loading and not connected
connected_sensitive = not self.loading and connected
self.prefs.page2.set_sensitive(connected_sensitive)
self.prefs.page3.set_sensitive(connected_sensitive)
self.prefs.page4.set_sensitive(connected_sensitive)
self.prefs.page5.set_sensitive(connected_sensitive)
details_box.set_sensitive(not_connected_sensitive)
helpbox.set_sensitive(not_connected_sensitive)
upd_spinner()
connect_btn.set_sensitive(not self.loading)
status = _('Connected ✅') if connected_sensitive else _('Not Connected ❌')
if self.loading:
btn_label = _('Disconnecting...') if not connected else _('Connecting...')
else:
btn_label = _('Connect') if not connected else _('Disconnect')
connect_btn.set_label(btn_label)
connect_status.set_markup('<small>%s: %s</small>' % (_('Status'), status))
return connected
def fill_account_details():
logo.set_size_request(500, -1)
(api_id, api_hash, phone_number, connected) = self.prefs.account.get_secure()
if connected:
self.prefs.emit('channels-reload')
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):
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():
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 False:
helpbox_wrap.set_property('height_request', -1)
if self.spinner:
self.spinner.stop()
helpbox_wrap.remove(self.spinner)
self.spinner = None
helpbox_wrap.pack_start(helpbox, True, True, 0)
self.loading = None
def connect_btn_clicked(event):
self.loading = True
if update_connect(not self.connected):
connect_api()
else:
disconnect_api()
def set_state(state, init_connection=False):
self.loading = False
update_connect(state)
self.prefs.account.set_connected(state)
if state:
if init_connection:
self.prefs.plugin.connect_api()
self.prefs.emit('api-connect')
self.prefs.emit('channels-fetch')
else:
self.prefs.emit('api-disconnect')
return state
def validate(api_id, api_hash, phone_number):
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):
(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)
AuthDialog(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, True)
else:
set_state(False)
def disconnect_api():
self.prefs.emit('channels-clear')
self.api.reset_chats()
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()