1 | # Module for the preferences dialog
|
---|
2 |
|
---|
3 | #------------------------------------------------------------------------------
|
---|
4 |
|
---|
5 | from common import *
|
---|
6 |
|
---|
7 | from mlx.i18n import xstr
|
---|
8 | import mlx.const as const
|
---|
9 |
|
---|
10 | import urlparse
|
---|
11 |
|
---|
12 | #------------------------------------------------------------------------------
|
---|
13 |
|
---|
14 | class Preferences(gtk.Dialog):
|
---|
15 | """The preferences dialog."""
|
---|
16 | def __init__(self, gui):
|
---|
17 | """Construct the dialog."""
|
---|
18 | super(Preferences, self).__init__(WINDOW_TITLE_BASE + " " +
|
---|
19 | xstr("prefs_title"),
|
---|
20 | gui.mainWindow,
|
---|
21 | DIALOG_MODAL)
|
---|
22 |
|
---|
23 | self.add_button(xstr("button_cancel"), RESPONSETYPE_REJECT)
|
---|
24 | self.add_button(xstr("button_ok"), RESPONSETYPE_ACCEPT)
|
---|
25 |
|
---|
26 | self._gui = gui
|
---|
27 |
|
---|
28 | contentArea = self.get_content_area()
|
---|
29 |
|
---|
30 | notebook = gtk.Notebook()
|
---|
31 | contentArea.pack_start(notebook, True, True, 4)
|
---|
32 |
|
---|
33 | general = self._buildGeneral()
|
---|
34 | label = gtk.Label(xstr("prefs_tab_general"))
|
---|
35 | label.set_use_underline(True)
|
---|
36 | label.set_tooltip_text(xstr("prefs_tab_general_tooltip"))
|
---|
37 | notebook.append_page(general, label)
|
---|
38 |
|
---|
39 | messages = self._buildMessages()
|
---|
40 | label = gtk.Label(xstr("prefs_tab_messages"))
|
---|
41 | label.set_use_underline(True)
|
---|
42 | label.set_tooltip_text(xstr("prefs_tab_message_tooltip"))
|
---|
43 | notebook.append_page(messages, label)
|
---|
44 |
|
---|
45 | advanced = self._buildAdvanced()
|
---|
46 | label = gtk.Label(xstr("prefs_tab_advanced"))
|
---|
47 | label.set_use_underline(True)
|
---|
48 | label.set_tooltip_text(xstr("prefs_tab_advanced_tooltip"))
|
---|
49 | notebook.append_page(advanced, label)
|
---|
50 |
|
---|
51 | def run(self, config):
|
---|
52 | """Run the preferences dialog.
|
---|
53 |
|
---|
54 | The dialog will be set up from data in the given configuration. If the
|
---|
55 | changes are accepted by the user, the configuration is updated and saved."""
|
---|
56 | self._fromConfig(config)
|
---|
57 |
|
---|
58 | self.show_all()
|
---|
59 | response = super(Preferences, self).run()
|
---|
60 | self.hide()
|
---|
61 |
|
---|
62 | if response==RESPONSETYPE_ACCEPT:
|
---|
63 | self._toConfig(config)
|
---|
64 | config.save()
|
---|
65 |
|
---|
66 | def _fromConfig(self, config):
|
---|
67 | """Setup the dialog from the given configuration."""
|
---|
68 | self._setLanguage(config.language)
|
---|
69 | self._hideMinimizedWindow.set_active(config.hideMinimizedWindow)
|
---|
70 | self._onlineGateSystem.set_active(config.onlineGateSystem)
|
---|
71 | self._onlineACARS.set_active(config.onlineACARS)
|
---|
72 | self._flareTimeFromFS.set_active(config.flareTimeFromFS)
|
---|
73 | self._syncFSTime.set_active(config.syncFSTime)
|
---|
74 |
|
---|
75 | pirepDirectory = config.pirepDirectory
|
---|
76 | self._pirepDirectory.set_text("" if pirepDirectory is None
|
---|
77 | else pirepDirectory)
|
---|
78 |
|
---|
79 | for messageType in const.messageTypes:
|
---|
80 | level = config.getMessageTypeLevel(messageType)
|
---|
81 | button = self._msgFSCheckButtons[messageType]
|
---|
82 | button.set_active(level == const.MESSAGELEVEL_FS or
|
---|
83 | level == const.MESSAGELEVEL_BOTH)
|
---|
84 | button = self._msgSoundCheckButtons[messageType]
|
---|
85 | button.set_active(level == const.MESSAGELEVEL_SOUND or
|
---|
86 | level == const.MESSAGELEVEL_BOTH)
|
---|
87 |
|
---|
88 | self._togglingAutoUpdate = True
|
---|
89 | self._autoUpdate.set_active(config.autoUpdate)
|
---|
90 | self._togglingAutoUpdate = False
|
---|
91 | if not config.autoUpdate:
|
---|
92 | self._warnedAutoUpdate = True
|
---|
93 |
|
---|
94 | self._updateURL.set_text(config.updateURL)
|
---|
95 |
|
---|
96 | def _toConfig(self, config):
|
---|
97 | """Setup the given config from the settings in the dialog."""
|
---|
98 | config.language = self._getLanguage()
|
---|
99 | config.hideMinimizedWindow = self._hideMinimizedWindow.get_active()
|
---|
100 | config.onlineGateSystem = self._onlineGateSystem.get_active()
|
---|
101 | config.onlineACARS = self._onlineACARS.get_active()
|
---|
102 | config.flareTimeFromFS = self._flareTimeFromFS.get_active()
|
---|
103 | config.syncFSTime = self._syncFSTime.get_active()
|
---|
104 | config.pirepDirectory = self._pirepDirectory.get_text()
|
---|
105 |
|
---|
106 | for messageType in const.messageTypes:
|
---|
107 | fsButtonActive = self._msgFSCheckButtons[messageType].get_active()
|
---|
108 | soundButtonActive = self._msgSoundCheckButtons[messageType].get_active()
|
---|
109 | if fsButtonActive:
|
---|
110 | level = const.MESSAGELEVEL_BOTH if soundButtonActive \
|
---|
111 | else const.MESSAGELEVEL_FS
|
---|
112 | elif soundButtonActive:
|
---|
113 | level = const.MESSAGELEVEL_SOUND
|
---|
114 | else:
|
---|
115 | level = const.MESSAGELEVEL_NONE
|
---|
116 | config.setMessageTypeLevel(messageType, level)
|
---|
117 |
|
---|
118 | config.autoUpdate = self._autoUpdate.get_active()
|
---|
119 | config.updateURL = self._updateURL.get_text()
|
---|
120 |
|
---|
121 | def _buildGeneral(self):
|
---|
122 | """Build the page for the general settings."""
|
---|
123 | mainAlignment = gtk.Alignment(xalign = 0.0, yalign = 0.0,
|
---|
124 | xscale = 1.0, yscale = 0.0)
|
---|
125 | mainAlignment.set_padding(padding_top = 16, padding_bottom = 8,
|
---|
126 | padding_left = 4, padding_right = 4)
|
---|
127 | mainBox = gtk.VBox()
|
---|
128 | mainAlignment.add(mainBox)
|
---|
129 |
|
---|
130 | languageBox = gtk.HBox()
|
---|
131 | mainBox.pack_start(languageBox, False, False, 4)
|
---|
132 |
|
---|
133 | label = gtk.Label(xstr("prefs_language"))
|
---|
134 | label.set_use_underline(True)
|
---|
135 |
|
---|
136 | languageBox.pack_start(label, False, False, 4)
|
---|
137 |
|
---|
138 | self._languageList = gtk.ListStore(str, str)
|
---|
139 | for language in const.languages:
|
---|
140 | self._languageList.append([xstr("prefs_lang_" + language),
|
---|
141 | language])
|
---|
142 |
|
---|
143 | self._languageComboBox = languageComboBox = \
|
---|
144 | gtk.ComboBox(model = self._languageList)
|
---|
145 | cell = gtk.CellRendererText()
|
---|
146 | languageComboBox.pack_start(cell, True)
|
---|
147 | languageComboBox.add_attribute(cell, 'text', 0)
|
---|
148 | languageComboBox.set_tooltip_text(xstr("prefs_language_tooltip"))
|
---|
149 | languageComboBox.connect("changed", self._languageChanged)
|
---|
150 | languageBox.pack_start(languageComboBox, False, False, 4)
|
---|
151 |
|
---|
152 | label.set_mnemonic_widget(languageComboBox)
|
---|
153 |
|
---|
154 | self._changingLanguage = False
|
---|
155 | self._warnedRestartNeeded = False
|
---|
156 |
|
---|
157 | self._hideMinimizedWindow = gtk.CheckButton(xstr("prefs_hideMinimizedWindow"))
|
---|
158 | self._hideMinimizedWindow.set_use_underline(True)
|
---|
159 | self._hideMinimizedWindow.set_tooltip_text(xstr("prefs_hideMinimizedWindow_tooltip"))
|
---|
160 | mainBox.pack_start(self._hideMinimizedWindow, False, False, 4)
|
---|
161 |
|
---|
162 | self._onlineGateSystem = gtk.CheckButton(xstr("prefs_onlineGateSystem"))
|
---|
163 | self._onlineGateSystem.set_use_underline(True)
|
---|
164 | self._onlineGateSystem.set_tooltip_text(xstr("prefs_onlineGateSystem_tooltip"))
|
---|
165 | mainBox.pack_start(self._onlineGateSystem, False, False, 4)
|
---|
166 |
|
---|
167 | self._onlineACARS = gtk.CheckButton(xstr("prefs_onlineACARS"))
|
---|
168 | self._onlineACARS.set_use_underline(True)
|
---|
169 | self._onlineACARS.set_tooltip_text(xstr("prefs_onlineACARS_tooltip"))
|
---|
170 | mainBox.pack_start(self._onlineACARS, False, False, 4)
|
---|
171 |
|
---|
172 | self._flareTimeFromFS = gtk.CheckButton(xstr("prefs_flaretimeFromFS"))
|
---|
173 | self._flareTimeFromFS.set_use_underline(True)
|
---|
174 | self._flareTimeFromFS.set_tooltip_text(xstr("prefs_flaretimeFromFS_tooltip"))
|
---|
175 | mainBox.pack_start(self._flareTimeFromFS, False, False, 4)
|
---|
176 |
|
---|
177 | self._syncFSTime = gtk.CheckButton(xstr("prefs_syncFSTime"))
|
---|
178 | self._syncFSTime.set_use_underline(True)
|
---|
179 | self._syncFSTime.set_tooltip_text(xstr("prefs_syncFSTime_tooltip"))
|
---|
180 | mainBox.pack_start(self._syncFSTime, False, False, 4)
|
---|
181 |
|
---|
182 | pirepBox = gtk.HBox()
|
---|
183 | mainBox.pack_start(pirepBox, False, False, 4)
|
---|
184 |
|
---|
185 | label = gtk.Label(xstr("prefs_pirepDirectory"))
|
---|
186 | label.set_use_underline(True)
|
---|
187 | pirepBox.pack_start(label, False, False, 4)
|
---|
188 |
|
---|
189 | self._pirepDirectory = gtk.Entry()
|
---|
190 | self._pirepDirectory.set_tooltip_text(xstr("prefs_pirepDirectory_tooltip"))
|
---|
191 | label.set_mnemonic_widget(self._pirepDirectory)
|
---|
192 | pirepBox.pack_start(self._pirepDirectory, True, True, 4)
|
---|
193 |
|
---|
194 | self._pirepDirectoryButton = gtk.Button(xstr("button_browse"))
|
---|
195 | self._pirepDirectoryButton.connect("clicked",
|
---|
196 | self._pirepDirectoryButtonClicked)
|
---|
197 | pirepBox.pack_start(self._pirepDirectoryButton, False, False, 4)
|
---|
198 |
|
---|
199 | return mainAlignment
|
---|
200 |
|
---|
201 | def _setLanguage(self, language):
|
---|
202 | """Set the language to the given one."""
|
---|
203 | iter = self._languageList.get_iter_first()
|
---|
204 | while iter is not None:
|
---|
205 | (lang,) = self._languageList.get(iter, 1)
|
---|
206 | if (not language and lang=="$system") or \
|
---|
207 | lang==language:
|
---|
208 | self._changingLanguage = True
|
---|
209 | self._languageComboBox.set_active_iter(iter)
|
---|
210 | self._changingLanguage = False
|
---|
211 | break
|
---|
212 | else:
|
---|
213 | iter = self._languageList.iter_next(iter)
|
---|
214 |
|
---|
215 | def _getLanguage(self):
|
---|
216 | """Get the language selected by the user."""
|
---|
217 | iter = self._languageComboBox.get_active_iter()
|
---|
218 | (lang,) = self._languageList.get(iter, 1)
|
---|
219 | return "" if lang=="$system" else lang
|
---|
220 |
|
---|
221 | def _languageChanged(self, comboBox):
|
---|
222 | """Called when the language has changed."""
|
---|
223 | if not self._changingLanguage and not self._warnedRestartNeeded:
|
---|
224 | dialog = gtk.MessageDialog(parent = self,
|
---|
225 | type = MESSAGETYPE_INFO,
|
---|
226 | message_format = xstr("prefs_restart"))
|
---|
227 | dialog.add_button(xstr("button_ok"), RESPONSETYPE_OK)
|
---|
228 | dialog.set_title(self.get_title())
|
---|
229 | dialog.format_secondary_markup(xstr("prefs_language_restart_sec"))
|
---|
230 | dialog.run()
|
---|
231 | dialog.hide()
|
---|
232 | self._warnedRestartNeeded = True
|
---|
233 |
|
---|
234 | def _pirepDirectoryButtonClicked(self, button):
|
---|
235 | """Called when the PIREP directory button is clicked."""
|
---|
236 | dialog = gtk.FileChooserDialog(title = WINDOW_TITLE_BASE + " - " +
|
---|
237 | xstr("prefs_pirepDirectory_browser_title"),
|
---|
238 | action = FILE_CHOOSER_ACTION_SELECT_FOLDER,
|
---|
239 | buttons = (gtk.STOCK_CANCEL, RESPONSETYPE_CANCEL,
|
---|
240 | gtk.STOCK_OK, RESPONSETYPE_OK))
|
---|
241 |
|
---|
242 | directory = self._pirepDirectory.get_text()
|
---|
243 | if directory:
|
---|
244 | dialog.select_filename(directory)
|
---|
245 |
|
---|
246 | result = dialog.run()
|
---|
247 | dialog.hide()
|
---|
248 |
|
---|
249 | if result==RESPONSETYPE_OK:
|
---|
250 | self._pirepDirectory.set_text(dialog.get_filename())
|
---|
251 |
|
---|
252 | def _buildMessages(self):
|
---|
253 | """Build the page for the message settings."""
|
---|
254 |
|
---|
255 | mainAlignment = gtk.Alignment(xalign = 0.0, yalign = 0.0,
|
---|
256 | xscale = 0.0, yscale = 0.0)
|
---|
257 | mainAlignment.set_padding(padding_top = 16, padding_bottom = 8,
|
---|
258 | padding_left = 4, padding_right = 4)
|
---|
259 | mainBox = gtk.VBox()
|
---|
260 | mainAlignment.add(mainBox)
|
---|
261 |
|
---|
262 | table = gtk.Table(len(const.messageTypes) + 1, 3)
|
---|
263 | table.set_row_spacings(8)
|
---|
264 | table.set_col_spacings(32)
|
---|
265 | table.set_homogeneous(False)
|
---|
266 | mainBox.pack_start(table, False, False, 4)
|
---|
267 |
|
---|
268 | label = gtk.Label(xstr("prefs_msgs_fs"))
|
---|
269 | label.set_justify(JUSTIFY_CENTER)
|
---|
270 | label.set_alignment(0.5, 1.0)
|
---|
271 | table.attach(label, 1, 2, 0, 1)
|
---|
272 |
|
---|
273 | label = gtk.Label(xstr("prefs_msgs_sound"))
|
---|
274 | label.set_justify(JUSTIFY_CENTER)
|
---|
275 | label.set_alignment(0.5, 1.0)
|
---|
276 | table.attach(label, 2, 3, 0, 1)
|
---|
277 |
|
---|
278 | self._msgFSCheckButtons = {}
|
---|
279 | self._msgSoundCheckButtons = {}
|
---|
280 | row = 1
|
---|
281 | for messageType in const.messageTypes:
|
---|
282 | messageTypeStr = const.messageType2string(messageType)
|
---|
283 | label = gtk.Label(xstr("prefs_msgs_type_" + messageTypeStr))
|
---|
284 | label.set_justify(JUSTIFY_CENTER)
|
---|
285 | label.set_use_underline(True)
|
---|
286 | label.set_alignment(0.5, 0.5)
|
---|
287 | table.attach(label, 0, 1, row, row+1)
|
---|
288 |
|
---|
289 | fsCheckButton = gtk.CheckButton()
|
---|
290 | alignment = gtk.Alignment(xscale = 0.0, yscale = 0.0,
|
---|
291 | xalign = 0.5, yalign = 0.5)
|
---|
292 | alignment.add(fsCheckButton)
|
---|
293 | table.attach(alignment, 1, 2, row, row+1)
|
---|
294 | self._msgFSCheckButtons[messageType] = fsCheckButton
|
---|
295 |
|
---|
296 | soundCheckButton = gtk.CheckButton()
|
---|
297 | alignment = gtk.Alignment(xscale = 0.0, yscale = 0.0,
|
---|
298 | xalign = 0.5, yalign = 0.5)
|
---|
299 | alignment.add(soundCheckButton)
|
---|
300 | table.attach(alignment, 2, 3, row, row+1)
|
---|
301 | self._msgSoundCheckButtons[messageType] = soundCheckButton
|
---|
302 |
|
---|
303 | mnemonicWidget = gtk.Label("")
|
---|
304 | table.attach(mnemonicWidget, 3, 4, row, row+1)
|
---|
305 | label.set_mnemonic_widget(mnemonicWidget)
|
---|
306 | mnemonicWidget.connect("mnemonic-activate",
|
---|
307 | self._msgLabelActivated,
|
---|
308 | messageType)
|
---|
309 |
|
---|
310 | row += 1
|
---|
311 |
|
---|
312 | return mainAlignment
|
---|
313 |
|
---|
314 | def _msgLabelActivated(self, button, cycle_group, messageType):
|
---|
315 | """Called when the mnemonic of a label is activated.
|
---|
316 |
|
---|
317 | It cycles the corresponding options."""
|
---|
318 | fsCheckButton = self._msgFSCheckButtons[messageType]
|
---|
319 | soundCheckButton = self._msgSoundCheckButtons[messageType]
|
---|
320 |
|
---|
321 | num = 1 if fsCheckButton.get_active() else 0
|
---|
322 | num += 2 if soundCheckButton.get_active() else 0
|
---|
323 | num += 1
|
---|
324 |
|
---|
325 | fsCheckButton.set_active((num&0x01)==0x01)
|
---|
326 | soundCheckButton.set_active((num&0x02)==0x02)
|
---|
327 |
|
---|
328 | return True
|
---|
329 |
|
---|
330 | def _buildAdvanced(self):
|
---|
331 | """Build the page for the advanced settings."""
|
---|
332 |
|
---|
333 | mainAlignment = gtk.Alignment(xalign = 0.0, yalign = 0.0,
|
---|
334 | xscale = 1.0, yscale = 0.0)
|
---|
335 | mainAlignment.set_padding(padding_top = 16, padding_bottom = 8,
|
---|
336 | padding_left = 4, padding_right = 4)
|
---|
337 | mainBox = gtk.VBox()
|
---|
338 | mainAlignment.add(mainBox)
|
---|
339 |
|
---|
340 | self._autoUpdate = gtk.CheckButton(xstr("prefs_update_auto"))
|
---|
341 | mainBox.pack_start(self._autoUpdate, False, False, 4)
|
---|
342 |
|
---|
343 | self._autoUpdate.set_use_underline(True)
|
---|
344 | self._autoUpdate.connect("toggled", self._autoUpdateToggled)
|
---|
345 | self._autoUpdate.set_tooltip_text(xstr("prefs_update_auto_tooltip"))
|
---|
346 | self._warnedAutoUpdate = False
|
---|
347 | self._togglingAutoUpdate = False
|
---|
348 |
|
---|
349 | updateURLBox = gtk.HBox()
|
---|
350 | mainBox.pack_start(updateURLBox, False, False, 4)
|
---|
351 | label = gtk.Label(xstr("prefs_update_url"))
|
---|
352 | label.set_use_underline(True)
|
---|
353 | updateURLBox.pack_start(label, False, False, 4)
|
---|
354 |
|
---|
355 | self._updateURL = gtk.Entry()
|
---|
356 | label.set_mnemonic_widget(self._updateURL)
|
---|
357 | self._updateURL.set_width_chars(40)
|
---|
358 | self._updateURL.set_tooltip_text(xstr("prefs_update_url_tooltip"))
|
---|
359 | self._updateURL.connect("changed", self._updateURLChanged)
|
---|
360 | updateURLBox.pack_start(self._updateURL, True, True, 4)
|
---|
361 |
|
---|
362 | return mainAlignment
|
---|
363 |
|
---|
364 | def _setOKButtonSensitivity(self):
|
---|
365 | """Set the sensitive state of the OK button."""
|
---|
366 | sensitive = False
|
---|
367 | try:
|
---|
368 | result = urlparse.urlparse(self._updateURL.get_text())
|
---|
369 | sensitive = result.scheme!="" and (result.netloc + result.path)!=""
|
---|
370 | except:
|
---|
371 | pass
|
---|
372 |
|
---|
373 | okButton = self.get_widget_for_response(RESPONSETYPE_ACCEPT)
|
---|
374 | okButton.set_sensitive(sensitive)
|
---|
375 |
|
---|
376 | def _autoUpdateToggled(self, button):
|
---|
377 | """Called when the auto update check button is toggled."""
|
---|
378 | if not self._togglingAutoUpdate and not self._warnedAutoUpdate and \
|
---|
379 | not self._autoUpdate.get_active():
|
---|
380 | dialog = gtk.MessageDialog(parent = self,
|
---|
381 | type = MESSAGETYPE_INFO,
|
---|
382 | message_format = xstr("prefs_update_auto_warning"))
|
---|
383 | dialog.add_button(xstr("button_ok"), RESPONSETYPE_OK)
|
---|
384 | dialog.set_title(self.get_title())
|
---|
385 | dialog.run()
|
---|
386 | dialog.hide()
|
---|
387 | self._warnedAutoUpdate = True
|
---|
388 |
|
---|
389 | def _updateURLChanged(self, entry):
|
---|
390 | """Called when the update URL is changed."""
|
---|
391 | self._setOKButtonSensitivity()
|
---|