Mercurial > libervia-desktop-kivy
annotate src/profile_manager.py @ 1:189b76859110
Profile manager: new profile creation is handled
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 26 Mar 2016 18:58:13 +0100 |
parents | 160cc95ad7ea |
children | 8f9ed634a5eb |
rev | line source |
---|---|
0 | 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_frontends.constants import Const as C | |
22 from sat_frontends.quick_frontend.quick_profile_manager import QuickProfileManager | |
23 from kivy.uix.boxlayout import BoxLayout | |
24 from kivy.uix import listview | |
25 from kivy.uix.button import Button | |
26 from kivy.uix.screenmanager import ScreenManager, Screen | |
27 from kivy.adapters import listadapter | |
1
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
28 from kivy import properties |
0 | 29 |
30 | |
31 class ProfileItem(listview.ListItemButton): | |
32 pass | |
33 | |
34 | |
35 class ProfileListAdapter(listadapter.ListAdapter): | |
36 | |
37 def __init__(self, pm, *args, **kwargs): | |
38 super(ProfileListAdapter, self).__init__(*args, **kwargs) | |
39 self.pm = pm | |
40 self.host = pm.host | |
41 | |
42 def closeUI(self, xmlui): | |
43 self.pm.screen_manager.transition.direction = 'right' | |
44 self.pm.screen_manager.current = 'profiles' | |
45 | |
46 def showUI(self, xmlui): | |
47 xmlui.setCloseCb(self.closeUI) | |
48 if xmlui.type == 'popup': | |
49 xmlui.bind(on_touch_up=lambda obj, value: self.closeUI(xmlui)) | |
50 self.pm.xmlui_screen.clear_widgets() | |
51 self.pm.xmlui_screen.add_widget(xmlui) | |
52 self.pm.screen_manager.transition.direction = 'left' | |
53 self.pm.screen_manager.current = 'xmlui' | |
54 | |
55 def select_item_view(self, view): | |
56 def authenticate_cb(data, cb_id, profile): | |
57 if C.bool(data.pop('validated', C.BOOL_FALSE)): | |
58 super(ProfileListAdapter, self).select_item_view(view) | |
59 self.host.actionManager(data, callback=authenticate_cb, ui_show_cb=self.showUI, profile=profile) | |
60 | |
61 self.host.launchAction(C.AUTHENTICATE_PROFILE_ID, callback=authenticate_cb, profile=view.text) | |
62 | |
63 | |
64 class ConnectButton(Button): | |
65 pass | |
66 | |
67 | |
1
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
68 class NewProfileScreen(Screen): |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
69 profile_name = properties.ObjectProperty(None) |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
70 jid = properties.ObjectProperty(None) |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
71 password = properties.ObjectProperty(None) |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
72 error_msg = properties.StringProperty('') |
0 | 73 |
74 def __init__(self, pm): | |
1
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
75 super(NewProfileScreen, self).__init__(name=u'new_profile') |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
76 self.pm = pm |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
77 self.host = pm.host |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
78 |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
79 def onCreationFailure(self, failure): |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
80 msg = [l for l in unicode(failure).split('\n') if l][-1] |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
81 self.error_msg = unicode(msg) |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
82 |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
83 def onCreationSuccess(self, profile): |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
84 self.pm.profiles_screen.reload() |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
85 self.host.bridge.profileStartSession(self.password.text, profile, callback=lambda dummy: self._sessionStarted(profile), errback=self.onCreationFailure) |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
86 |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
87 def _sessionStarted(self, profile): |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
88 jid = self.jid.text.strip() |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
89 self.host.bridge.setParam("JabberID", jid, "Connection", -1, profile) |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
90 self.host.bridge.setParam("Password", self.password.text, "Connection", -1, profile) |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
91 self.pm.screen_manager.transition.direction = 'right' |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
92 self.pm.screen_manager.current = 'profiles' |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
93 |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
94 def doCreate(self): |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
95 name = self.profile_name.text.strip() |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
96 # XXX: we use XMPP password for profile password to simplify |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
97 # if user want to change profile password, he can do it in preferences |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
98 self.host.bridge.asyncCreateProfile(name, self.password.text, callback=lambda: self.onCreationSuccess(name), errback=self.onCreationFailure) |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
99 |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
100 |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
101 class ProfilesScreen(Screen): |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
102 layout = properties.ObjectProperty(None) |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
103 |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
104 def __init__(self, pm): |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
105 super(ProfilesScreen, self).__init__(name=u'profiles') |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
106 self.pm = pm |
0 | 107 profiles = pm.host.bridge.getProfilesList() |
108 profiles.sort() | |
1
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
109 self.list_adapter = ProfileListAdapter(pm, |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
110 data=profiles, |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
111 cls=ProfileItem, |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
112 selection_mode='multiple', |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
113 allow_empty_selection=True, |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
114 ) |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
115 self.layout.add_widget(listview.ListView(adapter=self.list_adapter)) |
0 | 116 connect_btn = ConnectButton() |
117 self.layout.add_widget(connect_btn) | |
118 | |
1
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
119 def reload(self): |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
120 """Reload profiles list""" |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
121 profiles = self.pm.host.bridge.getProfilesList() |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
122 profiles.sort() |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
123 self.list_adapter.data = profiles |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
124 |
0 | 125 |
126 class ProfileManager(QuickProfileManager, BoxLayout): | |
127 | |
128 def __init__(self, host, autoconnect=None): | |
129 QuickProfileManager.__init__(self, host, autoconnect) | |
130 BoxLayout.__init__(self, orientation="vertical") | |
131 self.screen_manager = ScreenManager() | |
1
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
132 self.profiles_screen = ProfilesScreen(self) |
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
133 self.new_profile_screen = NewProfileScreen(self) |
0 | 134 self.xmlui_screen = Screen(name=u'xmlui') |
135 self.screen_manager.add_widget(self.profiles_screen) | |
136 self.screen_manager.add_widget(self.xmlui_screen) | |
1
189b76859110
Profile manager: new profile creation is handled
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
137 self.screen_manager.add_widget(self.new_profile_screen) |
0 | 138 self.add_widget(self.screen_manager) |
139 |