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 | config.save()
|
---|
100 | self._wizard.nextPage()
|
---|
101 | else:
|
---|
102 | dialog = gtk.MessageDialog(type = MESSAGETYPE_ERROR,
|
---|
103 | buttons = BUTTONSTYPE_OK,
|
---|
104 | message_format =
|
---|
105 | "Invalid pilot's ID or password.")
|
---|
106 | dialog.format_secondary_markup("Check the ID and try to reenter"
|
---|
107 | " the password.")
|
---|
108 | dialog.run()
|
---|
109 | dialog.hide()
|
---|
110 | else:
|
---|
111 | dialog = gtk.MessageDialog(type = MESSAGETYPE_ERROR,
|
---|
112 | buttons = BUTTONSTYPE_OK,
|
---|
113 | message_format =
|
---|
114 | "Failed to connect to the MAVA website.")
|
---|
115 | dialog.format_secondary_markup("Try again in a few minutes.")
|
---|
116 | dialog.run()
|
---|
117 | dialog.hide()
|
---|
118 |
|
---|
119 | #-----------------------------------------------------------------------------
|
---|
120 |
|
---|
121 | class FlightSelectionPage(Page):
|
---|
122 | """The page to select the flight."""
|
---|
123 | def __init__(self, wizard):
|
---|
124 | """Construct the flight selection page."""
|
---|
125 | super(FlightSelectionPage, self).__init__(wizard)
|
---|
126 | self.pack_start(gtk.Label("Hello, te lo!"), False, False, 0)
|
---|
127 |
|
---|
128 | #-----------------------------------------------------------------------------
|
---|
129 |
|
---|
130 | class Wizard(gtk.VBox):
|
---|
131 | """The flight wizard."""
|
---|
132 | def __init__(self, gui):
|
---|
133 | """Construct the wizard."""
|
---|
134 | super(Wizard, self).__init__()
|
---|
135 |
|
---|
136 | self.gui = gui
|
---|
137 |
|
---|
138 | self._pages = []
|
---|
139 | self._currentPage = None
|
---|
140 |
|
---|
141 | self._pages.append(LoginPage(self))
|
---|
142 | self._pages.append(FlightSelectionPage(self))
|
---|
143 |
|
---|
144 | self.setCurrentPage(0)
|
---|
145 |
|
---|
146 | def setCurrentPage(self, index):
|
---|
147 | """Set the current page to the one with the given index."""
|
---|
148 | assert index < len(self._pages)
|
---|
149 |
|
---|
150 | if self._currentPage is not None:
|
---|
151 | self.remove(self._pages[self._currentPage])
|
---|
152 |
|
---|
153 | self._currentPage = index
|
---|
154 | self.add(self._pages[index])
|
---|
155 | self.show_all()
|
---|
156 |
|
---|
157 | def nextPage(self):
|
---|
158 | """Go to the next page."""
|
---|
159 | self.setCurrentPage(self._currentPage + 1)
|
---|
160 |
|
---|
161 | #-----------------------------------------------------------------------------
|
---|
162 |
|
---|