From 5cc4e81dd5cb5a455cee641834a8c65076540a7f Mon Sep 17 00:00:00 2001 From: June Tate-Gans Date: Tue, 4 May 2021 02:20:34 -0500 Subject: [PATCH] saving: Re-add preferences saving to disk At this point, functionality is nearly the same as what we had before I started the migration away from g13d. We can now save prefs to disk! The next trick is to make sure that the backlighting works, and I think we can call that the next version, and maybe tag it for a alpha release. --- g13gui/g13gui/model/prefsstore.py | 12 +++++--- g13gui/g13gui/ui/mainwindow.py | 51 +++++++++---------------------- 2 files changed, 23 insertions(+), 40 deletions(-) diff --git a/g13gui/g13gui/model/prefsstore.py b/g13gui/g13gui/model/prefsstore.py index 6d8fb34..54e916a 100644 --- a/g13gui/g13gui/model/prefsstore.py +++ b/g13gui/g13gui/model/prefsstore.py @@ -1,10 +1,13 @@ import json +import threading from g13gui.model.prefs import Preferences from g13gui.common import PROFILES_CONFIG_PATH class PreferencesStore(object): + SaveLock = threading.Lock() + def getPrefs(): try: with open(PROFILES_CONFIG_PATH, 'r') as f: @@ -17,8 +20,9 @@ class PreferencesStore(object): return Preferences() def storePrefs(prefs): - prefsDict = prefs.saveToDict() + with PreferencesStore.SaveLock: + prefsDict = prefs.saveToDict() - with open(PROFILES_CONFIG_PATH, 'w') as f: - f.write(json.dumps(prefsDict, default=str)) - f.flush() + with open(PROFILES_CONFIG_PATH, 'w') as f: + f.write(json.dumps(prefsDict, default=str)) + f.flush() diff --git a/g13gui/g13gui/ui/mainwindow.py b/g13gui/g13gui/ui/mainwindow.py index ccf65ef..c18998e 100644 --- a/g13gui/g13gui/ui/mainwindow.py +++ b/g13gui/g13gui/ui/mainwindow.py @@ -1,10 +1,12 @@ #!/usr/bin/python import gi +import threading import g13gui.ui as ui from g13gui.observer.gtkobserver import GtkObserver +from g13gui.model.prefsstore import PreferencesStore gi.require_version('Gtk', '3.0') gi.require_version('Gdk', '3.0') @@ -44,9 +46,20 @@ class MainWindow(Gtk.Window, GtkObserver): self.setupG13ButtonGrid() + def _updateProfileRegistration(self): + self._lastProfileName.removeObserver(self) + self._lastProfileName = self._prefs.selectedProfile() + self._lastProfileName.registerObserver(self) + def onChangeTrigger(self, subject, changeType, key, data=None): - print('Subject changed! Need to save!') - pass + if key == 'selectedProfile': + self._updateProfileRegistration(self) + elif key == 'profile': + pass + + t = threading.Thread( + target=PreferencesStore.storePrefs, args=(self._prefs,)) + t.start() def setupHeaderBar(self): self._headerBar = Gtk.HeaderBar() @@ -72,42 +85,8 @@ class MainWindow(Gtk.Window, GtkObserver): editProfileButton.set_popover(editProfilePopover) self._headerBar.add(editProfileButton) - self._uploadButton = Gtk.Button.new_from_icon_name( - "document-send-symbolic", 1) - self._uploadButton.connect("clicked", self.uploadClicked) - self._headerBar.add(self._uploadButton) - Gtk.Window.set_titlebar(self, self._headerBar) - @GObject.Signal(name='daemon-connection-changed', arg_types=(bool,)) - def daemonConnectionChanged(self, connected): - self._connected = connected - if connected: - self._uploadButton.set_state_flags(Gtk.StateFlags.NORMAL, True) - self._infoBar.hide() - self._doUpload() - else: - self._uploadButton.set_state_flags(Gtk.StateFlags.INSENSITIVE, - True) - self._infoBar.set_message_type(Gtk.MessageType.WARNING) - self._infoBarLabel.set_text( - 'The G13 user space driver is not running. ' - 'Attempting to reconnect.') - self._infoBar.show() - - @GObject.Signal(name='uploading', arg_types=(float,)) - def uploadStatusChanged(self, percentage): - if percentage < 1.0: - self._infoBar.set_message_type(Gtk.MessageType.INFO) - self._infoBarLabel.set_text('Uploading to the G13...') - self._infoBar.show() - else: - self._infoBar.hide() - - def uploadClicked(self, widget): - self._doUpload() - self._doSave() - def setupG13ButtonGrid(self): self._mButtons = Gtk.ButtonBox( spacing=3,