Skip to content

Commit 00dbc73

Browse files
committedDec 18, 2017
Setup QgsSettings in the correct order
We don't have the location of QGIS.ini until we know the profile so we can't use QgsSettings to get the global settings before this.
1 parent d49a3a2 commit 00dbc73

File tree

1 file changed

+41
-32
lines changed

1 file changed

+41
-32
lines changed
 

‎src/app/main.cpp

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -798,16 +798,47 @@ int main( int argc, char *argv[] )
798798
QCoreApplication::setAttribute( Qt::AA_DisableWindowContextHelpButton, true );
799799
#endif
800800

801-
QgsSettings settings;
801+
// SetUp the QgsSettings Global Settings:
802+
// - use the path specified with --globalsettingsfile path,
803+
// - use the environment if not found
804+
// - use a default location as a fallback
805+
if ( globalsettingsfile.isEmpty() )
806+
{
807+
globalsettingsfile = getenv( "QGIS_GLOBAL_SETTINGS_FILE" );
808+
}
809+
810+
if ( globalsettingsfile.isEmpty() )
811+
{
812+
QString default_globalsettingsfile = QgsApplication::pkgDataPath() + "/qgis_global_settings.ini";
813+
if ( QFile::exists( default_globalsettingsfile ) )
814+
{
815+
globalsettingsfile = default_globalsettingsfile;
816+
}
817+
}
818+
819+
if ( !globalsettingsfile.isEmpty() )
820+
{
821+
if ( ! QgsSettings::setGlobalSettingsPath( globalsettingsfile ) )
822+
{
823+
QgsMessageLog::logMessage( QStringLiteral( "Invalid globalsettingsfile path: %1" ).arg( globalsettingsfile ), QStringLiteral( "QGIS" ) );
824+
}
825+
else
826+
{
827+
QgsMessageLog::logMessage( QStringLiteral( "Successfully loaded globalsettingsfile path: %1" ).arg( globalsettingsfile ), QStringLiteral( "QGIS" ) );
828+
}
829+
}
830+
831+
QSettings *globalSettings = new QSettings( globalsettingsfile, QSettings::IniFormat );
832+
globalSettings->setIniCodec( "UTF-8" );
802833
if ( configLocalStorageLocation.isEmpty() )
803834
{
804835
if ( getenv( "QGIS_CUSTOM_CONFIG_PATH" ) )
805836
{
806837
configLocalStorageLocation = getenv( "QGIS_CUSTOM_CONFIG_PATH" );
807838
}
808-
else if ( settings.contains( QStringLiteral( "profilesPath" ), QgsSettings::Core ) )
839+
else if ( globalSettings->contains( QStringLiteral( "core/profilesPath" ) ) )
809840
{
810-
configLocalStorageLocation = settings.value( QStringLiteral( "profilesPath" ), "", QgsSettings::Core ).toString();
841+
configLocalStorageLocation = globalSettings->value( QStringLiteral( "core/profilesPath" ), "" ).toString();
811842
QgsDebugMsg( QString( "Loading profiles path from global config at %1" ).arg( configLocalStorageLocation ) );
812843
}
813844

@@ -817,6 +848,7 @@ int main( int argc, char *argv[] )
817848
configLocalStorageLocation = QStandardPaths::standardLocations( QStandardPaths::AppDataLocation ).value( 0 );
818849
}
819850
}
851+
delete globalSettings;
820852

821853
QString rootProfileFolder = QgsUserProfileManager::resolveProfilesFolder( configLocalStorageLocation );
822854
QgsUserProfileManager manager( rootProfileFolder );
@@ -825,49 +857,26 @@ int main( int argc, char *argv[] )
825857
profileName = profile->name();
826858
delete profile;
827859

860+
// We can't use QgsSettings until this point because the format and
861+
// folder isn't set until profile is fetch.
862+
// Should be cleaned up in future to make this cleaner.
863+
QgsSettings settings;
864+
828865
QgsDebugMsg( "User profile details:" );
829866
QgsDebugMsg( QString( "\t - %1" ).arg( profileName ) );
830867
QgsDebugMsg( QString( "\t - %1" ).arg( profileFolder ) );
831868
QgsDebugMsg( QString( "\t - %1" ).arg( rootProfileFolder ) );
832869

833870
QgsApplication myApp( argc, argv, myUseGuiFlag, profileFolder );
834871

835-
// SetUp the QgsSettings Global Settings:
836-
// - use the path specified with --globalsettingsfile path,
837-
// - use the environment if not found
838-
// - use a default location as a fallback
839-
if ( globalsettingsfile.isEmpty() )
840-
{
841-
globalsettingsfile = getenv( "QGIS_GLOBAL_SETTINGS_FILE" );
842-
}
843-
if ( globalsettingsfile.isEmpty() )
844-
{
845-
QString default_globalsettingsfile = QgsApplication::pkgDataPath() + "/qgis_global_settings.ini";
846-
if ( QFile::exists( default_globalsettingsfile ) )
847-
{
848-
globalsettingsfile = default_globalsettingsfile;
849-
}
850-
}
851-
if ( !globalsettingsfile.isEmpty() )
852-
{
853-
if ( ! QgsSettings::setGlobalSettingsPath( globalsettingsfile ) )
854-
{
855-
QgsMessageLog::logMessage( QStringLiteral( "Invalid globalsettingsfile path: %1" ).arg( globalsettingsfile ), QStringLiteral( "QGIS" ) );
856-
}
857-
else
858-
{
859-
QgsMessageLog::logMessage( QStringLiteral( "Successfully loaded globalsettingsfile path: %1" ).arg( globalsettingsfile ), QStringLiteral( "QGIS" ) );
860-
}
861-
}
862-
863872
// Settings migration is only supported on the default profile for now.
864873
if ( profileName == "default" )
865874
{
866875
// Note: this flag is ka version number so that we can reset it once we change the version.
867876
// Note2: Is this a good idea can we do it better.
868877

869878
int firstRunVersion = settings.value( QStringLiteral( "migration/firstRunVersionFlag" ), 0 ).toInt();
870-
bool showWelcome = ( firstRunVersion == 0 || Qgis::QGIS_VERSION_INT > firstRunVersion );
879+
bool showWelcome = ( firstRunVersion == 0 || Qgis::QGIS_VERSION_INT > firstRunVersion );
871880

872881
std::unique_ptr< QgsVersionMigration > migration( QgsVersionMigration::canMigrate( 20000, Qgis::QGIS_VERSION_INT ) );
873882
if ( migration && ( mySettingsMigrationForce || migration->requiresMigration() ) )

0 commit comments

Comments
 (0)
Please sign in to comment.