//
#include <QDesktopServices>
+#include <QDebug>
#include <QDir>
#include <QDomDocument>
#include <QDomElement>
#include <stdlib.h>
-static inline void initDataDir()
-{
#ifdef Q_OS_WIN32
- dataDir=getenv("APPDATA");
+#define BASEDIRVAR "APPDATA"
#else
- dataDir=getenv("HOME");
+#define BASEDIRVAR "HOME"
#endif
- if(dataDir=="")
- qFatal("Cannot determine application data directory.");
- dataDir+="/.magicSmoke2";
+
+static inline QString resolveDir(const QString &dirspec)
+{
+ //resolve pattern
+ const QStringList dirspecl=dirspec.split('/',QString::KeepEmptyParts);
+ bool notfirst=false;
+ QString rdir;
+ for(QString dcom:dirspecl){
+ if(dcom=="$BASE"){
+ dcom=getenv(BASEDIRVAR);
+ if(dcom.isEmpty())
+ qFatal("Cannot determine base application data directory. While resolving %s",dirspec.toLatin1().data());
+ }else if(dcom=="$APP")
+ dcom=qApp->applicationDirPath();
+ else if(dcom.startsWith("$"))
+ dcom=getenv(dcom.mid(1).toLatin1().data());
+ //append component
+ if(notfirst)rdir+="/";
+ else notfirst=true;
+ rdir+=dcom;
+ }
+ //make sure it exists
+ if(!QDir::current().mkpath(rdir))
+ qFatal("Unable to create path '%s' for spec '%s'.",rdir.toLatin1().data(),dirspec.toLatin1().data());
+ //return it
+ return rdir;
}
#define HOMEPAGE_BASEURL "http://smoke.silmor.de"
//install event filter for random generator
installEventFilter(ef=new EFilter);
+ //check parameters
+ qDebug()<<"arguments"<<arguments();
+ for(const QString& param:arguments()){
+ if(param.startsWith("-confdir=")){
+ setConfigDir(param.mid(9));
+ }else if(param.startsWith("-datadir=")){
+ setDataDir(param.mid(9));
+ }
+ }
+
//data directory
- initDataDir();
- //initialize log file
- initDebug();
- //init help menu layout
- initHelpUrl();
+ if(::dataDir.isEmpty())
+ setDataDir("$BASE/.magicSmoke2");
}
+void MApplication::setConfigDir(QString d)
+{
+ QSettings::setDefaultFormat(QSettings::IniFormat);
+ QSettings::setPath(QSettings::IniFormat,QSettings::UserScope,resolveDir(d));
+ qDebug()<<"Setting the config directory to"<<::dataDir<<"from"<<d;
+}
+
+void MApplication::setDataDir(QString d)
+{
+ ::dataDir=resolveDir(d);
+ qDebug()<<"Setting the data directory to"<<::dataDir<<"from"<<d;
+}
+
+
void MApplication::setMyStyle()
{
QSettings set;
void MApplication::initialize()
{
+ //load the correct style
setMyStyle();
+ //initialize log file
+ initDebug();
+ //init help menu layout
+ initHelpUrl();
+ //init localization and profiles
initLanguage();
initProfile();
}
static int main(int,char**);
/**contains the directory that is used for external data storage*/
static QString dataDir();
+
+ ///override the data directory
+ ///- call it before any of the init functions
+ ///- never call this while a session is active!
+ static void setDataDir(QString);
+ ///override the configuration directory
+ ///- call this before initialize
+ ///- on Windows and MacOS, this also switches the settings mode to ini-files
+ static void setConfigDir(QString);
public slots:
///complete initialization: style, language, data dir
void initialize();
QTranslator*qttrans,*mstrans;
};
+///return the instance of the MApplication
#define mApp (qobject_cast<MApplication*>(qApp))
#endif