Commit 176a0e18 authored by Ahmad Samir's avatar Ahmad Samir Committed by Kurt Hindenburg
Browse files

Don't edit the Fallback profile, instead create a new one

This is a first step in simplifying the code; since the Fallback profile
doesn't have a file on disk, it's basically a corner-case that we have to
babysit in various places in the code.

Now when the user tries to "Edit current profile", if it's the Fallback
profile, a new profile is created, with a unique name "Profile 1",
"Profile 2" ...etc. This is similar to using the "New" button in the
ProfileSettings dialog.

(cherry picked from commit 5ab84231)
parent 57f4dd22
......@@ -270,6 +270,19 @@ Profile::Ptr ProfileManager::fallbackProfile() const
return _fallbackProfile;
}
QString ProfileManager::generateUniqueName() const
{
const QStringList existingProfileNames = availableProfileNames();
int nameSuffix = 1;
QString uniqueProfileName;
do {
uniqueProfileName = QStringLiteral("Profile ") + QString::number(nameSuffix);
++nameSuffix;
} while (existingProfileNames.contains(uniqueProfileName));
return uniqueProfileName;
}
QString ProfileManager::saveProfile(const Profile::Ptr &profile)
{
ProfileWriter writer;
......@@ -292,16 +305,7 @@ void ProfileManager::changeProfile(Profile::Ptr profile,
const bool isFallback = profile->isFallback();
const QString origPath = profile->path();
const QStringList existingProfileNames = availableProfileNames();
// Generate a unique profile name
int nameSuffix = 1;
QString uniqueProfileName;
do {
uniqueProfileName = QStringLiteral("Profile ") + QString::number(nameSuffix);
++nameSuffix;
} while (existingProfileNames.contains(uniqueProfileName));
const QString uniqueProfileName = generateUniqueName();
// Don't save a profile with an empty name on disk
persistent = persistent && !profile->name().isEmpty();
......
......@@ -170,6 +170,11 @@ public:
/** Returns the shortcut associated with a particular profile. */
QKeySequence shortcut(Profile::Ptr profile) const;
/**
* Creates a unique name for a new profile, e.g. "Profile 1", "Profile 2" ...etc.
*/
QString generateUniqueName() const;
Q_SIGNALS:
/** Emitted when a profile is added to the manager. */
......
......@@ -923,8 +923,26 @@ void SessionController::editCurrentProfile()
_editProfileDialog->deleteLater();
}
auto profile = SessionManager::instance()->sessionProfile(session());
// Don't edit the Fallback profile, instead create a new one
if (profile->isFallback()) {
auto newProfile = Profile::Ptr(new Profile(profile));
newProfile->clone(profile, true);
const QString uniqueName = ProfileManager::instance()->generateUniqueName();
newProfile->setProperty(Profile::Name, uniqueName);
newProfile->setProperty(Profile::UntranslatedName, uniqueName);
profile = newProfile;
SessionManager::instance()->setSessionProfile(session(), profile);
}
_editProfileDialog = new EditProfileDialog(QApplication::activeWindow());
_editProfileDialog->setProfile(SessionManager::instance()->sessionProfile(session()));
_editProfileDialog->setProfile(profile);
connect(_editProfileDialog, &QDialog::accepted, this, [profile]() {
ProfileManager::instance()->addProfile(profile);
ProfileManager::instance()->changeProfile(profile, profile->setProperties());
});
_editProfileDialog->show();
}
......
Markdown is supported
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