Skip to content

Commit

Permalink
Setup QgsSettings in the correct order
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
NathanW2 committed Dec 18, 2017
1 parent d49a3a2 commit 00dbc73
Showing 1 changed file with 41 additions and 32 deletions.
73 changes: 41 additions & 32 deletions src/app/main.cpp
Expand Up @@ -798,16 +798,47 @@ int main( int argc, char *argv[] )
QCoreApplication::setAttribute( Qt::AA_DisableWindowContextHelpButton, true );
#endif

QgsSettings settings;
// SetUp the QgsSettings Global Settings:
// - use the path specified with --globalsettingsfile path,
// - use the environment if not found
// - use a default location as a fallback
if ( globalsettingsfile.isEmpty() )
{
globalsettingsfile = getenv( "QGIS_GLOBAL_SETTINGS_FILE" );
}

if ( globalsettingsfile.isEmpty() )
{
QString default_globalsettingsfile = QgsApplication::pkgDataPath() + "/qgis_global_settings.ini";
if ( QFile::exists( default_globalsettingsfile ) )
{
globalsettingsfile = default_globalsettingsfile;
}
}

if ( !globalsettingsfile.isEmpty() )
{
if ( ! QgsSettings::setGlobalSettingsPath( globalsettingsfile ) )
{
QgsMessageLog::logMessage( QStringLiteral( "Invalid globalsettingsfile path: %1" ).arg( globalsettingsfile ), QStringLiteral( "QGIS" ) );
}
else
{
QgsMessageLog::logMessage( QStringLiteral( "Successfully loaded globalsettingsfile path: %1" ).arg( globalsettingsfile ), QStringLiteral( "QGIS" ) );
}
}

QSettings *globalSettings = new QSettings( globalsettingsfile, QSettings::IniFormat );
globalSettings->setIniCodec( "UTF-8" );
if ( configLocalStorageLocation.isEmpty() )
{
if ( getenv( "QGIS_CUSTOM_CONFIG_PATH" ) )
{
configLocalStorageLocation = getenv( "QGIS_CUSTOM_CONFIG_PATH" );
}
else if ( settings.contains( QStringLiteral( "profilesPath" ), QgsSettings::Core ) )
else if ( globalSettings->contains( QStringLiteral( "core/profilesPath" ) ) )
{
configLocalStorageLocation = settings.value( QStringLiteral( "profilesPath" ), "", QgsSettings::Core ).toString();
configLocalStorageLocation = globalSettings->value( QStringLiteral( "core/profilesPath" ), "" ).toString();
QgsDebugMsg( QString( "Loading profiles path from global config at %1" ).arg( configLocalStorageLocation ) );
}

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

QString rootProfileFolder = QgsUserProfileManager::resolveProfilesFolder( configLocalStorageLocation );
QgsUserProfileManager manager( rootProfileFolder );
Expand All @@ -825,49 +857,26 @@ 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;

QgsDebugMsg( "User profile details:" );
QgsDebugMsg( QString( "\t - %1" ).arg( profileName ) );
QgsDebugMsg( QString( "\t - %1" ).arg( profileFolder ) );
QgsDebugMsg( QString( "\t - %1" ).arg( rootProfileFolder ) );

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

// SetUp the QgsSettings Global Settings:
// - use the path specified with --globalsettingsfile path,
// - use the environment if not found
// - use a default location as a fallback
if ( globalsettingsfile.isEmpty() )
{
globalsettingsfile = getenv( "QGIS_GLOBAL_SETTINGS_FILE" );
}
if ( globalsettingsfile.isEmpty() )
{
QString default_globalsettingsfile = QgsApplication::pkgDataPath() + "/qgis_global_settings.ini";
if ( QFile::exists( default_globalsettingsfile ) )
{
globalsettingsfile = default_globalsettingsfile;
}
}
if ( !globalsettingsfile.isEmpty() )
{
if ( ! QgsSettings::setGlobalSettingsPath( globalsettingsfile ) )
{
QgsMessageLog::logMessage( QStringLiteral( "Invalid globalsettingsfile path: %1" ).arg( globalsettingsfile ), QStringLiteral( "QGIS" ) );
}
else
{
QgsMessageLog::logMessage( QStringLiteral( "Successfully loaded globalsettingsfile path: %1" ).arg( globalsettingsfile ), QStringLiteral( "QGIS" ) );
}
}

// Settings migration is only supported on the default profile for now.
if ( profileName == "default" )
{
// 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();
bool showWelcome = ( firstRunVersion == 0 || Qgis::QGIS_VERSION_INT > firstRunVersion );
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() ) )
Expand Down

0 comments on commit 00dbc73

Please sign in to comment.