aboutsummaryrefslogtreecommitdiffstats
path: root/src/keymap.cpp
blob: 42ccd4074baebc767045c8a335de0d2187687416 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * 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"),
    };
}

QList<QPair<QString, QString>> KeyMap::defaultBindings()
{
    // Modifier shortcuts throughout, rather than the bare letters of 0.1.0.
    // Two reasons. A bare capital never worked: "N" parses to plain Key_N
    // while typing a capital emits Shift+N, so toggle_unread, flag and sync
    // were dead keys. And a single letter cannot be a QAction shortcut in a
    // menu without stealing that letter from every text field in the window.
    //
    // Ordered as the menus present them; a QList keeps that order, which a
    // QHash would not.
    return {
        { QStringLiteral("Ctrl+J"),       QStringLiteral("next_thread") },
        { QStringLiteral("Ctrl+K"),       QStringLiteral("prev_thread") },
        { QStringLiteral("Return"),       QStringLiteral("open_thread") },
        { QStringLiteral("Ctrl+E"),       QStringLiteral("archive") },
        { QStringLiteral("Ctrl+D"),       QStringLiteral("delete") },
        { QStringLiteral("Ctrl+Shift+S"), QStringLiteral("spam") },
        { QStringLiteral("Ctrl+U"),       QStringLiteral("toggle_unread") },
        { QStringLiteral("Ctrl+I"),       QStringLiteral("flag") },
        { QStringLiteral("Ctrl+L"),       QStringLiteral("focus_query") },
        { QStringLiteral("Ctrl+H"),       QStringLiteral("toggle_html") },
        { QStringLiteral("Ctrl+M"),       QStringLiteral("load_remote") },
        { QStringLiteral("Ctrl+Z"),       QStringLiteral("undo") },
        { QStringLiteral("Ctrl+G"),       QStringLiteral("sync") },
        { QStringLiteral("Ctrl+Q"),       QStringLiteral("quit") },
    };
}

QStringList KeyMap::defaultActions()
{
    QStringList actions;
    const auto bindings = defaultBindings();
    actions.reserve(bindings.size());
    for (const auto &binding : bindings)
        actions.append(binding.second);
    return actions;
}

QKeySequence KeyMap::normalizeSequence(const QString &text)
{
    const QKeySequence sequence = QKeySequence::fromString(text);

    // fromString() does not return an empty sequence for unparseable input;
    // it returns a non-empty one whose toString() is empty (verified on
    // Qt 6.11). Both checks are needed to detect garbage.
    if (sequence.isEmpty() || sequence.toString().isEmpty())
        return {};

    // A bare uppercase letter, no modifiers: the user wrote "N" meaning the
    // key they press to type a capital N, which is Shift+N. fromString()
    // folded the case away, so put the Shift back.
    if (text.size() == 1 && text.at(0).isUpper() && text.at(0).isLetter())
        return QKeySequence(sequence[0].key() | Qt::SHIFT);

    return sequence;
}

void KeyMap::loadDefaults()
{
    for (const auto &binding : defaultBindings())
        m_bindings.insert(normalizeSequence(binding.first), binding.second);
}

QKeySequence KeyMap::sequenceFor(const QString &action) const
{
    // Several sequences can reach one action: the built-in default, which
    // loadOverrides() does not remove, plus whatever the user added. Their
    // binding is the one to show and to put on the QAction, or configuring
    // "Ctrl+Alt+A = archive" would leave the menu still advertising Ctrl+E.
    //
    // QHash iteration order is unspecified, so ties are broken on the text
    // rather than left to chance.
    const QKeySequence builtIn = defaultSequenceFor(action);
    QKeySequence best;
    bool bestIsBuiltIn = false;

    for (auto it = m_bindings.cbegin(); it != m_bindings.cend(); ++it) {
        if (it.value() != action)
            continue;

        const bool isBuiltIn = !builtIn.isEmpty() && it.key() == builtIn;
        if (best.isEmpty()) {
            best = it.key();
            bestIsBuiltIn = isBuiltIn;
            continue;
        }
        // A user binding always beats the default.
        if (bestIsBuiltIn && !isBuiltIn) {
            best = it.key();
            bestIsBuiltIn = false;
        } else if (bestIsBuiltIn == isBuiltIn
                   && it.key().toString() < best.toString()) {
            best = it.key();
        }
    }
    return best;
}

QKeySequence KeyMap::defaultSequenceFor(const QString &action)
{
    for (const auto &binding : defaultBindings()) {
        if (binding.second == action)
            return normalizeSequence(binding.first);
    }
    return {};
}

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();

        // Shares the defaults' normalization, so a hand-written "N" binds the
        // key the user actually presses rather than one nothing emits.
        const QKeySequence sequence = normalizeSequence(key);
        if (sequence.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);
}