diff 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
line wrap: on
line diff
--- a/src/profile_manager.py	Sat Mar 26 16:20:52 2016 +0100
+++ b/src/profile_manager.py	Sat Mar 26 18:58:13 2016 +0100
@@ -25,7 +25,7 @@
 from kivy.uix.button import Button
 from kivy.uix.screenmanager import ScreenManager, Screen
 from kivy.adapters import listadapter
-from kivy.properties import ObjectProperty
+from kivy import properties
 
 
 class ProfileItem(listview.ListItemButton):
@@ -65,23 +65,63 @@
     pass
 
 
-class ProfileScreen(Screen):
-    layout = ObjectProperty(None)
+class NewProfileScreen(Screen):
+    profile_name = properties.ObjectProperty(None)
+    jid = properties.ObjectProperty(None)
+    password = properties.ObjectProperty(None)
+    error_msg = properties.StringProperty('')
 
     def __init__(self, pm):
-        super(ProfileScreen, self).__init__(name=u'profiles')
+        super(NewProfileScreen, self).__init__(name=u'new_profile')
+        self.pm = pm
+        self.host = pm.host
+
+    def onCreationFailure(self, failure):
+        msg = [l for l in unicode(failure).split('\n') if l][-1]
+        self.error_msg = unicode(msg)
+
+    def onCreationSuccess(self, profile):
+        self.pm.profiles_screen.reload()
+        self.host.bridge.profileStartSession(self.password.text, profile, callback=lambda dummy: self._sessionStarted(profile), errback=self.onCreationFailure)
+
+    def _sessionStarted(self, profile):
+        jid = self.jid.text.strip()
+        self.host.bridge.setParam("JabberID", jid, "Connection", -1, profile)
+        self.host.bridge.setParam("Password", self.password.text, "Connection", -1, profile)
+        self.pm.screen_manager.transition.direction = 'right'
+        self.pm.screen_manager.current = 'profiles'
+
+    def doCreate(self):
+        name = self.profile_name.text.strip()
+        # XXX: we use XMPP password for profile password to simplify
+        #      if user want to change profile password, he can do it in preferences
+        self.host.bridge.asyncCreateProfile(name, self.password.text, callback=lambda: self.onCreationSuccess(name), errback=self.onCreationFailure)
+
+
+class ProfilesScreen(Screen):
+    layout = properties.ObjectProperty(None)
+
+    def __init__(self, pm):
+        super(ProfilesScreen, self).__init__(name=u'profiles')
+        self.pm = pm
         profiles = pm.host.bridge.getProfilesList()
         profiles.sort()
-        list_adapter = ProfileListAdapter(pm,
-                                          data=profiles,
-                                          cls=ProfileItem,
-                                          selection_mode='multiple',
-                                          allow_empty_selection=True,
-                                          )
-        self.layout.add_widget(listview.ListView(adapter=list_adapter))
+        self.list_adapter = ProfileListAdapter(pm,
+                                               data=profiles,
+                                               cls=ProfileItem,
+                                               selection_mode='multiple',
+                                               allow_empty_selection=True,
+                                              )
+        self.layout.add_widget(listview.ListView(adapter=self.list_adapter))
         connect_btn = ConnectButton()
         self.layout.add_widget(connect_btn)
 
+    def reload(self):
+        """Reload profiles list"""
+        profiles = self.pm.host.bridge.getProfilesList()
+        profiles.sort()
+        self.list_adapter.data = profiles
+
 
 class ProfileManager(QuickProfileManager, BoxLayout):
 
@@ -89,9 +129,11 @@
         QuickProfileManager.__init__(self, host, autoconnect)
         BoxLayout.__init__(self, orientation="vertical")
         self.screen_manager = ScreenManager()
-        self.profiles_screen = ProfileScreen(self)
+        self.profiles_screen = ProfilesScreen(self)
+        self.new_profile_screen = NewProfileScreen(self)
         self.xmlui_screen = Screen(name=u'xmlui')
         self.screen_manager.add_widget(self.profiles_screen)
         self.screen_manager.add_widget(self.xmlui_screen)
+        self.screen_manager.add_widget(self.new_profile_screen)
         self.add_widget(self.screen_manager)