Mercurial > libervia-backend
diff frontends/wix/profile_manager.py @ 68:9b842086d915
multiple profiles update
- Wix: new profile managed, it appear at launch in place of the contact list, and disappear once the profile is choosen
- SàT: default profile is now saved, first one is choosed is no default profile
- Bridge: new delete profile method
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 25 Feb 2010 17:09:18 +1100 |
parents | |
children | 8f2ed279784b |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/frontends/wix/profile_manager.py Thu Feb 25 17:09:18 2010 +1100 @@ -0,0 +1,150 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +""" +wix: a SAT frontend +Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org) + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see <http://www.gnu.org/licenses/>. +""" + + + +import wx +import pdb +from logging import debug, info, error +from tools.jid import JID +import pdb + + +class ProfileManager(wx.Panel): + def __init__(self, host): + super(ProfileManager, self).__init__(host) + self.host = host + + #self.sizer = wx.FlexGridSizer(cols=2) + self.sizer = wx.BoxSizer(wx.VERTICAL) + self.SetSizer(self.sizer) + + profiles = self.host.bridge.getProfilesList() + self.profile_name = wx.ComboBox(self, -1, style=wx.CB_READONLY|wx.CB_SORT) + self.__refillProfiles() + self.Bind(wx.EVT_COMBOBOX, self.onProfileChange) + self.panel_id = wx + + self.sizer.Add(wx.Window(self, -1), 1) + self.sizer.Add(wx.StaticText(self, -1, "Profile:"), 0, flag=wx.ALIGN_CENTER) + self.sizer.Add(self.profile_name, 0, flag=wx.ALIGN_CENTER) + button_panel = wx.Panel(self) + button_panel.sizer = wx.BoxSizer(wx.HORIZONTAL) + button_panel.SetSizer(button_panel.sizer) + button_new = wx.Button(button_panel, -1, "New") + button_del = wx.Button(button_panel, -1, "Delete") + button_panel.sizer.Add(button_new) + button_panel.sizer.Add(button_del) + self.sizer.Add(button_panel, flag=wx.CENTER) + self.Bind(wx.EVT_BUTTON, self.onNewProfile, button_new) + self.Bind(wx.EVT_BUTTON, self.onDeleteProfile, button_del) + + login_box = wx.StaticBox(self, -1, "Login") + self.login_sizer = wx.StaticBoxSizer(login_box, wx.VERTICAL) + self.sizer.Add(self.login_sizer, 1, wx.EXPAND | wx.ALL) + self.login_jid = wx.TextCtrl(self, -1) + self.login_sizer.Add(wx.StaticText(self, -1, "JID:"), 0, flag=wx.ALIGN_CENTER) + self.login_sizer.Add(self.login_jid, flag=wx.EXPAND) + self.login_pass = wx.TextCtrl(self, -1, style = wx.TE_PASSWORD) + self.login_sizer.Add(wx.StaticText(self, -1, "Password:"), 0, flag=wx.ALIGN_CENTER) + self.login_sizer.Add(self.login_pass, flag=wx.EXPAND) + + loggin_button = wx.Button(self, -1, "Connect") + self.Bind(wx.EVT_BUTTON, self.onConnectButton, loggin_button) + self.login_sizer.Add(loggin_button, flag=wx.ALIGN_CENTER) + + self.sizer.Add(wx.Window(self, -1), 1) + + #Now we can set the default value + self.__setDefault() + + + def __setDefault(self): + profile_default = self.host.bridge.getProfileName("@DEFAULT@") + if profile_default: + self.profile_name.SetValue(profile_default) + self.onProfileChange(None) + + def __refillProfiles(self): + """Update profiles with current names. Must be called after a profile change""" + self.profile_name.Clear() + profiles = self.host.bridge.getProfilesList() + profiles.sort() + for profile in profiles: + self.profile_name.Append(profile) + + + def onNewProfile(self, event): + dlg = wx.TextEntryDialog(self, "Please enter the new profile name", "New profile", style = wx.OK | wx.CANCEL) + if dlg.ShowModal() == wx.ID_OK: + name = dlg.GetValue() + if name: + if name[0]=='@': + wx.MessageDialog(self, "A profile name can't start with a @", "Bad profile name", wx.ICON_ERROR).ShowModal() + else: + profile = self.host.bridge.createProfile(name) + self.__refillProfiles() + self.profile_name.SetValue(name) + dlg.Destroy() + + def onDeleteProfile(self, event): + name = self.profile_name.GetValue() + if not name: + return + dlg = wx.MessageDialog(self, "Are you sure to delete the profile [%s]" % name, "Confirmation", wx.ICON_QUESTION | wx.YES_NO) + if dlg.ShowModal() == wx.ID_YES: + self.host.bridge.deleteProfile(name) + self.__refillProfiles() + self.__setDefault() + dlg.Destroy() + + def onProfileChange(self, event): + """Called when a profile is choosen in the combo box""" + jabberID = self.host.bridge.getParamA("JabberID", "Connection", profile_key=self.profile_name.GetValue()) + password = self.host.bridge.getParamA("Password", "Connection", profile_key=self.profile_name.GetValue()) + self.login_jid.SetValue(jabberID) + self.login_pass.SetValue(password) + + def onConnectButton(self, event): + """Called when the Connect button is pressed""" + name = self.profile_name.GetValue() + if not name: + wx.MessageDialog(self, "You must select a profile a create a new one before connecting", "No profile selected", wx.ICON_ERROR).ShowModal() + return + if name[0]=='@': + wx.MessageDialog(self, "A profile name can't start with a @", "Bad profile name", wx.ICON_ERROR).ShowModal() + return + profile = None # gof + profile = self.host.bridge.getProfileName(name) + if not profile: + debug("The profile is new, we create it") + old_jid = self.host.bridge.getParamA("JabberID", "Connection", profile_key=profile) + old_pass = self.host.bridge.getParamA("Password", "Connection", profile_key=profile) + new_jid = self.login_jid.GetValue() + new_pass = self.login_pass.GetValue() + if old_jid != new_jid: + debug('Saving new JID') + self.host.bridge.setParam("JabberID", new_jid, "Connection", profile) + if old_pass != new_pass: + debug('Saving new password') + self.host.bridge.setParam("JabberID", new_pass, "Connection", profile) + self.host.plug_profile(name) +