Skip to content

Commit 9c5d397

Browse files
committedSep 22, 2018
reorder application initialization (fixes #19879, #19916, followup d679581)
1 parent b6ec1bc commit 9c5d397

File tree

3 files changed

+347
-331
lines changed

3 files changed

+347
-331
lines changed
 

‎src/app/main.cpp

Lines changed: 257 additions & 263 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ void usage( const QString &appName )
123123
<< QStringLiteral( "\t[--snapshot filename]\temit snapshot of loaded datasets to given file\n" )
124124
<< QStringLiteral( "\t[--width width]\twidth of snapshot to emit\n" )
125125
<< QStringLiteral( "\t[--height height]\theight of snapshot to emit\n" )
126-
<< QStringLiteral( "\t[--lang language]\tuse language for interface text\n" )
126+
<< QStringLiteral( "\t[--lang language]\tuse language for interface text (changes existing override)\n" )
127127
<< QStringLiteral( "\t[--project projectfile]\tload the given QGIS project\n" )
128128
<< QStringLiteral( "\t[--extent xmin,ymin,xmax,ymax]\tset initial map extent\n" )
129129
<< QStringLiteral( "\t[--nologo]\thide splash screen\n" )
@@ -509,7 +509,7 @@ int main( int argc, char *argv[] )
509509
int mySnapshotHeight = 600;
510510

511511
bool myHideSplash = false;
512-
bool mySettingsMigrationForce = false;
512+
bool settingsMigrationForce = false;
513513
bool mySkipVersionCheck = false;
514514
#if defined(ANDROID)
515515
QgsDebugMsg( QString( "Android: Splash hidden" ) );
@@ -563,193 +563,197 @@ int main( int argc, char *argv[] )
563563

564564
QStringList args;
565565

566-
if ( !bundleclicked( argc, argv ) )
567566
{
568-
// Build a local QCoreApplication from arguments. This way, arguments are correctly parsed from their native locale
569-
// It will use QString::fromLocal8Bit( argv ) under Unix and GetCommandLine() under Windows.
570567
QCoreApplication coreApp( argc, argv );
571-
args = QCoreApplication::arguments();
568+
( void ) QgsApplication::resolvePkgPath(); // trigger storing of application path in QgsApplication
572569

573-
for ( int i = 1; i < args.size(); ++i )
570+
if ( !bundleclicked( argc, argv ) )
574571
{
575-
const QString &arg = args[i];
572+
// Build a local QCoreApplication from arguments. This way, arguments are correctly parsed from their native locale
573+
// It will use QString::fromLocal8Bit( argv ) under Unix and GetCommandLine() under Windows.
574+
args = QCoreApplication::arguments();
576575

577-
if ( arg == QLatin1String( "--help" ) || arg == QLatin1String( "-?" ) )
576+
for ( int i = 1; i < args.size(); ++i )
578577
{
579-
usage( args[0] );
580-
return 2;
581-
}
582-
else if ( arg == QLatin1String( "--nologo" ) || arg == QLatin1String( "-n" ) )
583-
{
584-
myHideSplash = true;
585-
}
586-
else if ( arg == QLatin1String( "--version-migration" ) )
587-
{
588-
mySettingsMigrationForce = true;
589-
}
590-
else if ( arg == QLatin1String( "--noversioncheck" ) || arg == QLatin1String( "-V" ) )
591-
{
592-
mySkipVersionCheck = true;
593-
}
594-
else if ( arg == QLatin1String( "--noplugins" ) || arg == QLatin1String( "-P" ) )
595-
{
596-
myRestorePlugins = false;
597-
}
598-
else if ( arg == QLatin1String( "--nocustomization" ) || arg == QLatin1String( "-C" ) )
599-
{
600-
myCustomization = false;
601-
}
602-
else if ( i + 1 < argc && ( arg == QLatin1String( "--profile" ) ) )
603-
{
604-
profileName = args[++i];
605-
}
606-
else if ( i + 1 < argc && ( arg == QLatin1String( "--profiles-path" ) || arg == QLatin1String( "-s" ) ) )
607-
{
608-
configLocalStorageLocation = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
609-
}
610-
else if ( i + 1 < argc && ( arg == QLatin1String( "--snapshot" ) || arg == QLatin1String( "-s" ) ) )
611-
{
612-
mySnapshotFileName = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
613-
}
614-
else if ( i + 1 < argc && ( arg == QLatin1String( "--width" ) || arg == QLatin1String( "-w" ) ) )
615-
{
616-
mySnapshotWidth = QString( args[++i] ).toInt();
617-
}
618-
else if ( i + 1 < argc && ( arg == QLatin1String( "--height" ) || arg == QLatin1String( "-h" ) ) )
619-
{
620-
mySnapshotHeight = QString( args[++i] ).toInt();
621-
}
622-
else if ( i + 1 < argc && ( arg == QLatin1String( "--lang" ) || arg == QLatin1String( "-l" ) ) )
623-
{
624-
translationCode = args[++i];
625-
}
626-
else if ( i + 1 < argc && ( arg == QLatin1String( "--project" ) || arg == QLatin1String( "-p" ) ) )
627-
{
628-
sProjectFileName = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
629-
}
630-
else if ( i + 1 < argc && ( arg == QLatin1String( "--extent" ) || arg == QLatin1String( "-e" ) ) )
631-
{
632-
myInitialExtent = args[++i];
633-
}
634-
else if ( i + 1 < argc && ( arg == QLatin1String( "--authdbdirectory" ) || arg == QLatin1String( "-a" ) ) )
635-
{
636-
authdbdirectory = QDir::toNativeSeparators( QDir( args[++i] ).absolutePath() );
637-
}
638-
else if ( i + 1 < argc && ( arg == QLatin1String( "--code" ) || arg == QLatin1String( "-f" ) ) )
639-
{
640-
pythonfile = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
641-
}
642-
else if ( i + 1 < argc && ( arg == QLatin1String( "--customizationfile" ) || arg == QLatin1String( "-z" ) ) )
643-
{
644-
customizationfile = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
645-
}
646-
else if ( i + 1 < argc && ( arg == QLatin1String( "--globalsettingsfile" ) || arg == QLatin1String( "-g" ) ) )
647-
{
648-
globalsettingsfile = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
649-
}
650-
else if ( arg == QLatin1String( "--defaultui" ) || arg == QLatin1String( "-d" ) )
651-
{
652-
myRestoreDefaultWindowState = true;
653-
}
654-
else if ( arg == QLatin1String( "--dxf-export" ) )
655-
{
656-
dxfOutputFile = args[++i];
657-
}
658-
else if ( arg == QLatin1String( "--dxf-extent" ) )
659-
{
660-
QgsLocaleNumC l;
661-
QString ext( args[++i] );
662-
QStringList coords( ext.split( ',' ) );
578+
const QString &arg = args[i];
663579

664-
if ( coords.size() != 4 )
580+
if ( arg == QLatin1String( "--help" ) || arg == QLatin1String( "-?" ) )
665581
{
666-
std::cerr << "invalid dxf extent " << ext.toStdString() << std::endl;
582+
usage( args[0] );
667583
return 2;
668584
}
669-
670-
for ( int i = 0; i < 4; i++ )
585+
else if ( arg == QLatin1String( "--nologo" ) || arg == QLatin1String( "-n" ) )
671586
{
672-
bool ok;
673-
double d;
587+
myHideSplash = true;
588+
}
589+
else if ( arg == QLatin1String( "--version-migration" ) )
590+
{
591+
settingsMigrationForce = true;
592+
}
593+
else if ( arg == QLatin1String( "--noversioncheck" ) || arg == QLatin1String( "-V" ) )
594+
{
595+
mySkipVersionCheck = true;
596+
}
597+
else if ( arg == QLatin1String( "--noplugins" ) || arg == QLatin1String( "-P" ) )
598+
{
599+
myRestorePlugins = false;
600+
}
601+
else if ( arg == QLatin1String( "--nocustomization" ) || arg == QLatin1String( "-C" ) )
602+
{
603+
myCustomization = false;
604+
}
605+
else if ( i + 1 < argc && ( arg == QLatin1String( "--profile" ) ) )
606+
{
607+
profileName = args[++i];
608+
}
609+
else if ( i + 1 < argc && ( arg == QLatin1String( "--profiles-path" ) || arg == QLatin1String( "-s" ) ) )
610+
{
611+
configLocalStorageLocation = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
612+
}
613+
else if ( i + 1 < argc && ( arg == QLatin1String( "--snapshot" ) || arg == QLatin1String( "-s" ) ) )
614+
{
615+
mySnapshotFileName = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
616+
}
617+
else if ( i + 1 < argc && ( arg == QLatin1String( "--width" ) || arg == QLatin1String( "-w" ) ) )
618+
{
619+
mySnapshotWidth = QString( args[++i] ).toInt();
620+
}
621+
else if ( i + 1 < argc && ( arg == QLatin1String( "--height" ) || arg == QLatin1String( "-h" ) ) )
622+
{
623+
mySnapshotHeight = QString( args[++i] ).toInt();
624+
}
625+
else if ( i + 1 < argc && ( arg == QLatin1String( "--lang" ) || arg == QLatin1String( "-l" ) ) )
626+
{
627+
translationCode = args[++i];
628+
}
629+
else if ( i + 1 < argc && ( arg == QLatin1String( "--project" ) || arg == QLatin1String( "-p" ) ) )
630+
{
631+
sProjectFileName = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
632+
}
633+
else if ( i + 1 < argc && ( arg == QLatin1String( "--extent" ) || arg == QLatin1String( "-e" ) ) )
634+
{
635+
myInitialExtent = args[++i];
636+
}
637+
else if ( i + 1 < argc && ( arg == QLatin1String( "--authdbdirectory" ) || arg == QLatin1String( "-a" ) ) )
638+
{
639+
authdbdirectory = QDir::toNativeSeparators( QDir( args[++i] ).absolutePath() );
640+
}
641+
else if ( i + 1 < argc && ( arg == QLatin1String( "--code" ) || arg == QLatin1String( "-f" ) ) )
642+
{
643+
pythonfile = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
644+
}
645+
else if ( i + 1 < argc && ( arg == QLatin1String( "--customizationfile" ) || arg == QLatin1String( "-z" ) ) )
646+
{
647+
customizationfile = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
648+
}
649+
else if ( i + 1 < argc && ( arg == QLatin1String( "--globalsettingsfile" ) || arg == QLatin1String( "-g" ) ) )
650+
{
651+
globalsettingsfile = QDir::toNativeSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
652+
}
653+
else if ( arg == QLatin1String( "--defaultui" ) || arg == QLatin1String( "-d" ) )
654+
{
655+
myRestoreDefaultWindowState = true;
656+
}
657+
else if ( arg == QLatin1String( "--dxf-export" ) )
658+
{
659+
dxfOutputFile = args[++i];
660+
}
661+
else if ( arg == QLatin1String( "--dxf-extent" ) )
662+
{
663+
QgsLocaleNumC l;
664+
QString ext( args[++i] );
665+
QStringList coords( ext.split( ',' ) );
674666

675-
d = coords[i].toDouble( &ok );
676-
if ( !ok )
667+
if ( coords.size() != 4 )
677668
{
678-
std::cerr << "invalid dxf coordinate " << coords[i].toStdString() << " in extent " << ext.toStdString() << std::endl;
669+
std::cerr << "invalid dxf extent " << ext.toStdString() << std::endl;
679670
return 2;
680671
}
681672

682-
switch ( i )
673+
for ( int i = 0; i < 4; i++ )
683674
{
684-
case 0:
685-
dxfExtent.setXMinimum( d );
686-
break;
687-
case 1:
688-
dxfExtent.setYMinimum( d );
689-
break;
690-
case 2:
691-
dxfExtent.setXMaximum( d );
692-
break;
693-
case 3:
694-
dxfExtent.setYMaximum( d );
695-
break;
675+
bool ok;
676+
double d;
677+
678+
d = coords[i].toDouble( &ok );
679+
if ( !ok )
680+
{
681+
std::cerr << "invalid dxf coordinate " << coords[i].toStdString() << " in extent " << ext.toStdString() << std::endl;
682+
return 2;
683+
}
684+
685+
switch ( i )
686+
{
687+
case 0:
688+
dxfExtent.setXMinimum( d );
689+
break;
690+
case 1:
691+
dxfExtent.setYMinimum( d );
692+
break;
693+
case 2:
694+
dxfExtent.setXMaximum( d );
695+
break;
696+
case 3:
697+
dxfExtent.setYMaximum( d );
698+
break;
699+
}
696700
}
697701
}
698-
}
699-
else if ( arg == QLatin1String( "--dxf-symbology-mode" ) )
700-
{
701-
QString mode( args[++i] );
702-
if ( mode == QLatin1String( "none" ) )
702+
else if ( arg == QLatin1String( "--dxf-symbology-mode" ) )
703703
{
704-
dxfSymbologyMode = QgsDxfExport::NoSymbology;
704+
QString mode( args[++i] );
705+
if ( mode == QLatin1String( "none" ) )
706+
{
707+
dxfSymbologyMode = QgsDxfExport::NoSymbology;
708+
}
709+
else if ( mode == QLatin1String( "symbollayer" ) )
710+
{
711+
dxfSymbologyMode = QgsDxfExport::SymbolLayerSymbology;
712+
}
713+
else if ( mode == QLatin1String( "feature" ) )
714+
{
715+
dxfSymbologyMode = QgsDxfExport::FeatureSymbology;
716+
}
717+
else
718+
{
719+
std::cerr << "invalid dxf symbology mode " << mode.toStdString() << std::endl;
720+
return 2;
721+
}
705722
}
706-
else if ( mode == QLatin1String( "symbollayer" ) )
723+
else if ( arg == QLatin1String( "--dxf-scale-denom" ) )
707724
{
708-
dxfSymbologyMode = QgsDxfExport::SymbolLayerSymbology;
725+
bool ok;
726+
QString scale( args[++i] );
727+
dxfScale = scale.toDouble( &ok );
728+
if ( !ok )
729+
{
730+
std::cerr << "invalid dxf scale " << scale.toStdString() << std::endl;
731+
return 2;
732+
}
709733
}
710-
else if ( mode == QLatin1String( "feature" ) )
734+
else if ( arg == QLatin1String( "--dxf-encoding" ) )
711735
{
712-
dxfSymbologyMode = QgsDxfExport::FeatureSymbology;
736+
dxfEncoding = args[++i];
713737
}
714-
else
738+
else if ( arg == QLatin1String( "--dxf-map-theme" ) )
715739
{
716-
std::cerr << "invalid dxf symbology mode " << mode.toStdString() << std::endl;
717-
return 2;
740+
dxfMapTheme = args[++i];
718741
}
719-
}
720-
else if ( arg == QLatin1String( "--dxf-scale-denom" ) )
721-
{
722-
bool ok;
723-
QString scale( args[++i] );
724-
dxfScale = scale.toDouble( &ok );
725-
if ( !ok )
742+
#ifdef HAVE_OPENCL
743+
else if ( arg == QLatin1String( "--openclprogramfolder" ) )
726744
{
727-
std::cerr << "invalid dxf scale " << scale.toStdString() << std::endl;
728-
return 2;
745+
openClProgramFolder = args[++i];
729746
}
730-
}
731-
else if ( arg == QLatin1String( "--dxf-encoding" ) )
732-
{
733-
dxfEncoding = args[++i];
734-
}
735-
else if ( arg == QLatin1String( "--dxf-map-theme" ) )
736-
{
737-
dxfMapTheme = args[++i];
738-
}
739-
#ifdef HAVE_OPENCL
740-
else if ( arg == QLatin1String( "--openclprogramfolder" ) )
741-
{
742-
openClProgramFolder = args[++i];
743-
}
744747
#endif
745-
else if ( arg == QLatin1String( "--" ) )
746-
{
747-
for ( i++; i < args.size(); ++i )
748+
else if ( arg == QLatin1String( "--" ) )
749+
{
750+
for ( i++; i < args.size(); ++i )
751+
sFileList.append( QDir::toNativeSeparators( QFileInfo( args[i] ).absoluteFilePath() ) );
752+
}
753+
else
754+
{
748755
sFileList.append( QDir::toNativeSeparators( QFileInfo( args[i] ).absoluteFilePath() ) );
749-
}
750-
else
751-
{
752-
sFileList.append( QDir::toNativeSeparators( QFileInfo( args[i] ).absoluteFilePath() ) );
756+
}
753757
}
754758
}
755759
}
@@ -775,10 +779,9 @@ int main( int argc, char *argv[] )
775779
}
776780
}
777781

778-
779782
/////////////////////////////////////////////////////////////////////
780783
// Now we have the handlers for the different behaviors...
781-
////////////////////////////////////////////////////////////////////
784+
/////////////////////////////////////////////////////////////////////
782785

783786
/////////////////////////////////////////////////////////////////////
784787
// Initialize the application and the translation stuff
@@ -814,74 +817,7 @@ int main( int argc, char *argv[] )
814817
QCoreApplication::setAttribute( Qt::AA_DisableWindowContextHelpButton, true );
815818
#endif
816819

817-
/* Translation file for QGIS.
818-
*/
819-
QString i18nPath = QgsApplication::i18nPath();
820-
QgsSettings mySettings;
821-
QString myUserTranslation = mySettings.value( QStringLiteral( "locale/userLocale" ), "" ).toString();
822-
QString myGlobalLocale = mySettings.value( QStringLiteral( "locale/globalLocale" ), "" ).toString();
823-
bool myShowGroupSeparatorFlag = false; // Default to false
824-
bool myLocaleOverrideFlag = mySettings.value( QStringLiteral( "locale/overrideFlag" ), false ).toBool();
825-
826-
// Override Show Group Separator if the global override flag is set
827-
if ( myLocaleOverrideFlag )
828-
{
829-
// Default to false again
830-
myShowGroupSeparatorFlag = mySettings.value( QStringLiteral( "locale/showGroupSeparator" ), false ).toBool();
831-
}
832-
833-
//
834-
// Priority of translation is:
835-
// - command line
836-
// - user specified in options dialog (with group checked on)
837-
// - system locale
838-
//
839-
// When specifying from the command line it will change the user
840-
// specified user locale
841-
//
842-
if ( !translationCode.isNull() && !translationCode.isEmpty() )
843-
{
844-
mySettings.setValue( QStringLiteral( "locale/userLocale" ), translationCode );
845-
}
846-
else
847-
{
848-
if ( !myLocaleOverrideFlag || myUserTranslation.isEmpty() )
849-
{
850-
translationCode = QLocale().name();
851-
//setting the locale/userLocale when the --lang= option is not set will allow third party
852-
//plugins to always use the same locale as the QGIS, otherwise they can be out of sync
853-
mySettings.setValue( QStringLiteral( "locale/userLocale" ), translationCode );
854-
}
855-
else
856-
{
857-
translationCode = myUserTranslation;
858-
}
859-
}
860-
861-
// Global locale settings
862-
if ( myLocaleOverrideFlag && ! myGlobalLocale.isEmpty( ) )
863-
{
864-
QLocale currentLocale( myGlobalLocale );
865-
QLocale::setDefault( currentLocale );
866-
}
867-
868-
// Number settings
869-
QLocale currentLocale;
870-
if ( myShowGroupSeparatorFlag )
871-
{
872-
currentLocale.setNumberOptions( currentLocale.numberOptions() &= ~QLocale::NumberOption::OmitGroupSeparator );
873-
}
874-
else
875-
{
876-
currentLocale.setNumberOptions( currentLocale.numberOptions() |= QLocale::NumberOption::OmitGroupSeparator );
877-
}
878-
QLocale::setDefault( currentLocale );
879-
880-
QgsApplication::setTranslation( translationCode );
881-
882-
QgsApplication myApp( argc, argv, myUseGuiFlag );
883-
884-
// SetUp the QgsSettings Global Settings:
820+
// Set up the QgsSettings Global Settings:
885821
// - use the path specified with --globalsettingsfile path,
886822
// - use the environment if not found
887823
// - use a default location as a fallback
@@ -901,7 +837,7 @@ int main( int argc, char *argv[] )
901837

902838
if ( !globalsettingsfile.isEmpty() )
903839
{
904-
if ( ! QgsSettings::setGlobalSettingsPath( globalsettingsfile ) )
840+
if ( !QgsSettings::setGlobalSettingsPath( globalsettingsfile ) )
905841
{
906842
QgsMessageLog::logMessage( QObject::tr( "Invalid globalsettingsfile path: %1" ).arg( globalsettingsfile ), QStringLiteral( "QGIS" ) );
907843
}
@@ -938,37 +874,95 @@ int main( int argc, char *argv[] )
938874
profileName = profile->name();
939875
delete profile;
940876

941-
// We can't use QgsSettings until this point because the format and
942-
// folder isn't set until profile is fetch.
943-
// Should be cleaned up in future to make this cleaner.
944-
QgsSettings settings;
877+
{
878+
QgsSettings settings;
945879

946-
QgsDebugMsgLevel( QStringLiteral( "User profile details:" ), 2 );
947-
QgsDebugMsgLevel( QStringLiteral( "\t - %1" ).arg( profileName ), 2 );
948-
QgsDebugMsgLevel( QStringLiteral( "\t - %1" ).arg( profileFolder ), 2 );
949-
QgsDebugMsgLevel( QStringLiteral( "\t - %1" ).arg( rootProfileFolder ), 2 );
880+
/* Translation file for QGIS.
881+
*/
882+
QString myUserTranslation = settings.value( QStringLiteral( "locale/userLocale" ), "" ).toString();
883+
QString myGlobalLocale = settings.value( QStringLiteral( "locale/globalLocale" ), "" ).toString();
884+
bool myShowGroupSeparatorFlag = false; // Default to false
885+
bool myLocaleOverrideFlag = settings.value( QStringLiteral( "locale/overrideFlag" ), false ).toBool();
950886

951-
myApp.init( profileFolder );
887+
// Override Show Group Separator if the global override flag is set
888+
if ( myLocaleOverrideFlag )
889+
{
890+
// Default to false again
891+
myShowGroupSeparatorFlag = settings.value( QStringLiteral( "locale/showGroupSeparator" ), false ).toBool();
892+
}
893+
894+
//
895+
// Priority of translation is:
896+
// - command line
897+
// - user specified in options dialog (with group checked on)
898+
// - system locale
899+
//
900+
// When specifying from the command line it will change the user
901+
// specified user locale
902+
//
903+
if ( !translationCode.isNull() && !translationCode.isEmpty() )
904+
{
905+
settings.setValue( QStringLiteral( "locale/userLocale" ), translationCode );
906+
}
907+
else
908+
{
909+
if ( !myLocaleOverrideFlag || myUserTranslation.isEmpty() )
910+
{
911+
translationCode = QLocale().name();
912+
//setting the locale/userLocale when the --lang= option is not set will allow third party
913+
//plugins to always use the same locale as the QGIS, otherwise they can be out of sync
914+
settings.setValue( QStringLiteral( "locale/userLocale" ), translationCode );
915+
}
916+
else
917+
{
918+
translationCode = myUserTranslation;
919+
}
920+
}
921+
922+
// Global locale settings
923+
if ( myLocaleOverrideFlag && ! myGlobalLocale.isEmpty( ) )
924+
{
925+
QLocale currentLocale( myGlobalLocale );
926+
QLocale::setDefault( currentLocale );
927+
}
928+
929+
// Number settings
930+
QLocale currentLocale;
931+
if ( myShowGroupSeparatorFlag )
932+
{
933+
currentLocale.setNumberOptions( currentLocale.numberOptions() &= ~QLocale::NumberOption::OmitGroupSeparator );
934+
}
935+
else
936+
{
937+
currentLocale.setNumberOptions( currentLocale.numberOptions() |= QLocale::NumberOption::OmitGroupSeparator );
938+
}
939+
QLocale::setDefault( currentLocale );
940+
941+
QgsApplication::setTranslation( translationCode );
942+
}
943+
944+
QgsApplication myApp( argc, argv, myUseGuiFlag );
952945

953946
// Settings migration is only supported on the default profile for now.
954947
if ( profileName == "default" )
955948
{
956949
// Note: this flag is ka version number so that we can reset it once we change the version.
957950
// Note2: Is this a good idea can we do it better.
958951

959-
int firstRunVersion = settings.value( QStringLiteral( "migration/firstRunVersionFlag" ), 0 ).toInt();
952+
QgsSettings migSettings;
953+
int firstRunVersion = migSettings.value( QStringLiteral( "migration/firstRunVersionFlag" ), 0 ).toInt();
960954
bool showWelcome = ( firstRunVersion == 0 || Qgis::QGIS_VERSION_INT > firstRunVersion );
961955

962956
std::unique_ptr< QgsVersionMigration > migration( QgsVersionMigration::canMigrate( 20000, Qgis::QGIS_VERSION_INT ) );
963-
if ( migration && ( mySettingsMigrationForce || migration->requiresMigration() ) )
957+
if ( migration && ( settingsMigrationForce || migration->requiresMigration() ) )
964958
{
965959
bool runMigration = true;
966-
if ( !mySettingsMigrationForce && showWelcome )
960+
if ( !settingsMigrationForce && showWelcome )
967961
{
968962
QgsFirstRunDialog dlg;
969963
dlg.exec();
970964
runMigration = dlg.migrateSettings();
971-
settings.setValue( QStringLiteral( "migration/firstRunVersionFlag" ), Qgis::QGIS_VERSION_INT );
965+
migSettings.setValue( QStringLiteral( "migration/firstRunVersionFlag" ), Qgis::QGIS_VERSION_INT );
972966
}
973967

974968
if ( runMigration )
@@ -979,6 +973,18 @@ int main( int argc, char *argv[] )
979973
}
980974
}
981975

976+
// We can't use QgsSettings until this point because the format and
977+
// folder isn't set until profile is fetch.
978+
// Should be cleaned up in future to make this cleaner.
979+
QgsSettings settings;
980+
981+
QgsDebugMsgLevel( QStringLiteral( "User profile details:" ), 2 );
982+
QgsDebugMsgLevel( QStringLiteral( "\t - %1" ).arg( profileName ), 2 );
983+
QgsDebugMsgLevel( QStringLiteral( "\t - %1" ).arg( profileFolder ), 2 );
984+
QgsDebugMsgLevel( QStringLiteral( "\t - %1" ).arg( rootProfileFolder ), 2 );
985+
986+
myApp.init( profileFolder );
987+
982988
// Redefine QgsApplication::libraryPaths as necessary.
983989
// IMPORTANT: Do *after* QgsApplication myApp(...), but *before* Qt uses any plugins,
984990
// e.g. loading splash screen, setting window icon, etc.
@@ -1121,24 +1127,12 @@ int main( int argc, char *argv[] )
11211127
}
11221128
#endif
11231129

1124-
// update any saved setting for older themes to new default 'gis' theme (2013-04-15)
1125-
if ( mySettings.contains( QStringLiteral( "/Themes" ) ) )
1126-
{
1127-
QString theme = mySettings.value( QStringLiteral( "Themes" ), "default" ).toString();
1128-
if ( theme == QLatin1String( "gis" )
1129-
|| theme == QLatin1String( "classic" )
1130-
|| theme == QLatin1String( "nkids" ) )
1131-
{
1132-
mySettings.setValue( QStringLiteral( "Themes" ), QStringLiteral( "default" ) );
1133-
}
1134-
}
1135-
11361130
// custom environment variables
11371131
QMap<QString, QString> systemEnvVars = QgsApplication::systemEnvVars();
1138-
bool useCustomVars = mySettings.value( QStringLiteral( "qgis/customEnvVarsUse" ), QVariant( false ) ).toBool();
1132+
bool useCustomVars = settings.value( QStringLiteral( "qgis/customEnvVarsUse" ), QVariant( false ) ).toBool();
11391133
if ( useCustomVars )
11401134
{
1141-
QStringList customVarsList = mySettings.value( QStringLiteral( "qgis/customEnvVars" ), "" ).toStringList();
1135+
QStringList customVarsList = settings.value( QStringLiteral( "qgis/customEnvVars" ), "" ).toStringList();
11421136
if ( !customVarsList.isEmpty() )
11431137
{
11441138
Q_FOREACH ( const QString &varStr, customVarsList )
@@ -1193,7 +1187,7 @@ int main( int argc, char *argv[] )
11931187

11941188
// Set the application style. If it's not set QT will use the platform style except on Windows
11951189
// as it looks really ugly so we use QPlastiqueStyle.
1196-
QString presetStyle = mySettings.value( QStringLiteral( "qgis/style" ) ).toString();
1190+
QString presetStyle = settings.value( QStringLiteral( "qgis/style" ) ).toString();
11971191
QString activeStyleName = presetStyle;
11981192
if ( activeStyleName.isEmpty() ) // not set, using default style
11991193
{
@@ -1216,7 +1210,7 @@ int main( int argc, char *argv[] )
12161210
if ( !presetStyle.isEmpty() )
12171211
{
12181212
QApplication::setStyle( presetStyle );
1219-
mySettings.setValue( QStringLiteral( "qgis/style" ), QApplication::style()->objectName() );
1213+
settings.setValue( QStringLiteral( "qgis/style" ), QApplication::style()->objectName() );
12201214
}
12211215

12221216
// set authentication database directory
@@ -1233,7 +1227,7 @@ int main( int argc, char *argv[] )
12331227
int h = 300 * qApp->desktop()->logicalDpiY() / 96;
12341228

12351229
QSplashScreen *mypSplash = new QSplashScreen( myPixmap.scaled( w, h, Qt::KeepAspectRatio, Qt::SmoothTransformation ) );
1236-
if ( !myHideSplash && !mySettings.value( QStringLiteral( "qgis/hideSplash" ) ).toBool() )
1230+
if ( !myHideSplash && !settings.value( QStringLiteral( "qgis/hideSplash" ) ).toBool() )
12371231
{
12381232
//for win and linux we can just automask and png transparency areas will be used
12391233
mypSplash->setMask( myPixmap.mask() );
@@ -1242,16 +1236,16 @@ int main( int argc, char *argv[] )
12421236

12431237
// optionally restore default window state
12441238
// use restoreDefaultWindowState setting only if NOT using command line (then it is set already)
1245-
if ( myRestoreDefaultWindowState || mySettings.value( QStringLiteral( "qgis/restoreDefaultWindowState" ), false ).toBool() )
1239+
if ( myRestoreDefaultWindowState || settings.value( QStringLiteral( "qgis/restoreDefaultWindowState" ), false ).toBool() )
12461240
{
12471241
QgsDebugMsg( "Resetting /UI/state settings!" );
1248-
mySettings.remove( QStringLiteral( "/UI/state" ) );
1249-
mySettings.remove( QStringLiteral( "/qgis/restoreDefaultWindowState" ) );
1242+
settings.remove( QStringLiteral( "/UI/state" ) );
1243+
settings.remove( QStringLiteral( "/qgis/restoreDefaultWindowState" ) );
12501244
}
12511245

12521246
// set max. thread count
12531247
// this should be done in QgsApplication::init() but it doesn't know the settings dir.
1254-
QgsApplication::setMaxThreads( mySettings.value( QStringLiteral( "qgis/max_threads" ), -1 ).toInt() );
1248+
QgsApplication::setMaxThreads( settings.value( QStringLiteral( "qgis/max_threads" ), -1 ).toInt() );
12551249

12561250
QgisApp *qgis = new QgisApp( mypSplash, myRestorePlugins, mySkipVersionCheck, rootProfileFolder, profileName ); // "QgisApp" used to find canonical instance
12571251
qgis->setObjectName( QStringLiteral( "QgisApp" ) );

‎src/app/qgsversionmigration.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
---------------------
55
begin : 30.7.2017
6-
copyright : (C) 2017 by nathan
6+
copyright : (C) 2017 by Nathan Woodrow
77
email : woodrow.nathan at gmail dot com
88
***************************************************************************
99
* *
@@ -342,5 +342,5 @@ QPair<QString, QString> Qgs2To3Migration::transformKey( QString fullOldKey, QStr
342342

343343
QString Qgs2To3Migration::migrationFilePath()
344344
{
345-
return QgsApplication::pkgDataPath() + "/resources/2to3migration.txt";
345+
return QgsApplication::resolvePkgPath() + "/resources/2to3migration.txt";
346346
}

‎src/core/qgsapplication.cpp

Lines changed: 88 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ QString ABISYM( QgsApplication::mConfigPath );
106106
bool ABISYM( QgsApplication::mInitialized ) = false;
107107
bool ABISYM( QgsApplication::mRunningFromBuildDir ) = false;
108108
QString ABISYM( QgsApplication::mBuildSourcePath );
109-
#ifdef _MSC_VER
109+
#if defined(_MSC_VER) && !defined(USING_NMAKE) && !defined(USING_NINJA)
110110
QString ABISYM( QgsApplication::mCfgIntDir );
111111
#endif
112112
QString ABISYM( QgsApplication::mBuildOutputPath );
@@ -201,31 +201,7 @@ void QgsApplication::init( QString profileFolder )
201201
qRegisterMetaType<QgsStyle::StyleEntity>( "QgsStyle::StyleEntity" );
202202
qRegisterMetaType<QgsCoordinateReferenceSystem>( "QgsCoordinateReferenceSystem" );
203203

204-
QString prefixPath( getenv( "QGIS_PREFIX_PATH" ) ? getenv( "QGIS_PREFIX_PATH" ) : applicationDirPath() );
205-
// QgsDebugMsg( QString( "prefixPath(): %1" ).arg( prefixPath ) );
206-
207-
// check if QGIS is run from build directory (not the install directory)
208-
QFile f;
209-
// "/../../.." is for Mac bundled app in build directory
210-
Q_FOREACH ( const QString &path, QStringList() << "" << "/.." << "/bin" << "/../../.." )
211-
{
212-
f.setFileName( prefixPath + path + "/qgisbuildpath.txt" );
213-
if ( f.exists() )
214-
break;
215-
}
216-
if ( f.exists() && f.open( QIODevice::ReadOnly ) )
217-
{
218-
ABISYM( mRunningFromBuildDir ) = true;
219-
ABISYM( mBuildSourcePath ) = f.readLine().trimmed();
220-
ABISYM( mBuildOutputPath ) = f.readLine().trimmed();
221-
QgsDebugMsgLevel( QStringLiteral( "Running from build directory!" ), 4 );
222-
QgsDebugMsgLevel( QStringLiteral( "- source directory: %1" ).arg( ABISYM( mBuildSourcePath ).toUtf8().data() ), 4 );
223-
QgsDebugMsgLevel( QStringLiteral( "- output directory of the build: %1" ).arg( ABISYM( mBuildOutputPath ).toUtf8().data() ), 4 );
224-
#ifdef _MSC_VER
225-
ABISYM( mCfgIntDir ) = prefixPath.split( '/', QString::SkipEmptyParts ).last();
226-
qDebug( "- cfg: %s", ABISYM( mCfgIntDir ).toUtf8().data() );
227-
#endif
228-
}
204+
( void ) resolvePkgPath();
229205

230206
if ( ABISYM( mRunningFromBuildDir ) )
231207
{
@@ -257,7 +233,7 @@ void QgsApplication::init( QString profileFolder )
257233
#if defined(Q_OS_MACX) || defined(Q_OS_WIN)
258234
setPrefixPath( applicationDirPath(), true );
259235
#elif defined(ANDROID)
260-
// this is "/data/data/org.qgis.qgis" in android
236+
// this is "/data/data/org.qgis.qgis" in android
261237
QDir myDir( QDir::homePath() );
262238
myDir.cdUp();
263239
QString myPrefix = myDir.absolutePath();
@@ -290,7 +266,6 @@ void QgsApplication::init( QString profileFolder )
290266
setAuthDatabaseDirPath( getenv( "QGIS_AUTH_DB_DIR_PATH" ) );
291267
}
292268

293-
294269
// store system environment variables passed to application, before they are adjusted
295270
QMap<QString, QString> systemEnvVarMap;
296271
QString passfile( QStringLiteral( "QGIS_AUTH_PASSWORD_FILE" ) ); // QString, for comparison
@@ -494,7 +469,10 @@ QString QgsApplication::pluginPath()
494469
}
495470
QString QgsApplication::pkgDataPath()
496471
{
497-
return ABISYM( mPkgDataPath );
472+
if ( ABISYM( mPkgDataPath ).isNull() )
473+
return resolvePkgPath();
474+
else
475+
return ABISYM( mPkgDataPath );
498476
}
499477
QString QgsApplication::defaultThemePath()
500478
{
@@ -656,31 +634,77 @@ void QgsApplication::setThemeName( const QString &themeName )
656634

657635
QString QgsApplication::resolvePkgPath()
658636
{
659-
#if defined(ANDROID)
660-
QString prefixPath( getenv( "QGIS_PREFIX_PATH" ) ? getenv( "QGIS_PREFIX_PATH" ) : QDir::homePath() );
661-
#else
662-
QString prefixPath( getenv( "QGIS_PREFIX_PATH" ) ? getenv( "QGIS_PREFIX_PATH" ) : applicationDirPath() );
637+
static QString appPath;
638+
if ( appPath.isNull() )
639+
{
640+
if ( QCoreApplication::instance() )
641+
{
642+
appPath = applicationDirPath();
643+
}
644+
else
645+
{
646+
qWarning( "Application path not initialized" );
647+
}
648+
649+
// check if QGIS is run from build directory (not the install directory)
650+
QFile f;
651+
// "/../../.." is for Mac bundled app in build directory
652+
Q_FOREACH ( const QString &path, QStringList() << "" << "/.." << "/bin" << "/../../.." )
653+
{
654+
f.setFileName( appPath + path + "/qgisbuildpath.txt" );
655+
if ( f.exists() )
656+
break;
657+
}
658+
if ( f.exists() && f.open( QIODevice::ReadOnly ) )
659+
{
660+
ABISYM( mRunningFromBuildDir ) = true;
661+
ABISYM( mBuildSourcePath ) = f.readLine().trimmed();
662+
ABISYM( mBuildOutputPath ) = f.readLine().trimmed();
663+
QgsDebugMsgLevel( QStringLiteral( "Running from build directory!" ), 4 );
664+
QgsDebugMsgLevel( QStringLiteral( "- source directory: %1" ).arg( ABISYM( mBuildSourcePath ).toUtf8().data() ), 4 );
665+
QgsDebugMsgLevel( QStringLiteral( "- output directory of the build: %1" ).arg( ABISYM( mBuildOutputPath ).toUtf8().data() ), 4 );
666+
#if defined(_MSC_VER) && !defined(USING_NMAKE) && !defined(USING_NINJA)
667+
ABISYM( mCfgIntDir ) = appPath.split( '/', QString::SkipEmptyParts ).last();
668+
qDebug( "- cfg: %s", ABISYM( mCfgIntDir ).toUtf8().data() );
663669
#endif
664-
QFile f;
665-
// "/../../.." is for Mac bundled app in build directory
666-
const QStringList pathPrefixes = QStringList() << "" << "/.." << "/bin" << "/../../..";
667-
for ( const QString &path : pathPrefixes )
668-
{
669-
f.setFileName( prefixPath + path + "/qgisbuildpath.txt" );
670-
QgsDebugMsg( f.fileName() );
671-
if ( f.exists() )
672-
break;
670+
}
673671
}
674672

675-
if ( f.exists() && f.open( QIODevice::ReadOnly ) )
673+
QString prefixPath;
674+
if ( getenv( "QGIS_PREFIX_PATH" ) )
675+
prefixPath = getenv( "QGIS_PREFIX_PATH" );
676+
else
676677
{
677-
QgsDebugMsg( "Running from build dir!" );
678-
return f.readLine().trimmed();
678+
#if defined(ANDROID)
679+
// this is "/data/data/org.qgis.qgis" in android
680+
QDir dir( QDir::homePath() );
681+
dir.cdUp();
682+
prefixPath = dir.absolutePath();
683+
#else
684+
685+
#if defined(Q_OS_MACX) || defined(Q_OS_WIN)
686+
prefixPath = appPath;
687+
#if defined(_MSC_VER)
688+
if ( prefixPath.endsWith( "/bin" ) )
689+
prefixPath.chop( 4 );
690+
#endif
691+
#else
692+
QDir dir( appPath );
693+
// Fix for server which is one level deeper in /usr/lib/cgi-bin
694+
if ( appPath.contains( QStringLiteral( "cgi-bin" ) ) )
695+
{
696+
dir.cdUp();
697+
}
698+
dir.cdUp(); // Go from /usr/bin or /usr/lib (for server) to /usr
699+
prefixPath = dir.absolutePath();
700+
#endif
701+
#endif
679702
}
703+
704+
if ( ABISYM( mRunningFromBuildDir ) )
705+
return ABISYM( mBuildSourcePath );
680706
else
681-
{
682707
return prefixPath + '/' + QStringLiteral( QGIS_DATA_SUBDIR );
683-
}
684708
}
685709

686710
QString QgsApplication::themeName()
@@ -769,56 +793,54 @@ QHash<QString, QString> QgsApplication::uiThemes()
769793

770794
QString QgsApplication::authorsFilePath()
771795
{
772-
return ABISYM( mPkgDataPath ) + QStringLiteral( "/doc/AUTHORS" );
796+
return pkgDataPath() + QStringLiteral( "/doc/AUTHORS" );
773797
}
774798

775799
QString QgsApplication::contributorsFilePath()
776800
{
777-
return ABISYM( mPkgDataPath ) + QStringLiteral( "/doc/CONTRIBUTORS" );
801+
return pkgDataPath() + QStringLiteral( "/doc/CONTRIBUTORS" );
778802
}
779803
QString QgsApplication::developersMapFilePath()
780804
{
781-
return ABISYM( mPkgDataPath ) + QStringLiteral( "/doc/developersmap.html" );
805+
return pkgDataPath() + QStringLiteral( "/doc/developersmap.html" );
782806
}
783807

784808
QString QgsApplication::sponsorsFilePath()
785809
{
786-
return ABISYM( mPkgDataPath ) + QStringLiteral( "/doc/SPONSORS" );
810+
return pkgDataPath() + QStringLiteral( "/doc/SPONSORS" );
787811
}
788812

789813
QString QgsApplication::donorsFilePath()
790814
{
791-
return ABISYM( mPkgDataPath ) + QStringLiteral( "/doc/DONORS" );
815+
return pkgDataPath() + QStringLiteral( "/doc/DONORS" );
792816
}
793817

794818
QString QgsApplication::translatorsFilePath()
795819
{
796-
return ABISYM( mPkgDataPath ) + QStringLiteral( "/doc/TRANSLATORS" );
820+
return pkgDataPath() + QStringLiteral( "/doc/TRANSLATORS" );
797821
}
798822

799823
QString QgsApplication::licenceFilePath()
800824
{
801-
return ABISYM( mPkgDataPath ) + QStringLiteral( "/doc/LICENSE" );
825+
return pkgDataPath() + QStringLiteral( "/doc/LICENSE" );
802826
}
803827

804828
QString QgsApplication::i18nPath()
805829
{
806-
if ( !ABISYM( mInitialized ) )
807-
return resolvePkgPath() + QStringLiteral( "/i18n/" );
808-
else if ( ABISYM( mRunningFromBuildDir ) )
830+
if ( ABISYM( mRunningFromBuildDir ) )
809831
return ABISYM( mBuildOutputPath ) + QStringLiteral( "/i18n/" );
810832
else
811-
return ABISYM( mPkgDataPath ) + QStringLiteral( "/i18n/" );
833+
return pkgDataPath() + QStringLiteral( "/i18n/" );
812834
}
813835

814836
QString QgsApplication::metadataPath()
815837
{
816-
return ABISYM( mPkgDataPath ) + QStringLiteral( "/resources/metadata-ISO/" );
838+
return pkgDataPath() + QStringLiteral( "/resources/metadata-ISO/" );
817839
}
818840

819841
QString QgsApplication::qgisMasterDatabaseFilePath()
820842
{
821-
return ABISYM( mPkgDataPath ) + QStringLiteral( "/resources/qgis.db" );
843+
return pkgDataPath() + QStringLiteral( "/resources/qgis.db" );
822844
}
823845

824846
QString QgsApplication::qgisSettingsDirPath()
@@ -843,7 +865,7 @@ QString QgsApplication::splashPath()
843865

844866
QString QgsApplication::iconsPath()
845867
{
846-
return ABISYM( mPkgDataPath ) + QStringLiteral( "/images/icons/" );
868+
return pkgDataPath() + QStringLiteral( "/images/icons/" );
847869
}
848870

849871
QString QgsApplication::srsDatabaseFilePath()
@@ -854,7 +876,7 @@ QString QgsApplication::srsDatabaseFilePath()
854876

855877
if ( !QFile( tempCopy ).exists() )
856878
{
857-
QFile f( ABISYM( mPkgDataPath ) + "/resources/srs.db" );
879+
QFile f( pkgDataPath() + "/resources/srs.db" );
858880
if ( !f.copy( tempCopy ) )
859881
{
860882
qFatal( "Could not create temporary copy" );
@@ -865,7 +887,7 @@ QString QgsApplication::srsDatabaseFilePath()
865887
}
866888
else
867889
{
868-
return ABISYM( mPkgDataPath ) + QStringLiteral( "/resources/srs.db" );
890+
return pkgDataPath() + QStringLiteral( "/resources/srs.db" );
869891
}
870892
}
871893

@@ -1029,17 +1051,17 @@ QString QgsApplication::userThemesFolder()
10291051

10301052
QString QgsApplication::defaultStylePath()
10311053
{
1032-
return ABISYM( mPkgDataPath ) + QStringLiteral( "/resources/symbology-style.xml" );
1054+
return pkgDataPath() + QStringLiteral( "/resources/symbology-style.xml" );
10331055
}
10341056

10351057
QString QgsApplication::defaultThemesFolder()
10361058
{
1037-
return ABISYM( mPkgDataPath ) + QStringLiteral( "/resources/themes" );
1059+
return pkgDataPath() + QStringLiteral( "/resources/themes" );
10381060
}
10391061

10401062
QString QgsApplication::serverResourcesPath()
10411063
{
1042-
return ABISYM( mPkgDataPath ) + QStringLiteral( "/resources/server/" );
1064+
return pkgDataPath() + QStringLiteral( "/resources/server/" );
10431065
}
10441066

10451067
QString QgsApplication::libraryPath()

2 commit comments

Comments
 (2)

nyalldawson commented on Sep 23, 2018

@nyalldawson
Collaborator

@jef-n this commit has broken the Travis build. Can you please fix or revert? (I'd hint that a PR would be more appropriate then direct push to master for changes like this).

nyalldawson commented on Sep 23, 2018

@nyalldawson
Collaborator

Reverted in 469b4b4

Please sign in to comment.