diff frontends/primitivus/profile_manager.py @ 113:e5ca22113280

Primitivus: profile manager - new custom widgets: Password, List, GenericDialog, InputDialog, ConfirmationDialog
author Goffi <goffi@goffi.org>
date Thu, 01 Jul 2010 15:35:56 +0800
parents f551e44adb25
children eed4f77c942e
line wrap: on
line diff
--- a/frontends/primitivus/profile_manager.py	Wed Jun 30 14:24:24 2010 +0800
+++ b/frontends/primitivus/profile_manager.py	Thu Jul 01 15:35:56 2010 +0800
@@ -20,15 +20,81 @@
 """
 
 import urwid
+from custom_widgets import Password,List,InputDialog,ConfirmDialog
 
 
 class ProfileManager(urwid.WidgetWrap):
 
     def __init__(self, host):
         self.host = host
+        #profiles list
         profiles = self.host.bridge.getProfilesList()
-        widgets = [urwid.Text(str(profiles))]
-        content = urwid.SimpleListWalker(widgets)
-        display_widget = urwid.Frame(urwid.ListBox(content),urwid.AttrMap(urwid.Text("Profile Manager",align='center'),'title'))
-        urwid.WidgetWrap.__init__(self, display_widget)
+        profiles.sort()
+        
+        #login & password box must be created before list because of onProfileChange
+        self.login_wid = urwid.Edit(_('Login:'),align='center')
+        self.pass_wid = Password(_('Password:'),align='center')
+        
+        self.list_profile = List(profiles, style=['single'], align='center', on_state_change=self.onProfileChange)
+
+        #toto = urwid.Padding(urwid.Text('toto'), align='center')
+
+        #new & delete buttons
+        buttons = [urwid.Button(_("New"), self.onNewProfile),
+                  urwid.Button(_("Delete"), self.onDeleteProfile)]
+        buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center')
+
+        #second part: login information:
+        divider = urwid.Divider('-')
+
+        #connect button
+        connect_button = urwid.Button(_("Connect"), self.onConnectProfile)
+
+        #we now build the widget
+        body_content = urwid.SimpleListWalker([buttons_flow,self.list_profile,divider,self.login_wid, self.pass_wid, connect_button])
+        frame_body = urwid.ListBox(body_content)
+        frame = urwid.Frame(frame_body,urwid.AttrMap(urwid.Text("Profile Manager",align='center'),'title'))
+        self.main_widget = urwid.LineBox(frame)
+        urwid.WidgetWrap.__init__(self, self.main_widget)
+
+    def __refillProfiles(self):
+        """Update the list of profiles"""
+        profiles = self.host.bridge.getProfilesList()
+        profiles.sort()
+        self.list_profile.changeValues(profiles)
 
+    def cancelDialog(self, button):
+        self._set_w(self.main_widget)
+
+    def newProfile(self, button, edit):
+        self._set_w(self.main_widget)
+        name = edit.get_edit_text()
+        self.host.bridge.createProfile(name)
+        self.__refillProfiles()
+        self.list_profile.selectValue(name)
+
+    def deleteProfile(self, button):
+        self._set_w(self.main_widget)
+        self.host.bridge.deleteProfile(self.list_profile.getValues()[0])
+        self.__refillProfiles()
+        
+
+    def onNewProfile(self, e):
+        pop_up_widget = InputDialog(_("New profile"), _("Please enter a new profile name"), cancel_cb=self.cancelDialog, ok_cb=self.newProfile)
+        display_widget = urwid.Overlay(pop_up_widget, self.main_widget, 'center', ('relative', 40), 'middle', ('relative', 40))
+        self._set_w(display_widget)
+
+    def onDeleteProfile(self, e):
+        pop_up_widget = ConfirmDialog(_("Are you sure you want to delete the profile %s ?") % self.list_profile.getValues()[0], no_cb=self.cancelDialog, yes_cb=self.deleteProfile)
+        display_widget = urwid.Overlay(pop_up_widget, self.main_widget, 'center', ('relative', 40), 'middle', ('relative', 40))
+        self._set_w(display_widget)
+
+    def onProfileChange(self, list_wid):
+        profile_name = list_wid.getValues()[0]
+        jabberID = self.host.bridge.getParamA("JabberID", "Connection", profile_key=profile_name)
+        password = self.host.bridge.getParamA("Password", "Connection", profile_key=profile_name)
+        self.login_wid.set_edit_text(jabberID)
+        self.pass_wid.set_edit_text(password)
+        
+    def onConnectProfile(self, button):
+        pass