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