Refactor profile shortcuts handling code

- Don't use a QHash to hold a few profile shortcuts-related items, instead
use a vector of struct.

- Load profile shortcuts after loading the profiles; after commit
50041fd02e, all the profiles are loaded in the ProfileManager constructor.
Now there is no need to store the profile path as a member in ShortcutData;
and when saving the shortcut settings we can use profile->name(), since the
way the ProfileWriter works results in the filename being the
profile name + ".profile". When loading shortcuts, keep backwards
compatibility by checking for both "profile name" and "profile name.profile".
wilder
Ahmad Samir 5 years ago committed by Kurt Hindenburg
parent 0cf3a751a4
commit 124756f650
  1. 95
      src/profile/ProfileManager.cpp
  2. 9
      src/profile/ProfileManager.h

@ -71,14 +71,10 @@ ProfileManager::ProfileManager()
} }
loadAllProfiles(defaultProfileFileName); loadAllProfiles(defaultProfileFileName);
loadShortcuts();
Q_ASSERT(_profiles.size() > 0); Q_ASSERT(_profiles.size() > 0);
Q_ASSERT(_defaultProfile); Q_ASSERT(_defaultProfile);
// get shortcuts and paths of profiles associated with
// them - this doesn't load the shortcuts themselves,
// that is done on-demand.
loadShortcuts();
} }
ProfileManager::~ProfileManager() = default; ProfileManager::~ProfileManager() = default;
@ -452,78 +448,69 @@ void ProfileManager::loadShortcuts()
KSharedConfigPtr appConfig = KSharedConfig::openConfig(); KSharedConfigPtr appConfig = KSharedConfig::openConfig();
KConfigGroup shortcutGroup = appConfig->group("Profile Shortcuts"); KConfigGroup shortcutGroup = appConfig->group("Profile Shortcuts");
QMap<QString, QString> entries = shortcutGroup.entryMap(); const QLatin1String suffix(".profile");
auto findByName = [this, suffix](const QString &name) {
QMapIterator<QString, QString> iter(entries); return std::find_if(_profiles.cbegin(), _profiles.cend(), [&name, suffix](const Profile::Ptr &p) {
while (iter.hasNext()) { return p->name() == name //
iter.next(); || (p->name() + suffix) == name; // For backwards compatibility
});
QKeySequence shortcut = QKeySequence::fromString(iter.key()); };
QString profilePath = iter.value();
const QMap<QString, QString> entries = shortcutGroup.entryMap();
ShortcutData data; for (auto it = entries.cbegin(), endIt = entries.cend(); it != endIt; ++it) {
auto profileIt = findByName(it.value());
// if the file is not an absolute path, look it up if (profileIt == _profiles.cend()) {
QFileInfo fileInfo(profilePath); continue;
if (!fileInfo.isAbsolute()) {
profilePath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("konsole/") + profilePath);
} }
data.profilePath = profilePath; _shortcuts.push_back({*profileIt, QKeySequence::fromString(it.key())});
_shortcuts.insert(shortcut, data);
} }
} }
QString ProfileManager::normalizePath(const QString &path) const
{
QFileInfo fileInfo(path);
const QString location = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("konsole/") + fileInfo.fileName());
return (!fileInfo.isAbsolute()) || location.isEmpty() ? path : fileInfo.fileName();
}
void ProfileManager::saveShortcuts() void ProfileManager::saveShortcuts()
{ {
KSharedConfigPtr appConfig = KSharedConfig::openConfig(); KSharedConfigPtr appConfig = KSharedConfig::openConfig();
KConfigGroup shortcutGroup = appConfig->group("Profile Shortcuts"); KConfigGroup shortcutGroup = appConfig->group("Profile Shortcuts");
shortcutGroup.deleteGroup(); shortcutGroup.deleteGroup();
QMapIterator<QKeySequence, ShortcutData> iter(_shortcuts); for (const auto &[profile, keySeq] : _shortcuts) {
while (iter.hasNext()) { shortcutGroup.writeEntry(keySeq.toString(), profile->name());
iter.next();
QString shortcutString = iter.key().toString();
QString profileName = normalizePath(iter.value().profilePath);
shortcutGroup.writeEntry(shortcutString, profileName);
} }
} }
void ProfileManager::setShortcut(Profile::Ptr profile, const QKeySequence &keySequence) void ProfileManager::setShortcut(Profile::Ptr profile, const QKeySequence &keySequence)
{ {
QKeySequence existingShortcut = shortcut(profile); QKeySequence existingShortcut = shortcut(profile);
_shortcuts.remove(existingShortcut);
if (keySequence.isEmpty()) { auto profileIt = std::find_if(_shortcuts.begin(), _shortcuts.end(), [&profile](const ShortcutData &data) {
return; return data.profileKey == profile;
});
if (profileIt != _shortcuts.end()) {
// There is a previous shortcut for this profile, replace it with the new one
profileIt->keySeq = keySequence;
Q_EMIT shortcutChanged(profileIt->profileKey, profileIt->keySeq);
} else {
// No previous shortcut for this profile
const ShortcutData &newData = _shortcuts.emplace_back(ShortcutData{profile, keySequence});
Q_EMIT shortcutChanged(newData.profileKey, newData.keySeq);
} }
ShortcutData data; auto keySeqIt = std::find_if(_shortcuts.begin(), _shortcuts.end(), [&keySequence, &profile](const ShortcutData &data) {
data.profileKey = profile; return data.profileKey != profile && data.keySeq == keySequence;
data.profilePath = profile->path(); });
// TODO - This won't work if the profile doesn't if (keySeqIt != _shortcuts.end()) {
// have a path yet // There is a profile with shortcut "keySequence" which has been
_shortcuts.insert(keySequence, data); // associated with another profile >>> unset it
Q_EMIT shortcutChanged(keySeqIt->profileKey, {});
Q_EMIT shortcutChanged(profile, keySequence); _shortcuts.erase(keySeqIt);
}
} }
QKeySequence ProfileManager::shortcut(Profile::Ptr profile) const QKeySequence ProfileManager::shortcut(Profile::Ptr profile) const
{ {
QMapIterator<QKeySequence, ShortcutData> iter(_shortcuts); auto it = std::find_if(_shortcuts.cbegin(), _shortcuts.cend(), [&profile](const ShortcutData &data) {
while (iter.hasNext()) { return data.profileKey == profile;
iter.next(); });
if (iter.value().profileKey == profile || iter.value().profilePath == profile->path()) {
return iter.key();
}
}
return QKeySequence(); return it != _shortcuts.cend() ? it->keySeq : QKeySequence{};
} }

@ -16,6 +16,7 @@
#include <QSet> #include <QSet>
#include <QStack> #include <QStack>
#include <QStringList> #include <QStringList>
#include <QUrl>
#include <QVariant> #include <QVariant>
#include <vector> #include <vector>
@ -229,13 +230,9 @@ private:
struct ShortcutData { struct ShortcutData {
Profile::Ptr profileKey; Profile::Ptr profileKey;
QString profilePath; QKeySequence keySeq;
}; };
QMap<QKeySequence, ShortcutData> _shortcuts; // shortcut keys -> profile path std::vector<ShortcutData> _shortcuts;
// finds out if it's a internal profile or an external one,
// fixing the path to point to the correct location for the profile.
QString normalizePath(const QString &path) const;
}; };
} }

Loading…
Cancel
Save