Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store preferences (and user settings) in a cross-platform application?

I'm working on a GUI desktop application that should run natively on Windows, Mac OS X and Linux. What is the preferred way to store preferences in a cross-platform application? I'm using C++, but the question (and its answers) should be valid for any natively compiled language. (Solutions for dynamic languages and Java can be seen here.)

My research so far tells me, that there are at least two strategies:
(A) Use the OS-specific API preferences functions.
(B) Store the preferences in a file within an appropriate (OS-specific) folder.

Let's consider method (A): I assume NSUserDefaults is the correct method for Mac OS X. On Windows systems, I'd write to the registry via RegOpenKeyEx. But there arise some questions: Is there any comparable and portable Linux API for that? Is writing to the Windows registry really a future-proof solution?

To keep things simple, I'm inclined to follow method (B). Thus I just have OS-specific code to get the appropriate directory where I can store my data in a format of my choice. On Windows, I've learned SHGetFolderPath (or SHGetKnownFolderPath for recent Windows systems) and CSIDL_LOCAL_APPDATA is the way to go. On Macs, the NSSearchPathForDirectoriesInDomains API call should do the same; it's an Objective-C API though making things more complicated. Finally, for the Linux version using getenv("HOME") (and getpwuid() as a fallback solution) seems to be recommend.

To summarize my questions:
1. Are there any patterns considered as best-practice for this task?
2. Is there any C++ class abstracting all the dirty things like finding the correct folder away out there? (I came across QSetting, but I'm using FLTK and I don't want to change my GUI toolkit.)

Edit:
By "preferences" I mean data that may be changed by the application and the user, e.g. a list of recent files, the preferred window size and so on.

like image 836
z80crew Avatar asked Sep 27 '13 13:09

z80crew


People also ask

How do I store user preferences?

Usually to store user preferences, it is best to use localStorage. It does not have expiration so will be valid unless and until you clear it programatically or user clears it in browser manually .

What is the most appropriate way to store user settings in Android application?

User settings are generally saved locally in Android using SharedPreferences with a key-value pair. You use the String key to save or look up the associated value.


1 Answers

I'm also developing a set of cross-platform plugins and did exactly what you describe in method (B):

  1. Find the proper folder using platform specific code.
  2. Write the preferences to that folder with cross-platform code.

I would also note that:

  1. Using the Windows registry is very limited and problematic. I try to avoid that as much as possible.
  2. Lately apple has a new song: that you should not access the ~/Library/Preferences folder directly but rather use their API for saving preferences value. In effect this turns the preferences into registry like mechanism. I very much resent this approach.

I do not know of any library that implements finding the currect folder in cross-platform manner. Such function is not very hard to write using SHGetKnownFolderPath and NSSearchPathForDirectoriesInDomains, as you described.

like image 79
Periodic Maintenance Avatar answered Nov 14 '22 22:11

Periodic Maintenance