diff --git a/src/ProfileManager.cpp b/src/ProfileManager.cpp --- a/src/ProfileManager.cpp +++ b/src/ProfileManager.cpp @@ -68,6 +68,29 @@ std::stable_sort(list.begin(), list.end(), profileNameLessThan); } +static QString entryOrDefault(KConfigGroup group, QString key, QString defaultValue) { + QString read = group.readEntry(key, ""); + + if (read.isEmpty()) { + return defaultValue; + } + + return read; +} + +static Profile::Ptr loadDefaultOrCreateEmpty(ProfileManager *manager, QString path) { + Profile::Ptr profile = manager->loadProfile(path); + + if (!profile) { + profile = Profile::Ptr(new Profile()); + profile->useFallback(); + profile->setProperty(Profile::Path, path); + profile->setHidden(false); + } + + return profile; +} + ProfileManager::ProfileManager() : _profiles(QSet()) , _favorites(QSet()) @@ -78,38 +101,33 @@ , _shortcuts(QMap()) , _profileList(nullptr) { - //load fallback profile - _fallbackProfile = Profile::Ptr(new Profile()); - _fallbackProfile->useFallback(); - addProfile(_fallbackProfile); + const QString fallbackDefaultProfile = QDir(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)).filePath(QStringLiteral("konsole/default.profile")); + const QString sharedConfigFilename = QStringLiteral("konsolerc"); + const QString configGroupName = QStringLiteral("Desktop Entry"); + const QString defaultProfileEntry = QStringLiteral("DefaultProfile"); + + // if the hosting application of konsolepart does not specify its own + // default profile, use the default profile of stand-alone Konsole. + const KSharedConfigPtr konsoleConfig = KSharedConfig::openConfig(sharedConfigFilename); + const KConfigGroup konsoleGroup = konsoleConfig->group(configGroupName); + + const QString konsoleConfigDefaultProfile = entryOrDefault(konsoleGroup, defaultProfileEntry, fallbackDefaultProfile); // lookup the default profile specified in rc // for stand-alone Konsole, appConfig is just konsolerc // for konsolepart, appConfig might be yakuakerc, dolphinrc, katerc... - KSharedConfigPtr appConfig = KSharedConfig::openConfig(); - KConfigGroup group = appConfig->group("Desktop Entry"); - QString defaultProfileFileName = group.readEntry("DefaultProfile", ""); + const KSharedConfigPtr appConfig = KSharedConfig::openConfig(); + const KConfigGroup appGroup = appConfig->group(configGroupName); - // if the hosting application of konsolepart does not specify its own - // default profile, use the default profile of stand-alone Konsole. - if (defaultProfileFileName.isEmpty()) { - KSharedConfigPtr konsoleConfig = KSharedConfig::openConfig(QStringLiteral("konsolerc")); - group = konsoleConfig->group("Desktop Entry"); - defaultProfileFileName = group.readEntry("DefaultProfile", ""); - } + const QString configFile = entryOrDefault(appGroup, defaultProfileEntry, konsoleConfigDefaultProfile); - _defaultProfile = _fallbackProfile; - if (!defaultProfileFileName.isEmpty()) { - // load the default profile - const QString path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("konsole/") + defaultProfileFileName); + _fallbackProfile = Profile::Ptr(new Profile()); + _fallbackProfile->useFallback(); + _profiles.insert(_fallbackProfile); - if (!path.isEmpty()) { - Profile::Ptr profile = loadProfile(path); - if (profile) { - _defaultProfile = profile; - } - } - } + _defaultProfile = loadDefaultOrCreateEmpty(this, configFile); + _profiles.insert(_defaultProfile); + _favorites.insert(_defaultProfile); Q_ASSERT(_profiles.count() > 0); Q_ASSERT(_defaultProfile); @@ -455,10 +473,6 @@ void ProfileManager::addProfile(const Profile::Ptr &profile) { - if (_profiles.isEmpty()) { - _defaultProfile = profile; - } - _profiles.insert(profile); emit profileAdded(profile);