ProfileManager dialog: improve handling profiles

- Differentiate between a profile being deleteable and writable:
  - Disable the Edit key for read-only profiles, i.e. a '.profile' that
    doesn't have write permissions for the user
  - Disable the Delete key for a '.profile' that is in a directory that
    isn't writable for the user (i.e. a .profile can be read-only, but
    still deleteable by the user)

- Change the model selection mode to single selection, that simplifies
  the code, besides it looks like editing more than one profile at the
  same time hasn't worked for a while, and there are no complaints AFAICS;
  remove the now redundant selectedProfiles() method.
wilder
Ahmad Samir 5 years ago committed by Kurt Hindenburg
parent 91122e531d
commit 6cb301bdbe
  1. 107
      src/settings/ProfileSettings.cpp
  2. 3
      src/settings/ProfileSettings.h
  3. 6
      src/settings/ProfileSettings.ui

@ -32,6 +32,7 @@ ProfileSettings::ProfileSettings(QWidget* parent)
profilesList->setModel(ProfileModel::instance()); profilesList->setModel(ProfileModel::instance());
profilesList->setItemDelegateForColumn(ProfileModel::SHORTCUT, new ShortcutItemDelegate(this)); profilesList->setItemDelegateForColumn(ProfileModel::SHORTCUT, new ShortcutItemDelegate(this));
profilesList->setSelectionMode(QAbstractItemView::SingleSelection);
// double clicking the profile name opens the profile edit dialog // double clicking the profile name opens the profile edit dialog
connect(profilesList, &QAbstractItemView::doubleClicked, this, &Konsole::ProfileSettings::doubleClicked); connect(profilesList, &QAbstractItemView::doubleClicked, this, &Konsole::ProfileSettings::doubleClicked);
@ -61,7 +62,6 @@ void ProfileSettings::slotAccepted()
void ProfileSettings::doubleClicked(const QModelIndex &idx) void ProfileSettings::doubleClicked(const QModelIndex &idx)
{ {
if (idx.column() == ProfileModel::NAME) { if (idx.column() == ProfileModel::NAME) {
editSelected(); editSelected();
} }
@ -92,32 +92,28 @@ void ProfileSettings::populateTable()
void ProfileSettings::tableSelectionChanged(const QItemSelection&) void ProfileSettings::tableSelectionChanged(const QItemSelection&)
{ {
const ProfileManager* manager = ProfileManager::instance(); const auto profile = currentProfile();
bool isNotDefault = true; const bool isNotDefault = profile != ProfileManager::instance()->defaultProfile();
bool isDeletable = true;
newProfileButton->setEnabled(true);
const auto profiles = selectedProfiles();
for (const auto &profile: profiles) { // See comment about isProfileWritable(profile) in editSelected()
isNotDefault = isNotDefault && (profile != manager->defaultProfile()); editProfileButton->setEnabled(isProfileWritable(profile));
isDeletable = isDeletable && isProfileDeletable(profile);
}
newProfileButton->setEnabled(profiles.count() < 2); // Do not allow the current default profile of the session to be removed
// FIXME: At some point editing 2+ profiles no longer works deleteProfileButton->setEnabled(isNotDefault && isProfileDeletable(profile));
editProfileButton->setEnabled(profiles.count() == 1);
// do not allow the default session type to be removed setAsDefaultButton->setEnabled(isNotDefault);
deleteProfileButton->setEnabled(isDeletable && isNotDefault && (profiles.count() > 0));
setAsDefaultButton->setEnabled(isNotDefault && (profiles.count() == 1));
} }
void ProfileSettings::deleteSelected() void ProfileSettings::deleteSelected()
{ {
const QList<Profile::Ptr> profiles = selectedProfiles(); const auto profile = currentProfile();
for (const Profile::Ptr &profile : profiles) {
if (profile != ProfileManager::instance()->defaultProfile()) { // The "Delete" button is disabled for the current default profile
ProfileManager::instance()->deleteProfile(profile); Q_ASSERT(profile != ProfileManager::instance()->defaultProfile());
}
} ProfileManager::instance()->deleteProfile(profile);
} }
void ProfileSettings::setSelectedAsDefault() void ProfileSettings::setSelectedAsDefault()
@ -156,54 +152,40 @@ void ProfileSettings::createProfile()
} }
void ProfileSettings::editSelected() void ProfileSettings::editSelected()
{ {
const QList<Profile::Ptr> profiles = selectedProfiles(); const auto profile = currentProfile();
// Read-only profiles, i.e. oens with .profile's that aren't writable
// for the user aren't editable, only clone-able by using the "New"
// button, this includes the Default/fallback profile, which is hardcoded.
if (!isProfileWritable(profile)) {
return;
}
EditProfileDialog *profileDialog = nullptr; EditProfileDialog *profileDialog = nullptr;
// sessions() returns a const QList const auto sessionsList = SessionManager::instance()->sessions();
for (const Session *session : SessionManager::instance()->sessions()) { for (const Session *session : sessionsList) {
for (TerminalDisplay *terminalDisplay : session->views()) { for (TerminalDisplay *terminalDisplay : session->views()) {
// Searching for opened profiles // Searching for already open EditProfileDialog instances
// for this profile
profileDialog = terminalDisplay->sessionController()->profileDialogPointer(); profileDialog = terminalDisplay->sessionController()->profileDialogPointer();
if (profileDialog == nullptr) { if (profileDialog == nullptr) {
continue; continue;
} }
for (const Profile::Ptr &profile : profiles) {
if (profile->name() == profileDialog->lookupProfile()->name() if (profile->name() == profileDialog->lookupProfile()->name()
&& profileDialog->isVisible()) { && profileDialog->isVisible()) {
// close opened edit dialog // close opened edit dialog
profileDialog->close(); profileDialog->close();
}
} }
} }
} }
EditProfileDialog dialog(this); EditProfileDialog dialog(this);
// the dialog will delete the profile group when it is destroyed
ProfileGroup* group = new ProfileGroup;
for (const Profile::Ptr &profile : profiles) {
group->addProfile(profile);
}
group->updateValues();
dialog.setProfile(Profile::Ptr(group)); dialog.setProfile(profile);
dialog.exec(); dialog.exec();
} }
QList<Profile::Ptr> ProfileSettings::selectedProfiles() const
{
QList<Profile::Ptr> list;
QItemSelectionModel* selection = profilesList->selectionModel();
if (selection == nullptr) {
return list;
}
const QList<QModelIndex> selectedIndexes = selection->selectedIndexes();
for (const QModelIndex &index : selectedIndexes) {
if (index.column() == ProfileModel::PROFILE) {
list << index.data(ProfileModel::ProfilePtrRole).value<Profile::Ptr>();
}
}
return list;
}
Profile::Ptr ProfileSettings::currentProfile() const Profile::Ptr ProfileSettings::currentProfile() const
{ {
QItemSelectionModel* selection = profilesList->selectionModel(); QItemSelectionModel* selection = profilesList->selectionModel();
@ -221,17 +203,20 @@ Profile::Ptr ProfileSettings::currentProfile() const
bool ProfileSettings::isProfileDeletable(Profile::Ptr profile) const bool ProfileSettings::isProfileDeletable(Profile::Ptr profile) const
{ {
if (!profile) { if (!profile || profile->isFallback()) {
return false; return false;
} }
const QFileInfo fileInfo(profile->path()); const QFileInfo fileInfo(profile->path());
if (!fileInfo.exists()) { return fileInfo.exists()
return false; && QFileInfo(fileInfo.path()).isWritable(); // To delete a file, parent dir must be writable
} }
const QFileInfo dirInfo(fileInfo.path()); bool ProfileSettings::isProfileWritable(Profile::Ptr profile) const
return dirInfo.isWritable(); {
return profile
&& !profile->isFallback() // Default/fallback profile is hardcoded
&& QFileInfo(profile->path()).isWritable();
} }
void ProfileSettings::setShortcutEditorVisible(bool visible) void ProfileSettings::setShortcutEditorVisible(bool visible)

@ -71,8 +71,9 @@ private Q_SLOTS:
private: private:
QExplicitlySharedDataPointer<Profile> currentProfile() const; QExplicitlySharedDataPointer<Profile> currentProfile() const;
QList<QExplicitlySharedDataPointer<Profile>> selectedProfiles() const;
bool isProfileDeletable(QExplicitlySharedDataPointer<Profile> profile) const; bool isProfileDeletable(QExplicitlySharedDataPointer<Profile> profile) const;
bool isProfileWritable(QExplicitlySharedDataPointer<Profile> profile) const;
// updates the profile table to be in sync with the // updates the profile table to be in sync with the
// session manager // session manager

@ -36,7 +36,7 @@
<item> <item>
<widget class="QPushButton" name="newProfileButton"> <widget class="QPushButton" name="newProfileButton">
<property name="toolTip"> <property name="toolTip">
<string>Create a new profile based upon the selected profile</string> <string>Create a new profile based on the selected profile</string>
</property> </property>
<property name="text"> <property name="text">
<string>&amp;New...</string> <string>&amp;New...</string>
@ -49,7 +49,7 @@
<bool>false</bool> <bool>false</bool>
</property> </property>
<property name="toolTip"> <property name="toolTip">
<string>Edit the selected profile(s)</string> <string>Edit the selected profile (this button is disabled for read-only profiles)</string>
</property> </property>
<property name="text"> <property name="text">
<string>&amp;Edit...</string> <string>&amp;Edit...</string>
@ -62,7 +62,7 @@
<bool>false</bool> <bool>false</bool>
</property> </property>
<property name="toolTip"> <property name="toolTip">
<string>Delete the selected profile(s)</string> <string>Delete the selected profile</string>
</property> </property>
<property name="text"> <property name="text">
<string>&amp;Remove</string> <string>&amp;Remove</string>

Loading…
Cancel
Save