[moserial] Do not access keyFile directly. Add internal keyFile change detection.



commit d5eb3a6db3c781f32ce330836580b3840d436f00
Author: Michael J. Chudobiak <mjc avtechpulse com>
Date:   Fri Jan 22 09:03:52 2021 -0500

    Do not access keyFile directly. Add internal keyFile change detection.

 src/DefaultPaths.vala |  6 +++---
 src/MainWindow.vala   |  2 --
 src/Preferences.vala  | 24 ++++++++++++------------
 src/Profile.vala      | 47 +++++++++++++++++++++++++++++++++++------------
 src/Settings.vala     | 16 ++++++++--------
 5 files changed, 58 insertions(+), 37 deletions(-)
---
diff --git a/src/DefaultPaths.vala b/src/DefaultPaths.vala
index 4fb3ce6..c6eb81c 100644
--- a/src/DefaultPaths.vala
+++ b/src/DefaultPaths.vala
@@ -34,11 +34,11 @@ public class DefaultPaths : GLib.Object
     public void saveToProfile (Profile profile)
     {
         if (recordTo != null)
-            profile.keyFile.set_string ("paths", "last_record_path", recordTo);
+            profile.setString ("paths", "last_record_path", recordTo);
         if (receiveTo != null)
-            profile.keyFile.set_string ("paths", "last_receive_path", receiveTo);
+            profile.setString ("paths", "last_receive_path", receiveTo);
         if (sendFrom != null)
-            profile.keyFile.set_string ("paths", "last_send_path", sendFrom);
+            profile.setString ("paths", "last_send_path", sendFrom);
     }
 
     public static DefaultPaths loadFromProfile (Profile profile)
diff --git a/src/MainWindow.vala b/src/MainWindow.vala
index bc1cf69..30c76be 100644
--- a/src/MainWindow.vala
+++ b/src/MainWindow.vala
@@ -729,7 +729,6 @@ public class moserial.MainWindow : Gtk.Window // Have to extend Gtk.Winow to get
         statusbar.pop (statusbarContext);
         statusbar.push (statusbarContext, currentSettings.getStatusbarString (false));
         updateOutgoingInputArea ();
-        profile.profileChanged = true;
     }
 
     private void updateOutgoingInputArea ()
@@ -814,7 +813,6 @@ font-weight:
         echoTag.foreground = currentPreferences.highlightColor;
         incomingHexTextBuffer.applyPreferences (currentPreferences);
         outgoingHexTextBuffer.applyPreferences (currentPreferences);
-        profile.profileChanged = true;
     }
 
     private void showSettingsDialog (GLib.Object o)
diff --git a/src/Preferences.vala b/src/Preferences.vala
index e974c88..34b0855 100644
--- a/src/Preferences.vala
+++ b/src/Preferences.vala
@@ -125,18 +125,18 @@ public class Preferences : GLib.Object
 
     public void saveToProfile (Profile profile)
     {
-        profile.keyFile.set_boolean ("preferences", "use_system_monospace_font", useSystemMonospaceFont);
-        profile.keyFile.set_string ("preferences", "font", font);
-        profile.keyFile.set_string ("preferences", "font_color", fontColor);
-        profile.keyFile.set_string ("preferences", "background_color", backgroundColor);
-        profile.keyFile.set_string ("preferences", "highlight_color", highlightColor);
-        profile.keyFile.set_boolean ("preferences", "record_launch", recordLaunch);
-        profile.keyFile.set_boolean ("preferences", "enable_timeout", enableTimeout);
-        profile.keyFile.set_integer ("preferences", "timeout", timeout);
-        profile.keyFile.set_boolean("preferences", "record_auto_name", recordAutoName);
-        profile.keyFile.set_integer("preferences", "record_auto_direction", recordAutoDirection);
-        profile.keyFile.set_string("preferences", "record_auto_extension", recordAutoExtension);
-        profile.keyFile.set_string("preferences", "record_auto_folder", recordAutoFolder);
+        profile.setBoolean ("preferences", "use_system_monospace_font", useSystemMonospaceFont);
+        profile.setString ("preferences", "font", font);
+        profile.setString ("preferences", "font_color", fontColor);
+        profile.setString ("preferences", "background_color", backgroundColor);
+        profile.setString ("preferences", "highlight_color", highlightColor);
+        profile.setBoolean ("preferences", "record_launch", recordLaunch);
+        profile.setBoolean ("preferences", "enable_timeout", enableTimeout);
+        profile.setInteger ("preferences", "timeout", timeout);
+        profile.setBoolean("preferences", "record_auto_name", recordAutoName);
+        profile.setInteger("preferences", "record_auto_direction", recordAutoDirection);
+        profile.setString("preferences", "record_auto_extension", recordAutoExtension);
+        profile.setString("preferences", "record_auto_folder", recordAutoFolder);
     }
 
     public static Preferences loadFromProfile (Profile profile)
diff --git a/src/Profile.vala b/src/Profile.vala
index 59be5e3..f9d7805 100644
--- a/src/Profile.vala
+++ b/src/Profile.vala
@@ -23,7 +23,7 @@ public class Profile : GLib.Object
 {
     public bool profileChanged = false;
 
-    public KeyFile keyFile;
+    private KeyFile keyFile;
     construct {
         keyFile = new KeyFile ();
     }
@@ -39,6 +39,15 @@ public class Profile : GLib.Object
         return result;
     }
 
+    public void setString (string group, string key, string new_val)
+    {
+        bool changed = (getString (group, key) != new_val);
+        keyFile.set_string (group, key, new_val);
+        if (changed) {
+            profileChanged = true;
+        }
+    }
+
     public int getInteger (string group, string key, int default_val)
     {
         int result = default_val;
@@ -50,6 +59,15 @@ public class Profile : GLib.Object
         return result;
     }
 
+    public void setInteger (string group, string key, int new_val)
+    {
+        bool changed = (getInteger (group, key, 0) != new_val);
+        keyFile.set_integer (group, key, new_val);
+        if (changed) {
+            profileChanged = true;
+        }
+    }
+
     public bool getBoolean (string group, string key, bool default_val)
     {
         bool result = default_val;
@@ -61,17 +79,26 @@ public class Profile : GLib.Object
         return result;
     }
 
+    public void setBoolean (string group, string key, bool new_val)
+    {
+        bool changed = (getBoolean (group, key, false) != new_val);
+        keyFile.set_boolean (group, key, new_val);
+        if (changed) {
+            profileChanged = true;
+        }
+    }
+
     public void saveWindowSize (int w, int h)
     {
         if (w > 0)
-            keyFile.set_integer ("window", "width", w);
+            setInteger ("window", "width", w);
         if (h > 0)
-            keyFile.set_integer ("window", "height", h);
+            setInteger ("window", "height", h);
     }
 
     public void saveWindowPanedPosition (int pos)
     {
-        keyFile.set_integer ("window", "paned_pos", pos);
+        setInteger ("window", "paned_pos", pos);
     }
 
     public void setNotebookTab (bool outgoing, uint tab)
@@ -82,24 +109,20 @@ public class Profile : GLib.Object
         }
 
         if (tab != 0) {
-            keyFile.set_integer ("main_ui_controls", n, 1);
+            setInteger ("main_ui_controls", n, 1);
         } else {
-            keyFile.set_integer ("main_ui_controls", n, 0);
+            setInteger ("main_ui_controls", n, 0);
         }
-
-        profileChanged = true;
     }
 
     public void setInputModeHex (bool hex)
     {
-        keyFile.set_boolean ("main_ui_controls", "input_mode_hex", hex);
-        profileChanged = true;
+        setBoolean ("main_ui_controls", "input_mode_hex", hex);
     }
 
     public void setInputLineEnd (int end)
     {
-        keyFile.set_integer ("main_ui_controls", "input_line_end", end);
-        profileChanged = true;
+        setInteger ("main_ui_controls", "input_line_end", end);
     }
 
     public bool load (string ? filename, Gtk.Window window)
diff --git a/src/Settings.vala b/src/Settings.vala
index 10e5275..2ff76cc 100644
--- a/src/Settings.vala
+++ b/src/Settings.vala
@@ -126,14 +126,14 @@ public class Settings : GLib.Object
 
     public void saveToProfile (Profile profile)
     {
-        profile.keyFile.set_string ("port_settings", "device", device);
-        profile.keyFile.set_integer ("port_settings", "baud_rate", baudRate);
-        profile.keyFile.set_integer ("port_settings", "data_bits", dataBits);
-        profile.keyFile.set_integer ("port_settings", "stop_bits", stopBits);
-        profile.keyFile.set_integer ("port_settings", "parity", parity);
-        profile.keyFile.set_integer ("port_settings", "handshake", handshake);
-        profile.keyFile.set_integer ("port_settings", "access_mode", accessMode);
-        profile.keyFile.set_boolean ("port_settings", "local_echo", localEcho);
+        profile.setString ("port_settings", "device", device);
+        profile.setInteger ("port_settings", "baud_rate", baudRate);
+        profile.setInteger ("port_settings", "data_bits", dataBits);
+        profile.setInteger ("port_settings", "stop_bits", stopBits);
+        profile.setInteger ("port_settings", "parity", parity);
+        profile.setInteger ("port_settings", "handshake", handshake);
+        profile.setInteger ("port_settings", "access_mode", accessMode);
+        profile.setBoolean ("port_settings", "local_echo", localEcho);
     }
 
     public static Settings loadFromProfile (Profile profile)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]