Commit d6e64f52 authored by Pablo Rauzy's avatar Pablo Rauzy Committed by Christoph Cullmann
Browse files

manage JSON reading error in case users badly manipulate keyboardmacros.json file

parent 7c0b72ed
......@@ -113,7 +113,13 @@ void KeyboardMacrosPlugin::loadNamedMacros(bool locked)
for (auto it = json.constBegin(); it != json.constEnd(); ++it) {
// don't load macros we have wiped during this session
if (!m_wipedMacros.contains(it.key())) {
m_namedMacros.insert(it.key(), Macro(it.value()));
auto maybeMacro = Macro::fromJson(it.value());
if (!maybeMacro.second) {
sendMessage(i18n("Could not load '%1': malformed macro; wiping it.", it.key()), false);
m_wipedMacros.insert(it.key());
continue;
}
m_namedMacros.insert(it.key(), maybeMacro.first);
}
}
storage.close();
......
......@@ -17,25 +17,29 @@
class KeyCombination
{
private:
int m_key;
int m_key = -1;
Qt::KeyboardModifiers m_modifiers;
QString m_text;
public:
KeyCombination(){};
KeyCombination(int key, Qt::KeyboardModifiers modifiers, QString text)
: m_key(key)
, m_modifiers(modifiers)
, m_text(text){};
explicit KeyCombination(const QKeyEvent *keyEvent)
: m_key(keyEvent->key())
, m_modifiers(keyEvent->modifiers())
, m_text(keyEvent->text()){};
explicit KeyCombination(const QJsonArray &json)
static const QPair<KeyCombination, bool> fromJson(const QJsonArray &json)
{
Q_ASSERT(json.size() == 3);
Q_ASSERT(json.at(0).type() == QJsonValue::Double);
Q_ASSERT(json.at(1).type() == QJsonValue::Double);
Q_ASSERT(json.at(2).type() == QJsonValue::String);
m_key = json.at(0).toInt(0);
m_modifiers = static_cast<Qt::KeyboardModifiers>(json.at(1).toInt(0));
m_text = json.at(2).toString();
if (json.size() != 3 || json[0].type() != QJsonValue::Double || json[1].type() != QJsonValue::Double || json[2].type() != QJsonValue::String) {
return QPair(KeyCombination(), false);
}
return QPair(KeyCombination(json[0].toInt(0), static_cast<Qt::KeyboardModifiers>(json[1].toInt(0)), json[2].toString()), true);
};
const QKeyEvent keyPress() const
......@@ -67,7 +71,7 @@ public:
bool isVisibleInput() const
{
return m_text.size() == 1 && (m_modifiers == Qt::NoModifier || m_modifiers == Qt::ShiftModifier) && m_text.at(0).isPrint();
return m_text.size() == 1 && (m_modifiers == Qt::NoModifier || m_modifiers == Qt::ShiftModifier) && m_text[0].isPrint();
}
friend QDebug operator<<(QDebug dbg, const KeyCombination &kc)
......
......@@ -21,14 +21,23 @@ public:
explicit Macro()
: QList<KeyCombination>(){};
explicit Macro(const QJsonValue &json)
static const QPair<Macro, bool> fromJson(const QJsonValue &json)
{
Q_ASSERT(json.type() == QJsonValue::Array);
QJsonArray::ConstIterator it;
for (it = json.toArray().constBegin(); it != json.toArray().constEnd(); ++it) {
Q_ASSERT(it->type() == QJsonValue::Array);
this->append(KeyCombination(it->toArray()));
if (json.type() != QJsonValue::Array) {
QPair(Macro(), false);
}
Macro macro;
for (const auto &jsonKeyCombination : json.toArray()) {
if (jsonKeyCombination.type() != QJsonValue::Array) {
return QPair(Macro(), false);
}
auto maybeKeyCombination = KeyCombination::fromJson(jsonKeyCombination.toArray());
if (!maybeKeyCombination.second) {
return QPair(Macro(), false);
}
macro.append(maybeKeyCombination.first);
}
return QPair(macro, true);
};
QJsonArray toJson() const
......
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