Mercurial > libervia-backend
annotate frontends/src/primitivus/profile_manager.py @ 415:3348331e0f09
primitivus: fixed screen redraw after plugin profile
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 01 Nov 2011 22:55:58 +0100 |
parents | dd4caab17008 |
children | 28e4299d4553 |
rev | line source |
---|---|
112 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 Primitivus: a SAT frontend | |
228 | 6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org) |
112 | 7 |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 import urwid | |
222
3198bfd66daa
primitivus: refactoring to use urwid-satext which is now a separate project
Goffi <goffi@goffi.org>
parents:
217
diff
changeset
|
23 from urwid_satext.sat_widgets import Password,List,InputDialog,ConfirmDialog,Alert,FocusFrame |
225
fd9b7834d98a
distutils installation script, draft
Goffi <goffi@goffi.org>
parents:
223
diff
changeset
|
24 from sat.tools.jid import JID |
112 | 25 |
26 | |
27 class ProfileManager(urwid.WidgetWrap): | |
28 | |
29 def __init__(self, host): | |
30 self.host = host | |
113 | 31 #profiles list |
112 | 32 profiles = self.host.bridge.getProfilesList() |
113 | 33 profiles.sort() |
34 | |
35 #login & password box must be created before list because of onProfileChange | |
36 self.login_wid = urwid.Edit(_('Login:'),align='center') | |
37 self.pass_wid = Password(_('Password:'),align='center') | |
38 | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
39 self.list_profile = List(profiles, style=['single'], align='center', on_click=self.onProfileChange) |
113 | 40 |
41 #new & delete buttons | |
42 buttons = [urwid.Button(_("New"), self.onNewProfile), | |
43 urwid.Button(_("Delete"), self.onDeleteProfile)] | |
44 buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center') | |
45 | |
46 #second part: login information: | |
47 divider = urwid.Divider('-') | |
48 | |
49 #connect button | |
50 connect_button = urwid.Button(_("Connect"), self.onConnectProfile) | |
51 | |
52 #we now build the widget | |
53 body_content = urwid.SimpleListWalker([buttons_flow,self.list_profile,divider,self.login_wid, self.pass_wid, connect_button]) | |
54 frame_body = urwid.ListBox(body_content) | |
115
eed4f77c942e
primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents:
113
diff
changeset
|
55 frame = urwid.Frame(frame_body,urwid.AttrMap(urwid.Text(_("Profile Manager"),align='center'),'title')) |
113 | 56 self.main_widget = urwid.LineBox(frame) |
57 urwid.WidgetWrap.__init__(self, self.main_widget) | |
58 | |
59 def __refillProfiles(self): | |
60 """Update the list of profiles""" | |
61 profiles = self.host.bridge.getProfilesList() | |
62 profiles.sort() | |
63 self.list_profile.changeValues(profiles) | |
112 | 64 |
113 | 65 def cancelDialog(self, button): |
117 | 66 self.host.removePopUp() |
113 | 67 |
68 def newProfile(self, button, edit): | |
217 | 69 """Create the profile""" |
113 | 70 name = edit.get_edit_text() |
71 self.host.bridge.createProfile(name) | |
72 self.__refillProfiles() | |
217 | 73 #We select the profile created in the list |
113 | 74 self.list_profile.selectValue(name) |
117 | 75 self.host.removePopUp() |
113 | 76 |
77 def deleteProfile(self, button): | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
78 profile_name = self.list_profile.getSelectedValue() |
115
eed4f77c942e
primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents:
113
diff
changeset
|
79 if profile_name: |
eed4f77c942e
primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents:
113
diff
changeset
|
80 self.host.bridge.deleteProfile(profile_name) |
eed4f77c942e
primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents:
113
diff
changeset
|
81 self.__refillProfiles() |
117 | 82 self.host.removePopUp() |
113 | 83 |
84 | |
85 def onNewProfile(self, e): | |
86 pop_up_widget = InputDialog(_("New profile"), _("Please enter a new profile name"), cancel_cb=self.cancelDialog, ok_cb=self.newProfile) | |
117 | 87 self.host.showPopUp(pop_up_widget) |
113 | 88 |
89 def onDeleteProfile(self, e): | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
90 pop_up_widget = ConfirmDialog(_("Are you sure you want to delete the profile %s ?") % self.list_profile.getSelectedValue(), no_cb=self.cancelDialog, yes_cb=self.deleteProfile) |
117 | 91 self.host.showPopUp(pop_up_widget) |
113 | 92 |
93 def onProfileChange(self, list_wid): | |
413
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
94 def setJID(jabberID): |
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
95 self.login_wid.set_edit_text(jabberID) |
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
96 def setPassword(password): |
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
97 self.pass_wid.set_edit_text(password) |
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
98 |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
99 profile_name = list_wid.getSelectedValue() |
115
eed4f77c942e
primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents:
113
diff
changeset
|
100 if profile_name: |
413
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
101 self.host.bridge.asyncGetParamA("JabberID", "Connection", profile_key=profile_name, callback=setJID, errback=self.getParamError) |
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
102 self.host.bridge.asyncGetParamA("Password", "Connection", profile_key=profile_name, callback=setPassword, errback=self.getParamError) |
113 | 103 |
104 def onConnectProfile(self, button): | |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
117
diff
changeset
|
105 profile_name = self.list_profile.getSelectedValue() |
115
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) |
117 | 108 self.host.showPopUp(pop_up_widget) |
115
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) |
117 | 111 self.host.showPopUp(pop_up_widget) |
115
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 |
413
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
116 self.host.bridge.asyncGetParamA("JabberID", "Connection", profile_key=profile, |
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
117 callback=lambda old_jid: self.__old_jidReceived(old_jid, profile), errback=self.getParamError) |
115
eed4f77c942e
primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents:
113
diff
changeset
|
118 |
413
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
119 def __old_jidReceived(self, old_jid, profile): |
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
120 self.host.bridge.asyncGetParamA("Password", "Connection", profile_key=profile, |
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
121 callback=lambda old_pass: self.__old_passReceived(old_jid, old_pass, profile), errback=self.getParamError) |
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
122 |
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
123 def __old_passReceived(self, old_jid, old_pass, profile): |
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
124 """Check if we have new jid/pass, save them if it is the case, and plug profile""" |
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
125 new_jid = self.login_wid.get_edit_text() |
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
126 new_pass = self.pass_wid.get_edit_text() |
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
127 |
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
128 if old_jid != new_jid: |
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
129 self.host.bridge.setParam("JabberID", new_jid, "Connection", profile) |
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
130 self.host.bridge.setParam("Server", JID(new_jid).domain, "Connection", profile) |
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
131 if old_pass != new_pass: |
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
132 self.host.bridge.setParam("Password", new_pass, "Connection", profile) |
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
133 self.host.plug_profile(profile) |
115
eed4f77c942e
primitivus: Profile Manager should now be fully fonctionnal
Goffi <goffi@goffi.org>
parents:
113
diff
changeset
|
134 |
413
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
135 def getParamError(self): |
dd4caab17008
core: - individual parameters managed through sqlite
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
136 error(_("Can't get profile parameter")) |