17#include "moc_qgsuserprofilemanager.cpp"
27#include <QStandardPaths>
38 return basePath + QDir::separator() +
"profiles";
43 const QString profileName = defaultProfile.isEmpty() ?
defaultProfileName() : defaultProfile;
59 mRootProfilePath = rootProfileLocation;
64 mSettings.reset(
new QSettings( settingsFile(), QSettings::IniFormat ) );
69 mWatchProfiles = enabled;
70 if ( mWatchProfiles && !mRootProfilePath.isEmpty() && QDir( mRootProfilePath ).exists() )
72 mWatcher.reset(
new QFileSystemWatcher() );
73 mWatcher->addPath( mRootProfilePath );
74 connect( mWatcher.get(), &QFileSystemWatcher::directoryChanged,
this, [
this]
76 emit profilesChanged();
87 return static_cast< bool >( mWatcher.get() );
92 return !mRootProfilePath.isEmpty();
97 const QString defaultName = QStringLiteral(
"default" );
103 if ( !mSettings->contains( QStringLiteral(
"/core/defaultProfile" ) ) || globalSettings.
value( QStringLiteral(
"overrideLocalProfile" ),
false,
QgsSettings::Core ).toBool() )
105 return globalSettings.
value( QStringLiteral(
"defaultProfile" ), defaultName,
QgsSettings::Core ).toString();
107 return mSettings->value( QStringLiteral(
"/core/defaultProfile" ), defaultName ).toString();
112 mSettings->setValue( QStringLiteral(
"/core/defaultProfile" ), name );
123 return mSettings->value( QStringLiteral(
"/core/lastProfile" ), QString() ).toString();
128 mSettings->setValue( QStringLiteral(
"/core/lastProfile" ),
userProfile()->name() );
139 mSettings->setValue( QStringLiteral(
"/core/selectionPolicy" ),
static_cast< int >( policy ) );
145 return QDir( mRootProfilePath ).entryList( QDir::Dirs | QDir::NoDotAndDotDot );
155 const QString profilePath = mRootProfilePath + QDir::separator() + name;
165 const QDir folder( mRootProfilePath + QDir::separator() + name );
166 if ( !folder.exists() )
168 if ( !QDir().mkpath( folder.absolutePath() ) )
170 error.
append( tr(
"Cannot write '%1'" ).arg( folder.absolutePath() ) );
175 QFile qgisPrivateDbFile( folder.absolutePath() + QDir::separator() +
"qgis.db" );
178 if ( !qgisPrivateDbFile.exists() )
182 QFile masterFile( qgisMasterDbFileName );
185 masterFile.copy( qgisPrivateDbFile.fileName() );
189 const QFile::Permissions perms = QFile( qgisPrivateDbFile.fileName() ).permissions();
190 if ( !( perms & QFile::WriteOwner ) )
192 if ( !qgisPrivateDbFile.setPermissions( perms | QFile::WriteOwner ) )
194 error.
append( tr(
"Can not make '%1' user writable" ).arg( qgisPrivateDbFile.fileName() ) );
210 QDir folder( mRootProfilePath + QDir::separator() + name );
213 const bool deleted = folder.removeRecursively();
216 error.
append( ( tr(
"Unable to fully delete user profile folder" ) ) );
225QString QgsUserProfileManager::settingsFile()
const
227 return mRootProfilePath + QDir::separator() +
"profiles.ini";
232 return mSettings.get();
237 return mUserProfile.get();
242#if QT_CONFIG(process)
243 const QString path = QDir::toNativeSeparators( QCoreApplication::applicationFilePath() );
244 QStringList arguments;
245 arguments << QCoreApplication::arguments();
249 arguments.removeFirst();
250 arguments << QStringLiteral(
"--profile" ) << name;
251 QgsDebugMsgLevel( QStringLiteral(
"Starting instance from %1 with %2" ).arg( path ).arg( arguments.join(
" " ) ), 2 );
252 QProcess::startDetached( path, arguments, QDir::toNativeSeparators( QCoreApplication::applicationDirPath() ) );
255 Q_ASSERT(
"Starting the user profile is not supported on the platform" );
261 if ( ! mUserProfile.get() )
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 any error is set.
void append(const QString &message, const QString &tag)
Append new error message.
This class is a composition of two QSettings instances:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
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.
QString rootLocation()
Returns the path to the root profiles location.
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.
QgsUserProfile * profileForName(const QString &name) const
Returns the profile found for a given name.
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.
void initSettings() const
Init the settings from the user folder.
#define QgsDebugMsgLevel(str, level)