From 6ecb08c4f70f9d3c770e938e90f76876294db486 Mon Sep 17 00:00:00 2001 From: "Juergen E. Fischer" Date: Mon, 24 Sep 2018 01:29:38 +0200 Subject: [PATCH] Revert "Revert "reorder application initialization (fixes #19879, #19916, followup d6795818)"" This reverts commit 469b4b4a57fb2c30441b5de393662cdde36031b2. --- src/app/main.cpp | 530 ++++++++++++++++---------------- src/app/qgsversionmigration.cpp | 4 +- src/core/qgsapplication.cpp | 154 ++++++---- 3 files changed, 354 insertions(+), 334 deletions(-) diff --git a/src/app/main.cpp b/src/app/main.cpp index e84bd7a5749d..e7328e9d218b 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -123,7 +123,7 @@ void usage( const QString &appName ) << QStringLiteral( "\t[--snapshot filename]\temit snapshot of loaded datasets to given file\n" ) << QStringLiteral( "\t[--width width]\twidth of snapshot to emit\n" ) << QStringLiteral( "\t[--height height]\theight of snapshot to emit\n" ) - << QStringLiteral( "\t[--lang language]\tuse language for interface text\n" ) + << QStringLiteral( "\t[--lang language]\tuse language for interface text (changes existing override)\n" ) << QStringLiteral( "\t[--project projectfile]\tload the given QGIS project\n" ) << QStringLiteral( "\t[--extent xmin,ymin,xmax,ymax]\tset initial map extent\n" ) << QStringLiteral( "\t[--nologo]\thide splash screen\n" ) @@ -510,7 +510,7 @@ int main( int argc, char *argv[] ) int mySnapshotHeight = 600; bool myHideSplash = false; - bool mySettingsMigrationForce = false; + bool settingsMigrationForce = false; bool mySkipVersionCheck = false; bool hideBrowser = false; #if defined(ANDROID) @@ -565,193 +565,201 @@ int main( int argc, char *argv[] ) QStringList args; - if ( !bundleclicked( argc, argv ) ) { - // Build a local QCoreApplication from arguments. This way, arguments are correctly parsed from their native locale - // It will use QString::fromLocal8Bit( argv ) under Unix and GetCommandLine() under Windows. QCoreApplication coreApp( argc, argv ); - args = QCoreApplication::arguments(); + ( void ) QgsApplication::resolvePkgPath(); // trigger storing of application path in QgsApplication - for ( int i = 1; i < args.size(); ++i ) + if ( !bundleclicked( argc, argv ) ) { - const QString &arg = args[i]; + // Build a local QCoreApplication from arguments. This way, arguments are correctly parsed from their native locale + // It will use QString::fromLocal8Bit( argv ) under Unix and GetCommandLine() under Windows. + args = QCoreApplication::arguments(); - if ( arg == QLatin1String( "--help" ) || arg == QLatin1String( "-?" ) ) + for ( int i = 1; i < args.size(); ++i ) { - usage( args[0] ); - return 2; - } - else if ( arg == QLatin1String( "--nologo" ) || arg == QLatin1String( "-n" ) ) - { - myHideSplash = true; - } - else if ( arg == QLatin1String( "--version-migration" ) ) - { - mySettingsMigrationForce = true; - } - else if ( arg == QLatin1String( "--noversioncheck" ) || arg == QLatin1String( "-V" ) ) - { - mySkipVersionCheck = true; - } - else if ( arg == QLatin1String( "--noplugins" ) || arg == QLatin1String( "-P" ) ) - { - myRestorePlugins = false; - } - else if ( arg == QLatin1String( "--nocustomization" ) || arg == QLatin1String( "-C" ) ) - { - myCustomization = false; - } - else if ( i + 1 < argc && ( arg == QLatin1String( "--profile" ) ) ) - { - profileName = args[++i]; - } - else if ( i + 1 < argc && ( arg == QLatin1String( "--profiles-path" ) || arg == QLatin1String( "-s" ) ) ) - { - configLocalStorageLocation = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() ); - } - else if ( i + 1 < argc && ( arg == QLatin1String( "--snapshot" ) || arg == QLatin1String( "-s" ) ) ) - { - mySnapshotFileName = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() ); - } - else if ( i + 1 < argc && ( arg == QLatin1String( "--width" ) || arg == QLatin1String( "-w" ) ) ) - { - mySnapshotWidth = QString( args[++i] ).toInt(); - } - else if ( i + 1 < argc && ( arg == QLatin1String( "--height" ) || arg == QLatin1String( "-h" ) ) ) - { - mySnapshotHeight = QString( args[++i] ).toInt(); - } - else if ( i + 1 < argc && ( arg == QLatin1String( "--lang" ) || arg == QLatin1String( "-l" ) ) ) - { - translationCode = args[++i]; - } - else if ( i + 1 < argc && ( arg == QLatin1String( "--project" ) || arg == QLatin1String( "-p" ) ) ) - { - sProjectFileName = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() ); - } - else if ( i + 1 < argc && ( arg == QLatin1String( "--extent" ) || arg == QLatin1String( "-e" ) ) ) - { - myInitialExtent = args[++i]; - } - else if ( i + 1 < argc && ( arg == QLatin1String( "--authdbdirectory" ) || arg == QLatin1String( "-a" ) ) ) - { - authdbdirectory = QDir::toNativeSeparators( QDir( args[++i] ).absolutePath() ); - } - else if ( i + 1 < argc && ( arg == QLatin1String( "--code" ) || arg == QLatin1String( "-f" ) ) ) - { - pythonfile = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() ); - } - else if ( i + 1 < argc && ( arg == QLatin1String( "--customizationfile" ) || arg == QLatin1String( "-z" ) ) ) - { - customizationfile = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() ); - } - else if ( i + 1 < argc && ( arg == QLatin1String( "--globalsettingsfile" ) || arg == QLatin1String( "-g" ) ) ) - { - globalsettingsfile = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() ); - } - else if ( arg == QLatin1String( "--defaultui" ) || arg == QLatin1String( "-d" ) ) - { - myRestoreDefaultWindowState = true; - } - else if ( arg == QLatin1String( "--dxf-export" ) ) - { - dxfOutputFile = args[++i]; - } - else if ( arg == QLatin1String( "--dxf-extent" ) ) - { - QgsLocaleNumC l; - QString ext( args[++i] ); - QStringList coords( ext.split( ',' ) ); + const QString &arg = args[i]; - if ( coords.size() != 4 ) + if ( arg == QLatin1String( "--help" ) || arg == QLatin1String( "-?" ) ) { - std::cerr << "invalid dxf extent " << ext.toStdString() << std::endl; + usage( args[0] ); return 2; } - - for ( int i = 0; i < 4; i++ ) + else if ( arg == QLatin1String( "--nologo" ) || arg == QLatin1String( "-n" ) ) { - bool ok; - double d; + myHideSplash = true; + } + else if ( arg == QLatin1String( "--version-migration" ) ) + { + settingsMigrationForce = true; + } + else if ( arg == QLatin1String( "--noversioncheck" ) || arg == QLatin1String( "-V" ) ) + { + mySkipVersionCheck = true; + } + else if ( arg == QLatin1String( "--noplugins" ) || arg == QLatin1String( "-P" ) ) + { + myRestorePlugins = false; + } + else if ( arg == QLatin1String( "--nocustomization" ) || arg == QLatin1String( "-C" ) ) + { + myCustomization = false; + } + else if ( i + 1 < argc && ( arg == QLatin1String( "--profile" ) ) ) + { + profileName = args[++i]; + } + else if ( i + 1 < argc && ( arg == QLatin1String( "--profiles-path" ) || arg == QLatin1String( "-s" ) ) ) + { + configLocalStorageLocation = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() ); + } + else if ( i + 1 < argc && ( arg == QLatin1String( "--snapshot" ) || arg == QLatin1String( "-s" ) ) ) + { + mySnapshotFileName = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() ); + } + else if ( i + 1 < argc && ( arg == QLatin1String( "--width" ) || arg == QLatin1String( "-w" ) ) ) + { + mySnapshotWidth = QString( args[++i] ).toInt(); + } + else if ( arg == QLatin1String( "--hide-browser" ) ) + { + hideBrowser = true; + } + else if ( i + 1 < argc && ( arg == QLatin1String( "--height" ) || arg == QLatin1String( "-h" ) ) ) + { + mySnapshotHeight = QString( args[++i] ).toInt(); + } + else if ( i + 1 < argc && ( arg == QLatin1String( "--lang" ) || arg == QLatin1String( "-l" ) ) ) + { + translationCode = args[++i]; + } + else if ( i + 1 < argc && ( arg == QLatin1String( "--project" ) || arg == QLatin1String( "-p" ) ) ) + { + sProjectFileName = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() ); + } + else if ( i + 1 < argc && ( arg == QLatin1String( "--extent" ) || arg == QLatin1String( "-e" ) ) ) + { + myInitialExtent = args[++i]; + } + else if ( i + 1 < argc && ( arg == QLatin1String( "--authdbdirectory" ) || arg == QLatin1String( "-a" ) ) ) + { + authdbdirectory = QDir::toNativeSeparators( QDir( args[++i] ).absolutePath() ); + } + else if ( i + 1 < argc && ( arg == QLatin1String( "--code" ) || arg == QLatin1String( "-f" ) ) ) + { + pythonfile = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() ); + } + else if ( i + 1 < argc && ( arg == QLatin1String( "--customizationfile" ) || arg == QLatin1String( "-z" ) ) ) + { + customizationfile = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() ); + } + else if ( i + 1 < argc && ( arg == QLatin1String( "--globalsettingsfile" ) || arg == QLatin1String( "-g" ) ) ) + { + globalsettingsfile = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() ); + } + else if ( arg == QLatin1String( "--defaultui" ) || arg == QLatin1String( "-d" ) ) + { + myRestoreDefaultWindowState = true; + } + else if ( arg == QLatin1String( "--dxf-export" ) ) + { + dxfOutputFile = args[++i]; + } + else if ( arg == QLatin1String( "--dxf-extent" ) ) + { + QgsLocaleNumC l; + QString ext( args[++i] ); + QStringList coords( ext.split( ',' ) ); - d = coords[i].toDouble( &ok ); - if ( !ok ) + if ( coords.size() != 4 ) { - std::cerr << "invalid dxf coordinate " << coords[i].toStdString() << " in extent " << ext.toStdString() << std::endl; + std::cerr << "invalid dxf extent " << ext.toStdString() << std::endl; return 2; } - switch ( i ) + for ( int i = 0; i < 4; i++ ) { - case 0: - dxfExtent.setXMinimum( d ); - break; - case 1: - dxfExtent.setYMinimum( d ); - break; - case 2: - dxfExtent.setXMaximum( d ); - break; - case 3: - dxfExtent.setYMaximum( d ); - break; + bool ok; + double d; + + d = coords[i].toDouble( &ok ); + if ( !ok ) + { + std::cerr << "invalid dxf coordinate " << coords[i].toStdString() << " in extent " << ext.toStdString() << std::endl; + return 2; + } + + switch ( i ) + { + case 0: + dxfExtent.setXMinimum( d ); + break; + case 1: + dxfExtent.setYMinimum( d ); + break; + case 2: + dxfExtent.setXMaximum( d ); + break; + case 3: + dxfExtent.setYMaximum( d ); + break; + } } } - } - else if ( arg == QLatin1String( "--dxf-symbology-mode" ) ) - { - QString mode( args[++i] ); - if ( mode == QLatin1String( "none" ) ) + else if ( arg == QLatin1String( "--dxf-symbology-mode" ) ) { - dxfSymbologyMode = QgsDxfExport::NoSymbology; + QString mode( args[++i] ); + if ( mode == QLatin1String( "none" ) ) + { + dxfSymbologyMode = QgsDxfExport::NoSymbology; + } + else if ( mode == QLatin1String( "symbollayer" ) ) + { + dxfSymbologyMode = QgsDxfExport::SymbolLayerSymbology; + } + else if ( mode == QLatin1String( "feature" ) ) + { + dxfSymbologyMode = QgsDxfExport::FeatureSymbology; + } + else + { + std::cerr << "invalid dxf symbology mode " << mode.toStdString() << std::endl; + return 2; + } } - else if ( mode == QLatin1String( "symbollayer" ) ) + else if ( arg == QLatin1String( "--dxf-scale-denom" ) ) { - dxfSymbologyMode = QgsDxfExport::SymbolLayerSymbology; + bool ok; + QString scale( args[++i] ); + dxfScale = scale.toDouble( &ok ); + if ( !ok ) + { + std::cerr << "invalid dxf scale " << scale.toStdString() << std::endl; + return 2; + } } - else if ( mode == QLatin1String( "feature" ) ) + else if ( arg == QLatin1String( "--dxf-encoding" ) ) { - dxfSymbologyMode = QgsDxfExport::FeatureSymbology; + dxfEncoding = args[++i]; } - else + else if ( arg == QLatin1String( "--dxf-map-theme" ) ) { - std::cerr << "invalid dxf symbology mode " << mode.toStdString() << std::endl; - return 2; + dxfMapTheme = args[++i]; } - } - else if ( arg == QLatin1String( "--dxf-scale-denom" ) ) - { - bool ok; - QString scale( args[++i] ); - dxfScale = scale.toDouble( &ok ); - if ( !ok ) +#ifdef HAVE_OPENCL + else if ( arg == QLatin1String( "--openclprogramfolder" ) ) { - std::cerr << "invalid dxf scale " << scale.toStdString() << std::endl; - return 2; + openClProgramFolder = args[++i]; } - } - else if ( arg == QLatin1String( "--dxf-encoding" ) ) - { - dxfEncoding = args[++i]; - } - else if ( arg == QLatin1String( "--dxf-map-theme" ) ) - { - dxfMapTheme = args[++i]; - } -#ifdef HAVE_OPENCL - else if ( arg == QLatin1String( "--openclprogramfolder" ) ) - { - openClProgramFolder = args[++i]; - } #endif - else if ( arg == QLatin1String( "--" ) ) - { - for ( i++; i < args.size(); ++i ) + else if ( arg == QLatin1String( "--" ) ) + { + for ( i++; i < args.size(); ++i ) + sFileList.append( QDir::toNativeSeparators( QFileInfo( args[i] ).absoluteFilePath() ) ); + } + else + { sFileList.append( QDir::toNativeSeparators( QFileInfo( args[i] ).absoluteFilePath() ) ); - } - else - { - sFileList.append( QDir::toNativeSeparators( QFileInfo( args[i] ).absoluteFilePath() ) ); + } } } } @@ -777,10 +785,9 @@ int main( int argc, char *argv[] ) } } - ///////////////////////////////////////////////////////////////////// // Now we have the handlers for the different behaviors... - //////////////////////////////////////////////////////////////////// + ///////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// // Initialize the application and the translation stuff @@ -816,74 +823,7 @@ int main( int argc, char *argv[] ) QCoreApplication::setAttribute( Qt::AA_DisableWindowContextHelpButton, true ); #endif - /* Translation file for QGIS. - */ - QString i18nPath = QgsApplication::i18nPath(); - QgsSettings mySettings; - QString myUserTranslation = mySettings.value( QStringLiteral( "locale/userLocale" ), "" ).toString(); - QString myGlobalLocale = mySettings.value( QStringLiteral( "locale/globalLocale" ), "" ).toString(); - bool myShowGroupSeparatorFlag = false; // Default to false - bool myLocaleOverrideFlag = mySettings.value( QStringLiteral( "locale/overrideFlag" ), false ).toBool(); - - // Override Show Group Separator if the global override flag is set - if ( myLocaleOverrideFlag ) - { - // Default to false again - myShowGroupSeparatorFlag = mySettings.value( QStringLiteral( "locale/showGroupSeparator" ), false ).toBool(); - } - - // - // Priority of translation is: - // - command line - // - user specified in options dialog (with group checked on) - // - system locale - // - // When specifying from the command line it will change the user - // specified user locale - // - if ( !translationCode.isNull() && !translationCode.isEmpty() ) - { - mySettings.setValue( QStringLiteral( "locale/userLocale" ), translationCode ); - } - else - { - if ( !myLocaleOverrideFlag || myUserTranslation.isEmpty() ) - { - translationCode = QLocale().name(); - //setting the locale/userLocale when the --lang= option is not set will allow third party - //plugins to always use the same locale as the QGIS, otherwise they can be out of sync - mySettings.setValue( QStringLiteral( "locale/userLocale" ), translationCode ); - } - else - { - translationCode = myUserTranslation; - } - } - - // Global locale settings - if ( myLocaleOverrideFlag && ! myGlobalLocale.isEmpty( ) ) - { - QLocale currentLocale( myGlobalLocale ); - QLocale::setDefault( currentLocale ); - } - - // Number settings - QLocale currentLocale; - if ( myShowGroupSeparatorFlag ) - { - currentLocale.setNumberOptions( currentLocale.numberOptions() &= ~QLocale::NumberOption::OmitGroupSeparator ); - } - else - { - currentLocale.setNumberOptions( currentLocale.numberOptions() |= QLocale::NumberOption::OmitGroupSeparator ); - } - QLocale::setDefault( currentLocale ); - - QgsApplication::setTranslation( translationCode ); - - QgsApplication myApp( argc, argv, myUseGuiFlag ); - - // SetUp the QgsSettings Global Settings: + // Set up the QgsSettings Global Settings: // - use the path specified with --globalsettingsfile path, // - use the environment if not found // - use a default location as a fallback @@ -903,7 +843,7 @@ int main( int argc, char *argv[] ) if ( !globalsettingsfile.isEmpty() ) { - if ( ! QgsSettings::setGlobalSettingsPath( globalsettingsfile ) ) + if ( !QgsSettings::setGlobalSettingsPath( globalsettingsfile ) ) { QgsMessageLog::logMessage( QObject::tr( "Invalid globalsettingsfile path: %1" ).arg( globalsettingsfile ), QStringLiteral( "QGIS" ) ); } @@ -940,17 +880,74 @@ int main( int argc, char *argv[] ) profileName = profile->name(); delete profile; - // We can't use QgsSettings until this point because the format and - // folder isn't set until profile is fetch. - // Should be cleaned up in future to make this cleaner. - QgsSettings settings; + { + QgsSettings settings; - QgsDebugMsgLevel( QStringLiteral( "User profile details:" ), 2 ); - QgsDebugMsgLevel( QStringLiteral( "\t - %1" ).arg( profileName ), 2 ); - QgsDebugMsgLevel( QStringLiteral( "\t - %1" ).arg( profileFolder ), 2 ); - QgsDebugMsgLevel( QStringLiteral( "\t - %1" ).arg( rootProfileFolder ), 2 ); + /* Translation file for QGIS. + */ + QString myUserTranslation = settings.value( QStringLiteral( "locale/userLocale" ), "" ).toString(); + QString myGlobalLocale = settings.value( QStringLiteral( "locale/globalLocale" ), "" ).toString(); + bool myShowGroupSeparatorFlag = false; // Default to false + bool myLocaleOverrideFlag = settings.value( QStringLiteral( "locale/overrideFlag" ), false ).toBool(); - myApp.init( profileFolder ); + // Override Show Group Separator if the global override flag is set + if ( myLocaleOverrideFlag ) + { + // Default to false again + myShowGroupSeparatorFlag = settings.value( QStringLiteral( "locale/showGroupSeparator" ), false ).toBool(); + } + + // + // Priority of translation is: + // - command line + // - user specified in options dialog (with group checked on) + // - system locale + // + // When specifying from the command line it will change the user + // specified user locale + // + if ( !translationCode.isNull() && !translationCode.isEmpty() ) + { + settings.setValue( QStringLiteral( "locale/userLocale" ), translationCode ); + } + else + { + if ( !myLocaleOverrideFlag || myUserTranslation.isEmpty() ) + { + translationCode = QLocale().name(); + //setting the locale/userLocale when the --lang= option is not set will allow third party + //plugins to always use the same locale as the QGIS, otherwise they can be out of sync + settings.setValue( QStringLiteral( "locale/userLocale" ), translationCode ); + } + else + { + translationCode = myUserTranslation; + } + } + + // Global locale settings + if ( myLocaleOverrideFlag && ! myGlobalLocale.isEmpty( ) ) + { + QLocale currentLocale( myGlobalLocale ); + QLocale::setDefault( currentLocale ); + } + + // Number settings + QLocale currentLocale; + if ( myShowGroupSeparatorFlag ) + { + currentLocale.setNumberOptions( currentLocale.numberOptions() &= ~QLocale::NumberOption::OmitGroupSeparator ); + } + else + { + currentLocale.setNumberOptions( currentLocale.numberOptions() |= QLocale::NumberOption::OmitGroupSeparator ); + } + QLocale::setDefault( currentLocale ); + + QgsApplication::setTranslation( translationCode ); + } + + QgsApplication myApp( argc, argv, myUseGuiFlag ); // Settings migration is only supported on the default profile for now. if ( profileName == "default" ) @@ -958,19 +955,20 @@ int main( int argc, char *argv[] ) // Note: this flag is ka version number so that we can reset it once we change the version. // Note2: Is this a good idea can we do it better. - int firstRunVersion = settings.value( QStringLiteral( "migration/firstRunVersionFlag" ), 0 ).toInt(); + QgsSettings migSettings; + int firstRunVersion = migSettings.value( QStringLiteral( "migration/firstRunVersionFlag" ), 0 ).toInt(); bool showWelcome = ( firstRunVersion == 0 || Qgis::QGIS_VERSION_INT > firstRunVersion ); std::unique_ptr< QgsVersionMigration > migration( QgsVersionMigration::canMigrate( 20000, Qgis::QGIS_VERSION_INT ) ); - if ( migration && ( mySettingsMigrationForce || migration->requiresMigration() ) ) + if ( migration && ( settingsMigrationForce || migration->requiresMigration() ) ) { bool runMigration = true; - if ( !mySettingsMigrationForce && showWelcome ) + if ( !settingsMigrationForce && showWelcome ) { QgsFirstRunDialog dlg; dlg.exec(); runMigration = dlg.migrateSettings(); - settings.setValue( QStringLiteral( "migration/firstRunVersionFlag" ), Qgis::QGIS_VERSION_INT ); + migSettings.setValue( QStringLiteral( "migration/firstRunVersionFlag" ), Qgis::QGIS_VERSION_INT ); } if ( runMigration ) @@ -981,6 +979,18 @@ int main( int argc, char *argv[] ) } } + // We can't use QgsSettings until this point because the format and + // folder isn't set until profile is fetch. + // Should be cleaned up in future to make this cleaner. + QgsSettings settings; + + QgsDebugMsgLevel( QStringLiteral( "User profile details:" ), 2 ); + QgsDebugMsgLevel( QStringLiteral( "\t - %1" ).arg( profileName ), 2 ); + QgsDebugMsgLevel( QStringLiteral( "\t - %1" ).arg( profileFolder ), 2 ); + QgsDebugMsgLevel( QStringLiteral( "\t - %1" ).arg( rootProfileFolder ), 2 ); + + myApp.init( profileFolder ); + // Redefine QgsApplication::libraryPaths as necessary. // IMPORTANT: Do *after* QgsApplication myApp(...), but *before* Qt uses any plugins, // e.g. loading splash screen, setting window icon, etc. @@ -1123,24 +1133,12 @@ int main( int argc, char *argv[] ) } #endif - // update any saved setting for older themes to new default 'gis' theme (2013-04-15) - if ( mySettings.contains( QStringLiteral( "/Themes" ) ) ) - { - QString theme = mySettings.value( QStringLiteral( "Themes" ), "default" ).toString(); - if ( theme == QLatin1String( "gis" ) - || theme == QLatin1String( "classic" ) - || theme == QLatin1String( "nkids" ) ) - { - mySettings.setValue( QStringLiteral( "Themes" ), QStringLiteral( "default" ) ); - } - } - // custom environment variables QMap systemEnvVars = QgsApplication::systemEnvVars(); - bool useCustomVars = mySettings.value( QStringLiteral( "qgis/customEnvVarsUse" ), QVariant( false ) ).toBool(); + bool useCustomVars = settings.value( QStringLiteral( "qgis/customEnvVarsUse" ), QVariant( false ) ).toBool(); if ( useCustomVars ) { - QStringList customVarsList = mySettings.value( QStringLiteral( "qgis/customEnvVars" ), "" ).toStringList(); + QStringList customVarsList = settings.value( QStringLiteral( "qgis/customEnvVars" ), "" ).toStringList(); if ( !customVarsList.isEmpty() ) { Q_FOREACH ( const QString &varStr, customVarsList ) @@ -1195,7 +1193,7 @@ int main( int argc, char *argv[] ) // Set the application style. If it's not set QT will use the platform style except on Windows // as it looks really ugly so we use QPlastiqueStyle. - QString presetStyle = mySettings.value( QStringLiteral( "qgis/style" ) ).toString(); + QString presetStyle = settings.value( QStringLiteral( "qgis/style" ) ).toString(); QString activeStyleName = presetStyle; if ( activeStyleName.isEmpty() ) // not set, using default style { @@ -1218,7 +1216,7 @@ int main( int argc, char *argv[] ) if ( !presetStyle.isEmpty() ) { QApplication::setStyle( presetStyle ); - mySettings.setValue( QStringLiteral( "qgis/style" ), QApplication::style()->objectName() ); + settings.setValue( QStringLiteral( "qgis/style" ), QApplication::style()->objectName() ); } // set authentication database directory @@ -1235,7 +1233,7 @@ int main( int argc, char *argv[] ) int h = 300 * qApp->desktop()->logicalDpiY() / 96; QSplashScreen *mypSplash = new QSplashScreen( myPixmap.scaled( w, h, Qt::KeepAspectRatio, Qt::SmoothTransformation ) ); - if ( !myHideSplash && !mySettings.value( QStringLiteral( "qgis/hideSplash" ) ).toBool() ) + if ( !myHideSplash && !settings.value( QStringLiteral( "qgis/hideSplash" ) ).toBool() ) { //for win and linux we can just automask and png transparency areas will be used mypSplash->setMask( myPixmap.mask() ); @@ -1244,23 +1242,23 @@ int main( int argc, char *argv[] ) // optionally restore default window state // use restoreDefaultWindowState setting only if NOT using command line (then it is set already) - if ( myRestoreDefaultWindowState || mySettings.value( QStringLiteral( "qgis/restoreDefaultWindowState" ), false ).toBool() ) + if ( myRestoreDefaultWindowState || settings.value( QStringLiteral( "qgis/restoreDefaultWindowState" ), false ).toBool() ) { QgsDebugMsg( "Resetting /UI/state settings!" ); - mySettings.remove( QStringLiteral( "/UI/state" ) ); - mySettings.remove( QStringLiteral( "/qgis/restoreDefaultWindowState" ) ); + settings.remove( QStringLiteral( "/UI/state" ) ); + settings.remove( QStringLiteral( "/qgis/restoreDefaultWindowState" ) ); } if ( hideBrowser ) { - if ( mySettings.value( QStringLiteral( "/Windows/Data Source Manager/tab" ) ).toInt() == 0 ) - mySettings.setValue( QStringLiteral( "/Windows/Data Source Manager/tab" ), 1 ); - mySettings.setValue( QStringLiteral( "/UI/hidebrowser" ), true ); + if ( settings.value( QStringLiteral( "/Windows/Data Source Manager/tab" ) ).toInt() == 0 ) + settings.setValue( QStringLiteral( "/Windows/Data Source Manager/tab" ), 1 ); + settings.setValue( QStringLiteral( "/UI/hidebrowser" ), true ); } // set max. thread count // this should be done in QgsApplication::init() but it doesn't know the settings dir. - QgsApplication::setMaxThreads( mySettings.value( QStringLiteral( "qgis/max_threads" ), -1 ).toInt() ); + QgsApplication::setMaxThreads( settings.value( QStringLiteral( "qgis/max_threads" ), -1 ).toInt() ); QgisApp *qgis = new QgisApp( mypSplash, myRestorePlugins, mySkipVersionCheck, rootProfileFolder, profileName ); // "QgisApp" used to find canonical instance qgis->setObjectName( QStringLiteral( "QgisApp" ) ); diff --git a/src/app/qgsversionmigration.cpp b/src/app/qgsversionmigration.cpp index e6daeafd10a7..603298acbf16 100644 --- a/src/app/qgsversionmigration.cpp +++ b/src/app/qgsversionmigration.cpp @@ -3,7 +3,7 @@ --------------------- begin : 30.7.2017 - copyright : (C) 2017 by nathan + copyright : (C) 2017 by Nathan Woodrow email : woodrow.nathan at gmail dot com *************************************************************************** * * @@ -342,5 +342,5 @@ QPair Qgs2To3Migration::transformKey( QString fullOldKey, QStr QString Qgs2To3Migration::migrationFilePath() { - return QgsApplication::pkgDataPath() + "/resources/2to3migration.txt"; + return QgsApplication::resolvePkgPath() + "/resources/2to3migration.txt"; } diff --git a/src/core/qgsapplication.cpp b/src/core/qgsapplication.cpp index 5ede1c61c7ae..c96b05b2de79 100644 --- a/src/core/qgsapplication.cpp +++ b/src/core/qgsapplication.cpp @@ -106,7 +106,7 @@ QString ABISYM( QgsApplication::mConfigPath ); bool ABISYM( QgsApplication::mInitialized ) = false; bool ABISYM( QgsApplication::mRunningFromBuildDir ) = false; QString ABISYM( QgsApplication::mBuildSourcePath ); -#ifdef _MSC_VER +#if defined(_MSC_VER) && !defined(USING_NMAKE) && !defined(USING_NINJA) QString ABISYM( QgsApplication::mCfgIntDir ); #endif QString ABISYM( QgsApplication::mBuildOutputPath ); @@ -201,31 +201,7 @@ void QgsApplication::init( QString profileFolder ) qRegisterMetaType( "QgsStyle::StyleEntity" ); qRegisterMetaType( "QgsCoordinateReferenceSystem" ); - QString prefixPath( getenv( "QGIS_PREFIX_PATH" ) ? getenv( "QGIS_PREFIX_PATH" ) : applicationDirPath() ); - // QgsDebugMsg( QString( "prefixPath(): %1" ).arg( prefixPath ) ); - - // check if QGIS is run from build directory (not the install directory) - QFile f; - // "/../../.." is for Mac bundled app in build directory - Q_FOREACH ( const QString &path, QStringList() << "" << "/.." << "/bin" << "/../../.." ) - { - f.setFileName( prefixPath + path + "/qgisbuildpath.txt" ); - if ( f.exists() ) - break; - } - if ( f.exists() && f.open( QIODevice::ReadOnly ) ) - { - ABISYM( mRunningFromBuildDir ) = true; - ABISYM( mBuildSourcePath ) = f.readLine().trimmed(); - ABISYM( mBuildOutputPath ) = f.readLine().trimmed(); - QgsDebugMsgLevel( QStringLiteral( "Running from build directory!" ), 4 ); - QgsDebugMsgLevel( QStringLiteral( "- source directory: %1" ).arg( ABISYM( mBuildSourcePath ).toUtf8().data() ), 4 ); - QgsDebugMsgLevel( QStringLiteral( "- output directory of the build: %1" ).arg( ABISYM( mBuildOutputPath ).toUtf8().data() ), 4 ); -#ifdef _MSC_VER - ABISYM( mCfgIntDir ) = prefixPath.split( '/', QString::SkipEmptyParts ).last(); - qDebug( "- cfg: %s", ABISYM( mCfgIntDir ).toUtf8().data() ); -#endif - } + ( void ) resolvePkgPath(); if ( ABISYM( mRunningFromBuildDir ) ) { @@ -257,7 +233,7 @@ void QgsApplication::init( QString profileFolder ) #if defined(Q_OS_MACX) || defined(Q_OS_WIN) setPrefixPath( applicationDirPath(), true ); #elif defined(ANDROID) - // this is "/data/data/org.qgis.qgis" in android + // this is "/data/data/org.qgis.qgis" in android QDir myDir( QDir::homePath() ); myDir.cdUp(); QString myPrefix = myDir.absolutePath(); @@ -290,7 +266,6 @@ void QgsApplication::init( QString profileFolder ) setAuthDatabaseDirPath( getenv( "QGIS_AUTH_DB_DIR_PATH" ) ); } - // store system environment variables passed to application, before they are adjusted QMap systemEnvVarMap; QString passfile( QStringLiteral( "QGIS_AUTH_PASSWORD_FILE" ) ); // QString, for comparison @@ -494,7 +469,10 @@ QString QgsApplication::pluginPath() } QString QgsApplication::pkgDataPath() { - return ABISYM( mPkgDataPath ); + if ( ABISYM( mPkgDataPath ).isNull() ) + return resolvePkgPath(); + else + return ABISYM( mPkgDataPath ); } QString QgsApplication::defaultThemePath() { @@ -656,31 +634,77 @@ void QgsApplication::setThemeName( const QString &themeName ) QString QgsApplication::resolvePkgPath() { -#if defined(ANDROID) - QString prefixPath( getenv( "QGIS_PREFIX_PATH" ) ? getenv( "QGIS_PREFIX_PATH" ) : QDir::homePath() ); -#else - QString prefixPath( getenv( "QGIS_PREFIX_PATH" ) ? getenv( "QGIS_PREFIX_PATH" ) : applicationDirPath() ); + static QString appPath; + if ( appPath.isNull() ) + { + if ( QCoreApplication::instance() ) + { + appPath = applicationDirPath(); + } + else + { + qWarning( "Application path not initialized" ); + } + + // check if QGIS is run from build directory (not the install directory) + QFile f; + // "/../../.." is for Mac bundled app in build directory + Q_FOREACH ( const QString &path, QStringList() << "" << "/.." << "/bin" << "/../../.." ) + { + f.setFileName( appPath + path + "/qgisbuildpath.txt" ); + if ( f.exists() ) + break; + } + if ( f.exists() && f.open( QIODevice::ReadOnly ) ) + { + ABISYM( mRunningFromBuildDir ) = true; + ABISYM( mBuildSourcePath ) = f.readLine().trimmed(); + ABISYM( mBuildOutputPath ) = f.readLine().trimmed(); + QgsDebugMsgLevel( QStringLiteral( "Running from build directory!" ), 4 ); + QgsDebugMsgLevel( QStringLiteral( "- source directory: %1" ).arg( ABISYM( mBuildSourcePath ).toUtf8().data() ), 4 ); + QgsDebugMsgLevel( QStringLiteral( "- output directory of the build: %1" ).arg( ABISYM( mBuildOutputPath ).toUtf8().data() ), 4 ); +#if defined(_MSC_VER) && !defined(USING_NMAKE) && !defined(USING_NINJA) + ABISYM( mCfgIntDir ) = appPath.split( '/', QString::SkipEmptyParts ).last(); + qDebug( "- cfg: %s", ABISYM( mCfgIntDir ).toUtf8().data() ); #endif - QFile f; - // "/../../.." is for Mac bundled app in build directory - const QStringList pathPrefixes = QStringList() << "" << "/.." << "/bin" << "/../../.."; - for ( const QString &path : pathPrefixes ) - { - f.setFileName( prefixPath + path + "/qgisbuildpath.txt" ); - QgsDebugMsg( f.fileName() ); - if ( f.exists() ) - break; + } } - if ( f.exists() && f.open( QIODevice::ReadOnly ) ) + QString prefixPath; + if ( getenv( "QGIS_PREFIX_PATH" ) ) + prefixPath = getenv( "QGIS_PREFIX_PATH" ); + else { - QgsDebugMsg( "Running from build dir!" ); - return f.readLine().trimmed(); +#if defined(ANDROID) + // this is "/data/data/org.qgis.qgis" in android + QDir dir( QDir::homePath() ); + dir.cdUp(); + prefixPath = dir.absolutePath(); +#else + +#if defined(Q_OS_MACX) || defined(Q_OS_WIN) + prefixPath = appPath; +#if defined(_MSC_VER) + if ( prefixPath.endsWith( "/bin" ) ) + prefixPath.chop( 4 ); +#endif +#else + QDir dir( appPath ); + // Fix for server which is one level deeper in /usr/lib/cgi-bin + if ( appPath.contains( QStringLiteral( "cgi-bin" ) ) ) + { + dir.cdUp(); + } + dir.cdUp(); // Go from /usr/bin or /usr/lib (for server) to /usr + prefixPath = dir.absolutePath(); +#endif +#endif } + + if ( ABISYM( mRunningFromBuildDir ) ) + return ABISYM( mBuildSourcePath ); else - { return prefixPath + '/' + QStringLiteral( QGIS_DATA_SUBDIR ); - } } QString QgsApplication::themeName() @@ -769,56 +793,54 @@ QHash QgsApplication::uiThemes() QString QgsApplication::authorsFilePath() { - return ABISYM( mPkgDataPath ) + QStringLiteral( "/doc/AUTHORS" ); + return pkgDataPath() + QStringLiteral( "/doc/AUTHORS" ); } QString QgsApplication::contributorsFilePath() { - return ABISYM( mPkgDataPath ) + QStringLiteral( "/doc/CONTRIBUTORS" ); + return pkgDataPath() + QStringLiteral( "/doc/CONTRIBUTORS" ); } QString QgsApplication::developersMapFilePath() { - return ABISYM( mPkgDataPath ) + QStringLiteral( "/doc/developersmap.html" ); + return pkgDataPath() + QStringLiteral( "/doc/developersmap.html" ); } QString QgsApplication::sponsorsFilePath() { - return ABISYM( mPkgDataPath ) + QStringLiteral( "/doc/SPONSORS" ); + return pkgDataPath() + QStringLiteral( "/doc/SPONSORS" ); } QString QgsApplication::donorsFilePath() { - return ABISYM( mPkgDataPath ) + QStringLiteral( "/doc/DONORS" ); + return pkgDataPath() + QStringLiteral( "/doc/DONORS" ); } QString QgsApplication::translatorsFilePath() { - return ABISYM( mPkgDataPath ) + QStringLiteral( "/doc/TRANSLATORS" ); + return pkgDataPath() + QStringLiteral( "/doc/TRANSLATORS" ); } QString QgsApplication::licenceFilePath() { - return ABISYM( mPkgDataPath ) + QStringLiteral( "/doc/LICENSE" ); + return pkgDataPath() + QStringLiteral( "/doc/LICENSE" ); } QString QgsApplication::i18nPath() { - if ( !ABISYM( mInitialized ) ) - return resolvePkgPath() + QStringLiteral( "/i18n/" ); - else if ( ABISYM( mRunningFromBuildDir ) ) + if ( ABISYM( mRunningFromBuildDir ) ) return ABISYM( mBuildOutputPath ) + QStringLiteral( "/i18n/" ); else - return ABISYM( mPkgDataPath ) + QStringLiteral( "/i18n/" ); + return pkgDataPath() + QStringLiteral( "/i18n/" ); } QString QgsApplication::metadataPath() { - return ABISYM( mPkgDataPath ) + QStringLiteral( "/resources/metadata-ISO/" ); + return pkgDataPath() + QStringLiteral( "/resources/metadata-ISO/" ); } QString QgsApplication::qgisMasterDatabaseFilePath() { - return ABISYM( mPkgDataPath ) + QStringLiteral( "/resources/qgis.db" ); + return pkgDataPath() + QStringLiteral( "/resources/qgis.db" ); } QString QgsApplication::qgisSettingsDirPath() @@ -843,7 +865,7 @@ QString QgsApplication::splashPath() QString QgsApplication::iconsPath() { - return ABISYM( mPkgDataPath ) + QStringLiteral( "/images/icons/" ); + return pkgDataPath() + QStringLiteral( "/images/icons/" ); } QString QgsApplication::srsDatabaseFilePath() @@ -854,7 +876,7 @@ QString QgsApplication::srsDatabaseFilePath() if ( !QFile( tempCopy ).exists() ) { - QFile f( ABISYM( mPkgDataPath ) + "/resources/srs.db" ); + QFile f( pkgDataPath() + "/resources/srs.db" ); if ( !f.copy( tempCopy ) ) { qFatal( "Could not create temporary copy" ); @@ -865,7 +887,7 @@ QString QgsApplication::srsDatabaseFilePath() } else { - return ABISYM( mPkgDataPath ) + QStringLiteral( "/resources/srs.db" ); + return pkgDataPath() + QStringLiteral( "/resources/srs.db" ); } } @@ -1029,17 +1051,17 @@ QString QgsApplication::userThemesFolder() QString QgsApplication::defaultStylePath() { - return ABISYM( mPkgDataPath ) + QStringLiteral( "/resources/symbology-style.xml" ); + return pkgDataPath() + QStringLiteral( "/resources/symbology-style.xml" ); } QString QgsApplication::defaultThemesFolder() { - return ABISYM( mPkgDataPath ) + QStringLiteral( "/resources/themes" ); + return pkgDataPath() + QStringLiteral( "/resources/themes" ); } QString QgsApplication::serverResourcesPath() { - return ABISYM( mPkgDataPath ) + QStringLiteral( "/resources/server/" ); + return pkgDataPath() + QStringLiteral( "/resources/server/" ); } QString QgsApplication::libraryPath()