Skip to content

Commit e1b6d8f

Browse files
YoannQDQnyalldawson
authored andcommittedApr 24, 2023
Add lastProfile / defaultProfile in QgsUserProfileManager
1 parent 283a8f5 commit e1b6d8f

File tree

5 files changed

+112
-7
lines changed

5 files changed

+112
-7
lines changed
 

‎python/core/auto_generated/qgsuserprofilemanager.sip.in

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ A user profile is all settings and anything that used to be found in .qgis3 in t
2929
%End
3030
public:
3131

32+
enum UserProfileSelectionPolicy
33+
{
34+
LastProfile,
35+
DefaultProfile,
36+
AskUser,
37+
};
38+
3239
QgsUserProfileManager( const QString &rootLocation = QString(), QObject *parent = 0 );
3340
%Docstring
3441
User profile manager used to manage user profiles for the instance of QGIS.
@@ -141,6 +148,30 @@ with no arguments.
141148
void setDefaultFromActive();
142149
%Docstring
143150
Set the default profile name from the current active profile.
151+
%End
152+
153+
QString lastProfileName() const;
154+
%Docstring
155+
Returns the name of the lastly closed profile. Empty if its the first time QGIS has been run.
156+
157+
.. versionadded:: 3.32
158+
%End
159+
160+
161+
UserProfileSelectionPolicy userProfileSelectionPolicy() const;
162+
%Docstring
163+
Returns the user profile selection policy.
164+
165+
.. versionadded:: 3.32
166+
%End
167+
168+
void setUserProfileSelectionPolicy( UserProfileSelectionPolicy policy );
169+
%Docstring
170+
Sets the user profile selection policy.
171+
172+
:param policy: The policy to use when selecting a user profile.
173+
174+
.. versionadded:: 3.32
144175
%End
145176

146177
QgsUserProfile *profileForName( const QString &name ) const /Factory/;

‎src/app/main.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,12 +1009,6 @@ int main( int argc, char *argv[] )
10091009
}
10101010
}
10111011

1012-
QString rootProfileFolder = QgsUserProfileManager::resolveProfilesFolder( configLocalStorageLocation );
1013-
QgsUserProfileManager manager( rootProfileFolder );
1014-
QgsUserProfile *profile = manager.getProfile( profileName, true );
1015-
QString profileFolder = profile->folder();
1016-
profileName = profile->name();
1017-
delete profile;
10181012

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

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

1080+
QString rootProfileFolder = QgsUserProfileManager::resolveProfilesFolder( configLocalStorageLocation );
1081+
QgsUserProfileManager manager( rootProfileFolder );
1082+
1083+
// If profile name was not explicitly set, use the policy to determine which profile to use
1084+
if ( profileName.isEmpty() )
1085+
{
1086+
switch ( manager.userProfileSelectionPolicy() )
1087+
{
1088+
case QgsUserProfileManager::UserProfileSelectionPolicy::LastProfile:
1089+
profileName = manager.lastProfileName();
1090+
break;
1091+
// case QgsUserProfileManager::UserProfileSelectionPolicy::AskUser:
1092+
// profileName = manager.askUserToChooseProfile();
1093+
// break;
1094+
case QgsUserProfileManager::UserProfileSelectionPolicy::DefaultProfile:
1095+
default:
1096+
profileName = manager.defaultProfileName();
1097+
break;
1098+
}
1099+
}
1100+
1101+
QgsUserProfile *profile = manager.getProfile( profileName, true );
1102+
QString profileFolder = profile->folder();
1103+
profileName = profile->name();
1104+
delete profile;
1105+
10861106
// Set locale to emit QgsApplication's localeChanged signal
10871107
QgsApplication::setLocale( QLocale() );
10881108

‎src/app/qgisapp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5660,7 +5660,7 @@ void QgisApp::fileExit()
56605660
if ( checkUnsavedLayerEdits() && checkMemoryLayers() && saveDirty() && checkExitBlockers() && checkUnsavedRasterAttributeTableEdits() )
56615661
{
56625662
closeProject();
5663-
userProfileManager()->setDefaultFromActive();
5663+
userProfileManager()->updateLastProfileName();
56645664

56655665
// shouldn't be needed, but from this stage on, we don't want/need ANY map canvas refreshes to take place
56665666
mFreezeCount = 1000000;

‎src/core/qgsuserprofilemanager.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,28 @@ void QgsUserProfileManager::setDefaultFromActive()
117117
setDefaultProfileName( userProfile()->name() );
118118
}
119119

120+
QString QgsUserProfileManager::lastProfileName() const
121+
{
122+
return mSettings->value( QStringLiteral( "/core/lastProfile" ), QString() ).toString();
123+
}
124+
125+
void QgsUserProfileManager::updateLastProfileName( )
126+
{
127+
mSettings->setValue( QStringLiteral( "/core/lastProfile" ), userProfile()->name() );
128+
mSettings->sync();
129+
}
130+
131+
QgsUserProfileManager::UserProfileSelectionPolicy QgsUserProfileManager::userProfileSelectionPolicy() const
132+
{
133+
return static_cast< UserProfileSelectionPolicy >( mSettings->value( QStringLiteral( "/core/selectionPolicy" ), 0 ).toInt() );
134+
}
135+
136+
void QgsUserProfileManager::setUserProfileSelectionPolicy( QgsUserProfileManager::UserProfileSelectionPolicy policy )
137+
{
138+
mSettings->setValue( QStringLiteral( "/core/selectionPolicy" ), static_cast< int >( policy ) );
139+
mSettings->sync();
140+
}
141+
120142
QStringList QgsUserProfileManager::allProfiles() const
121143
{
122144
return QDir( mRootProfilePath ).entryList( QDir::Dirs | QDir::NoDotAndDotDot );

‎src/core/qgsuserprofilemanager.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ class CORE_EXPORT QgsUserProfileManager : public QObject
4444

4545
public:
4646

47+
enum UserProfileSelectionPolicy
48+
{
49+
LastProfile = 0, //!< Open the last closed profile (only mode supported prior to QGIS 3.32)
50+
DefaultProfile, //!< Open a specific profile
51+
AskUser, //!< Let the user choose which profile to open
52+
};
53+
4754
/**
4855
* User profile manager used to manage user profiles for the instance of QGIS.
4956
*/
@@ -142,6 +149,31 @@ class CORE_EXPORT QgsUserProfileManager : public QObject
142149
*/
143150
void setDefaultFromActive();
144151

152+
/**
153+
* Returns the name of the lastly closed profile. Empty if its the first time QGIS has been run.
154+
* \since QGIS 3.32
155+
*/
156+
QString lastProfileName() const;
157+
158+
/**
159+
* Updates the last closed profile name. Called when QGIS is closed.
160+
* \since QGIS 3.32
161+
*/
162+
void updateLastProfileName() SIP_SKIP;
163+
164+
/**
165+
* Returns the user profile selection policy.
166+
* \since QGIS 3.32
167+
*/
168+
UserProfileSelectionPolicy userProfileSelectionPolicy() const;
169+
170+
/**
171+
* Sets the user profile selection policy.
172+
* \param policy The policy to use when selecting a user profile.
173+
* \since QGIS 3.32
174+
*/
175+
void setUserProfileSelectionPolicy( UserProfileSelectionPolicy policy );
176+
145177
/**
146178
* Returns the profile found for a given name.
147179
* \param name The name of the profile to return.

0 commit comments

Comments
 (0)
Please sign in to comment.