PDA

View Full Version : Best multiplatform policy to store per-user profile files?



Gnurou
2nd July 2008, 06:48
Hi everybody,

As this is my first message, I shall precise that I am a Qt newbie, although highly enthousiast about it!

I am currently coding my first real Qt application, which needs every user to have a personal Sqlite database. The situation is somehow similar to Firefox's cache file, which are properly stored into the user directory, and the problem is the same: figuring out where to store the file depending on the platform the application is running on.

I already use QSettings for my application settings, but it only allows to store small key/value pairs. Plus, on Windows the default policy is to use the registry, while I need a file system.

I have been thinking about the following workaround:

1) Create a QSettings(QSettings::IniFormat)
2) Call QSettings::fileName() to get the name of the configuration file
3) Strip the file name to keep the directory it is stored in
4) Add my own name

So ok, the generated file name would be what I would like it to be, but isn't there a Qt class that is better suited to handle this kind of cases?

Thanks,
Alex.

mcosta
2nd July 2008, 08:22
You can use QSettings constructor with your fileName



QString mySettings = QApplication::applicationDirPath() + "/mySettings.conf";

QSettings settings(mySettings, QSettings::IniFormat);

.....

Gnurou
3rd July 2008, 01:28
Wouldn't it return the same path for all users using the same installation? (e.g. /usr/bin/ on Linux)

I expected to generate something more like /home/user/.myprogram/ in order to have something unique. Eventually I came with this solution to create a per-user profile directory and register it to the settings:


void checkUserProfileDirectory()
{
QSettings settings;
QString profileDirName;
if (!settings.contains("userProfile")) {
profileDirName = QDir(QSettings(QSettings::IniFormat, QSettings::UserScope,
QCoreApplication::organizationName(), QCoreApplication::applicationName()).fileName()).p ath();
profileDirName.resize(profileDirName.lastIndexOf(' .'));
settings.setValue("userProfile", profileDirName);
}
else profileDirName = settings.value("userProfile").toString();

QDir profileDir(profileDirName);
if (!profileDir.exists()) profileDir.mkpath(".");
}

I'd gladly take any better solution though.