Commit 124756f6 authored by Ahmad Samir's avatar Ahmad Samir Committed by Kurt Hindenburg
Browse files

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
50041fd0, 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".
parent 0cf3a751
......@@ -71,14 +71,10 @@ ProfileManager::ProfileManager()
}
loadAllProfiles(defaultProfileFileName);
loadShortcuts();
Q_ASSERT(_profiles.size() > 0);
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;
......@@ -452,78 +448,69 @@ void ProfileManager::loadShortcuts()
KSharedConfigPtr appConfig = KSharedConfig::openConfig();
KConfigGroup shortcutGroup = appConfig->group("Profile Shortcuts");
QMap<QString, QString> entries = shortcutGroup.entryMap();
QMapIterator<QString, QString> iter(entries);
while (iter.hasNext()) {
iter.next();
QKeySequence shortcut = QKeySequence::fromString(iter.key());
QString profilePath = iter.value();
ShortcutData data;
// if the file is not an absolute path, look it up
QFileInfo fileInfo(profilePath);
if (!fileInfo.isAbsolute()) {
profilePath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("konsole/") + profilePath);
const QLatin1String suffix(".profile");
auto findByName = [this, suffix](const QString &name) {
return std::find_if(_profiles.cbegin(), _profiles.cend(), [&name, suffix](const Profile::Ptr &p) {
return p->name() == name //
|| (p->name() + suffix) == name; // For backwards compatibility
});
};
const QMap<QString, QString> entries = shortcutGroup.entryMap();
for (auto it = entries.cbegin(), endIt = entries.cend(); it != endIt; ++it) {
auto profileIt = findByName(it.value());
if (profileIt == _profiles.cend()) {
continue;
}
data.profilePath = profilePath;
_shortcuts.insert(shortcut, data);
_shortcuts.push_back({*profileIt, QKeySequence::fromString(it.key())});
}
}
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()
{
KSharedConfigPtr appConfig = KSharedConfig::openConfig();
KConfigGroup shortcutGroup = appConfig->group("Profile Shortcuts");
shortcutGroup.deleteGroup();
QMapIterator<QKeySequence, ShortcutData> iter(_shortcuts);
while (iter.hasNext()) {
iter.next();
QString shortcutString = iter.key().toString();
QString profileName = normalizePath(iter.value().profilePath);
shortcutGroup.writeEntry(shortcutString, profileName);
for (const auto &[profile, keySeq] : _shortcuts) {
shortcutGroup.writeEntry(keySeq.toString(), profile->name());
}
}
void ProfileManager::setShortcut(Profile::Ptr profile, const QKeySequence &keySequence)
{
QKeySequence existingShortcut = shortcut(profile);
_shortcuts.remove(existingShortcut);
if (keySequence.isEmpty()) {
return;
auto profileIt = std::find_if(_shortcuts.begin(), _shortcuts.end(), [&profile](const ShortcutData &data) {
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;
data.profileKey = profile;
data.profilePath = profile->path();
// TODO - This won't work if the profile doesn't
// have a path yet
_shortcuts.insert(keySequence, data);
Q_EMIT shortcutChanged(profile, keySequence);
auto keySeqIt = std::find_if(_shortcuts.begin(), _shortcuts.end(), [&keySequence, &profile](const ShortcutData &data) {
return data.profileKey != profile && data.keySeq == keySequence;
});
if (keySeqIt != _shortcuts.end()) {
// There is a profile with shortcut "keySequence" which has been
// associated with another profile >>> unset it
Q_EMIT shortcutChanged(keySeqIt->profileKey, {});
_shortcuts.erase(keySeqIt);
}
}
QKeySequence ProfileManager::shortcut(Profile::Ptr profile) const
{
QMapIterator<QKeySequence, ShortcutData> iter(_shortcuts);
while (iter.hasNext()) {
iter.next();
if (iter.value().profileKey == profile || iter.value().profilePath == profile->path()) {
return iter.key();
}
}
auto it = std::find_if(_shortcuts.cbegin(), _shortcuts.cend(), [&profile](const ShortcutData &data) {
return data.profileKey == profile;
});
return QKeySequence();
return it != _shortcuts.cend() ? it->keySeq : QKeySequence{};
}
......@@ -16,6 +16,7 @@
#include <QSet>
#include <QStack>
#include <QStringList>
#include <QUrl>
#include <QVariant>
#include <vector>
......@@ -229,13 +230,9 @@ private:
struct ShortcutData {
Profile::Ptr profileKey;
QString profilePath;
QKeySequence keySeq;
};
QMap<QKeySequence, ShortcutData> _shortcuts; // shortcut keys -> profile path
// 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;
std::vector<ShortcutData> _shortcuts;
};
}
......
Supports Markdown
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