diff --git a/src/autotests/ProfileTest.cpp b/src/autotests/ProfileTest.cpp index 96ec0858..d969ad1c 100644 --- a/src/autotests/ProfileTest.cpp +++ b/src/autotests/ProfileTest.cpp @@ -14,12 +14,20 @@ // Konsole #include "../profile/Profile.h" #include "../profile/ProfileGroup.h" +#include "../profile/ProfileManager.cpp" #include "../profile/ProfileWriter.h" +#include + #include using namespace Konsole; +void ProfileTest::initTestCase() +{ + QStandardPaths::setTestModeEnabled(true); +} + void ProfileTest::testProfile() { // create a new profile @@ -215,6 +223,40 @@ void ProfileTest::testProfileFileNames() delete writer; } +void ProfileTest::testProfileNameSorting() +{ + auto *manager = ProfileManager::instance(); + + const int origCount = manager->allProfiles().size(); + + Profile::Ptr profile1 = Profile::Ptr(new Profile); + profile1->setProperty(Profile::UntranslatedName, QStringLiteral("Indiana")); + manager->addProfile(profile1); + auto list = manager->allProfiles(); + int counter = 1; + QCOMPARE(list.size(), origCount + counter++); + // Built-in profile always at the top + QCOMPARE(list.at(0)->name(), QStringView(u"Default")); + + QVERIFY(std::is_sorted(list.cbegin(), list.cend(), profileNameLessThan)); + + Profile::Ptr profile2 = Profile::Ptr(new Profile); + profile2->setProperty(Profile::UntranslatedName, QStringLiteral("Old Paris")); + manager->addProfile(profile2); + list = manager->allProfiles(); + QCOMPARE(list.size(), origCount + counter++); + QVERIFY(std::is_sorted(list.cbegin(), list.cend(), profileNameLessThan)); + + Profile::Ptr profile3 = Profile::Ptr(new Profile); + profile3->setProperty(Profile::UntranslatedName, QStringLiteral("New Zealand")); + manager->addProfile(profile3); + list = manager->allProfiles(); + QCOMPARE(list.size(), origCount + counter++); + QVERIFY(std::is_sorted(list.cbegin(), list.cend(), profileNameLessThan)); + + QCOMPARE(list.at(0)->name(), QLatin1String("Default")); +} + void ProfileTest::testFallbackProfile() { // create a new profile diff --git a/src/autotests/ProfileTest.h b/src/autotests/ProfileTest.h index adde47d0..7b5a7322 100644 --- a/src/autotests/ProfileTest.h +++ b/src/autotests/ProfileTest.h @@ -16,10 +16,12 @@ class ProfileTest : public QObject Q_OBJECT private Q_SLOTS: + void initTestCase(); void testProfile(); void testClone(); void testProfileGroup(); void testProfileFileNames(); + void testProfileNameSorting(); void testFallbackProfile(); }; diff --git a/src/profile/ProfileManager.cpp b/src/profile/ProfileManager.cpp index 867b8132..25cf036b 100644 --- a/src/profile/ProfileManager.cpp +++ b/src/profile/ProfileManager.cpp @@ -37,6 +37,18 @@ static bool stringLessThan(const QString &p1, const QString &p2) return QString::localeAwareCompare(p1, p2) < 0; } +static bool profileNameLessThan(const Profile::Ptr &p1, const Profile::Ptr &p2) +{ + // Always put the Default/fallback profile at the top + if (p1->isFallback()) { + return true; + } else if (p2->isFallback()) { + return false; + } + + return stringLessThan(p1->name(), p2->name()); +} + ProfileManager::ProfileManager() : _defaultProfile(nullptr) , _fallbackProfile(nullptr) @@ -91,6 +103,11 @@ ProfileManager *ProfileManager::instance() return theProfileManager; } +ProfileManager::Iterator ProfileManager::findProfile(const Profile::Ptr &profile) const +{ + return std::find(_profiles.cbegin(), _profiles.cend(), profile); +} + void ProfileManager::initFallbackProfile() { _fallbackProfile = Profile::Ptr(new Profile()); @@ -230,10 +247,15 @@ void ProfileManager::saveSettings() appConfig->sync(); } +void ProfileManager::sortProfiles() +{ + std::sort(_profiles.begin(), _profiles.end(), profileNameLessThan); +} + QList ProfileManager::allProfiles() { loadAllProfiles(); - + sortProfiles(); return loadedProfiles(); } @@ -289,17 +311,20 @@ void ProfileManager::changeProfile(Profile::Ptr profile, QHashname().isEmpty(); bool messageShown = false; + bool isNameChanged = false; // Insert the changes into the existing Profile instance for (auto it = propertyMap.cbegin(); it != propertyMap.cend(); ++it) { const auto property = it.key(); auto value = it.value(); + isNameChanged = property == Profile::Name || property == Profile::UntranslatedName; + // "Default" is reserved for the fallback profile, override it; // The message is only shown if the user manually typed "Default" // in the name box in the edit profile dialog; i.e. saving the // fallback profile where the user didn't change the name at all, // the uniqueProfileName is used silently a couple of lines above. - if ((property == Profile::Name || property == Profile::UntranslatedName) && value == QLatin1String("Default")) { + if (isNameChanged && value == QLatin1String("Default")) { value = uniqueProfileName; if (!messageShown) { KMessageBox::sorry(nullptr, @@ -357,6 +382,10 @@ void ProfileManager::changeProfile(Profile::Ptr profile, QHash