Skip to content

Commit 6ecb08c

Browse files
committedSep 24, 2018
Revert "Revert "reorder application initialization (fixes #19879, #19916, followup d679581)""
This reverts commit 469b4b4.
1 parent b4c8f62 commit 6ecb08c

File tree

3 files changed

+354
-334
lines changed

3 files changed

+354
-334
lines changed
 

‎src/app/main.cpp

Lines changed: 264 additions & 266 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" )
@@ -510,7 +510,7 @@ int main( int argc, char *argv[] )
510510
int mySnapshotHeight = 600;
511511

512512
bool myHideSplash = false;
513-
bool mySettingsMigrationForce = false;
513+
bool settingsMigrationForce = false;
514514
bool mySkipVersionCheck = false;
515515
bool hideBrowser = false;
516516
#if defined(ANDROID)
@@ -565,193 +565,201 @@ int main( int argc, char *argv[] )
565565

566566
QStringList args;
567567

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

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

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

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

677-
d = coords[i].toDouble( &ok );
678-
if ( !ok )
673+
if ( coords.size() != 4 )
679674
{
680-
std::cerr << "invalid dxf coordinate " << coords[i].toStdString() << " in extent " << ext.toStdString() << std::endl;
675+
std::cerr << "invalid dxf extent " << ext.toStdString() << std::endl;
681676
return 2;
682677
}
683678

684-
switch ( i )
679+
for ( int i = 0; i < 4; i++ )
685680
{
686-
case 0:
687-
dxfExtent.setXMinimum( d );
688-
break;
689-
case 1:
690-
dxfExtent.setYMinimum( d );
691-
break;
692-
case 2:
693-
dxfExtent.setXMaximum( d );
694-
break;
695-
case 3:
696-
dxfExtent.setYMaximum( d );
697-
break;
681+
bool ok;
682+
double d;
683+
684+
d = coords[i].toDouble( &ok );
685+
if ( !ok )
686+
{
687+
std::cerr << "invalid dxf coordinate " << coords[i].toStdString() << " in extent " << ext.toStdString() << std::endl;
688+
return 2;
689+
}
690+
691+
switch ( i )
692+
{
693+
case 0:
694+
dxfExtent.setXMinimum( d );
695+
break;
696+
case 1:
697+
dxfExtent.setYMinimum( d );
698+
break;
699+
case 2:
700+
dxfExtent.setXMaximum( d );
701+
break;
702+
case 3:
703+
dxfExtent.setYMaximum( d );
704+
break;
705+
}
698706
}
699707
}
700-
}
701-
else if ( arg == QLatin1String( "--dxf-symbology-mode" ) )
702-
{
703-
QString mode( args[++i] );
704-
if ( mode == QLatin1String( "none" ) )
708+
else if ( arg == QLatin1String( "--dxf-symbology-mode" ) )
705709
{
706-
dxfSymbologyMode = QgsDxfExport::NoSymbology;
710+
QString mode( args[++i] );
711+
if ( mode == QLatin1String( "none" ) )
712+
{
713+
dxfSymbologyMode = QgsDxfExport::NoSymbology;
714+
}
715+
else if ( mode == QLatin1String( "symbollayer" ) )
716+
{
717+
dxfSymbologyMode = QgsDxfExport::SymbolLayerSymbology;
718+
}
719+
else if ( mode == QLatin1String( "feature" ) )
720+
{
721+
dxfSymbologyMode = QgsDxfExport::FeatureSymbology;
722+
}
723+
else
724+
{
725+
std::cerr << "invalid dxf symbology mode " << mode.toStdString() << std::endl;
726+
return 2;
727+
}
707728
}
708-
else if ( mode == QLatin1String( "symbollayer" ) )
729+
else if ( arg == QLatin1String( "--dxf-scale-denom" ) )
709730
{
710-
dxfSymbologyMode = QgsDxfExport::SymbolLayerSymbology;
731+
bool ok;
732+
QString scale( args[++i] );
733+
dxfScale = scale.toDouble( &ok );
734+
if ( !ok )
735+
{
736+
std::cerr << "invalid dxf scale " << scale.toStdString() << std::endl;
737+
return 2;
738+
}
711739
}
712-
else if ( mode == QLatin1String( "feature" ) )
740+
else if ( arg == QLatin1String( "--dxf-encoding" ) )
713741
{
714-
dxfSymbologyMode = QgsDxfExport::FeatureSymbology;
742+
dxfEncoding = args[++i];
715743
}
716-
else
744+
else if ( arg == QLatin1String( "--dxf-map-theme" ) )
717745
{
718-
std::cerr << "invalid dxf symbology mode " << mode.toStdString() << std::endl;
719-
return 2;
746+
dxfMapTheme = args[++i];
720747
}
721-
}
722-
else if ( arg == QLatin1String( "--dxf-scale-denom" ) )
723-
{
724-
bool ok;
725-
QString scale( args[++i] );
726-
dxfScale = scale.toDouble( &ok );
727-
if ( !ok )
748+
#ifdef HAVE_OPENCL
749+
else if ( arg == QLatin1String( "--openclprogramfolder" ) )
728750
{
729-
std::cerr << "invalid dxf scale " << scale.toStdString() << std::endl;
730-
return 2;
751+
openClProgramFolder = args[++i];
731752
}
732-
}
733-
else if ( arg == QLatin1String( "--dxf-encoding" ) )
734-
{
735-
dxfEncoding = args[++i];
736-
}
737-
else if ( arg == QLatin1String( "--dxf-map-theme" ) )
738-
{
739-
dxfMapTheme = args[++i];
740-
}
741-
#ifdef HAVE_OPENCL
742-
else if ( arg == QLatin1String( "--openclprogramfolder" ) )
743-
{
744-
openClProgramFolder = args[++i];
745-
}
746753
#endif
747-
else if ( arg == QLatin1String( "--" ) )
748-
{
749-
for ( i++; i < args.size(); ++i )
754+
else if ( arg == QLatin1String( "--" ) )
755+
{
756+
for ( i++; i < args.size(); ++i )
757+
sFileList.append( QDir::toNativeSeparators( QFileInfo( args[i] ).absoluteFilePath() ) );
758+
}
759+
else
760+
{
750761
sFileList.append( QDir::toNativeSeparators( QFileInfo( args[i] ).absoluteFilePath() ) );
751-
}
752-
else
753-
{
754-
sFileList.append( QDir::toNativeSeparators( QFileInfo( args[i] ).absoluteFilePath() ) );
762+
}
755763
}
756764
}
757765
}
@@ -777,10 +785,9 @@ int main( int argc, char *argv[] )
777785
}
778786
}
779787

780-
781788
/////////////////////////////////////////////////////////////////////
782789
// Now we have the handlers for the different behaviors...
783-
////////////////////////////////////////////////////////////////////
790+
/////////////////////////////////////////////////////////////////////
784791

785792
/////////////////////////////////////////////////////////////////////
786793
// Initialize the application and the translation stuff
@@ -816,74 +823,7 @@ int main( int argc, char *argv[] )
816823
QCoreApplication::setAttribute( Qt::AA_DisableWindowContextHelpButton, true );
817824
#endif
818825

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

904844
if ( !globalsettingsfile.isEmpty() )
905845
{
906-
if ( ! QgsSettings::setGlobalSettingsPath( globalsettingsfile ) )
846+
if ( !QgsSettings::setGlobalSettingsPath( globalsettingsfile ) )
907847
{
908848
QgsMessageLog::logMessage( QObject::tr( "Invalid globalsettingsfile path: %1" ).arg( globalsettingsfile ), QStringLiteral( "QGIS" ) );
909849
}
@@ -940,37 +880,95 @@ int main( int argc, char *argv[] )
940880
profileName = profile->name();
941881
delete profile;
942882

943-
// We can't use QgsSettings until this point because the format and
944-
// folder isn't set until profile is fetch.
945-
// Should be cleaned up in future to make this cleaner.
946-
QgsSettings settings;
883+
{
884+
QgsSettings settings;
947885

948-
QgsDebugMsgLevel( QStringLiteral( "User profile details:" ), 2 );
949-
QgsDebugMsgLevel( QStringLiteral( "\t - %1" ).arg( profileName ), 2 );
950-
QgsDebugMsgLevel( QStringLiteral( "\t - %1" ).arg( profileFolder ), 2 );
951-
QgsDebugMsgLevel( QStringLiteral( "\t - %1" ).arg( rootProfileFolder ), 2 );
886+
/* Translation file for QGIS.
887+
*/
888+
QString myUserTranslation = settings.value( QStringLiteral( "locale/userLocale" ), "" ).toString();
889+
QString myGlobalLocale = settings.value( QStringLiteral( "locale/globalLocale" ), "" ).toString();
890+
bool myShowGroupSeparatorFlag = false; // Default to false
891+
bool myLocaleOverrideFlag = settings.value( QStringLiteral( "locale/overrideFlag" ), false ).toBool();
952892

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

955952
// Settings migration is only supported on the default profile for now.
956953
if ( profileName == "default" )
957954
{
958955
// Note: this flag is ka version number so that we can reset it once we change the version.
959956
// Note2: Is this a good idea can we do it better.
960957

961-
int firstRunVersion = settings.value( QStringLiteral( "migration/firstRunVersionFlag" ), 0 ).toInt();
958+
QgsSettings migSettings;
959+
int firstRunVersion = migSettings.value( QStringLiteral( "migration/firstRunVersionFlag" ), 0 ).toInt();
962960
bool showWelcome = ( firstRunVersion == 0 || Qgis::QGIS_VERSION_INT > firstRunVersion );
963961

964962
std::unique_ptr< QgsVersionMigration > migration( QgsVersionMigration::canMigrate( 20000, Qgis::QGIS_VERSION_INT ) );
965-
if ( migration && ( mySettingsMigrationForce || migration->requiresMigration() ) )
963+
if ( migration && ( settingsMigrationForce || migration->requiresMigration() ) )
966964
{
967965
bool runMigration = true;
968-
if ( !mySettingsMigrationForce && showWelcome )
966+
if ( !settingsMigrationForce && showWelcome )
969967
{
970968
QgsFirstRunDialog dlg;
971969
dlg.exec();
972970
runMigration = dlg.migrateSettings();
973-
settings.setValue( QStringLiteral( "migration/firstRunVersionFlag" ), Qgis::QGIS_VERSION_INT );
971+
migSettings.setValue( QStringLiteral( "migration/firstRunVersionFlag" ), Qgis::QGIS_VERSION_INT );
974972
}
975973

976974
if ( runMigration )
@@ -981,6 +979,18 @@ int main( int argc, char *argv[] )
981979
}
982980
}
983981

982+
// We can't use QgsSettings until this point because the format and
983+
// folder isn't set until profile is fetch.
984+
// Should be cleaned up in future to make this cleaner.
985+
QgsSettings settings;
986+
987+
QgsDebugMsgLevel( QStringLiteral( "User profile details:" ), 2 );
988+
QgsDebugMsgLevel( QStringLiteral( "\t - %1" ).arg( profileName ), 2 );
989+
QgsDebugMsgLevel( QStringLiteral( "\t - %1" ).arg( profileFolder ), 2 );
990+
QgsDebugMsgLevel( QStringLiteral( "\t - %1" ).arg( rootProfileFolder ), 2 );
991+
992+
myApp.init( profileFolder );
993+
984994
// Redefine QgsApplication::libraryPaths as necessary.
985995
// IMPORTANT: Do *after* QgsApplication myApp(...), but *before* Qt uses any plugins,
986996
// e.g. loading splash screen, setting window icon, etc.
@@ -1123,24 +1133,12 @@ int main( int argc, char *argv[] )
11231133
}
11241134
#endif
11251135

1126-
// update any saved setting for older themes to new default 'gis' theme (2013-04-15)
1127-
if ( mySettings.contains( QStringLiteral( "/Themes" ) ) )
1128-
{
1129-
QString theme = mySettings.value( QStringLiteral( "Themes" ), "default" ).toString();
1130-
if ( theme == QLatin1String( "gis" )
1131-
|| theme == QLatin1String( "classic" )
1132-
|| theme == QLatin1String( "nkids" ) )
1133-
{
1134-
mySettings.setValue( QStringLiteral( "Themes" ), QStringLiteral( "default" ) );
1135-
}
1136-
}
1137-
11381136
// custom environment variables
11391137
QMap<QString, QString> systemEnvVars = QgsApplication::systemEnvVars();
1140-
bool useCustomVars = mySettings.value( QStringLiteral( "qgis/customEnvVarsUse" ), QVariant( false ) ).toBool();
1138+
bool useCustomVars = settings.value( QStringLiteral( "qgis/customEnvVarsUse" ), QVariant( false ) ).toBool();
11411139
if ( useCustomVars )
11421140
{
1143-
QStringList customVarsList = mySettings.value( QStringLiteral( "qgis/customEnvVars" ), "" ).toStringList();
1141+
QStringList customVarsList = settings.value( QStringLiteral( "qgis/customEnvVars" ), "" ).toStringList();
11441142
if ( !customVarsList.isEmpty() )
11451143
{
11461144
Q_FOREACH ( const QString &varStr, customVarsList )
@@ -1195,7 +1193,7 @@ int main( int argc, char *argv[] )
11951193

11961194
// Set the application style. If it's not set QT will use the platform style except on Windows
11971195
// as it looks really ugly so we use QPlastiqueStyle.
1198-
QString presetStyle = mySettings.value( QStringLiteral( "qgis/style" ) ).toString();
1196+
QString presetStyle = settings.value( QStringLiteral( "qgis/style" ) ).toString();
11991197
QString activeStyleName = presetStyle;
12001198
if ( activeStyleName.isEmpty() ) // not set, using default style
12011199
{
@@ -1218,7 +1216,7 @@ int main( int argc, char *argv[] )
12181216
if ( !presetStyle.isEmpty() )
12191217
{
12201218
QApplication::setStyle( presetStyle );
1221-
mySettings.setValue( QStringLiteral( "qgis/style" ), QApplication::style()->objectName() );
1219+
settings.setValue( QStringLiteral( "qgis/style" ), QApplication::style()->objectName() );
12221220
}
12231221

12241222
// set authentication database directory
@@ -1235,7 +1233,7 @@ int main( int argc, char *argv[] )
12351233
int h = 300 * qApp->desktop()->logicalDpiY() / 96;
12361234

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

12451243
// optionally restore default window state
12461244
// use restoreDefaultWindowState setting only if NOT using command line (then it is set already)
1247-
if ( myRestoreDefaultWindowState || mySettings.value( QStringLiteral( "qgis/restoreDefaultWindowState" ), false ).toBool() )
1245+
if ( myRestoreDefaultWindowState || settings.value( QStringLiteral( "qgis/restoreDefaultWindowState" ), false ).toBool() )
12481246
{
12491247
QgsDebugMsg( "Resetting /UI/state settings!" );
1250-
mySettings.remove( QStringLiteral( "/UI/state" ) );
1251-
mySettings.remove( QStringLiteral( "/qgis/restoreDefaultWindowState" ) );
1248+
settings.remove( QStringLiteral( "/UI/state" ) );
1249+
settings.remove( QStringLiteral( "/qgis/restoreDefaultWindowState" ) );
12521250
}
12531251

12541252
if ( hideBrowser )
12551253
{
1256-
if ( mySettings.value( QStringLiteral( "/Windows/Data Source Manager/tab" ) ).toInt() == 0 )
1257-
mySettings.setValue( QStringLiteral( "/Windows/Data Source Manager/tab" ), 1 );
1258-
mySettings.setValue( QStringLiteral( "/UI/hidebrowser" ), true );
1254+
if ( settings.value( QStringLiteral( "/Windows/Data Source Manager/tab" ) ).toInt() == 0 )
1255+
settings.setValue( QStringLiteral( "/Windows/Data Source Manager/tab" ), 1 );
1256+
settings.setValue( QStringLiteral( "/UI/hidebrowser" ), true );
12591257
}
12601258

12611259
// set max. thread count
12621260
// this should be done in QgsApplication::init() but it doesn't know the settings dir.
1263-
QgsApplication::setMaxThreads( mySettings.value( QStringLiteral( "qgis/max_threads" ), -1 ).toInt() );
1261+
QgsApplication::setMaxThreads( settings.value( QStringLiteral( "qgis/max_threads" ), -1 ).toInt() );
12641262

12651263
QgisApp *qgis = new QgisApp( mypSplash, myRestorePlugins, mySkipVersionCheck, rootProfileFolder, profileName ); // "QgisApp" used to find canonical instance
12661264
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()

0 commit comments

Comments
 (0)
Please sign in to comment.