aboutsummaryrefslogtreecommitdiffstats
path: root/src/keymap.cpp
blob: 39991dc1c3bebc2e4ba378c987a618bb7d6888a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * qtmaildir - a Qt6 mail client for notmuch-indexed Maildirs
 * Copyright (C) 2026 Danilo M. <danix@danix.xyz>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "keymap.h"

#include <QSettings>

QStringList KeyMap::knownActions()
{
    // Keep in sync with the actions MainWindow registers.
    return {
        QStringLiteral("next_thread"),
        QStringLiteral("prev_thread"),
        QStringLiteral("open_thread"),
        QStringLiteral("archive"),
        QStringLiteral("delete"),
        QStringLiteral("spam"),
        QStringLiteral("toggle_unread"),
        QStringLiteral("flag"),
        QStringLiteral("focus_query"),
        QStringLiteral("toggle_html"),
        QStringLiteral("load_remote"),
        QStringLiteral("undo"),
        QStringLiteral("sync"),
        QStringLiteral("quit"),
    };
}

void KeyMap::loadDefaults()
{
    const QHash<QString, QString> defaults = {
        { QStringLiteral("j"),      QStringLiteral("next_thread") },
        { QStringLiteral("k"),      QStringLiteral("prev_thread") },
        { QStringLiteral("Return"), QStringLiteral("open_thread") },
        { QStringLiteral("a"),      QStringLiteral("archive") },
        { QStringLiteral("d"),      QStringLiteral("delete") },
        { QStringLiteral("N"),      QStringLiteral("toggle_unread") },
        { QStringLiteral("F"),      QStringLiteral("flag") },
        { QStringLiteral("/"),      QStringLiteral("focus_query") },
        { QStringLiteral("h"),      QStringLiteral("toggle_html") },
        { QStringLiteral("u"),      QStringLiteral("undo") },
        { QStringLiteral("G"),      QStringLiteral("sync") },
        { QStringLiteral("Ctrl+Q"), QStringLiteral("quit") },
    };

    for (auto it = defaults.cbegin(); it != defaults.cend(); ++it)
        m_bindings.insert(QKeySequence::fromString(it.key()), it.value());
}

void KeyMap::loadOverrides(QSettings &settings)
{
    const QStringList known = knownActions();

    // Sequences bound so far *within this override pass*. Defaults already
    // sit in m_bindings before this runs, so a plain m_bindings.contains()
    // check would misfire on every legitimate override of a default (e.g.
    // "j=archive" overriding the default 'j' binding). Only a collision
    // between two entries in this same pass (e.g. two INI keys that
    // normalize to the same QKeySequence, such as "y" and "Y") is a bug.
    QHash<QKeySequence, QString> seenThisPass;

    settings.beginGroup(QStringLiteral("keys"));
    const QStringList keys = settings.childKeys();
    for (const QString &key : keys) {
        const QString action = settings.value(key).toString();

        const QKeySequence sequence = QKeySequence::fromString(key);
        // QKeySequence::fromString() does not return an empty sequence for
        // unparseable input; it returns a non-empty sequence whose
        // toString() is empty (verified on Qt 6.11). Use that to detect
        // garbage input instead.
        if (sequence.isEmpty() || sequence.toString().isEmpty()) {
            m_warnings.append(
                QStringLiteral("Unparseable key sequence '%1' in [keys]").arg(key));
            continue;
        }

        if (!known.contains(action)) {
            m_warnings.append(
                QStringLiteral("Unknown action '%1' bound to '%2' in [keys]")
                    .arg(action, key));
            continue;
        }

        const auto previous = seenThisPass.constFind(sequence);
        if (previous != seenThisPass.constEnd()) {
            m_warnings.append(
                QStringLiteral("Key sequence '%1' bound to both '%2' and '%3' "
                                "in [keys]; keeping '%2'")
                    .arg(key, previous.value(), action));
            continue;
        }
        seenThisPass.insert(sequence, action);

        m_bindings.insert(sequence, action);
    }
    settings.endGroup();
}

QString KeyMap::actionFor(const QKeySequence &sequence) const
{
    return m_bindings.value(sequence);
}