From 19506e9cddcb280e106df11b6af41ef77e2c7e97 Mon Sep 17 00:00:00 2001 From: wonder Date: Wed, 21 Jul 2010 13:12:47 +0000 Subject: [PATCH] [FEATURE] Added --configpath option that overrides the default path (~/.qgis) for user configuration and forces QSettings to use this directory, too.\ This allows users to e.g. carry QGIS installation on a flash drive together with all plugins and settings. Developed for Faunalia (http://www.faunalia.it) with funding from Regione Toscana - Sistema Informativo per la Gestione del Territorio e dell' Ambiente [RT-SIGTA]. For the project: "Sviluppo di prodotti software GIS open-source basati sui prodotti QuantumGIS e Postgis (CIG 037728516E)" git-svn-id: http://svn.osgeo.org/qgis/trunk@13951 c8812cc2-4d05-0410-92ff-de0c093fc19c --- python/core/qgsapplication.sip | 4 ++-- src/app/main.cpp | 30 ++++++++++++++++++++++++++---- src/core/qgsapplication.cpp | 10 ++++++++-- src/core/qgsapplication.h | 5 ++++- 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/python/core/qgsapplication.sip b/python/core/qgsapplication.sip index d664824b554..de59325efb6 100644 --- a/python/core/qgsapplication.sip +++ b/python/core/qgsapplication.sip @@ -58,7 +58,7 @@ static void qtgui_UpdatePyArgv(PyObject *argvlist, int argc, char **argv) public: //QgsApplication(int argc, char ** argv, bool GUIenabled); - QgsApplication(SIP_PYLIST argv, bool GUIenabled) /PostHook=__pyQtQAppHook__/ [(int &argc, char **argv, bool GUIenabled)]; + QgsApplication(SIP_PYLIST argv, bool GUIenabled, QString customConfigPath = QString() ) /PostHook=__pyQtQAppHook__/ [(int &argc, char **argv, bool GUIenabled, QString customConfigPath = QString() )]; %MethodCode // The Python interface is a list of argument strings that is modified. @@ -73,7 +73,7 @@ static void qtgui_UpdatePyArgv(PyObject *argvlist, int argc, char **argv) // Create it now the arguments are right. static int nargc = argc; - sipCpp = new sipQgsApplication(nargc, argv, a1); + sipCpp = new sipQgsApplication(nargc, argv, a1, *a2); // Now modify the original list. qtgui_UpdatePyArgv(a0, argc, argv); diff --git a/src/app/main.cpp b/src/app/main.cpp index 6e01645b150..16d5844eece 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -100,6 +100,7 @@ void usage( std::string const & appName ) << "\t[--nologo]\thide splash screen\n" << "\t[--noplugins]\tdon't restore plugins on startup\n" << "\t[--optionspath path]\tuse the given QSettings path\n" + << "\t[--configpath path]\tuse the given path for all user configuration\n" << "\t[--help]\t\tthis text\n\n" << " FILES:\n" << " Files specified on the command line can include rasters,\n" @@ -288,6 +289,10 @@ int main( int argc, char *argv[] ) // which is useful for testing QString myTranslationCode; + // The user can specify a path which will override the default path of custom + // user settings (~/.qgis) and it will be used for QSettings INI file + QString configpath; + #ifndef WIN32 if ( !bundleclicked( argc, argv ) ) { @@ -314,13 +319,14 @@ int main( int argc, char *argv[] ) {"project", required_argument, 0, 'p'}, {"extent", required_argument, 0, 'e'}, {"optionspath", required_argument, 0, 'o'}, + {"configpath", required_argument, 0, 'c'}, {0, 0, 0, 0} }; /* getopt_long stores the option index here. */ int option_index = 0; - optionChar = getopt_long( argc, argv, "swhlpeo", + optionChar = getopt_long( argc, argv, "swhlpeoc", long_options, &option_index ); /* Detect the end of the options. */ @@ -372,7 +378,11 @@ int main( int argc, char *argv[] ) break; case 'o': - QSettings::setPath( QSettings::NativeFormat, QSettings::UserScope, optarg ); + QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, optarg ); + break; + + case 'c': + configpath = optarg; break; case '?': @@ -445,7 +455,12 @@ int main( int argc, char *argv[] ) } else if ( i + 1 < argc && ( arg == "--optionspath" || arg == "-o" ) ) { - QSettings::setPath( QSettings::NativeFormat, QSettings::UserScope, argv[++i] ); + QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, argv[++i] ); + } + else if ( i + 1 < argc && ( arg == "--configpath" || arg == "-c" ) ) + { + configpath = argv[++i]; + QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, configpath ); } else { @@ -476,7 +491,14 @@ int main( int argc, char *argv[] ) ).toUtf8().constData(); exit( 1 ); //exit for now until a version of qgis is capabable of running non interactive } - QgsApplication myApp( argc, argv, myUseGuiFlag ); + + if ( !configpath.isEmpty() ) + { + // tell QSettings to use INI format and save the file in custom config path + QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, configpath ); + } + + QgsApplication myApp( argc, argv, myUseGuiFlag, configpath ); // (if Windows/Mac, use icon from resource) #if ! defined(Q_WS_WIN) && ! defined(Q_WS_MAC) diff --git a/src/core/qgsapplication.cpp b/src/core/qgsapplication.cpp index 9744481a801..ccf1eba641e 100644 --- a/src/core/qgsapplication.cpp +++ b/src/core/qgsapplication.cpp @@ -39,6 +39,7 @@ QString QgsApplication::mPluginPath; QString QgsApplication::mPkgDataPath; QString QgsApplication::mThemeName; QStringList QgsApplication::mDefaultSvgPaths; +QString QgsApplication::mConfigPath = QDir::homePath() + QString( "/.qgis/" ); /*! \class QgsApplication @@ -53,7 +54,7 @@ QStringList QgsApplication::mDefaultSvgPaths; so that platform-conditional code is minimized and paths are easier to change due to centralization. */ -QgsApplication::QgsApplication( int & argc, char ** argv, bool GUIenabled ) +QgsApplication::QgsApplication( int & argc, char ** argv, bool GUIenabled, QString customConfigPath ) : QApplication( argc, argv, GUIenabled ) { #if defined(Q_WS_MACX) || defined(Q_WS_WIN32) || defined(WIN32) @@ -65,6 +66,11 @@ QgsApplication::QgsApplication( int & argc, char ** argv, bool GUIenabled ) setPrefixPath( myPrefix, true ); #endif + if ( !customConfigPath.isEmpty() ) + { + mConfigPath = customConfigPath + "/"; // make sure trailing slash is included + } + mDefaultSvgPaths << mPkgDataPath + QString( "/svg/" ); mDefaultSvgPaths << qgisSettingsDirPath() + QString( "svg/" ); } @@ -284,7 +290,7 @@ const QString QgsApplication::qgisSpatialiteDbTemplatePath() */ const QString QgsApplication::qgisSettingsDirPath() { - return QDir::homePath() + QString( "/.qgis/" ); + return mConfigPath; } /*! diff --git a/src/core/qgsapplication.h b/src/core/qgsapplication.h index 11c8a4fcda3..afaf0b91e91 100644 --- a/src/core/qgsapplication.h +++ b/src/core/qgsapplication.h @@ -26,7 +26,8 @@ class CORE_EXPORT QgsApplication: public QApplication { Q_OBJECT public: - QgsApplication( int & argc, char ** argv, bool GUIenabled ); + //! @note customConfigDir parameter added in v1.6 + QgsApplication( int & argc, char ** argv, bool GUIenabled, QString customConfigPath = QString() ); virtual ~QgsApplication(); //! Catch exceptions when sending event to receiver. @@ -197,6 +198,8 @@ class CORE_EXPORT QgsApplication: public QApplication static QString mPkgDataPath; static QString mThemeName; static QStringList mDefaultSvgPaths; + + static QString mConfigPath; }; #endif