[42] | 1 | # The flight handling "wizard"
|
---|
| 2 |
|
---|
| 3 | from mlx.gui.common import *
|
---|
| 4 |
|
---|
| 5 | #-----------------------------------------------------------------------------
|
---|
| 6 |
|
---|
| 7 | class Page(gtk.VBox):
|
---|
| 8 | """A page in the flight wizard."""
|
---|
| 9 | def __init__(self, wizard):
|
---|
| 10 | """Construct the page."""
|
---|
| 11 | super(Page, self).__init__()
|
---|
| 12 | self._wizard = wizard
|
---|
| 13 |
|
---|
| 14 | #-----------------------------------------------------------------------------
|
---|
| 15 |
|
---|
| 16 | class LoginPage(Page):
|
---|
| 17 | """The login page."""
|
---|
| 18 | def __init__(self, wizard):
|
---|
| 19 | """Construct the login page."""
|
---|
| 20 | super(LoginPage, self).__init__(wizard)
|
---|
| 21 |
|
---|
| 22 | alignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
|
---|
| 23 | xscale = 0, yscale = 0.3)
|
---|
| 24 | label = gtk.Label("Enter your pilot's ID and password to\n"
|
---|
| 25 | "log in to the MAVA website and download\n"
|
---|
| 26 | "your booked flights")
|
---|
| 27 | label.set_justify(gtk.Justification.CENTER if pygobject
|
---|
| 28 | else gtk.JUSTIFY_CENTER)
|
---|
| 29 | alignment.add(label)
|
---|
| 30 | self.pack_start(alignment, True, True, 0)
|
---|
| 31 |
|
---|
| 32 | alignment = gtk.Alignment(xalign = 0.5, yalign = 0.5,
|
---|
| 33 | xscale = 0, yscale = 0.0)
|
---|
| 34 |
|
---|
| 35 | table = gtk.Table(2, 2)
|
---|
| 36 | table.set_row_spacings(4)
|
---|
| 37 | table.set_col_spacings(32)
|
---|
| 38 | alignment.add(table)
|
---|
| 39 |
|
---|
| 40 | labelAlignment = gtk.Alignment(xalign=1.0, xscale=0.0)
|
---|
| 41 | labelAlignment.add(gtk.Label("Pilot ID:"))
|
---|
| 42 | table.attach(labelAlignment, 0, 1, 0, 1)
|
---|
| 43 |
|
---|
| 44 | self._pilotID = gtk.Entry()
|
---|
| 45 | self._pilotID.connect("changed", self._setLoginButton)
|
---|
| 46 | table.attach(self._pilotID, 1, 2, 0, 1)
|
---|
| 47 |
|
---|
| 48 | labelAlignment = gtk.Alignment(xalign=1.0, xscale=0.0)
|
---|
| 49 | labelAlignment.add(gtk.Label("Password:"))
|
---|
| 50 | table.attach(labelAlignment, 0, 1, 1, 2)
|
---|
| 51 |
|
---|
| 52 | self._password = gtk.Entry()
|
---|
| 53 | self._password.set_visibility(False)
|
---|
| 54 | self._password.connect("changed", self._setLoginButton)
|
---|
| 55 | table.attach(self._password, 1, 2, 1, 2)
|
---|
| 56 |
|
---|
| 57 | self.pack_start(alignment, True, True, 0)
|
---|
| 58 |
|
---|
| 59 | alignment = gtk.Alignment(xalign = 1.0, xscale=0.0)
|
---|
| 60 | alignment.set_padding(padding_top = 4, padding_bottom = 10,
|
---|
| 61 | padding_left = 16, padding_right = 16)
|
---|
| 62 |
|
---|
| 63 | self._loginButton = gtk.Button("Login")
|
---|
| 64 | self._loginButton.set_sensitive(False)
|
---|
| 65 | self._loginButton.connect("clicked", self._loginClicked)
|
---|
| 66 |
|
---|
| 67 | alignment.add(self._loginButton)
|
---|
| 68 | self.pack_start(alignment, False, False, 0)
|
---|
| 69 |
|
---|
| 70 | config = self._wizard.gui.config
|
---|
| 71 | self._pilotID.set_text(config.pilotID)
|
---|
| 72 | self._password.set_text(config.password)
|
---|
| 73 |
|
---|
| 74 | def _setLoginButton(self, entry):
|
---|
| 75 | """Set the login button's sensitivity.
|
---|
| 76 |
|
---|
| 77 | The button is sensitive only if both the pilot ID and the password
|
---|
| 78 | fields contain values."""
|
---|
| 79 | self._loginButton.set_sensitive(self._pilotID.get_text()!="" and
|
---|
| 80 | self._password.get_text()!="")
|
---|
| 81 |
|
---|
| 82 | def _loginClicked(self, button):
|
---|
| 83 | """Called when the login button was clicked."""
|
---|
| 84 | self._wizard.gui.webHandler.login(self._pilotID.get_text(),
|
---|
| 85 | self._password.get_text(),
|
---|
| 86 | self._loginResultCallback)
|
---|
| 87 |
|
---|
| 88 | def _loginResultCallback(self, returned, result):
|
---|
| 89 | """The login result callback, called in the web handler's thread."""
|
---|
| 90 | gobject.idle_add(self._handleLoginResult, returned, result)
|
---|
| 91 |
|
---|
| 92 | def _handleLoginResult(self, returned, result):
|
---|
| 93 | """Handle the login result."""
|
---|
| 94 | if returned:
|
---|
| 95 | if result.loggedIn:
|
---|
| 96 | config = self._wizard.gui.config
|
---|
| 97 | config.pilotID = self._pilotID.get_text()
|
---|
| 98 | config.password = self._password.get_text()
|
---|
| 99 | self._wizard.nextPage()
|
---|
| 100 | else:
|
---|
| 101 | dialog = gtk.MessageDialog(type = MESSAGETYPE_ERROR,
|
---|
| 102 | buttons = BUTTONSTYPE_OK,
|
---|
| 103 | message_format =
|
---|
| 104 | "Invalid pilot's ID or password.")
|
---|
| 105 | dialog.format_secondary_markup("Check the ID and try to reenter"
|
---|
| 106 | " the password.")
|
---|
| 107 | dialog.run()
|
---|
| 108 | dialog.hide()
|
---|
| 109 | else:
|
---|
| 110 | dialog = gtk.MessageDialog(type = MESSAGETYPE_ERROR,
|
---|
| 111 | buttons = BUTTONSTYPE_OK,
|
---|
| 112 | message_format =
|
---|
| 113 | "Failed to connect to the MAVA website.")
|
---|
| 114 | dialog.format_secondary_markup("Try again in a few minutes.")
|
---|
| 115 | dialog.run()
|
---|
| 116 | dialog.hide()
|
---|
| 117 |
|
---|
| 118 | #-----------------------------------------------------------------------------
|
---|
| 119 |
|
---|
| 120 | class FlightSelectionPage(Page):
|
---|
| 121 | """The page to select the flight."""
|
---|
| 122 | def __init__(self, wizard):
|
---|
| 123 | """Construct the flight selection page."""
|
---|
| 124 | super(FlightSelectionPage, self).__init__(wizard)
|
---|
| 125 | self.pack_start(gtk.Label("Hello, te lo!"), False, False, 0)
|
---|
| 126 |
|
---|
| 127 | #-----------------------------------------------------------------------------
|
---|
| 128 |
|
---|
| 129 | class Wizard(gtk.VBox):
|
---|
| 130 | """The flight wizard."""
|
---|
| 131 | def __init__(self, gui):
|
---|
| 132 | """Construct the wizard."""
|
---|
| 133 | super(Wizard, self).__init__()
|
---|
| 134 |
|
---|
| 135 | self.gui = gui
|
---|
| 136 |
|
---|
| 137 | self._pages = []
|
---|
| 138 | self._currentPage = None
|
---|
| 139 |
|
---|
| 140 | self._pages.append(LoginPage(self))
|
---|
| 141 | self._pages.append(FlightSelectionPage(self))
|
---|
| 142 |
|
---|
| 143 | self.setCurrentPage(0)
|
---|
| 144 |
|
---|
| 145 | def setCurrentPage(self, index):
|
---|
| 146 | """Set the current page to the one with the given index."""
|
---|
| 147 | assert index < len(self._pages)
|
---|
| 148 |
|
---|
| 149 | if self._currentPage is not None:
|
---|
| 150 | self.remove(self._pages[self._currentPage])
|
---|
| 151 |
|
---|
| 152 | self._currentPage = index
|
---|
| 153 | self.add(self._pages[index])
|
---|
| 154 | self.show_all()
|
---|
| 155 |
|
---|
| 156 | def nextPage(self):
|
---|
| 157 | """Go to the next page."""
|
---|
| 158 | self.setCurrentPage(self._currentPage + 1)
|
---|
| 159 |
|
---|
| 160 | #-----------------------------------------------------------------------------
|
---|
| 161 |
|
---|