Skip to content

Commit c48ed95

Browse files
authored
Merge pull request #158 from MightyCreak/fix-prefs-crash
Fix preferences crash
2 parents 464f348 + 0f5122a commit c48ed95

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- Translation: added French translation
1212

1313
### Fixed
14+
- Fixed #156: Preferences cannot be saved in latest release
1415
- Cleanups: use constructors instead of `new()` whenever possible in GTK
1516
classes
1617

src/diffuse/preferences.py

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,13 @@ def __init__(self, path: str) -> None:
208208
if len(a) > 0:
209209
p = a[0]
210210
if len(a) == 2 and p in self.bool_prefs:
211-
self.bool_prefs[p] = (a[1] == 'True')
211+
self.setBool(p, a[1] == 'True')
212212
elif len(a) == 2 and p in self.int_prefs:
213-
self.int_prefs[p] = max(
213+
self.setInt(p, max(
214214
self.int_prefs_min[p],
215-
min(int(a[1]), self.int_prefs_max[p]))
215+
min(int(a[1]), self.int_prefs_max[p])))
216216
elif len(a) == 2 and p in self.string_prefs:
217-
self.string_prefs[p] = a[1]
217+
self.setString(p, a[1])
218218
else:
219219
raise ValueError()
220220
except ValueError:
@@ -237,13 +237,13 @@ def _initFromTemplate(self, template):
237237
self._initFromTemplate(template[i])
238238
i += 1
239239
elif template[0] == 'Boolean':
240-
self.bool_prefs[template[1]] = template[2]
240+
self.setBool(template[1], template[2])
241241
elif template[0] == 'Integer':
242-
self.int_prefs[template[1]] = template[2]
242+
self.setInt(template[1], template[2])
243243
self.int_prefs_min[template[1]] = template[4]
244244
self.int_prefs_max[template[1]] = template[5]
245245
elif template[0] in ['String', 'File', 'Font', 'Encoding']:
246-
self.string_prefs[template[1]] = template[2]
246+
self.setString(template[1], template[2])
247247

248248
# callback used when a preference is toggled
249249
def _toggled_cb(self, widget, widgets, name):
@@ -273,11 +273,12 @@ def runDialog(self, parent: Gtk.Widget) -> None:
273273
accept = (dialog.run() == Gtk.ResponseType.OK)
274274
if accept:
275275
for k in self.bool_prefs:
276-
self.bool_prefs[k] = widgets[k].get_active()
276+
self.setBool(k, widgets[k].get_active())
277277
for k in self.int_prefs:
278-
self.int_prefs[k] = widgets[k].get_value_as_int()
278+
self.setInt(k, widgets[k].get_value_as_int())
279279
for k in self.string_prefs:
280-
self.string_prefs[k] = utils.null_to_empty(widgets[k].get_text())
280+
text = self._getWidgetText(widgets[k])
281+
self.setString(k, utils.null_to_empty(text))
281282
try:
282283
ss = []
283284
for k, bool_value in self.bool_prefs.items():
@@ -329,7 +330,7 @@ def _buildPrefsDialog(self, parent, widgets, template):
329330
w.show()
330331
elif tpl_section == 'Boolean':
331332
button = Gtk.CheckButton.new_with_mnemonic(tpl[3])
332-
button.set_active(self.bool_prefs[tpl[1]])
333+
button.set_active(self.getBool(tpl[1]))
333334
widgets[tpl[1]] = button
334335
table.attach(button, 1, i, 1, 1)
335336
button.connect('toggled', self._toggled_cb, widgets, tpl[1])
@@ -340,14 +341,14 @@ def _buildPrefsDialog(self, parent, widgets, template):
340341
label.set_yalign(0.5)
341342
table.attach(label, 0, i, 1, 1)
342343
label.show()
343-
if tpl[0] in ['Font', 'Integer']:
344+
if tpl_section in ['Font', 'Integer']:
344345
entry = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
345-
if tpl[0] == 'Font':
346+
if tpl_section == 'Font':
346347
button = Gtk.FontButton()
347-
button.set_font(self.string_prefs[tpl[1]])
348+
button.set_font(self.getString(tpl[1]))
348349
else:
349350
adj = Gtk.Adjustment(
350-
value=self.int_prefs[tpl[1]],
351+
value=self.getInt(tpl[1]),
351352
lower=tpl[4],
352353
upper=tpl[5],
353354
step_increment=1,
@@ -361,20 +362,34 @@ def _buildPrefsDialog(self, parent, widgets, template):
361362
entry.pack_start(button, False, False, 0)
362363
button.show()
363364
else:
364-
if tpl[0] == 'Encoding':
365+
if tpl_section == 'Encoding':
365366
entry = utils.EncodingMenu(self)
366367
entry.set_text(tpl[3])
367-
elif tpl[0] == 'File':
368+
elif tpl_section == 'File':
368369
entry = _FileEntry(parent, tpl[3])
369370
else:
370371
entry = Gtk.Entry()
371372
widgets[tpl[1]] = entry
372-
entry.set_text(self.string_prefs[tpl[1]])
373+
entry.set_text(self.getString(tpl[1]))
373374
table.attach(entry, 1, i, 1, 1)
374375
entry.show()
375376
table.show()
376377
return table
377378

379+
def _getWidgetText(self, widget):
380+
text = ""
381+
if (
382+
isinstance(widget, Gtk.Entry) or
383+
isinstance(widget, utils.EncodingMenu) or
384+
isinstance(widget, _FileEntry)
385+
):
386+
text = widget.get_text()
387+
elif isinstance(widget, Gtk.FontButton):
388+
text = widget.get_font()
389+
else:
390+
raise TypeError(f"Don't know how to get text from type: {type(widget)}")
391+
return text
392+
378393
# get/set methods to manipulate the preference values
379394
def getBool(self, name: str) -> bool:
380395
return self.bool_prefs[name]
@@ -385,6 +400,9 @@ def setBool(self, name: str, value: bool) -> None:
385400
def getInt(self, name: str) -> int:
386401
return self.int_prefs[name]
387402

403+
def setInt(self, name: str, value: int) -> None:
404+
self.int_prefs[name] = value
405+
388406
def getString(self, name: str) -> str:
389407
return self.string_prefs[name]
390408

@@ -395,10 +413,10 @@ def getEncodings(self) -> List[Optional[str]]:
395413
return self.encodings
396414

397415
def _getDefaultEncodings(self) -> List[str]:
398-
return self.string_prefs['encoding_auto_detect_codecs'].split()
416+
return self.getString('encoding_auto_detect_codecs').split()
399417

400418
def getDefaultEncoding(self) -> str:
401-
return self.string_prefs['encoding_default_codec']
419+
return self.getString('encoding_default_codec')
402420

403421
# attempt to convert a string to unicode from an unknown encoding
404422
def convertToUnicode(self, s):

0 commit comments

Comments
 (0)