summaryrefslogtreecommitdiffstats
path: root/debian/pinentry-tqt/pinentry-tqt-1.2.1/qt/pinlineedit.cpp
blob: 286195d83404a4257b0ec9da689aad532c7c9da5 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* pinlineedit.cpp - Modified QLineEdit widget.
 * Copyright (C) 2018 Damien Goutte-Gattat
 * Copyright (C) 2021 g10 Code GmbH
 *
 * Software engineering by Ingo Klöcker <dev@ingo-kloecker.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * 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, see <https://www.gnu.org/licenses/>.
 * SPDX-License-Identifier: GPL-2.0+
 */

#include "pinlineedit.h"

#include <QClipboard>
#include <QGuiApplication>
#include <QKeyEvent>

static const int FormattedPassphraseGroupSize = 5;
static const QChar FormattedPassphraseSeparator = QChar::Nbsp;

namespace
{
struct Selection
{
    bool empty() const { return start < 0 || start >= end; }
    int length() const { return empty() ? 0 : end - start; }

    int start;
    int end;
};
}

class PinLineEdit::Private
{
    PinLineEdit *const q;

public:
    Private(PinLineEdit *q)
        : q{q}
    {}

    QString formatted(QString text) const
    {
        const int dashCount = text.size() / FormattedPassphraseGroupSize;
        text.reserve(text.size() + dashCount);
        for (int i = FormattedPassphraseGroupSize; i < text.size(); i += FormattedPassphraseGroupSize + 1) {
            text.insert(i, FormattedPassphraseSeparator);
        }
        return text;
    }

    Selection formattedSelection(Selection selection) const
    {
        if (selection.empty()) {
            return selection;
        }
        return {
            selection.start + selection.start / FormattedPassphraseGroupSize,
            selection.end + (selection.end - 1) / FormattedPassphraseGroupSize
        };
    }

    QString unformatted(QString text) const
    {
        for (int i = FormattedPassphraseGroupSize; i < text.size(); i += FormattedPassphraseGroupSize) {
            text.remove(i, 1);
        }
        return text;
    }

    Selection unformattedSelection(Selection selection) const
    {
        if (selection.empty()) {
            return selection;
        }
        return {
            selection.start - selection.start / (FormattedPassphraseGroupSize + 1),
            selection.end - selection.end / (FormattedPassphraseGroupSize + 1)
        };
    }

    void copyToClipboard()
    {
        if (q->echoMode() != QLineEdit::Normal) {
            return;
        }

        QString text = q->selectedText();
        if (mFormattedPassphrase) {
            text.remove(FormattedPassphraseSeparator);
        }
        if (!text.isEmpty()) {
            QGuiApplication::clipboard()->setText(text);
        }
    }

    int selectionEnd()
    {
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
        return q->selectionEnd();
#else
        return q->selectionStart() + q->selectedText().size();
#endif
    }

public:
    bool mFormattedPassphrase = false;
};

PinLineEdit::PinLineEdit(QWidget *parent)
    : QLineEdit(parent)
    , d{new Private{this}}
{
    connect(this, SIGNAL(textEdited(QString)),
            this, SLOT(textEdited()));
}

PinLineEdit::~PinLineEdit() = default;

void PinLineEdit::setFormattedPassphrase(bool on)
{
    if (on == d->mFormattedPassphrase) {
        return;
    }
    d->mFormattedPassphrase = on;
    Selection selection{selectionStart(), d->selectionEnd()};
    if (d->mFormattedPassphrase) {
        setText(d->formatted(text()));
        selection = d->formattedSelection(selection);
    } else {
        setText(d->unformatted(text()));
        selection = d->unformattedSelection(selection);
    }
    if (!selection.empty()) {
        setSelection(selection.start, selection.length());
    }
}

void PinLineEdit::copy() const
{
    d->copyToClipboard();
}

void PinLineEdit::cut()
{
    if (hasSelectedText()) {
        copy();
        del();
    }
}

void PinLineEdit::setPin(const QString &pin)
{
    setText(d->mFormattedPassphrase ? d->formatted(pin) : pin);
}

QString PinLineEdit::pin() const
{
    if (d->mFormattedPassphrase) {
        return d->unformatted(text());
    } else {
        return text();
    }
}

void PinLineEdit::keyPressEvent(QKeyEvent *e)
{
    if (e == QKeySequence::Copy) {
        copy();
        return;
    }
    else if (e == QKeySequence::Cut) {
        if (!isReadOnly() && hasSelectedText()) {
            copy();
            del();
        }
        return;
    }
    else if (e == QKeySequence::DeleteEndOfLine) {
        if (!isReadOnly()) {
            setSelection(cursorPosition(), text().size());
            copy();
            del();
        }
        return;
    }
    else if (e == QKeySequence::DeleteCompleteLine) {
        if (!isReadOnly()) {
            setSelection(0, text().size());
            copy();
            del();
        }
        return;
    }

    QLineEdit::keyPressEvent(e);

    if (e->key() == Qt::Key::Key_Backspace) {
        emit backspacePressed();
    }
}

void PinLineEdit::textEdited()
{
    if (!d->mFormattedPassphrase) {
        return;
    }
    auto currentText = text();
    // first calculate the cursor position in the reformatted text; the cursor
    // is put left of the separators, so that backspace works as expected
    auto cursorPos = cursorPosition();
    cursorPos -= QStringView{currentText}.left(cursorPos).count(FormattedPassphraseSeparator);
    cursorPos += std::max(cursorPos - 1, 0) / FormattedPassphraseGroupSize;
    // then reformat the text
    currentText.remove(FormattedPassphraseSeparator);
    currentText = d->formatted(currentText);
    // finally, set reformatted text and updated cursor position
    setText(currentText);
    setCursorPosition(cursorPos);
}

#include "pinlineedit.moc"