comparison cagou/core/profile_manager.py @ 126:cd99f70ea592

global file reorganisation: - follow common convention by puttin cagou in "cagou" instead of "src/cagou" - added VERSION in cagou with current version - updated dates - moved main executable in /bin - moved buildozer files in root directory - temporary moved platform to assets/platform
author Goffi <goffi@goffi.org>
date Thu, 05 Apr 2018 17:11:21 +0200
parents src/cagou/core/profile_manager.py@7f7f3b8eb154
children b270fcc87304
comparison
equal deleted inserted replaced
125:b6e6afb0dc46 126:cd99f70ea592
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
5 # Copyright (C) 2016-2018 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 sat.core import log as logging
22 log = logging.getLogger(__name__)
23 from .constants import Const as C
24 from sat_frontends.quick_frontend.quick_profile_manager import QuickProfileManager
25 from kivy.uix.boxlayout import BoxLayout
26 from kivy.uix import listview
27 from kivy.uix.button import Button
28 from kivy.uix.screenmanager import ScreenManager, Screen
29 from kivy.adapters import listadapter
30 from kivy.metrics import sp
31 from kivy import properties
32 from cagou import G
33
34
35 class ProfileItem(listview.ListItemButton):
36 pass
37
38
39 class ProfileListAdapter(listadapter.ListAdapter):
40
41 def __init__(self, pm, *args, **kwargs):
42 super(ProfileListAdapter, self).__init__(*args, **kwargs)
43 self.pm = pm
44
45 def closeUI(self, xmlui):
46 self.pm.screen_manager.transition.direction = 'right'
47 self.pm.screen_manager.current = 'profiles'
48
49 def showUI(self, xmlui):
50 xmlui.setCloseCb(self.closeUI)
51 if xmlui.type == 'popup':
52 xmlui.bind(on_touch_up=lambda obj, value: self.closeUI(xmlui))
53 self.pm.xmlui_screen.clear_widgets()
54 self.pm.xmlui_screen.add_widget(xmlui)
55 self.pm.screen_manager.transition.direction = 'left'
56 self.pm.screen_manager.current = 'xmlui'
57
58 def select_item_view(self, view):
59 def authenticate_cb(data, cb_id, profile):
60 if C.bool(data.pop('validated', C.BOOL_FALSE)):
61 super(ProfileListAdapter, self).select_item_view(view)
62 G.host.actionManager(data, callback=authenticate_cb, ui_show_cb=self.showUI, profile=profile)
63
64 G.host.launchAction(C.AUTHENTICATE_PROFILE_ID, callback=authenticate_cb, profile=view.text)
65
66
67 class ConnectButton(Button):
68
69 def __init__(self, profile_screen):
70 self.profile_screen = profile_screen
71 self.pm = profile_screen.pm
72 super(ConnectButton, self).__init__()
73
74
75 class NewProfileScreen(Screen):
76 profile_name = properties.ObjectProperty(None)
77 jid = properties.ObjectProperty(None)
78 password = properties.ObjectProperty(None)
79 error_msg = properties.StringProperty('')
80
81 def __init__(self, pm):
82 super(NewProfileScreen, self).__init__(name=u'new_profile')
83 self.pm = pm
84
85 def onCreationFailure(self, failure):
86 msg = [l for l in unicode(failure).split('\n') if l][-1]
87 self.error_msg = unicode(msg)
88
89 def onCreationSuccess(self, profile):
90 self.pm.profiles_screen.reload()
91 G.host.bridge.profileStartSession(self.password.text, profile, callback=lambda dummy: self._sessionStarted(profile), errback=self.onCreationFailure)
92
93 def _sessionStarted(self, profile):
94 jid = self.jid.text.strip()
95 G.host.bridge.setParam("JabberID", jid, "Connection", -1, profile)
96 G.host.bridge.setParam("Password", self.password.text, "Connection", -1, profile)
97 self.pm.screen_manager.transition.direction = 'right'
98 self.pm.screen_manager.current = 'profiles'
99
100 def doCreate(self):
101 name = self.profile_name.text.strip()
102 # XXX: we use XMPP password for profile password to simplify
103 # if user want to change profile password, he can do it in preferences
104 G.host.bridge.asyncCreateProfile(name, self.password.text, callback=lambda: self.onCreationSuccess(name), errback=self.onCreationFailure)
105
106
107 class DeleteProfilesScreen(Screen):
108
109 def __init__(self, pm):
110 self.pm = pm
111 super(DeleteProfilesScreen, self).__init__(name=u'delete_profiles')
112
113 def doDelete(self):
114 """This method will delete *ALL* selected profiles"""
115 to_delete = self.pm.getProfiles()
116 deleted = [0]
117
118 def deleteInc():
119 deleted[0] += 1
120 if deleted[0] == len(to_delete):
121 self.pm.profiles_screen.reload()
122 self.pm.screen_manager.transition.direction = 'right'
123 self.pm.screen_manager.current = 'profiles'
124
125 for profile in to_delete:
126 log.info(u"Deleteing profile [{}]".format(profile))
127 G.host.bridge.asyncDeleteProfile(profile, callback=deleteInc, errback=deleteInc)
128
129
130 class ProfilesScreen(Screen):
131 layout = properties.ObjectProperty(None)
132
133 def __init__(self, pm):
134 self.pm = pm
135 self.list_adapter = ProfileListAdapter(pm,
136 data=[],
137 cls=ProfileItem,
138 args_converter=self.converter,
139 selection_mode='multiple',
140 allow_empty_selection=True,
141 )
142 super(ProfilesScreen, self).__init__(name=u'profiles')
143 self.layout.add_widget(listview.ListView(adapter=self.list_adapter))
144 connect_btn = ConnectButton(self)
145 self.layout.add_widget(connect_btn)
146 self.reload()
147
148 def _profilesListGetCb(self, profiles):
149 profiles.sort()
150 self.list_adapter.data = profiles
151
152 def converter(self, row_idx, obj):
153 return {'text': obj,
154 'size_hint_y': None,
155 'height': sp(40)}
156
157 def reload(self):
158 """Reload profiles list"""
159 G.host.bridge.profilesListGet(callback=self._profilesListGetCb)
160
161
162 class ProfileManager(QuickProfileManager, BoxLayout):
163
164 def __init__(self, autoconnect=None):
165 QuickProfileManager.__init__(self, G.host, autoconnect)
166 BoxLayout.__init__(self, orientation="vertical")
167 self.screen_manager = ScreenManager()
168 self.profiles_screen = ProfilesScreen(self)
169 self.new_profile_screen = NewProfileScreen(self)
170 self.delete_profiles_screen = DeleteProfilesScreen(self)
171 self.xmlui_screen = Screen(name=u'xmlui')
172 self.screen_manager.add_widget(self.profiles_screen)
173 self.screen_manager.add_widget(self.xmlui_screen)
174 self.screen_manager.add_widget(self.new_profile_screen)
175 self.screen_manager.add_widget(self.delete_profiles_screen)
176 self.add_widget(self.screen_manager)
177
178 def getProfiles(self):
179 return [pi.text for pi in self.profiles_screen.list_adapter.selection]