Recent

Author Topic: [SOLVED] Save user settings  (Read 8629 times)

Hansvb

  • Hero Member
  • *****
  • Posts: 602
[SOLVED] Save user settings
« on: July 17, 2021, 11:24:18 am »
Hi,

I haven't been using Lazarus for a long time. When I want to save user settings I used to do that in an ini file. Is this still the way to go or are there better alternatives?

Greetings
« Last Edit: July 17, 2021, 10:34:18 pm by Hansvb »

Fantablup

  • Full Member
  • ***
  • Posts: 169
Re: Save user settings
« Reply #1 on: July 17, 2021, 11:31:30 am »
I also used to do that before.
Also used to save in registry.

But, now i always save these things as JSON. It is much more structured and easier.
You have to learn the JSON functions though.

Hansvb

  • Hero Member
  • *****
  • Posts: 602
Re: Save user settings
« Reply #2 on: July 17, 2021, 11:55:35 am »
Do you know a tutorial or small sample how to do that?

Bart

  • Hero Member
  • *****
  • Posts: 5274
    • Bart en Mariska's Webstek
Re: Save user settings
« Reply #3 on: July 17, 2021, 11:57:01 am »
Having you settings in a human readable format (INI, JSON, XML or whatever) has the advantage that it is easy to fix (if something went wrong), even for novice users.
(Most Windows users never ever opened the registry, or even know that it exists, als f*cking up the registry (once I accidentally removed all subkeys from Computer\HKEY_LOCAL_MACHINE\SOFTWARE ) can be unfixable.)
So, I tend not to use the Windows registry at all for my program settings.

If you don't need things like multi line values, then Ini files are perfectly OK, and easier to handle and understand than JSON or XML.

So, it all depends on your use case.

Bart

Fantablup

  • Full Member
  • ***
  • Posts: 169
Re: Save user settings
« Reply #4 on: July 17, 2021, 12:13:34 pm »
Do you know a tutorial or small sample how to do that?

Sorry, i have no tutorials you can use.
You have to search for examples. I guess it takes a day or two to get the understanding of it. So it is some work learning it.

But, as the above said. It is also perfectly OK to use INI if you only need a simple thing. But JSON is good for categorizing users, and have all settings in one node for each user.

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: Save user settings
« Reply #5 on: July 17, 2021, 03:28:21 pm »
I haven't been using Lazarus for a long time. When I want to save user settings I used to do that in an ini file. Is this still the way to go or are there better alternatives?
It depends on what you define as "better"...

If "better" is avoiding to write code you should try to learn how to use the PropStorage components on palette "Misc": TXMLPropStorage, TIniPropStorage, TJSONPropStorage. Depending on the component that you select you write an xml, ini, or json config file. They cooperate with the SessionProperties of the form where you can define which properties of the form or of any component on the form will be stored (and, of course, retrieved when the application restarts the next time). There's a wiki tutorial on https://wiki.lazarus.freepascal.org/TXMLPropStorage for XMLPropStorage, but the others work in the same way.

I personally, however, do not considers this as an adequate way to separate code and gui. But for "quick-and-dirty" programs the PropStorage components can be a real time-saver.

Renat.Su

  • Full Member
  • ***
  • Posts: 230
    • Renat.Su
Re: Save user settings
« Reply #6 on: July 17, 2021, 04:50:10 pm »
Hi,

I haven't been using Lazarus for a long time. When I want to save user settings I used to do that in an ini file. Is this still the way to go or are there better alternatives?

Greetings

Recently, I have been using serialization and deserialization in JSON more often. Here is an example of functions that load your any object in JSON and load from it https://gist.github.com/Al-Muhandis/1a4743e6447ee16696e6ef737a031be1

Hansvb

  • Hero Member
  • *****
  • Posts: 602
Re: Save user settings
« Reply #7 on: July 17, 2021, 10:34:03 pm »
Thanks..., for now, I will use IniFiles. later I will take a look at JSON.

alpine

  • Hero Member
  • *****
  • Posts: 1038
Re: Save user settings
« Reply #8 on: July 17, 2021, 11:23:48 pm »
Thanks..., for now, I will use IniFiles. later I will take a look at JSON.

Here is my small unit for simple ini configuration (attached). You can use it in a following way:
Code: Pascal  [Select][+][-]
  1. uses
  2.   iniprops;
  3.  
  4. type
  5.   TMyConfigObject = class(TIniFileProps)
  6.     FIntProp: Integer;
  7.     FBoolProp: Boolean;
  8.   published
  9.     property IntProp: Integer read FInitProp write FInitProp;
  10.     property BoolProp: Boolean read FBoolProp write FBoolProp;
  11.     ...
  12.   end;
  13.  
  14.   // to create/load
  15.   MyConf := TMyConfigObject.Create('config.ini');
  16.   MyConf.LoadAllProps;
  17.  
  18.   // to update
  19.   MyConf.IntProp := 5;
  20.   MyConf.BoolProp := False;
  21.   MyConf.StoreAllProps;
  22.  
  23.  

It loads/stores properties of type: Char, String, Integer, Int64, QWord, Bool, Float, Enumeration, Set.
Of course, the LoadAllProps and StoreAllProps are declared virtual and you can override them to do some more complicated load/store actions.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1111
  • Professional amateur ;-P
Re: Save user settings
« Reply #9 on: July 18, 2021, 01:56:21 am »
Hey Hansvb,

Thanks..., for now, I will use IniFiles. later I will take a look at JSON.

I strongly advise you to have a good look at the T[XML|INI|JSON]PropStorage like @wp suggested.

They take away a lot of the pain of loading and storing you values.

They also contain an area that you can use separate from the App's components. This way you can safely save some custom settings like the ones you have on a Settings form/section.

Attached is an Application that I've done as an example of a simple App with most of the boiler plate I do when starting a new application.

It has a TJSONPropStorage for the prop storage.
It has automated Windows and Linux exit short-cuts(I'm sorry but this is a pet peeve of mine :) )
It also has a TPairSplit thrown into the mix and, now, I can't remember why I put it there, LOL!!

Hope this helps in any way.

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Edson

  • Hero Member
  • *****
  • Posts: 1301
Re: [SOLVED] Save user settings
« Reply #10 on: July 18, 2021, 05:33:41 am »
Just in case you need to move information in this directions:

Disk(INI or XML) <---> Variables <---> Controls

You can use my library: https://github.com/t-edson/MiConfig
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: [SOLVED] Save user settings
« Reply #11 on: July 18, 2021, 08:58:37 am »
I am a bit puzzled about the "not inifile" attitude.

 It works and is certainly tried and tested. It works across any platform you may come across, if necessary, it can be hand edited and the Lazarus Ini file interface is very easy to use.

I wonder just how complicated a User Settings config we need before the ini file that Hansvb is familiar with is inadequate ?

Davo

Yeah, you guessed it, tomboy-ng uses ini files ......
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Hansvb

  • Hero Member
  • *****
  • Posts: 602
Re: [SOLVED] Save user settings
« Reply #12 on: July 18, 2021, 09:03:07 am »
Thanks, i will look at the different options. I also looked at some old code of myself. (Using inifiles). That still works, which i find great to see.
I just program as a hobby and i’m not verry good but like to do it. The last few years I used c# and the i saved settings in json format. I find it hard to find good examples in Pascal

jcmontherock

  • Full Member
  • ***
  • Posts: 234
Re: [SOLVED] Save user settings
« Reply #13 on: July 24, 2021, 12:03:33 pm »
Another way:

Code: Pascal  [Select][+][-]
  1. procedure Form1.SaveParms(fForm: TForm);
  2. var
  3.   MemoryStream: TMemoryStream;
  4.   FormRect:     TRect;
  5. begin
  6.   MemoryStream := TMemoryStream.Create;
  7.   FormRect := Bounds(fForm.Left, fForm.Top, fForm.Width, fForm.Height);
  8.   MemoryStream.Write(FormRect, SizeOf(FormRect));
  9.   MemoryStream.WriteAnsiString(lEditFile.Text);
  10.   MemoryStream.Position := 0;
  11.   sFilename := ExtractFilePath(ParamStr(0)) + ChangeFileExt({$I %FILE%}, '.parm');
  12.   MemoryStream.SaveToFile(sFilename);
  13.   MemoryStream.Free;
  14. end;
  15.  
  16. procedure Form1.ReadParms(fForm: TForm);
  17. var
  18.   MemoryStream: TMemoryStream;
  19.   FormRect:     TRect;
  20.   sFilename:    String;
  21. begin
  22.   sFilename := ExtractFilePath(ParamStr(0)) + ChangeFileExt({$I %FILE%}, '.parm');
  23.   if FileExists(sFilename) = False then Exit;
  24.   MemoryStream := TMemoryStream.Create;
  25.   MemoryStream.LoadFromFile(sFilename);
  26.   MemoryStream.Position := 0;
  27.   MemoryStream.Read(FormRect, SizeOf(FormRect));
  28.   fForm.SetBounds(FormRect.Left, FormRect.Top, FormRect.Width, FormRect.Height);
  29.   lEditFile.Text := MemoryStream.ReadAnsiString;
  30.   MemoryStream.Free;
  31. end;
« Last Edit: July 24, 2021, 05:03:53 pm by jcmontherock »
Windows 11 UTF8-64 - Lazarus 3.2-64 - FPC 3.2.2

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: [SOLVED] Save user settings
« Reply #14 on: July 25, 2021, 03:58:19 am »
I am a bit puzzled about the "not inifile" attitude.

Me too - I use INI files everywhere (FreeBSD, Linux, Windows) except macOS where I use the native solution.

 

TinyPortal © 2005-2018