annotate frontends/primitivus/profile_manager.py @ 115:eed4f77c942e

primitivus: Profile Manager should now be fully fonctionnal
author Goffi <goffi@goffi.org>
date Thu, 01 Jul 2010 18:19:20 +0800
parents e5ca22113280
children 1f0fd6f03e2b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
112
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
5 Primitivus: a SAT frontend
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org)
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22 import urwid
115
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
23 from custom_widgets import Password,List,InputDialog,ConfirmDialog,Alert
112
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 class ProfileManager(urwid.WidgetWrap):
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28 def __init__(self, host):
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29 self.host = host
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
30 #profiles list
112
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 profiles = self.host.bridge.getProfilesList()
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
32 profiles.sort()
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
33
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
34 #login & password box must be created before list because of onProfileChange
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
35 self.login_wid = urwid.Edit(_('Login:'),align='center')
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
36 self.pass_wid = Password(_('Password:'),align='center')
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
37
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
38 self.list_profile = List(profiles, style=['single'], align='center', on_state_change=self.onProfileChange)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
39
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
40 #toto = urwid.Padding(urwid.Text('toto'), align='center')
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
41
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
42 #new & delete buttons
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
43 buttons = [urwid.Button(_("New"), self.onNewProfile),
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
44 urwid.Button(_("Delete"), self.onDeleteProfile)]
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
45 buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center')
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
46
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
47 #second part: login information:
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
48 divider = urwid.Divider('-')
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
49
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
50 #connect button
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
51 connect_button = urwid.Button(_("Connect"), self.onConnectProfile)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
52
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
53 #we now build the widget
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
54 body_content = urwid.SimpleListWalker([buttons_flow,self.list_profile,divider,self.login_wid, self.pass_wid, connect_button])
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
55 frame_body = urwid.ListBox(body_content)
115
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
56 frame = urwid.Frame(frame_body,urwid.AttrMap(urwid.Text(_("Profile Manager"),align='center'),'title'))
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
57 self.main_widget = urwid.LineBox(frame)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
58 urwid.WidgetWrap.__init__(self, self.main_widget)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
59
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
60 def __refillProfiles(self):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
61 """Update the list of profiles"""
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
62 profiles = self.host.bridge.getProfilesList()
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
63 profiles.sort()
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
64 self.list_profile.changeValues(profiles)
112
f551e44adb25 Primitivus first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
65
115
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
66 def __showPopUp(self, pop_up_widget):
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
67 display_widget = urwid.Overlay(pop_up_widget, self.main_widget, 'center', ('relative', 40), 'middle', ('relative', 40))
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
68 self._set_w(display_widget)
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
69
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
70 def cancelDialog(self, button):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
71 self._set_w(self.main_widget)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
72
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
73 def newProfile(self, button, edit):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
74 self._set_w(self.main_widget)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
75 name = edit.get_edit_text()
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
76 self.host.bridge.createProfile(name)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
77 self.__refillProfiles()
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
78 self.list_profile.selectValue(name)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
79
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
80 def deleteProfile(self, button):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
81 self._set_w(self.main_widget)
115
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
82 profile_name = self.list_profile.getValue()
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
83 if profile_name:
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
84 self.host.bridge.deleteProfile(profile_name)
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
85 self.__refillProfiles()
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
86
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
87
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
88 def onNewProfile(self, e):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
89 pop_up_widget = InputDialog(_("New profile"), _("Please enter a new profile name"), cancel_cb=self.cancelDialog, ok_cb=self.newProfile)
115
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
90 self.__showPopUp(pop_up_widget)
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
91
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
92 def onDeleteProfile(self, e):
115
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
93 pop_up_widget = ConfirmDialog(_("Are you sure you want to delete the profile %s ?") % self.list_profile.getValue(), no_cb=self.cancelDialog, yes_cb=self.deleteProfile)
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
94 self.__showPopUp(pop_up_widget)
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
95
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
96 def onProfileChange(self, list_wid):
115
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
97 profile_name = list_wid.getValue()
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
98 if profile_name:
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
99 jabberID = self.host.bridge.getParamA("JabberID", "Connection", profile_key=profile_name)
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
100 password = self.host.bridge.getParamA("Password", "Connection", profile_key=profile_name)
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
101 self.login_wid.set_edit_text(jabberID)
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
102 self.pass_wid.set_edit_text(password)
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
103
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents: 112
diff changeset
104 def onConnectProfile(self, button):
115
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
105 profile_name = self.list_profile.getValue()
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
106 if not profile_name:
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
107 pop_up_widget = Alert(_('No profile selected'), _('You need to create and select a profile before connecting'), ok_cb=self.cancelDialog)
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
108 self.__showPopUp(pop_up_widget)
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
109 elif profile_name[0] == '@':
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
110 pop_up_widget = Alert(_('Bad profile name'), _("A profile name can't start with a @"), ok_cb=self.cancelDialog)
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
111 self.__showPopUp(pop_up_widget)
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
112 else:
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
113 profile = self.host.bridge.getProfileName(profile_name)
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
114 assert(profile)
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
115 #TODO: move this to quick_app
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
116 old_jid = self.host.bridge.getParamA("JabberID", "Connection", profile_key=profile)
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
117 old_pass = self.host.bridge.getParamA("Password", "Connection", profile_key=profile)
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
118 new_jid = self.login_wid.get_edit_text()
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
119 new_pass = self.pass_wid.get_edit_text()
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
120
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
121 if old_jid != new_jid:
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
122 self.host.bridge.setParam("JabberID", new_jid, "Connection", profile)
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
123 self.host.bridge.setParam("Server", JID(new_jid).domain, "Connection", profile)
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
124 if old_pass != new_pass:
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
125 self.host.bridge.setParam("Password", new_pass, "Connection", profile)
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
126 self.host.plug_profile(profile)
eed4f77c942e primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents: 113
diff changeset
127