make storage dirs configurable
authorKonrad Rosenbaum <konrad@silmor.de>
Sun, 10 Mar 2013 15:42:16 +0000 (16:42 +0100)
committerKonrad Rosenbaum <konrad@silmor.de>
Sun, 10 Mar 2013 15:42:16 +0000 (16:42 +0100)
src/main.cpp
src/main.h

index 5d87374..9a14b2a 100644 (file)
@@ -11,6 +11,7 @@
 //
 
 #include <QDesktopServices>
+#include <QDebug>
 #include <QDir>
 #include <QDomDocument>
 #include <QDomElement>
@@ -71,16 +72,37 @@ static QString dataDir;
 
 #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"
@@ -234,14 +256,35 @@ MApplication::MApplication(int&ac,char**av)
         //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;
@@ -296,7 +339,13 @@ void MApplication::initLanguage()
 
 void MApplication::initialize()
 {
+        //load the correct style
         setMyStyle();
+        //initialize log file
+        initDebug();
+        //init help menu layout
+        initHelpUrl();
+        //init localization and profiles
         initLanguage();
         initProfile();
 }
index b03a1f0..aec1fad 100644 (file)
@@ -38,6 +38,15 @@ class MApplication:public QApplication
                 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();
@@ -63,6 +72,7 @@ class MApplication:public QApplication
                 QTranslator*qttrans,*mstrans;
 };
 
+///return the instance of the MApplication 
 #define mApp (qobject_cast<MApplication*>(qApp))
 
 #endif