Recording user's preferences - what is best approach

I’ve sub-classed a listbox which when supplied correct parameters will be the one and only listbox that I use for my application. One of the setup procedures involves passing through a column width value. In most instances I will allow the user to resize the column width and I would prefer that the resized column width be saved in a preferences file somewhere.

I haven’t dealt with setting and retrieving these types of settings before. Is there a recommended way to accomplish this? Is it merely a text file, I’m familiar with the old INI files from years ago or should I employ SQLite database? Is there another approach? I know I’ve referenced a listbox as an example but other settings I’m assuming could be window positions and sizes etc. and so I reckon there could be quite a lot of such settings for an entire application.

There’s a bunch of ways you can do this. For large volumes of data, an SQLdatabase might be more appropriate, for things like window size and location, I wouldn’t imagine that it would be a benefit.

I’ve seen many people simply write out their variables in text files (one of my apps uses this method), if you developing x-plat then you may want to consider using your own format. Binary files can be a little more difficult to start with.

If you’re doing Mac only, then look at using NSDictionary and NSArray, these make it simple to store most data types in a structured manor and format independent (it could be a variety of formats, but you don’t need to worry as the OS deals with it for you). You then use them just like a Dictionary or Array object.

For simple preferences, I like: http://code.google.com/p/realeasyprefs/ … It works on all platforms.

I wrote an “INI like” class a few years back and use it in ALL of my applications.

http://www.rdS.com/classini_file.rbo

Hi Dave,

I noticed a few of your URLs are wrong, e.g. http://www.rdS.com/classini_file.rbo

You need to use a BACKslash, not a FORWARDslash

Best Regards

Markus

I recommend plist files, and use MacCrafters’ “pList Class for Realbasic” which sadly doesn’t seem to be supported anymore, but it’s easy to support one’s self as it’s all unencrypted and open. I use plists not only for prefs but for application data files; the human-readable XML text is a plus, as is the ability to use third-party readers for debugging. Being based on dictionaries, plists make for very readable code as well.

Here’s a useful thread about prefs on the old forum:

http://forums.realsoftware.com/viewtopic.php?f=7&t=24189

Julia

[quote=15765:@Markus Winter]Hi Dave,

I noticed a few of your URLs are wrong, e.g. http://www.rdS.com/classini_file.rbo

You need to use a BACKslash, not a FORWARDslash

Best Regards

Markus[/quote]

Confused… I used the same “slash” everyone else is using

/ is a backslash
\ is a forward slash… which I am NOT using

[quote=15770:@Dave S]Confused… I used the same “slash” everyone else is using

/ is a backslash
\ is a forward slash… which I am NOT using[/quote]

That’s backwards:

\ is a backslash
/ is a forwardslash

URLS should only use forward slashes, generally.

[Off topic but interesting] http://news.bbc.co.uk/2/hi/technology/8306631.stm “Berners-Lee ‘sorry’ for slashes”

WOw… I know my eyesight isn’t what it used to be…

but please show me ANY post on this forum (heck in this TOPIC) that uses a \

I have been typing and using URLS for many many years … and / is what has been used.

Look at your own last posting above… and tell me it has \ in it

[quote]
Sir Tim Berners-Lee, the creator of the World Wide Web, has confessed that the // in a web address were actually “unnecessary”.[/quote]

he didn’t say “\”

Just to clarify: Dave, I think your URLs are correct, but your terminology (back vs. forward-slash) was reversed.

I think Markus’ comment that you are using backslashes incorrectly seems wrong.

Maybe what Markus was referring to is the fact that Dave’s URL’s are not appearing as live links in the forum; I’m not sure why that is happening (but I don’t think it has anything to do with the slashes, lol).

I think it is becuase this forum seems to use a different tag method for URLS than the Real Studio one did

I am using {url}http://www.xxx.com{/url}

and this forum uses {url=http://example.com} description goes here {/url]}

Note I used {} instead of square brackets to keep the forum from “interpeting” them

and why can’t I edit a post?

http://www.rdS.com/classini_file.rbo

I usually save app preferences to a plain text file, in JSON format. The JSONItem class works a lot like the Dictionary class, with the added ability to be converted to and from strings.

e.g.

   Dim prefs As New JSONItem
   prefs.Value("Col1Width") = 100
   prefs.Value("Col1Head") = "Column One"
   prefs.Value("Col1BGColor") = &cC0C0C0
   Dim s As String = prefs.ToString() 'this string can be written to a file
   
   prefs = New JSONItem(s) 'constructing a JSONItem from a string

JSON is cross-platform, built-in to Xojo, and has a lot of 3rd party editors.

Awesome - forgot all about JSON…

Thanks everyone, have got a few ideas to play with.

Thinking about it, I would actually suggest that you use each platforms preferred preference mechanism. This would reduce the likelihood of a new version breaking your application.

For instance Apple advise against reading & writing files directly the Preferences Folder, instead they recommend usage of NSUserDefaults to store preference values. Document Data should be stored however suits you, but it should be in a location of the users choosing.

I’m not sure what Windows uses at the moment, back in early naughties, there seemed to be a push to move to XML files in a special location, but most people were still using the Windows Registry for storing preference values.

One down side to this is if your user wishes to move configuration files from one platform to another.