QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
SecretLineEdit Class Reference

A QLineEdit for a password that can be shown: whatever is copied out of it is marked as a secret and reported for autoclear. More...

#include <secretlineedit.h>

Inheritance diagram for SecretLineEdit:
Collaboration diagram for SecretLineEdit:

Signals

void copied (const QString &text)
 Selected text was copied out of the line edit.

Public Member Functions

 SecretLineEdit (QWidget *parent=nullptr)
 Constructs the line edit.
 SecretLineEdit (const QString &text, QWidget *parent=nullptr)
 Constructs the line edit showing text.
auto createSecretContextMenu () -> QMenu *
 The standard context menu, with Copy and Cut going through the secret copy.
void setStoredText (const QString &text)
 Shows a value read from the entry exactly as it is stored.

Static Public Member Functions

static auto isLineBreak (QChar ch) -> bool
 Whether ch breaks a line: LF, CR, NEL (U+0085), or the Unicode line and paragraph separators (U+2028, U+2029).
static auto withoutLineBreaks (QString text) -> QString
 text with every line break (see isLineBreak()) removed: what a one-line field keeps of a paste.

Static Public Attributes

static constexpr int kMaxLength = 1 << 20
 Most characters the line edit holds (1 MiB of them): its maxLength in place of QLineEdit's 32767.

Protected Member Functions

void keyPressEvent (QKeyEvent *event) override
 Ctrl+C copies with the hints, Ctrl+X as well before it deletes; after any other key the primary selection gets the marked selection, not QLineEdit's plain-text copy.
void mouseReleaseEvent (QMouseEvent *event) override
 Fills the primary selection with the marked selection on a left-button release, instead of QLineEdit's plain-text copy.
void contextMenuEvent (QContextMenuEvent *event) override
 Shows createSecretContextMenu().

Private Member Functions

void copySelection (QClipboard::Mode mode)
 Puts the selection on the clipboard or in the primary selection as secret-marked MIME data and emits copied(). Does nothing in Password echo mode or without a selection, as QLineEdit::copy() itself.
void cutSelection ()
 Copies the selection through copySelection() and then deletes it. Does nothing when read-only or without a selection, as QLineEdit::cut().
void withoutPlainSelectionCopy (const std::function< void()> &handler)
 Runs a base-class event handler so that it cannot fill the primary selection with plain text: in a read-only field in Normal echo mode the handler runs under Password mode, which makes QLineEdit skip the copy without moving cursor or selection. Editable fields run the handler as is and rely on publishSelection() replacing the plain copy at once.
void publishSelection ()
 Fills the X11 primary selection with the marked selection, on platforms that have one; called after a handled key or a left-button release, where QLineEdit would have copied plain text.
void pasteOneLine (QClipboard::Mode mode)
 Pastes the clipboard or the primary selection with its line breaks removed, as one undoable edit. Does nothing when read-only.
void markUndoBoundary ()
 Ends the current undo step, so that the next edit starts its own, as QLineEdit::paste() does around a paste. Moves the cursor away and back, which is the public way to reach QLineEdit's private separate().

Detailed Description

A QLineEdit for a password that can be shown: whatever is copied out of it is marked as a secret and reported for autoclear.

QLineEdit copies nothing in Password echo mode, but once a "show" button switches it to Normal it copies plain text, without the hints, on four paths: Ctrl+C, the context menu's Copy, and the X11 primary selection after a mouse selection or a key. Unlike QTextEdit it has no createMimeDataFromSelection() to hook. Here Ctrl+C and Copy put buildClipboardMimeData() on the clipboard instead, and the key and mouse handlers keep QLineEdit from filling the primary selection at all (it runs them in Password echo mode) and fill it with the marked data instead, so plain text never gets there, not even for a moment. Each copy emits copied() so the text is cleared with the rest when the autoclear timer runs. Dragging is off, as QLineEdit has it by default.

In an editable field (the password editor) Ctrl+X, Cut, and the keys that delete to the end of the line or the whole line copy the same way before they delete. Password mode would turn off undo, make Ctrl+arrow jump to the ends and change the input method hints there, so the base handlers run as they are and the marked selection replaces QLineEdit's plain one at once, in the same event: X11 and Wayland hand selection data over only when another program asks for it, which reaches QtPass through its event loop, after the replacement.

Definition at line 44 of file secretlineedit.h.

Constructor & Destructor Documentation

◆ SecretLineEdit() [1/2]

SecretLineEdit::SecretLineEdit ( QWidget * parent = nullptr)
explicit

Constructs the line edit.

Parameters
parentOwner.

Definition at line 75 of file secretlineedit.cpp.

Here is the caller graph for this function:

◆ SecretLineEdit() [2/2]

SecretLineEdit::SecretLineEdit ( const QString & text,
QWidget * parent = nullptr )
explicit

Constructs the line edit showing text.

Not the same as setText() afterwards: QLineEdit takes the constructor's text as already finished, so losing focus without a change does not emit editingFinished().

Parameters
textThe initial text.
parentOwner.

Definition at line 82 of file secretlineedit.cpp.

Member Function Documentation

◆ contextMenuEvent()

void SecretLineEdit::contextMenuEvent ( QContextMenuEvent * event)
overrideprotected

Shows createSecretContextMenu().

Parameters
eventThe context menu event.

Definition at line 167 of file secretlineedit.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ copied

void SecretLineEdit::copied ( const QString & text)
signal

Selected text was copied out of the line edit.

Parameters
textThe text, as put on the clipboard or in the selection.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ copySelection()

void SecretLineEdit::copySelection ( QClipboard::Mode mode)
private

Puts the selection on the clipboard or in the primary selection as secret-marked MIME data and emits copied(). Does nothing in Password echo mode or without a selection, as QLineEdit::copy() itself.

Parameters
modeClipboard for Ctrl+C and Copy, Selection for the X11 primary selection.

Definition at line 173 of file secretlineedit.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ createSecretContextMenu()

auto SecretLineEdit::createSecretContextMenu ( ) -> QMenu *
nodiscard

The standard context menu, with Copy and Cut going through the secret copy.

Returns
A new menu; the caller owns it.

Definition at line 88 of file secretlineedit.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cutSelection()

void SecretLineEdit::cutSelection ( )
private

Copies the selection through copySelection() and then deletes it. Does nothing when read-only or without a selection, as QLineEdit::cut().

Definition at line 183 of file secretlineedit.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isLineBreak()

auto SecretLineEdit::isLineBreak ( QChar ch) -> bool
staticnodiscard

Whether ch breaks a line: LF, CR, NEL (U+0085), or the Unicode line and paragraph separators (U+2028, U+2029).

Parameters
chThe character.
Returns
true for those five.

Definition at line 55 of file secretlineedit.cpp.

Here is the caller graph for this function:

◆ keyPressEvent()

void SecretLineEdit::keyPressEvent ( QKeyEvent * event)
overrideprotected

Ctrl+C copies with the hints, Ctrl+X as well before it deletes; after any other key the primary selection gets the marked selection, not QLineEdit's plain-text copy.

Parameters
eventThe key event.

Definition at line 109 of file secretlineedit.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ markUndoBoundary()

void SecretLineEdit::markUndoBoundary ( )
private

Ends the current undo step, so that the next edit starts its own, as QLineEdit::paste() does around a paste. Moves the cursor away and back, which is the public way to reach QLineEdit's private separate().

Definition at line 234 of file secretlineedit.cpp.

Here is the caller graph for this function:

◆ mouseReleaseEvent()

void SecretLineEdit::mouseReleaseEvent ( QMouseEvent * event)
overrideprotected

Fills the primary selection with the marked selection on a left-button release, instead of QLineEdit's plain-text copy.

Parameters
eventThe mouse event.

Definition at line 151 of file secretlineedit.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pasteOneLine()

void SecretLineEdit::pasteOneLine ( QClipboard::Mode mode)
private

Pastes the clipboard or the primary selection with its line breaks removed, as one undoable edit. Does nothing when read-only.

The validator would remove them too, but a correction it makes resets QLineEdit's undo history; removing them before the insert keeps Undo for Ctrl+V, Paste and a middle click. Drops and input methods still go through the validator alone.

Parameters
modeClipboard for Ctrl+V and Paste, Selection for a middle click.

Definition at line 217 of file secretlineedit.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ publishSelection()

void SecretLineEdit::publishSelection ( )
private

Fills the X11 primary selection with the marked selection, on platforms that have one; called after a handled key or a left-button release, where QLineEdit would have copied plain text.

Definition at line 211 of file secretlineedit.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setStoredText()

void SecretLineEdit::setStoredText ( const QString & text)

Shows a value read from the entry exactly as it is stored.

setText() runs the line-break filter too (QLineEdit validates every change), which would alter a stored value with a NEL or a Unicode separator inside before the user touched it, and an unedited save would write the altered value back. The filter is for typed and pasted input, so it is set aside for this one setText().

Parameters
textThe stored value.

Definition at line 66 of file secretlineedit.cpp.

◆ withoutLineBreaks()

auto SecretLineEdit::withoutLineBreaks ( QString text) -> QString
staticnodiscard

text with every line break (see isLineBreak()) removed: what a one-line field keeps of a paste.

Parameters
textThe text.
Returns
The text on one line.

Definition at line 61 of file secretlineedit.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ withoutPlainSelectionCopy()

void SecretLineEdit::withoutPlainSelectionCopy ( const std::function< void()> & handler)
private

Runs a base-class event handler so that it cannot fill the primary selection with plain text: in a read-only field in Normal echo mode the handler runs under Password mode, which makes QLineEdit skip the copy without moving cursor or selection. Editable fields run the handler as is and rely on publishSelection() replacing the plain copy at once.

Parameters
handlerThe base handler call, e.g. QLineEdit::keyPressEvent.

Definition at line 192 of file secretlineedit.cpp.

Here is the caller graph for this function:

Member Data Documentation

◆ kMaxLength

int SecretLineEdit::kMaxLength = 1 << 20
staticconstexpr

Most characters the line edit holds (1 MiB of them): its maxLength in place of QLineEdit's 32767.

32767 cut a key file kept as one line of base64, and the password dialog saved the cut value. Without a bound, a line of tens of megabytes (an entry may decrypt to Pass::kMaxPlaintextBytes) costs gigabytes and seconds: measured offscreen, about 75 bytes per character, and about 80 ms on every paint per 1,048,576 characters. The password dialog does not load an entry with a longer line, so nothing is cut there.

Definition at line 60 of file secretlineedit.h.


The documentation for this class was generated from the following files: