Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add lastProfile / defaultProfile in QgsUserProfileManager
  • Loading branch information
YoannQDQ authored and nyalldawson committed Apr 24, 2023
1 parent 283a8f5 commit e1b6d8f
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 7 deletions.
31 changes: 31 additions & 0 deletions python/core/auto_generated/qgsuserprofilemanager.sip.in
Expand Up @@ -29,6 +29,13 @@ A user profile is all settings and anything that used to be found in .qgis3 in t
%End
public:

enum UserProfileSelectionPolicy
{
LastProfile,
DefaultProfile,
AskUser,
};

QgsUserProfileManager( const QString &rootLocation = QString(), QObject *parent = 0 );
%Docstring
User profile manager used to manage user profiles for the instance of QGIS.
Expand Down Expand Up @@ -141,6 +148,30 @@ with no arguments.
void setDefaultFromActive();
%Docstring
Set the default profile name from the current active profile.
%End

QString lastProfileName() const;
%Docstring
Returns the name of the lastly closed profile. Empty if its the first time QGIS has been run.

.. versionadded:: 3.32
%End


UserProfileSelectionPolicy userProfileSelectionPolicy() const;
%Docstring
Returns the user profile selection policy.

.. versionadded:: 3.32
%End

void setUserProfileSelectionPolicy( UserProfileSelectionPolicy policy );
%Docstring
Sets the user profile selection policy.

:param policy: The policy to use when selecting a user profile.

.. versionadded:: 3.32
%End

QgsUserProfile *profileForName( const QString &name ) const /Factory/;
Expand Down
32 changes: 26 additions & 6 deletions src/app/main.cpp
Expand Up @@ -1009,12 +1009,6 @@ int main( int argc, char *argv[] )
}
}

QString rootProfileFolder = QgsUserProfileManager::resolveProfilesFolder( configLocalStorageLocation );
QgsUserProfileManager manager( rootProfileFolder );
QgsUserProfile *profile = manager.getProfile( profileName, true );
QString profileFolder = profile->folder();
profileName = profile->name();
delete profile;

{
/* Translation file for QGIS.
Expand Down Expand Up @@ -1083,6 +1077,32 @@ int main( int argc, char *argv[] )

QgsApplication myApp( argc, argv, myUseGuiFlag, QString(), QStringLiteral( "desktop" ) );

QString rootProfileFolder = QgsUserProfileManager::resolveProfilesFolder( configLocalStorageLocation );
QgsUserProfileManager manager( rootProfileFolder );

// If profile name was not explicitly set, use the policy to determine which profile to use
if ( profileName.isEmpty() )
{
switch ( manager.userProfileSelectionPolicy() )
{
case QgsUserProfileManager::UserProfileSelectionPolicy::LastProfile:
profileName = manager.lastProfileName();
break;
// case QgsUserProfileManager::UserProfileSelectionPolicy::AskUser:
// profileName = manager.askUserToChooseProfile();
// break;
case QgsUserProfileManager::UserProfileSelectionPolicy::DefaultProfile:
default:
profileName = manager.defaultProfileName();
break;
}
}

QgsUserProfile *profile = manager.getProfile( profileName, true );
QString profileFolder = profile->folder();
profileName = profile->name();
delete profile;

// Set locale to emit QgsApplication's localeChanged signal
QgsApplication::setLocale( QLocale() );

Expand Down
2 changes: 1 addition & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -5660,7 +5660,7 @@ void QgisApp::fileExit()
if ( checkUnsavedLayerEdits() && checkMemoryLayers() && saveDirty() && checkExitBlockers() && checkUnsavedRasterAttributeTableEdits() )
{
closeProject();
userProfileManager()->setDefaultFromActive();
userProfileManager()->updateLastProfileName();

// shouldn't be needed, but from this stage on, we don't want/need ANY map canvas refreshes to take place
mFreezeCount = 1000000;
Expand Down
22 changes: 22 additions & 0 deletions src/core/qgsuserprofilemanager.cpp
Expand Up @@ -117,6 +117,28 @@ void QgsUserProfileManager::setDefaultFromActive()
setDefaultProfileName( userProfile()->name() );
}

QString QgsUserProfileManager::lastProfileName() const
{
return mSettings->value( QStringLiteral( "/core/lastProfile" ), QString() ).toString();
}

void QgsUserProfileManager::updateLastProfileName( )
{
mSettings->setValue( QStringLiteral( "/core/lastProfile" ), userProfile()->name() );
mSettings->sync();
}

QgsUserProfileManager::UserProfileSelectionPolicy QgsUserProfileManager::userProfileSelectionPolicy() const
{
return static_cast< UserProfileSelectionPolicy >( mSettings->value( QStringLiteral( "/core/selectionPolicy" ), 0 ).toInt() );
}

void QgsUserProfileManager::setUserProfileSelectionPolicy( QgsUserProfileManager::UserProfileSelectionPolicy policy )
{
mSettings->setValue( QStringLiteral( "/core/selectionPolicy" ), static_cast< int >( policy ) );
mSettings->sync();
}

QStringList QgsUserProfileManager::allProfiles() const
{
return QDir( mRootProfilePath ).entryList( QDir::Dirs | QDir::NoDotAndDotDot );
Expand Down
32 changes: 32 additions & 0 deletions src/core/qgsuserprofilemanager.h
Expand Up @@ -44,6 +44,13 @@ class CORE_EXPORT QgsUserProfileManager : public QObject

public:

enum UserProfileSelectionPolicy
{
LastProfile = 0, //!< Open the last closed profile (only mode supported prior to QGIS 3.32)
DefaultProfile, //!< Open a specific profile
AskUser, //!< Let the user choose which profile to open
};

/**
* User profile manager used to manage user profiles for the instance of QGIS.
*/
Expand Down Expand Up @@ -142,6 +149,31 @@ class CORE_EXPORT QgsUserProfileManager : public QObject
*/
void setDefaultFromActive();

/**
* Returns the name of the lastly closed profile. Empty if its the first time QGIS has been run.
* \since QGIS 3.32
*/
QString lastProfileName() const;

/**
* Updates the last closed profile name. Called when QGIS is closed.
* \since QGIS 3.32
*/
void updateLastProfileName() SIP_SKIP;

/**
* Returns the user profile selection policy.
* \since QGIS 3.32
*/
UserProfileSelectionPolicy userProfileSelectionPolicy() const;

/**
* Sets the user profile selection policy.
* \param policy The policy to use when selecting a user profile.
* \since QGIS 3.32
*/
void setUserProfileSelectionPolicy( UserProfileSelectionPolicy policy );

/**
* Returns the profile found for a given name.
* \param name The name of the profile to return.
Expand Down

0 comments on commit e1b6d8f

Please sign in to comment.