Mercurial > libervia-desktop-kivy
comparison libervia/desktop_kivy/core/profile_manager.py @ 493:b3cedbee561d
refactoring: rename `cagou` to `libervia.desktop_kivy` + update imports and names following backend changes
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 02 Jun 2023 18:26:16 +0200 |
parents | cagou/core/profile_manager.py@203755bbe0fe |
children |
comparison
equal
deleted
inserted
replaced
492:5114bbb5daa3 | 493:b3cedbee561d |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 | |
4 #Libervia Desktop-Kivy | |
5 # Copyright (C) 2016-2021 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 | |
21 from libervia.backend.core import log as logging | |
22 log = logging.getLogger(__name__) | |
23 from .constants import Const as C | |
24 from libervia.frontends.quick_frontend.quick_profile_manager import QuickProfileManager | |
25 from kivy.uix.boxlayout import BoxLayout | |
26 from kivy.uix.togglebutton import ToggleButton | |
27 from kivy.uix.screenmanager import ScreenManager, Screen | |
28 from kivy.metrics import sp | |
29 from kivy import properties | |
30 from libervia.desktop_kivy import G | |
31 | |
32 | |
33 class ProfileItem(ToggleButton): | |
34 ps = properties.ObjectProperty() | |
35 index = properties.NumericProperty(0) | |
36 | |
37 | |
38 class NewProfileScreen(Screen): | |
39 profile_name = properties.ObjectProperty(None) | |
40 jid = properties.ObjectProperty(None) | |
41 password = properties.ObjectProperty(None) | |
42 error_msg = properties.StringProperty('') | |
43 | |
44 def __init__(self, pm): | |
45 super(NewProfileScreen, self).__init__(name='new_profile') | |
46 self.pm = pm | |
47 | |
48 def on_creation_failure(self, failure): | |
49 msg = [l for l in str(failure).split('\n') if l][-1] | |
50 self.error_msg = str(msg) | |
51 | |
52 def on_creation_success(self, profile): | |
53 self.pm.profiles_screen.reload() | |
54 G.host.bridge.profile_start_session( | |
55 self.password.text, profile, | |
56 callback=lambda __: self._session_started(profile), | |
57 errback=self.on_creation_failure) | |
58 | |
59 def _session_started(self, profile): | |
60 jid = self.jid.text.strip() | |
61 G.host.bridge.param_set("JabberID", jid, "Connection", -1, profile) | |
62 G.host.bridge.param_set("Password", self.password.text, "Connection", -1, profile) | |
63 self.pm.screen_manager.transition.direction = 'right' | |
64 self.pm.screen_manager.current = 'profiles' | |
65 | |
66 def doCreate(self): | |
67 name = self.profile_name.text.strip() | |
68 # XXX: we use XMPP password for profile password to simplify | |
69 # if user want to change profile password, he can do it in preferences | |
70 G.host.bridge.profile_create( | |
71 name, self.password.text, '', | |
72 callback=lambda: self.on_creation_success(name), | |
73 errback=self.on_creation_failure) | |
74 | |
75 | |
76 class DeleteProfilesScreen(Screen): | |
77 | |
78 def __init__(self, pm): | |
79 self.pm = pm | |
80 super(DeleteProfilesScreen, self).__init__(name='delete_profiles') | |
81 | |
82 def do_delete(self): | |
83 """This method will delete *ALL* selected profiles""" | |
84 to_delete = self.pm.get_profiles() | |
85 deleted = [0] | |
86 | |
87 def delete_inc(): | |
88 deleted[0] += 1 | |
89 if deleted[0] == len(to_delete): | |
90 self.pm.profiles_screen.reload() | |
91 self.pm.screen_manager.transition.direction = 'right' | |
92 self.pm.screen_manager.current = 'profiles' | |
93 | |
94 for profile in to_delete: | |
95 log.info("Deleteing profile [{}]".format(profile)) | |
96 G.host.bridge.profile_delete_async( | |
97 profile, callback=delete_inc, errback=delete_inc) | |
98 | |
99 | |
100 class ProfilesScreen(Screen): | |
101 layout = properties.ObjectProperty(None) | |
102 profiles = properties.ListProperty() | |
103 | |
104 def __init__(self, pm): | |
105 self.pm = pm | |
106 super(ProfilesScreen, self).__init__(name='profiles') | |
107 self.reload() | |
108 | |
109 def _profiles_list_get_cb(self, profiles): | |
110 profiles.sort() | |
111 self.profiles = profiles | |
112 for idx, profile in enumerate(profiles): | |
113 item = ProfileItem(ps=self, index=idx, text=profile, group='profiles') | |
114 self.layout.add_widget(item) | |
115 | |
116 def converter(self, row_idx, obj): | |
117 return {'text': obj, | |
118 'size_hint_y': None, | |
119 'height': sp(40)} | |
120 | |
121 def reload(self): | |
122 """Reload profiles list""" | |
123 self.layout.clear_widgets() | |
124 G.host.bridge.profiles_list_get(callback=self._profiles_list_get_cb) | |
125 | |
126 | |
127 class ProfileManager(QuickProfileManager, BoxLayout): | |
128 selected = properties.ObjectProperty(None) | |
129 | |
130 def __init__(self, autoconnect=None): | |
131 QuickProfileManager.__init__(self, G.host, autoconnect) | |
132 BoxLayout.__init__(self, orientation="vertical") | |
133 self.screen_manager = ScreenManager() | |
134 self.profiles_screen = ProfilesScreen(self) | |
135 self.new_profile_screen = NewProfileScreen(self) | |
136 self.delete_profiles_screen = DeleteProfilesScreen(self) | |
137 self.xmlui_screen = Screen(name='xmlui') | |
138 self.screen_manager.add_widget(self.profiles_screen) | |
139 self.screen_manager.add_widget(self.xmlui_screen) | |
140 self.screen_manager.add_widget(self.new_profile_screen) | |
141 self.screen_manager.add_widget(self.delete_profiles_screen) | |
142 self.add_widget(self.screen_manager) | |
143 | |
144 def close_ui(self, xmlui, reason=None): | |
145 self.screen_manager.transition.direction = 'right' | |
146 self.screen_manager.current = 'profiles' | |
147 | |
148 def show_ui(self, xmlui): | |
149 xmlui.set_close_cb(self.close_ui) | |
150 if xmlui.type == 'popup': | |
151 xmlui.bind(on_touch_up=lambda obj, value: self.close_ui(xmlui)) | |
152 self.xmlui_screen.clear_widgets() | |
153 self.xmlui_screen.add_widget(xmlui) | |
154 self.screen_manager.transition.direction = 'left' | |
155 self.screen_manager.current = 'xmlui' | |
156 | |
157 def select_profile(self, profile_item): | |
158 if not profile_item.selected: | |
159 return | |
160 def authenticate_cb(data, cb_id, profile): | |
161 if not C.bool(data.pop('validated', C.BOOL_FALSE)): | |
162 # profile didn't validate, we unselect it | |
163 profile_item.state = 'normal' | |
164 self.selected = '' | |
165 else: | |
166 # state may have been modified so we need to be sure it's down | |
167 profile_item.state = 'down' | |
168 self.selected = profile_item | |
169 G.host.action_manager(data, callback=authenticate_cb, ui_show_cb=self.show_ui, | |
170 profile=profile) | |
171 | |
172 G.host.action_launch(C.AUTHENTICATE_PROFILE_ID, callback=authenticate_cb, | |
173 profile=profile_item.text) | |
174 | |
175 def get_profiles(self): | |
176 # for now we restrict to a single profile in LiberviaDesktopKivy | |
177 # TODO: handle multi-profiles | |
178 return [self.selected.text] if self.selected else [] |