1 | # The flight handling "wizard"
|
---|
2 |
|
---|
3 | from mlx.gui.common import *
|
---|
4 |
|
---|
5 | #-----------------------------------------------------------------------------
|
---|
6 |
|
---|
7 | class Page(gtk.Alignment):
|
---|
8 | """A page in the flight wizard."""
|
---|
9 | def __init__(self, wizard, title, help):
|
---|
10 | """Construct the page."""
|
---|
11 | super(Page, self).__init__(xalign = 0.0, yalign = 0.0,
|
---|
12 | xscale = 1.0, yscale = 1.0)
|
---|
13 | self.set_padding(padding_top = 4, padding_bottom = 4,
|
---|
14 | padding_left = 12, padding_right = 12)
|
---|
15 |
|
---|
16 | frame = gtk.Frame()
|
---|
17 | self.add(frame)
|
---|
18 |
|
---|
19 | print self.get_style()
|
---|
20 | style = self.get_style() if pygobject else self.rc_get_style()
|
---|
21 |
|
---|
22 | self._vbox = gtk.VBox()
|
---|
23 | frame.add(self._vbox)
|
---|
24 |
|
---|
25 | eventBox = gtk.EventBox()
|
---|
26 | eventBox.modify_bg(0, style.bg[3])
|
---|
27 |
|
---|
28 | alignment = gtk.Alignment(xalign = 0.0, xscale = 0.0)
|
---|
29 |
|
---|
30 | label = gtk.Label(title)
|
---|
31 | label.modify_fg(0, style.fg[3])
|
---|
32 | label.modify_font(pango.FontDescription("bold 24"))
|
---|
33 | alignment.set_padding(padding_top = 4, padding_bottom = 4,
|
---|
34 | padding_left = 6, padding_right = 0)
|
---|
35 |
|
---|
36 | alignment.add(label)
|
---|
37 | eventBox.add(alignment)
|
---|
38 |
|
---|
39 | self._vbox.pack_start(eventBox, False, False, 0)
|
---|
40 |
|
---|
41 | alignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
|
---|
42 | xscale = 0, yscale = 0.3)
|
---|
43 | label = gtk.Label(help)
|
---|
44 | label.set_justify(gtk.Justification.CENTER if pygobject
|
---|
45 | else gtk.JUSTIFY_CENTER)
|
---|
46 | alignment.add(label)
|
---|
47 | self._vbox.pack_start(alignment, True, True, 0)
|
---|
48 |
|
---|
49 | self._mainAlignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
|
---|
50 | xscale = 0, yscale = 0.0)
|
---|
51 | self._vbox.pack_start(self._mainAlignment, True, True, 0)
|
---|
52 |
|
---|
53 | buttonAlignment = gtk.Alignment(xalign = 1.0, xscale=0.0)
|
---|
54 | buttonAlignment.set_padding(padding_top = 4, padding_bottom = 10,
|
---|
55 | padding_left = 16, padding_right = 16)
|
---|
56 |
|
---|
57 | self._buttonBox = gtk.HButtonBox()
|
---|
58 | buttonAlignment.add(self._buttonBox)
|
---|
59 |
|
---|
60 | self._vbox.pack_start(buttonAlignment, False, False, 0)
|
---|
61 |
|
---|
62 | self._wizard = wizard
|
---|
63 |
|
---|
64 | def setMainWidget(self, widget):
|
---|
65 | """Set the given widget as the main one."""
|
---|
66 | self._mainAlignment.add(widget)
|
---|
67 |
|
---|
68 | def addButton(self, label):
|
---|
69 | """Add a button with the given label.
|
---|
70 |
|
---|
71 | Return the button object created."""
|
---|
72 | button = gtk.Button(label)
|
---|
73 | self._buttonBox.add(button)
|
---|
74 | return button
|
---|
75 |
|
---|
76 | #-----------------------------------------------------------------------------
|
---|
77 |
|
---|
78 | class LoginPage(Page):
|
---|
79 | """The login page."""
|
---|
80 | def __init__(self, wizard):
|
---|
81 | """Construct the login page."""
|
---|
82 | help = "Enter your MAVA pilot's ID and password to\n" \
|
---|
83 | "log in to the MAVA website and download\n" \
|
---|
84 | "your booked flights."
|
---|
85 | super(LoginPage, self).__init__(wizard, "Login", help)
|
---|
86 |
|
---|
87 | table = gtk.Table(2, 3)
|
---|
88 | table.set_row_spacings(4)
|
---|
89 | table.set_col_spacings(32)
|
---|
90 | self.setMainWidget(table)
|
---|
91 |
|
---|
92 | labelAlignment = gtk.Alignment(xalign=1.0, xscale=0.0)
|
---|
93 | label = gtk.Label("Pilot _ID:")
|
---|
94 | label.set_use_underline(True)
|
---|
95 | labelAlignment.add(label)
|
---|
96 | table.attach(labelAlignment, 0, 1, 0, 1)
|
---|
97 |
|
---|
98 | self._pilotID = gtk.Entry()
|
---|
99 | self._pilotID.connect("changed", self._setLoginButton)
|
---|
100 | self._pilotID.set_tooltip_text("Enter your MAVA pilot's ID. This "
|
---|
101 | "usually starts with a "
|
---|
102 | "'P' followed by 3 digits.")
|
---|
103 | table.attach(self._pilotID, 1, 2, 0, 1)
|
---|
104 | label.set_mnemonic_widget(self._pilotID)
|
---|
105 |
|
---|
106 | labelAlignment = gtk.Alignment(xalign=1.0, xscale=0.0)
|
---|
107 | label = gtk.Label("_Password:")
|
---|
108 | label.set_use_underline(True)
|
---|
109 | labelAlignment.add(label)
|
---|
110 | table.attach(labelAlignment, 0, 1, 1, 2)
|
---|
111 |
|
---|
112 | self._password = gtk.Entry()
|
---|
113 | self._password.set_visibility(False)
|
---|
114 | self._password.connect("changed", self._setLoginButton)
|
---|
115 | self._password.set_tooltip_text("Enter the password for your pilot's ID")
|
---|
116 | table.attach(self._password, 1, 2, 1, 2)
|
---|
117 | label.set_mnemonic_widget(self._password)
|
---|
118 |
|
---|
119 | self._rememberButton = gtk.CheckButton("_Remember password")
|
---|
120 | self._rememberButton.set_use_underline(True)
|
---|
121 | self._rememberButton.set_tooltip_text("If checked, your password will "
|
---|
122 | "be stored, so that you should "
|
---|
123 | "not have to enter it every time. "
|
---|
124 | "Note, however, that the password "
|
---|
125 | "is stored as text, and anybody "
|
---|
126 | "who can access your files will "
|
---|
127 | "be able to read it.")
|
---|
128 | table.attach(self._rememberButton, 1, 2, 2, 3, ypadding = 8)
|
---|
129 |
|
---|
130 | self._loginButton = self.addButton("_Login")
|
---|
131 | self._loginButton.set_sensitive(False)
|
---|
132 | self._loginButton.set_use_underline(True)
|
---|
133 | self._loginButton.connect("clicked", self._loginClicked)
|
---|
134 | self._loginButton.set_tooltip_text("Click to log in.")
|
---|
135 |
|
---|
136 | config = self._wizard.gui.config
|
---|
137 | self._pilotID.set_text(config.pilotID)
|
---|
138 | self._password.set_text(config.password)
|
---|
139 | self._rememberButton.set_active(config.rememberPassword)
|
---|
140 |
|
---|
141 | def _setLoginButton(self, entry):
|
---|
142 | """Set the login button's sensitivity.
|
---|
143 |
|
---|
144 | The button is sensitive only if both the pilot ID and the password
|
---|
145 | fields contain values."""
|
---|
146 | self._loginButton.set_sensitive(self._pilotID.get_text()!="" and
|
---|
147 | self._password.get_text()!="")
|
---|
148 |
|
---|
149 | def _loginClicked(self, button):
|
---|
150 | """Called when the login button was clicked."""
|
---|
151 | self._wizard.gui.webHandler.login(self._pilotID.get_text(),
|
---|
152 | self._password.get_text(),
|
---|
153 | self._loginResultCallback)
|
---|
154 |
|
---|
155 | def _loginResultCallback(self, returned, result):
|
---|
156 | """The login result callback, called in the web handler's thread."""
|
---|
157 | gobject.idle_add(self._handleLoginResult, returned, result)
|
---|
158 |
|
---|
159 | def _handleLoginResult(self, returned, result):
|
---|
160 | """Handle the login result."""
|
---|
161 | if returned:
|
---|
162 | if result.loggedIn:
|
---|
163 | config = self._wizard.gui.config
|
---|
164 |
|
---|
165 | config.pilotID = self._pilotID.get_text()
|
---|
166 |
|
---|
167 | rememberPassword = self._rememberButton.get_active()
|
---|
168 | config.password = self._password.get_text() if rememberPassword \
|
---|
169 | else ""
|
---|
170 |
|
---|
171 | config.rememberPassword = rememberPassword
|
---|
172 |
|
---|
173 | config.save()
|
---|
174 | self._wizard.nextPage()
|
---|
175 | else:
|
---|
176 | dialog = gtk.MessageDialog(type = MESSAGETYPE_ERROR,
|
---|
177 | buttons = BUTTONSTYPE_OK,
|
---|
178 | message_format =
|
---|
179 | "Invalid pilot's ID or password.")
|
---|
180 | dialog.format_secondary_markup("Check the ID and try to reenter"
|
---|
181 | " the password.")
|
---|
182 | dialog.run()
|
---|
183 | dialog.hide()
|
---|
184 | else:
|
---|
185 | dialog = gtk.MessageDialog(type = MESSAGETYPE_ERROR,
|
---|
186 | buttons = BUTTONSTYPE_OK,
|
---|
187 | message_format =
|
---|
188 | "Failed to connect to the MAVA website.")
|
---|
189 | dialog.format_secondary_markup("Try again in a few minutes.")
|
---|
190 | dialog.run()
|
---|
191 | dialog.hide()
|
---|
192 |
|
---|
193 | #-----------------------------------------------------------------------------
|
---|
194 |
|
---|
195 | class FlightSelectionPage(Page):
|
---|
196 | """The page to select the flight."""
|
---|
197 | def __init__(self, wizard):
|
---|
198 | """Construct the flight selection page."""
|
---|
199 | super(FlightSelectionPage, self).__init__(wizard, "Flight selection",
|
---|
200 | "Hello, te lo!")
|
---|
201 |
|
---|
202 | #-----------------------------------------------------------------------------
|
---|
203 |
|
---|
204 | class Wizard(gtk.VBox):
|
---|
205 | """The flight wizard."""
|
---|
206 | def __init__(self, gui):
|
---|
207 | """Construct the wizard."""
|
---|
208 | super(Wizard, self).__init__()
|
---|
209 |
|
---|
210 | self.gui = gui
|
---|
211 |
|
---|
212 | self._pages = []
|
---|
213 | self._currentPage = None
|
---|
214 |
|
---|
215 | self._pages.append(LoginPage(self))
|
---|
216 | self._pages.append(FlightSelectionPage(self))
|
---|
217 |
|
---|
218 | self.setCurrentPage(0)
|
---|
219 |
|
---|
220 | def setCurrentPage(self, index):
|
---|
221 | """Set the current page to the one with the given index."""
|
---|
222 | assert index < len(self._pages)
|
---|
223 |
|
---|
224 | if self._currentPage is not None:
|
---|
225 | self.remove(self._pages[self._currentPage])
|
---|
226 |
|
---|
227 | self._currentPage = index
|
---|
228 | self.add(self._pages[index])
|
---|
229 | self.show_all()
|
---|
230 |
|
---|
231 | def nextPage(self):
|
---|
232 | """Go to the next page."""
|
---|
233 | self.setCurrentPage(self._currentPage + 1)
|
---|
234 |
|
---|
235 | #-----------------------------------------------------------------------------
|
---|
236 |
|
---|