add method to locate additional config files
This commit is contained in:
parent
721e79d027
commit
ba77d0315b
|
@ -4,6 +4,10 @@
|
|||
#include <QLocale>
|
||||
#include <QTranslator>
|
||||
#include <QLibraryInfo>
|
||||
#include <QFile>
|
||||
#include <QDir>
|
||||
#include <QStringBuilder>
|
||||
#include <QSettings>
|
||||
#if defined(GUI_QTWIDGETS)
|
||||
# include <QApplication>
|
||||
# include <QIcon>
|
||||
|
@ -57,7 +61,7 @@ void loadQtTranslationFile()
|
|||
if(locale.language() != QLocale::English) {
|
||||
QTranslator *qtTranslator = new QTranslator;
|
||||
if(qtTranslator->load(QStringLiteral("qt_%1").arg(locale.name()),
|
||||
QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
|
||||
QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
|
||||
QCoreApplication::installTranslator(qtTranslator);
|
||||
} else if(qtTranslator->load(QStringLiteral("qt_%1").arg(locale.name()), QStringLiteral("../share/qt/translations"))) {
|
||||
// used in Windows
|
||||
|
@ -153,3 +157,53 @@ bool hasCoreApp()
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
namespace ConfigFile {
|
||||
|
||||
QString locateConfigFile(const QString &applicationName, const QString &fileName, const QSettings *settings)
|
||||
{
|
||||
// check whether the file is in the current working directory
|
||||
if(QFile::exists(fileName)) {
|
||||
return fileName;
|
||||
} else {
|
||||
// check whether the file is in the settings directory used by QSettings
|
||||
QString path;
|
||||
if(settings) {
|
||||
path = QFileInfo(settings->fileName()).absoluteDir().absoluteFilePath(fileName);
|
||||
if(QFile::exists(path)) {
|
||||
return path;
|
||||
}
|
||||
}
|
||||
// check whether there is a user created version of the file under /etc/app/
|
||||
#if Q_OS_WIN32
|
||||
// use relative paths on Windows
|
||||
path = QStringLiteral("../etc/") % applicationName % QChar('/') % fileName;
|
||||
if(QFile::exists(path)) {
|
||||
return path;
|
||||
} else {
|
||||
// check whether there is the default version of the file under /usr/share/app/
|
||||
path = QStringLiteral("../share/") % applicationName % QChar('/') % fileName;
|
||||
if(QFile::exists(path)) {
|
||||
return path;
|
||||
} else {
|
||||
return QString(); // file is not present
|
||||
}
|
||||
}
|
||||
#else
|
||||
path = QStringLiteral("/etc/") % applicationName % QChar('/') % fileName;
|
||||
if(QFile::exists(path)) {
|
||||
return path;
|
||||
} else {
|
||||
// check whether there is the default version of the file under /usr/share/app/
|
||||
path = QStringLiteral("/usr/share/") % applicationName % QChar('/') % fileName;
|
||||
if(QFile::exists(path)) {
|
||||
return path;
|
||||
} else {
|
||||
return QString(); // file is not present
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -5,10 +5,9 @@
|
|||
|
||||
#include <QtGlobal>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QString;
|
||||
class QStringList;
|
||||
QT_END_NAMESPACE
|
||||
QT_FORWARD_DECLARE_CLASS(QString)
|
||||
QT_FORWARD_DECLARE_CLASS(QStringList)
|
||||
QT_FORWARD_DECLARE_CLASS(QSettings)
|
||||
|
||||
#define SET_QT_APPLICATION_INFO \
|
||||
QCoreApplication::setOrganizationName(QStringLiteral(APP_AUTHOR)); \
|
||||
|
@ -47,4 +46,10 @@ LIB_EXPORT bool hasCoreApp();
|
|||
|
||||
}
|
||||
|
||||
namespace ConfigFile {
|
||||
|
||||
LIB_EXPORT QString locateConfigFile(const QString &applicationName, const QString &fileName, const QSettings *settings = nullptr);
|
||||
|
||||
}
|
||||
|
||||
#endif // RESOURCES_H
|
||||
|
|
Loading…
Reference in New Issue