PDA

View Full Version : Shared Files



mauer
27th May 2009, 19:55
Hi all,

I currently developing a Linux application which uses some external xml files to hold report schemas, etc.

Under Linux those files are normally stored in /usr/share/<app_name> or /usr/share/local/<app_name>, as the user should not be allowed to change them. I could include them as resources in the binary directly, but prefer to have them outside.

Now my question, how do I know in the source code where do find the xml filse?

For QFile I have to specify a filename, but the file could be anywhere on the harddisc. Is there a prefered way to deal with this? I'm surely not the only one trying to use "external" files.

Many thanks.

Marco

tpf80
28th May 2009, 02:59
QFile can take a whole path + filename in its argument, and you can also use QDir along with it for more advanced features. You can also do a path like this:



//this opens a file in the app's installed folder for example.
filepath = qApp->applicationDirPath() + "/" + filename;


QFile file(filepath);

NoRulez
28th May 2009, 11:10
You could place your xml files in "/etc/<app>/" or you create your own application directories (etc, lib, ...) and place it into the "/opt/<app>" directory.

<app> could be "QApplication::applicationName()", so if you have the following folder structure:


/opt
/opt/demo
/opt/demo/bin
/opt/demo/etc
/opt/demo/log


You could than write:


// Get the application config (/opt/demo/etc/demo.xml)
QString configFilename = QString("%0%1..%1etc%1demo.xml").arg(qApp->applicationDirPath()).arg(QDir::separator());
QString logFilename = QString("%0%1..%1log%1demo.log").arg(qApp->applicationDirPath()).arg(QDir::separator());

QFile configFile(configFilename);
QFile logFile(logFilename);
.
.


Regards NoRulez

mauer
31st May 2009, 13:37
Hello,

Many thanks for your replies. What I'm wondering is how other projects deal with such problems.

My application works on Mac OS X and Linux. This means that on a Linux system, my report definitions will be in a folder like /usr/share/<appname> or /usr/local/share/<appname>, while on a Mac OS X system the files would be within the application bundle in the Contents folder.

My questions are:

1. How do I now where the files are installed on a Linux system? Should I use a config file stored in /etc to hold the exact location of the report definitions or is there a better way?

2. How do I deal with the cross-platform problem?

Many thanks for your help.

Regards,
Marco

Lykurg
31st May 2009, 19:15
I see two possibilities.


You choose any location and hard code the path in your app using Q_OS_UNIX or Q_OS_MAC etc. to set different location for the different platforms. Then you must ensure, that the admin who installs your app place the file in the right location.

You use internal an ordered list with all possible paths and loop through that list till you find a valid file each time your app starts. To optimize you could store the founded path in a user config file.


Lykurg

EDIT:
How do I now where the files are installed on a Linux system
You are the application developer you decide! You could use qmake:install to define the path for example or use the native *dev or *rpm or ebuild....

tpf80
31st May 2009, 22:20
What I would probably do is use QSettings to store the location where the reports will be.

Then, I would have code which runs when the program starts up to load the settings using QSettings.

If no settings are found (meaning first time this program has been used), then make the default settings in QSettings using Q_OS_UNIX, Q_OS_MAC, Q_OS_WIN for the platform that the program is installed on.

You could then give the user the option of changing where they want the reports to come from in a dialog, which would alter the settings. Or if you have an installer for each OS and give the user the option to choose the install path within it, the installer could make the initial settings which the program would read on startup.

faldzip
31st May 2009, 23:28
you can get some common locations (DOcuments, Desktop, Temp ...) by using QDesktopServices::storageLocation(), so maybe there is also some interesting location for you.