comparison sat_frontends/primitivus/profile_manager.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents frontends/src/primitivus/profile_manager.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # Primitivus: a SAT frontend
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from sat.core.i18n import _
21 from sat.core import log as logging
22 log = logging.getLogger(__name__)
23 from sat_frontends.quick_frontend.quick_profile_manager import QuickProfileManager
24 from sat_frontends.primitivus.constants import Const as C
25 from sat_frontends.primitivus.keys import action_key_map as a_key
26 from urwid_satext import sat_widgets
27 import urwid
28
29
30 class ProfileManager(QuickProfileManager, urwid.WidgetWrap):
31
32 def __init__(self, host, autoconnect=None):
33 QuickProfileManager.__init__(self, host, autoconnect)
34
35 #login & password box must be created before list because of onProfileChange
36 self.login_wid = sat_widgets.AdvancedEdit(_('Login:'), align='center')
37 self.pass_wid = sat_widgets.Password(_('Password:'), align='center')
38
39 style = ['no_first_select']
40 profiles = host.bridge.profilesListGet()
41 profiles.sort()
42 self.list_profile = sat_widgets.List(profiles, style=style, align='center', on_change=self.onProfileChange)
43
44 #new & delete buttons
45 buttons = [urwid.Button(_("New"), self.onNewProfile),
46 urwid.Button(_("Delete"), self.onDeleteProfile)]
47 buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center')
48
49 #second part: login information:
50 divider = urwid.Divider('-')
51
52 #connect button
53 connect_button = sat_widgets.CustomButton(_("Connect"), self.onConnectProfiles, align='center')
54
55 #we now build the widget
56 list_walker = urwid.SimpleFocusListWalker([buttons_flow,self.list_profile, divider, self.login_wid, self.pass_wid, connect_button])
57 frame_body = urwid.ListBox(list_walker)
58 frame = urwid.Frame(frame_body,urwid.AttrMap(urwid.Text(_("Profile Manager"),align='center'),'title'))
59 self.main_widget = urwid.LineBox(frame)
60 urwid.WidgetWrap.__init__(self, self.main_widget)
61
62 self.go(autoconnect)
63
64
65 def keypress(self, size, key):
66 if key == a_key['APP_QUIT']:
67 self.host.onExit()
68 raise urwid.ExitMainLoop()
69 elif key in (a_key['FOCUS_UP'], a_key['FOCUS_DOWN']):
70 focus_diff = 1 if key==a_key['FOCUS_DOWN'] else -1
71 list_box = self.main_widget.base_widget.body
72 current_focus = list_box.body.get_focus()[1]
73 if current_focus is None:
74 return
75 while True:
76 current_focus += focus_diff
77 if current_focus < 0 or current_focus >= len(list_box.body):
78 break
79 if list_box.body[current_focus].selectable():
80 list_box.set_focus(current_focus, 'above' if focus_diff == 1 else 'below')
81 list_box._invalidate()
82 return
83 return super(ProfileManager, self).keypress(size, key)
84
85 def cancelDialog(self, button):
86 self.host.removePopUp()
87
88 def newProfile(self, button, edit):
89 """Create the profile"""
90 name = edit.get_edit_text()
91 self.host.bridge.profileCreate(name, callback=lambda: self.newProfileCreated(name), errback=self.profileCreationFailure)
92
93 def newProfileCreated(self, profile):
94 # new profile will be selected, and a selected profile assume the session is started
95 self.host.bridge.profileStartSession('', profile, callback=lambda dummy: self.newProfileSessionStarted(profile), errback=self.profileCreationFailure)
96
97 def newProfileSessionStarted(self, profile):
98 self.host.removePopUp()
99 self.refillProfiles()
100 self.list_profile.selectValue(profile)
101 self.current.profile=profile
102 self.getConnectionParams(profile)
103 self.host.redraw()
104
105 def profileCreationFailure(self, reason):
106 self.host.removePopUp()
107 message = self._getErrorMessage(reason)
108 self.host.alert(_("Can't create profile"), message)
109
110 def deleteProfile(self, button):
111 self._deleteProfile()
112 self.host.removePopUp()
113
114 def onNewProfile(self, e):
115 pop_up_widget = sat_widgets.InputDialog(_("New profile"), _("Please enter a new profile name"), cancel_cb=self.cancelDialog, ok_cb=self.newProfile)
116 self.host.showPopUp(pop_up_widget)
117
118 def onDeleteProfile(self, e):
119 if self.current.profile:
120 pop_up_widget = sat_widgets.ConfirmDialog(_("Are you sure you want to delete the profile {} ?").format(self.current.profile), no_cb=self.cancelDialog, yes_cb=self.deleteProfile)
121 self.host.showPopUp(pop_up_widget)
122
123 def onConnectProfiles(self, button):
124 """Connect the profiles and start the main widget
125
126 @param button: the connect button
127 """
128 self._onConnectProfiles()
129
130 def resetFields(self):
131 """Set profile to None, and reset fields"""
132 super(ProfileManager, self).resetFields()
133 self.list_profile.unselectAll(invisible=True)
134
135 def setProfiles(self, profiles):
136 """Update the list of profiles"""
137 self.list_profile.changeValues(profiles)
138 self.host.redraw()
139
140 def getProfiles(self):
141 return self.list_profile.getSelectedValues()
142
143 def getJID(self):
144 return self.login_wid.get_edit_text()
145
146 def getPassword(self):
147 return self.pass_wid.get_edit_text()
148
149 def setJID(self, jid_):
150 self.login_wid.set_edit_text(jid_)
151 self.current.login = jid_
152 self.host.redraw() # FIXME: redraw should be avoided
153
154 def setPassword(self, password):
155 self.pass_wid.set_edit_text(password)
156 self.current.password = password
157 self.host.redraw()
158
159 def onProfileChange(self, list_wid, widget=None, selected=None):
160 """This is called when a profile is selected in the profile list.
161
162 @param list_wid: the List widget who sent the event
163 """
164 self.updateConnectionParams()
165 focused = list_wid.focus
166 selected = focused.getState() if focused is not None else False
167 if not selected: # profile was just unselected
168 return
169 focused.setState(False, invisible=True) # we don't want the widget to be selected until we are sure we can access it
170 def authenticate_cb(data, cb_id, profile):
171 if C.bool(data.pop('validated', C.BOOL_FALSE)):
172 self.current.profile = profile
173 focused.setState(True, invisible=True)
174 self.getConnectionParams(profile)
175 self.host.redraw()
176 self.host.actionManager(data, callback=authenticate_cb, profile=profile)
177
178 self.host.launchAction(C.AUTHENTICATE_PROFILE_ID, callback=authenticate_cb, profile=focused.text)
179