28#include <QStandardPaths>
32#include "moc_qgsuserprofilemanager.cpp"
34using namespace Qt::StringLiterals;
44 return basePath + QDir::separator() +
"profiles";
49 const QString profileName = defaultProfile.isEmpty() ?
defaultProfileName() : defaultProfile;
56 std::unique_ptr< QgsUserProfile > profile =
profileForName( profileName );
58 profile->initSettings();
65 mRootProfilePath = rootProfileLocation;
70 mSettings = std::make_unique<QSettings>( settingsFile(), QSettings::IniFormat );
75 mWatchProfiles = enabled;
76 if ( mWatchProfiles && !mRootProfilePath.isEmpty() && QDir( mRootProfilePath ).exists() )
78 mWatcher = std::make_unique<QFileSystemWatcher>( );
79 mWatcher->addPath( mRootProfilePath );
80 connect( mWatcher.get(), &QFileSystemWatcher::directoryChanged,
this, [
this]
82 emit profilesChanged();
93 return static_cast< bool >( mWatcher.get() );
98 return !mRootProfilePath.isEmpty();
103 const QString defaultName = u
"default"_s;
109 if ( !mSettings->contains( u
"/core/defaultProfile"_s ) || globalSettings.
value( u
"overrideLocalProfile"_s,
false,
QgsSettings::Core ).toBool() )
113 return mSettings->
value( u
"/core/defaultProfile"_s, defaultName ).toString();
118 mSettings->setValue( u
"/core/defaultProfile"_s, name );
129 return mSettings->value( u
"/core/lastProfile"_s, QString() ).toString();
134 mSettings->setValue( u
"/core/lastProfile"_s,
userProfile()->name() );
145 mSettings->setValue( u
"/core/selectionPolicy"_s,
static_cast< int >( policy ) );
151 return QDir( mRootProfilePath ).entryList( QDir::Dirs | QDir::NoDotAndDotDot );
161 const QString profilePath = mRootProfilePath + QDir::separator() + name;
162 return std::make_unique< QgsUserProfile >( profilePath );
171 const QDir folder( mRootProfilePath + QDir::separator() + name );
172 if ( !folder.exists() )
174 if ( !QDir().mkpath( folder.absolutePath() ) )
176 error.
append( tr(
"Cannot write '%1'" ).arg( folder.absolutePath() ) );
181 QFile qgisPrivateDbFile( folder.absolutePath() + QDir::separator() +
"qgis.db" );
184 if ( !qgisPrivateDbFile.exists() )
188 QFile masterFile( qgisMasterDbFileName );
191 if ( !masterFile.copy( qgisPrivateDbFile.fileName() ) )
193 error.
append( tr(
"Could not copy master database to %1" ).arg( qgisPrivateDbFile.fileName() ) );
199 const QFile::Permissions perms = QFile( qgisPrivateDbFile.fileName() ).permissions();
200 if ( !( perms & QFile::WriteOwner ) )
202 if ( !qgisPrivateDbFile.setPermissions( perms | QFile::WriteOwner ) )
204 error.
append( tr(
"Can not make '%1' user writable" ).arg( qgisPrivateDbFile.fileName() ) );
221 QDir folder( mRootProfilePath + QDir::separator() + name );
224 const bool deleted = folder.removeRecursively();
227 error.
append( ( tr(
"Unable to fully delete user profile folder" ) ) );
236QString QgsUserProfileManager::settingsFile()
const
238 return mRootProfilePath + QDir::separator() +
"profiles.ini";
243 return mSettings.get();
248 return mUserProfile.get();
253#if QT_CONFIG(process)
254 const QString path = QDir::toNativeSeparators( QCoreApplication::applicationFilePath() );
255 QStringList arguments;
256 arguments << QCoreApplication::arguments();
260 arguments.removeFirst();
261 arguments << u
"--profile"_s << name;
262 QgsDebugMsgLevel( u
"Starting instance from %1 with %2"_s.arg( path ).arg( arguments.join(
" " ) ), 2 );
263 QProcess::startDetached( path, arguments, QDir::toNativeSeparators( QCoreApplication::applicationDirPath() ) );
266 Q_ASSERT(
"Starting the user profile is not supported on the platform" );
272 if ( ! mUserProfile )
UserProfileSelectionPolicy
User profile selection policy.
static QString qgisMasterDatabaseFilePath()
Returns the path to the master qgis.db file.
A container for error messages.
bool isEmpty() const
Test if no error is set.
void append(const QString &message, const QString &tag)
Append new error message.
Stores settings for use within QGIS.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
std::unique_ptr< QgsUserProfile > profileForName(const QString &name) const
Returns the profile found for a given name.
std::unique_ptr< QgsUserProfile > getProfile(const QString &defaultProfile="default", bool createNew=true, bool initSettings=true)
Returns the profile from the given root profile location.
void setRootLocation(const QString &rootProfileLocation)
Set the root profile location for the profile manager.
QgsError createUserProfile(const QString &name)
Create a user profile given by the name.
void setActiveUserProfile(const QString &profile)
Sets the active profile in the manager.
bool profileExists(const QString &name) const
Check if a profile exists.
QgsUserProfile * userProfile()
The currently active user profile.
void setDefaultProfileName(const QString &name)
Sets the default profile name.
void setDefaultFromActive()
Set the default profile name from the current active profile.
QStringList allProfiles() const
Returns a list of all found profile names.
bool rootLocationIsSet() const
Check if the root location has been set for the manager.
QgsError deleteProfile(const QString &name)
Deletes a profile from the root profiles folder.
QSettings * settings()
Returns the settings for the profile manager.
static QString resolveProfilesFolder(const QString &basePath=QString())
Resolves the profiles folder for the given path.
void setUserProfileSelectionPolicy(Qgis::UserProfileSelectionPolicy policy)
Sets the user profile selection policy.
void setNewProfileNotificationEnabled(bool enabled)
Sets whether the manager should watch for the creation of new user profiles and emit the profilesChan...
QString defaultProfileName() const
Returns the name of the default profile that has been set in .default.
QgsUserProfileManager(const QString &rootLocation=QString(), QObject *parent=nullptr)
User profile manager used to manage user profiles for the instance of QGIS.
Qgis::UserProfileSelectionPolicy userProfileSelectionPolicy() const
Returns the user profile selection policy.
void profilesChanged()
Emitted when the list of profiles is changed.
QString lastProfileName() const
Returns the name of the most recently closed profile.
void updateLastProfileName()
Updates the last closed profile name.
QString rootLocation() const
Returns the path to the root profiles location.
void loadUserProfile(const QString &name)
Starts a new instance of QGIS for the given profile.
bool isNewProfileNotificationEnabled() const
Returns whether the manager is watching for the creation of new user profiles and emitting the profil...
User profile contains information about the user profile folders on the machine.
#define QgsDebugMsgLevel(str, level)