comparison src/cagou/core/profile_manager.py @ 15:56838ad5c84b

files reorganisation, cagou is now launched with python2 cagou.py in src/
author Goffi <goffi@goffi.org>
date Sat, 09 Jul 2016 17:24:01 +0200
parents src/profile_manager.py@33b619506832
children ba14b596b90e
comparison
equal deleted inserted replaced
14:21a432afd06d 15:56838ad5c84b
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
5 # Copyright (C) 2016 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 sat_frontends.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 import properties
31
32
33 class ProfileItem(listview.ListItemButton):
34 pass
35
36
37 class ProfileListAdapter(listadapter.ListAdapter):
38
39 def __init__(self, pm, *args, **kwargs):
40 super(ProfileListAdapter, self).__init__(*args, **kwargs)
41 self.pm = pm
42 self.host = pm.host
43
44 def closeUI(self, xmlui):
45 self.pm.screen_manager.transition.direction = 'right'
46 self.pm.screen_manager.current = 'profiles'
47
48 def showUI(self, xmlui):
49 xmlui.setCloseCb(self.closeUI)
50 if xmlui.type == 'popup':
51 xmlui.bind(on_touch_up=lambda obj, value: self.closeUI(xmlui))
52 self.pm.xmlui_screen.clear_widgets()
53 self.pm.xmlui_screen.add_widget(xmlui)
54 self.pm.screen_manager.transition.direction = 'left'
55 self.pm.screen_manager.current = 'xmlui'
56
57 def select_item_view(self, view):
58 def authenticate_cb(data, cb_id, profile):
59 if C.bool(data.pop('validated', C.BOOL_FALSE)):
60 super(ProfileListAdapter, self).select_item_view(view)
61 self.host.actionManager(data, callback=authenticate_cb, ui_show_cb=self.showUI, profile=profile)
62
63 self.host.launchAction(C.AUTHENTICATE_PROFILE_ID, callback=authenticate_cb, profile=view.text)
64
65
66 class ConnectButton(Button):
67
68 def __init__(self, profile_screen):
69 self.profile_screen = profile_screen
70 self.pm = profile_screen.pm
71 super(ConnectButton, self).__init__()
72
73
74 class NewProfileScreen(Screen):
75 profile_name = properties.ObjectProperty(None)
76 jid = properties.ObjectProperty(None)
77 password = properties.ObjectProperty(None)
78 error_msg = properties.StringProperty('')
79
80 def __init__(self, pm):
81 super(NewProfileScreen, self).__init__(name=u'new_profile')
82 self.pm = pm
83 self.host = pm.host
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 self.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 self.host.bridge.setParam("JabberID", jid, "Connection", -1, profile)
96 self.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 self.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 self.host = pm.host
112 super(DeleteProfilesScreen, self).__init__(name=u'delete_profiles')
113
114 def doDelete(self):
115 """This method will delete *ALL* selected profiles"""
116 to_delete = self.pm.getProfiles()
117 deleted = [0]
118
119 def deleteInc():
120 deleted[0] += 1
121 if deleted[0] == len(to_delete):
122 self.pm.profiles_screen.reload()
123 self.pm.screen_manager.transition.direction = 'right'
124 self.pm.screen_manager.current = 'profiles'
125
126 for profile in to_delete:
127 log.info(u"Deleteing profile [{}]".format(profile))
128 self.host.bridge.asyncDeleteProfile(profile, callback=deleteInc, errback=deleteInc)
129
130
131 class ProfilesScreen(Screen):
132 layout = properties.ObjectProperty(None)
133
134 def __init__(self, pm):
135 self.pm = pm
136 profiles = pm.host.bridge.getProfilesList()
137 profiles.sort()
138 self.list_adapter = ProfileListAdapter(pm,
139 data=profiles,
140 cls=ProfileItem,
141 selection_mode='multiple',
142 allow_empty_selection=True,
143 )
144 super(ProfilesScreen, self).__init__(name=u'profiles')
145 self.layout.add_widget(listview.ListView(adapter=self.list_adapter))
146 connect_btn = ConnectButton(self)
147 self.layout.add_widget(connect_btn)
148
149 def reload(self):
150 """Reload profiles list"""
151 profiles = self.pm.host.bridge.getProfilesList()
152 profiles.sort()
153 self.list_adapter.data = profiles
154
155
156 class ProfileManager(QuickProfileManager, BoxLayout):
157
158 def __init__(self, host, autoconnect=None):
159 QuickProfileManager.__init__(self, host, autoconnect)
160 BoxLayout.__init__(self, orientation="vertical")
161 self.screen_manager = ScreenManager()
162 self.profiles_screen = ProfilesScreen(self)
163 self.new_profile_screen = NewProfileScreen(self)
164 self.delete_profiles_screen = DeleteProfilesScreen(self)
165 self.xmlui_screen = Screen(name=u'xmlui')
166 self.screen_manager.add_widget(self.profiles_screen)
167 self.screen_manager.add_widget(self.xmlui_screen)
168 self.screen_manager.add_widget(self.new_profile_screen)
169 self.screen_manager.add_widget(self.delete_profiles_screen)
170 self.add_widget(self.screen_manager)
171
172 def getProfiles(self):
173 return [pi.text for pi in self.profiles_screen.list_adapter.selection]