Commit 87e25c62 authored by Jose Flores's avatar Jose Flores Committed by Kurt Hindenburg
Browse files

Checks if a profile is editable before showing the edit profile menu action

parent 7c508753
Pipeline #238742 passed with stage
in 5 minutes and 43 seconds
......@@ -11,6 +11,7 @@
#include "Profile.h"
// Qt
#include <QFileInfo>
#include <QTextCodec>
// KDE
......@@ -250,6 +251,20 @@ bool Profile::isBuiltin() const
return path() == BUILTIN_MAGIC_PATH;
}
bool Profile::isEditable() const
{
// Read-only profiles (i.e. with non-user-writable .profile location)
// aren't editable. This includes the built-in profile, which is hardcoded.
return !isBuiltin() && QFileInfo(path()).isWritable();
}
bool Profile::isDeletable() const
{
// To delete a file, parent dir must be writable
const QFileInfo fileInfo(path());
return !isBuiltin() && fileInfo.exists() && QFileInfo(fileInfo.path()).isWritable();
}
bool Profile::isHidden() const
{
return _hidden;
......
......@@ -474,6 +474,16 @@ public:
*/
bool isBuiltin() const;
/**
* Returns true if this profile is editable.
*/
bool isEditable() const;
/**
* Returns true if this profile can be deleted.
*/
bool isDeletable() const;
/**
* Returns true if this is a 'hidden' profile which should not be
* displayed in menus or saved to disk.
......
......@@ -946,7 +946,7 @@ void SessionController::switchProfile(const Profile::Ptr &profile)
void SessionController::setEditProfileActionText(const Profile::Ptr &profile)
{
QAction *action = actionCollection()->action(QStringLiteral("edit-current-profile"));
if (profile->isBuiltin()) {
if (!profile->isEditable()) {
action->setText(i18n("Create New Profile..."));
} else {
action->setText(i18n("Edit Current Profile..."));
......@@ -981,8 +981,8 @@ void SessionController::editCurrentProfile()
auto profile = SessionManager::instance()->sessionProfile(session());
auto state = EditProfileDialog::ExistingProfile;
// Don't edit the built-in profile, instead create a new one
if (profile->isBuiltin()) {
// Don't edit uneditable profiles, instead create a new one
if (!profile->isEditable()) {
auto newProfile = Profile::Ptr(new Profile(profile));
newProfile->clone(profile, true);
const QString uniqueName = ProfileManager::instance()->generateUniqueName();
......
......@@ -101,11 +101,10 @@ void ProfileSettings::tableSelectionChanged(const QItemSelection &selected)
const auto profile = currentProfile();
const bool isNotDefault = profile != ProfileManager::instance()->defaultProfile();
// See comment about isProfileWritable(profile) in editSelected()
editProfileButton->setEnabled(isProfileWritable(profile));
editProfileButton->setEnabled(profile && profile->isEditable());
// Do not allow the current default profile of the session to be removed
deleteProfileButton->setEnabled(isNotDefault && isProfileDeletable(profile));
deleteProfileButton->setEnabled(profile && isNotDefault && profile->isDeletable());
setAsDefaultButton->setEnabled(isNotDefault);
}
......@@ -154,10 +153,8 @@ void ProfileSettings::editSelected()
{
const auto profile = currentProfile();
// Read-only profiles (i.e. with non-user-writable .profile location)
// aren't editable, and can only be cloned using the "New" button.
// This includes the built-in profile, which is hardcoded.
if (!isProfileWritable(profile)) {
// Uneditable profiles can only be cloned using the "New" button.
if (!(profile && profile->isEditable())) {
return;
}
......@@ -178,19 +175,3 @@ Profile::Ptr ProfileSettings::currentProfile() const
return selection->selectedIndexes().at(ProfileModel::PROFILE).data(ProfileModel::ProfilePtrRole).value<Profile::Ptr>();
}
bool ProfileSettings::isProfileDeletable(Profile::Ptr profile) const
{
if (!profile || profile->isBuiltin()) {
return false;
}
const QFileInfo fileInfo(profile->path());
return fileInfo.exists() && QFileInfo(fileInfo.path()).isWritable(); // To delete a file, parent dir must be writable
}
bool ProfileSettings::isProfileWritable(Profile::Ptr profile) const
{
return profile && !profile->isBuiltin() // Built-in profile is hardcoded and never stored.
&& QFileInfo(profile->path()).isWritable();
}
......@@ -59,9 +59,6 @@ private Q_SLOTS:
private:
QExplicitlySharedDataPointer<Profile> currentProfile() const;
bool isProfileDeletable(QExplicitlySharedDataPointer<Profile> profile) const;
bool isProfileWritable(QExplicitlySharedDataPointer<Profile> profile) const;
// updates the profile table to be in sync with the
// session manager
void populateTable();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment