From e19f1a192b45356e8035dd962a3a1076408261fb Mon Sep 17 00:00:00 2001 From: Romain Failliot Date: Fri, 15 Apr 2022 12:43:22 -0400 Subject: [PATCH 1/3] fix: get_text was not defined on all the widgets * Use `get_text` when available * Use `get_font` when Gtk.FontButton * Raise TypeError exception otherwise --- src/diffuse/preferences.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/diffuse/preferences.py b/src/diffuse/preferences.py index 76df451..d2f68e2 100644 --- a/src/diffuse/preferences.py +++ b/src/diffuse/preferences.py @@ -277,7 +277,8 @@ class Preferences: for k in self.int_prefs: self.int_prefs[k] = widgets[k].get_value_as_int() for k in self.string_prefs: - self.string_prefs[k] = utils.null_to_empty(widgets[k].get_text()) + text = self._getWidgetText(widgets[k]) + self.string_prefs[k] = utils.null_to_empty(text) try: ss = [] for k, bool_value in self.bool_prefs.items(): @@ -375,6 +376,20 @@ class Preferences: table.show() return table + def _getWidgetText(self, widget): + text = "" + if ( + isinstance(widget, Gtk.Entry) or + isinstance(widget, utils.EncodingMenu) or + isinstance(widget, _FileEntry) + ): + text = widget.get_text() + elif isinstance(widget, Gtk.FontButton): + text = widget.get_font() + else: + raise TypeError(f"Don't know how to get text from type: {type(widget)}") + return text + # get/set methods to manipulate the preference values def getBool(self, name: str) -> bool: return self.bool_prefs[name] From 6e96b1383ed84b48be5b32c711e8db00a9804e7c Mon Sep 17 00:00:00 2001 From: Romain Failliot Date: Fri, 15 Apr 2022 12:45:57 -0400 Subject: [PATCH 2/3] cleanups: use `get`/`set` methods instead of direct access --- src/diffuse/preferences.py | 43 ++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/diffuse/preferences.py b/src/diffuse/preferences.py index d2f68e2..3e57988 100644 --- a/src/diffuse/preferences.py +++ b/src/diffuse/preferences.py @@ -208,13 +208,13 @@ class Preferences: if len(a) > 0: p = a[0] if len(a) == 2 and p in self.bool_prefs: - self.bool_prefs[p] = (a[1] == 'True') + self.setBool(p, a[1] == 'True') elif len(a) == 2 and p in self.int_prefs: - self.int_prefs[p] = max( + self.setInt(p, max( self.int_prefs_min[p], - min(int(a[1]), self.int_prefs_max[p])) + min(int(a[1]), self.int_prefs_max[p]))) elif len(a) == 2 and p in self.string_prefs: - self.string_prefs[p] = a[1] + self.setString(p, a[1]) else: raise ValueError() except ValueError: @@ -237,13 +237,13 @@ class Preferences: self._initFromTemplate(template[i]) i += 1 elif template[0] == 'Boolean': - self.bool_prefs[template[1]] = template[2] + self.setBool(template[1], template[2]) elif template[0] == 'Integer': - self.int_prefs[template[1]] = template[2] + self.setInt(template[1], template[2]) self.int_prefs_min[template[1]] = template[4] self.int_prefs_max[template[1]] = template[5] elif template[0] in ['String', 'File', 'Font', 'Encoding']: - self.string_prefs[template[1]] = template[2] + self.setString(template[1], template[2]) # callback used when a preference is toggled def _toggled_cb(self, widget, widgets, name): @@ -273,12 +273,12 @@ class Preferences: accept = (dialog.run() == Gtk.ResponseType.OK) if accept: for k in self.bool_prefs: - self.bool_prefs[k] = widgets[k].get_active() + self.setBool(k, widgets[k].get_active()) for k in self.int_prefs: - self.int_prefs[k] = widgets[k].get_value_as_int() + self.setInt(k, widgets[k].get_value_as_int()) for k in self.string_prefs: text = self._getWidgetText(widgets[k]) - self.string_prefs[k] = utils.null_to_empty(text) + self.setString(k, utils.null_to_empty(text)) try: ss = [] for k, bool_value in self.bool_prefs.items(): @@ -330,7 +330,7 @@ class Preferences: w.show() elif tpl_section == 'Boolean': button = Gtk.CheckButton.new_with_mnemonic(tpl[3]) - button.set_active(self.bool_prefs[tpl[1]]) + button.set_active(self.getBool(tpl[1])) widgets[tpl[1]] = button table.attach(button, 1, i, 1, 1) button.connect('toggled', self._toggled_cb, widgets, tpl[1]) @@ -341,14 +341,14 @@ class Preferences: label.set_yalign(0.5) table.attach(label, 0, i, 1, 1) label.show() - if tpl[0] in ['Font', 'Integer']: + if tpl_section in ['Font', 'Integer']: entry = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0) - if tpl[0] == 'Font': + if tpl_section == 'Font': button = Gtk.FontButton() - button.set_font(self.string_prefs[tpl[1]]) + button.set_font(self.getString(tpl[1])) else: adj = Gtk.Adjustment( - value=self.int_prefs[tpl[1]], + value=self.getInt(tpl[1]), lower=tpl[4], upper=tpl[5], step_increment=1, @@ -362,15 +362,15 @@ class Preferences: entry.pack_start(button, False, False, 0) button.show() else: - if tpl[0] == 'Encoding': + if tpl_section == 'Encoding': entry = utils.EncodingMenu(self) entry.set_text(tpl[3]) - elif tpl[0] == 'File': + elif tpl_section == 'File': entry = _FileEntry(parent, tpl[3]) else: entry = Gtk.Entry() widgets[tpl[1]] = entry - entry.set_text(self.string_prefs[tpl[1]]) + entry.set_text(self.getString(tpl[1])) table.attach(entry, 1, i, 1, 1) entry.show() table.show() @@ -400,6 +400,9 @@ class Preferences: def getInt(self, name: str) -> int: return self.int_prefs[name] + def setInt(self, name: str, value: int) -> None: + self.int_prefs[name] = value + def getString(self, name: str) -> str: return self.string_prefs[name] @@ -410,10 +413,10 @@ class Preferences: return self.encodings def _getDefaultEncodings(self) -> List[str]: - return self.string_prefs['encoding_auto_detect_codecs'].split() + return self.getString('encoding_auto_detect_codecs').split() def getDefaultEncoding(self) -> str: - return self.string_prefs['encoding_default_codec'] + return self.getString('encoding_default_codec') # attempt to convert a string to unicode from an unknown encoding def convertToUnicode(self, s): From 0f5122a0c1aa0087a433572228b9bfc0571b5483 Mon Sep 17 00:00:00 2001 From: Romain Failliot Date: Fri, 15 Apr 2022 12:57:46 -0400 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3513e2c..5208e94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Translation: added French translation ### Fixed +- Fixed #156: Preferences cannot be saved in latest release - Cleanups: use constructors instead of `new()` whenever possible in GTK classes