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 | advanced = self._buildAdvanced()
|
---|
40 | label = gtk.Label(xstr("prefs_tab_advanced"))
|
---|
41 | label.set_use_underline(True)
|
---|
42 | label.set_tooltip_text(xstr("prefs_tab_advanced_tooltip"))
|
---|
43 | notebook.append_page(advanced, label)
|
---|
44 |
|
---|
45 | def run(self, config):
|
---|
46 | """Run the preferences dialog.
|
---|
47 |
|
---|
48 | The dialog will be set up from data in the given configuration. If the
|
---|
49 | changes are accepted by the user, the configuration is updated and saved."""
|
---|
50 | self._fromConfig(config)
|
---|
51 |
|
---|
52 | self.show_all()
|
---|
53 | response = super(Preferences, self).run()
|
---|
54 | self.hide()
|
---|
55 |
|
---|
56 | if response==RESPONSETYPE_ACCEPT:
|
---|
57 | self._toConfig(config)
|
---|
58 | config.save()
|
---|
59 |
|
---|
60 | def _fromConfig(self, config):
|
---|
61 | """Setup the dialog from the given configuration."""
|
---|
62 | self._setLanguage(config.language)
|
---|
63 |
|
---|
64 | self._togglingAutoUpdate = True
|
---|
65 | self._autoUpdate.set_active(config.autoUpdate)
|
---|
66 | self._togglingAutoUpdate = False
|
---|
67 | if not config.autoUpdate:
|
---|
68 | self._warnedAutoUpdate = True
|
---|
69 |
|
---|
70 | self._updateURL.set_text(config.updateURL)
|
---|
71 |
|
---|
72 | def _toConfig(self, config):
|
---|
73 | """Setup the given config from the settings in the dialog."""
|
---|
74 | config.language = self._getLanguage()
|
---|
75 | config.autoUpdate = self._autoUpdate.get_active()
|
---|
76 | config.updateURL = self._updateURL.get_text()
|
---|
77 |
|
---|
78 | def _buildGeneral(self):
|
---|
79 | """Build the page for the general settings."""
|
---|
80 | mainAlignment = gtk.Alignment(xalign = 0.0, yalign = 0.0,
|
---|
81 | xscale = 0.0, yscale = 0.0)
|
---|
82 | mainAlignment.set_padding(padding_top = 16, padding_bottom = 32,
|
---|
83 | padding_left = 4, padding_right = 48)
|
---|
84 | mainBox = gtk.VBox()
|
---|
85 | mainAlignment.add(mainBox)
|
---|
86 |
|
---|
87 | languageBox = gtk.HBox()
|
---|
88 | mainBox.pack_start(languageBox, False, False, 4)
|
---|
89 |
|
---|
90 | label = gtk.Label(xstr("prefs_language"))
|
---|
91 | label.set_use_underline(True)
|
---|
92 |
|
---|
93 | languageBox.pack_start(label, False, False, 4)
|
---|
94 |
|
---|
95 | self._languageList = gtk.ListStore(str, str)
|
---|
96 | for language in const.languages:
|
---|
97 | self._languageList.append([xstr("prefs_lang_" + language),
|
---|
98 | language])
|
---|
99 |
|
---|
100 | self._languageComboBox = languageComboBox = \
|
---|
101 | gtk.ComboBox(model = self._languageList)
|
---|
102 | cell = gtk.CellRendererText()
|
---|
103 | languageComboBox.pack_start(cell, True)
|
---|
104 | languageComboBox.add_attribute(cell, 'text', 0)
|
---|
105 | languageComboBox.set_tooltip_text(xstr("prefs_language_tooltip"))
|
---|
106 | languageComboBox.connect("changed", self._languageChanged)
|
---|
107 | languageBox.pack_start(languageComboBox, False, False, 4)
|
---|
108 |
|
---|
109 | self._changingLanguage = False
|
---|
110 | self._warnedRestartNeeded = False
|
---|
111 |
|
---|
112 | label.set_mnemonic_widget(languageComboBox)
|
---|
113 |
|
---|
114 | return mainAlignment
|
---|
115 |
|
---|
116 | def _setLanguage(self, language):
|
---|
117 | """Set the language to the given one."""
|
---|
118 | iter = self._languageList.get_iter_first()
|
---|
119 | while iter is not None:
|
---|
120 | (lang,) = self._languageList.get(iter, 1)
|
---|
121 | if (not language and lang=="$system") or \
|
---|
122 | lang==language:
|
---|
123 | self._changingLanguage = True
|
---|
124 | self._languageComboBox.set_active_iter(iter)
|
---|
125 | self._changingLanguage = False
|
---|
126 | break
|
---|
127 | else:
|
---|
128 | iter = self._languageList.iter_next(iter)
|
---|
129 |
|
---|
130 | def _getLanguage(self):
|
---|
131 | """Get the language selected by the user."""
|
---|
132 | iter = self._languageComboBox.get_active_iter()
|
---|
133 | (lang,) = self._languageList.get(iter, 1)
|
---|
134 | return "" if lang=="$system" else lang
|
---|
135 |
|
---|
136 | def _languageChanged(self, comboBox):
|
---|
137 | """Called when the language has changed."""
|
---|
138 | if not self._changingLanguage and not self._warnedRestartNeeded:
|
---|
139 | dialog = gtk.MessageDialog(parent = self,
|
---|
140 | type = MESSAGETYPE_INFO,
|
---|
141 | message_format = xstr("prefs_restart"))
|
---|
142 | dialog.add_button(xstr("button_ok"), RESPONSETYPE_OK)
|
---|
143 | dialog.set_title(self.get_title())
|
---|
144 | dialog.format_secondary_markup(xstr("prefs_language_restart_sec"))
|
---|
145 | dialog.run()
|
---|
146 | dialog.hide()
|
---|
147 | self._warnedRestartNeeded = True
|
---|
148 |
|
---|
149 | def _buildAdvanced(self):
|
---|
150 | """Build the page for the advanced settings."""
|
---|
151 |
|
---|
152 | mainAlignment = gtk.Alignment(xalign = 0.0, yalign = 0.0,
|
---|
153 | xscale = 0.0, yscale = 0.0)
|
---|
154 | mainAlignment.set_padding(padding_top = 16, padding_bottom = 32,
|
---|
155 | padding_left = 4, padding_right = 48)
|
---|
156 | mainBox = gtk.VBox()
|
---|
157 | mainAlignment.add(mainBox)
|
---|
158 |
|
---|
159 | self._autoUpdate = gtk.CheckButton(xstr("prefs_update_auto"))
|
---|
160 | mainBox.pack_start(self._autoUpdate, False, False, 4)
|
---|
161 |
|
---|
162 | self._autoUpdate.set_use_underline(True)
|
---|
163 | self._autoUpdate.connect("toggled", self._autoUpdateToggled)
|
---|
164 | self._autoUpdate.set_tooltip_text(xstr("prefs_update_auto_tooltip"))
|
---|
165 | self._warnedAutoUpdate = False
|
---|
166 | self._togglingAutoUpdate = False
|
---|
167 |
|
---|
168 | updateURLBox = gtk.HBox()
|
---|
169 | mainBox.pack_start(updateURLBox, False, False, 4)
|
---|
170 | label = gtk.Label(xstr("prefs_update_url"))
|
---|
171 | label.set_use_underline(True)
|
---|
172 | updateURLBox.pack_start(label, False, False, 4)
|
---|
173 |
|
---|
174 | self._updateURL = gtk.Entry()
|
---|
175 | label.set_mnemonic_widget(self._updateURL)
|
---|
176 | self._updateURL.set_width_chars(40)
|
---|
177 | self._updateURL.set_tooltip_text(xstr("prefs_update_url_tooltip"))
|
---|
178 | self._updateURL.connect("changed", self._updateURLChanged)
|
---|
179 | updateURLBox.pack_start(self._updateURL, False, False, 4)
|
---|
180 |
|
---|
181 | return mainAlignment
|
---|
182 |
|
---|
183 | def _setOKButtonSensitivity(self):
|
---|
184 | """Set the sensitive state of the OK button."""
|
---|
185 | sensitive = False
|
---|
186 | try:
|
---|
187 | result = urlparse.urlparse(self._updateURL.get_text())
|
---|
188 | sensitive = result.scheme!="" and (result.netloc + result.path)!=""
|
---|
189 | except:
|
---|
190 | pass
|
---|
191 |
|
---|
192 | okButton = self.get_widget_for_response(RESPONSETYPE_ACCEPT)
|
---|
193 | okButton.set_sensitive(sensitive)
|
---|
194 |
|
---|
195 | def _autoUpdateToggled(self, button):
|
---|
196 | """Called when the auto update check button is toggled."""
|
---|
197 | if not self._togglingAutoUpdate and not self._warnedAutoUpdate and \
|
---|
198 | not self._autoUpdate.get_active():
|
---|
199 | dialog = gtk.MessageDialog(parent = self,
|
---|
200 | type = MESSAGETYPE_INFO,
|
---|
201 | message_format = xstr("prefs_update_auto_warning"))
|
---|
202 | dialog.add_button(xstr("button_ok"), RESPONSETYPE_OK)
|
---|
203 | dialog.set_title(self.get_title())
|
---|
204 | dialog.run()
|
---|
205 | dialog.hide()
|
---|
206 | self._warnedAutoUpdate = True
|
---|
207 |
|
---|
208 | def _updateURLChanged(self, entry):
|
---|
209 | """Called when the update URL is changed."""
|
---|
210 | self._setOKButtonSensitivity()
|
---|
211 |
|
---|
212 |
|
---|