[chronojump/michrolab] DB change updates config file for next boot, ensuring its default config file



commit 8206b45ab18e5f1b0e46880bdbdc7e02eed02b94
Author: Xavier de Blas <xaviblas gmail com>
Date:   Tue Jun 14 18:25:20 2022 +0200

    DB change updates config file for next boot, ensuring its default config file

 src/config.cs       | 59 +++++++++++++++++++++++++++++++++--------------------
 src/gui/networks.cs | 16 +++++++--------
 2 files changed, 44 insertions(+), 31 deletions(-)
---
diff --git a/src/config.cs b/src/config.cs
index 705447f4d..34c6daddd 100644
--- a/src/config.cs
+++ b/src/config.cs
@@ -183,50 +183,65 @@ public class Config
                return (Compujump && pID == CompujumpAdminID);
        }
 
-       /*
-       public static void UpdateField(string field, string text)
+       //adapted from: http://stackoverflow.com/a/2401873
+       //useDefaultConfigFile is the default. It ensures no use the config of another database
+       public void UpdateFieldEnsuringDefaultConfigFile (string field, string text)
        {
-               //adapted from
-               //http://stackoverflow.com/a/2401873
-                               
-               string tempfile = Path.GetTempFileName();
+               string storedLastDBFullPathStatic = Config.LastDBFullPathStatic;
+               Config.LastDBFullPathStatic = "";
+
+               UpdateField (field, text);
 
-               LogB.Information("UpdateField, field: " + field + ", text: " + text);
+               Config.LastDBFullPathStatic = storedLastDBFullPathStatic;
+       }
+       public void UpdateField (string field, string text)
+       {
+               string tempfile = Path.GetTempFileName ();
+               string configFile = Util.GetConfigFileName ();
+               LogB.Information( string.Format ("Config.UpdateField tempfile: {0}, configFile: {1}, field: 
{2}, text: {3}",
+                                       tempfile, configFile, field, text));
                
-               if(! File.Exists(Util.GetConfigFileName())) {
+               if(! File.Exists (configFile)) {
                        try {
-                               using (var writer = new StreamWriter(tempfile))
+                               using (var writer = new StreamWriter (tempfile))
                                {
-                                       writer.WriteLine(field + "=" + text);
+                                       writer.WriteLine (field + "=" + text);
                                }
-                               File.Copy(tempfile, Util.GetConfigFileName(), true);
+                               File.Copy (tempfile, configFile, true);
                        } catch {
-                               LogB.Warning("Cannot write at Config.UpdateField");
+                               LogB.Warning ("Cannot write at Config.UpdateField");
                        }
                } else {
                        try {
                                using (var writer = new StreamWriter(tempfile))
-                                       using (var reader = new StreamReader(Util.GetConfigFileName()))
+                                       using (var reader = new StreamReader (configFile))
                                        {
-                                               while (! reader.EndOfStream) {
-                                                       string line = reader.ReadLine();
+                                               bool found = false;
+                                               while (! reader.EndOfStream)
+                                               {
+                                                       string line = reader.ReadLine ();
                                                        if (line != "" && line[0] != '#') 
                                                        {
-                                                               string [] parts = line.Split(new char[] 
{'='});
-                                                               if(parts.Length == 2 && parts[0] == field)
+                                                               string [] parts = line.Split (new char[] 
{'='});
+                                                               if (parts.Length == 2 && parts[0] == field)
+                                                               {
                                                                        line = field + "=" + text;
+                                                                       found = true;
+                                                               }
                                                        }
-
-                                                       writer.WriteLine(line);
+                                                       writer.WriteLine (line);
                                                }
+
+                                               //if not found it adds the command
+                                               if (! found)
+                                                       writer.WriteLine (field + "=" + text);
                                        }
-                               File.Copy(tempfile, Util.GetConfigFileName(), true);
+                               File.Copy (tempfile, configFile, true);
                        } catch {
-                               LogB.Warning("Cannot write at Config.UpdateField");
+                               LogB.Warning ("Cannot write at Config.UpdateField");
                        }
                }
        }
-       */
 
        public override string ToString() 
        {
diff --git a/src/gui/networks.cs b/src/gui/networks.cs
index d5e9576f7..cf34132c9 100644
--- a/src/gui/networks.cs
+++ b/src/gui/networks.cs
@@ -301,7 +301,7 @@ public partial class ChronoJumpWindow
                        button_menu_database.Visible = true;
 
                        if (configChronojump.LastDBFullPath != "")
-                               databaseChange (false);
+                               databaseChange ();
                }
 
                configDo();
@@ -372,7 +372,7 @@ public partial class ChronoJumpWindow
        }
 
        // updateConfigFile only if selected a new db by user: on_button_database_change_clicked ()
-       private void databaseChange (bool updateConfigFile)
+       private void databaseChange ()
        {
                closeSession ();
 
@@ -392,11 +392,6 @@ public partial class ChronoJumpWindow
 
                label_current_database.UseMarkup = true;
                label_current_database.TooltipText = configChronojump.LastDBFullPath;
-
-               /*
-               //TODO
-               if (updateConfigFile)
-               */
        }
 
        Gtk.FileChooserDialog database_fc;
@@ -419,9 +414,12 @@ public partial class ChronoJumpWindow
                        // 2) reassing configChronojump.LastDBFullPath
                        configChronojump.LastDBFullPath = database_fc.Filename;
 
-                       // 2) open database
+                       // 3) update config file (taking care of being default config file)
+                       configChronojump.UpdateFieldEnsuringDefaultConfigFile ("LastDBFullPath", 
database_fc.Filename);
+
+                       // 4) change database
                        //TODO: think where to put a try/catch, eg if there is no database file, or search 
database/chronojump.db before
-                       databaseChange (true);
+                       databaseChange ();
                }
 
                database_fc.Hide ();


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