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

Dialog for importing GPG keys from file or clipboard. More...

#include <importkeydialog.h>

Inheritance diagram for ImportKeyDialog:
Collaboration diagram for ImportKeyDialog:

Classes

struct  BlockKeys
 How many keys an armored block holds, by kind. More...

Public Member Functions

 ImportKeyDialog (QString gpgExe, QWidget *parent=nullptr, int timeoutMs=kImportTimeoutMs)
 Construct an ImportKeyDialog.
 ~ImportKeyDialog () override
 Destroys the dialog and its generated UI.
auto importedKeyId () const -> QString
 Get the imported key id on success.

Static Public Member Functions

static auto parseGpgImportOutput (const QString &output) -> QString
 Parse GPG import output to extract a key id or fingerprint.
static auto keysInBlock (const QString &listing) -> BlockKeys
 Count the keys in the listing of gpg --with-colons --import-options show-only --import.
static auto fitsTheEditor (QStringView text) -> bool
 Whether text is laid out as armor can be, so the input field can show it at a bounded cost.

Static Public Attributes

static constexpr int kImportTimeoutMs = 60000
 How long gpg may take to import before it is stopped: the import runs on the GUI thread, and a key crafted with many user IDs kept gpg busy for minutes, the window and its autoclear timers frozen with it.
static constexpr qsizetype kMaxKeyLine = 1024
 Longest line fitsTheEditor() lets through, in characters.
static constexpr qsizetype kMaxKeyLines = 16 * 1024 * 1024 / 64
 Most lines fitsTheEditor() lets through: a 16 MiB key file in armor's 64-character lines.

Private Slots

void on_fileButton_clicked ()
 "From file" button: picks a file, reads it as a regular file of at most 16 MiB, requires the -----BEGIN PGP armor header and loads the text into the input field. Binary keyrings are refused with a hint to gpg --armor --export first, and text that fails fitsTheEditor() with the limits it broke.
void on_pasteButton_clicked ()
 "From clipboard" button: replaces the input field with the clipboard text, unless the clipboard is empty or the text fails fitsTheEditor().
void on_importButton_clicked ()
 "Import" button: runs importFromString() on the trimmed input and closes the dialog with accept() on success.
void on_inputTextEdit_textChanged ()
 Enables the Import button only while the input field holds non-blank text.

Private Member Functions

auto importFromString (const QString &input) -> bool
 Lists the keys in input first, with nothing written, and refuses a block that holds a private key or more than one key. Then feeds it to gpg --no-options --status-fd 1 --import --batch --yes via Executor::executeBlocking() and records the imported key id. Neither run reads gpg.conf, so options there cannot make the listing and the import see different keys.
auto runGpg (const QStringList &args, const QString &input, QString *stdOut, QString *stdErr, const QElapsedTimer &timer) -> bool
 Runs gpg with args on input within what is left of m_timeoutMs since timer started.
void showError (const QString &message)
 Shows message in a plain-text warning box (gpg's stderr quotes user IDs, which must not be read as markup).
void showSuccess (const QString &keyId)
 Shows the "imported, now verify the fingerprint" information box.

Private Attributes

QScopedPointer< Ui::ImportKeyDialog > ui
 Generated form (importkeydialog.ui); owned.
QString m_importedKeyId
 Key id or fingerprint of the last successful import; empty until then. Read back through importedKeyId().
QString m_gpgExe
 Path of the gpg binary to run; empty means plain gpg on PATH.
int m_timeoutMs
 Most milliseconds gpg may take to import (kImportTimeoutMs).

Detailed Description

Dialog for importing GPG keys from file or clipboard.

Allows users to import GPG public keys without leaving QtPass. Offers two import paths: from file or from clipboard text.

Definition at line 29 of file importkeydialog.h.

Constructor & Destructor Documentation

◆ ImportKeyDialog()

ImportKeyDialog::ImportKeyDialog ( QString gpgExe,
QWidget * parent = nullptr,
int timeoutMs = kImportTimeoutMs )
explicit

Construct an ImportKeyDialog.

Parameters
gpgExePath to the gpg executable (falls back to "gpg" if empty).
parentOptional parent widget.
timeoutMsMost milliseconds gpg may take to import.

Definition at line 40 of file importkeydialog.cpp.

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

◆ ~ImportKeyDialog()

ImportKeyDialog::~ImportKeyDialog ( )
overridedefault

Destroys the dialog and its generated UI.

Here is the call graph for this function:

Member Function Documentation

◆ fitsTheEditor()

auto ImportKeyDialog::fitsTheEditor ( QStringView text) -> bool
staticnodiscard

Whether text is laid out as armor can be, so the input field can show it at a bounded cost.

Armor lines are 64 characters (76 at most) and a header line not much longer. A line without break opportunity takes Qt time growing with the square of its length to wrap, and each line costs the editor some 440 bytes, so a file of one long line or of line breaks only froze or filled the GUI before gpg ever saw it.

Parameters
textThe text to show. A line ends where the editor starts a new one: LF, CR (CRLF once), U+2028, U+2029, U+FDD0 and U+FDD1.
Returns
False when a line is longer than kMaxKeyLine or there are more than kMaxKeyLines lines.

Definition at line 57 of file importkeydialog.cpp.

Here is the caller graph for this function:

◆ importedKeyId()

auto ImportKeyDialog::importedKeyId ( ) const -> QString

Get the imported key id on success.

Returns
The imported key id (or fingerprint when GnuPG status output provides one), or empty if import failed.

Definition at line 49 of file importkeydialog.cpp.

Here is the caller graph for this function:

◆ importFromString()

bool ImportKeyDialog::importFromString ( const QString & input) -> bool
private

Lists the keys in input first, with nothing written, and refuses a block that holds a private key or more than one key. Then feeds it to gpg --no-options --status-fd 1 --import --batch --yes via Executor::executeBlocking() and records the imported key id. Neither run reads gpg.conf, so options there cannot make the listing and the import see different keys.

Parameters
inputArmored key text, trimmed.
Returns
true when the block held one public key, gpg exited with 0 and a key id could be parsed from its output; false after showError() otherwise.

Definition at line 193 of file importkeydialog.cpp.

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

◆ keysInBlock()

auto ImportKeyDialog::keysInBlock ( const QString & listing) -> BlockKeys
static

Count the keys in the listing of gpg --with-colons --import-options show-only --import.

Only the primary-key records count (pub and sec); subkeys, user IDs and fingerprints belong to the key before them.

Parameters
listinggpg's colon listing of the block.
Returns
The public and the secret keys in it.

Definition at line 281 of file importkeydialog.cpp.

Here is the caller graph for this function:

◆ on_fileButton_clicked

void ImportKeyDialog::on_fileButton_clicked ( )
privateslot

"From file" button: picks a file, reads it as a regular file of at most 16 MiB, requires the -----BEGIN PGP armor header and loads the text into the input field. Binary keyrings are refused with a hint to gpg --armor --export first, and text that fails fitsTheEditor() with the limits it broke.

Definition at line 100 of file importkeydialog.cpp.

Here is the call graph for this function:

◆ on_importButton_clicked

void ImportKeyDialog::on_importButton_clicked ( )
privateslot

"Import" button: runs importFromString() on the trimmed input and closes the dialog with accept() on success.

Definition at line 177 of file importkeydialog.cpp.

Here is the call graph for this function:

◆ on_inputTextEdit_textChanged

void ImportKeyDialog::on_inputTextEdit_textChanged ( )
privateslot

Enables the Import button only while the input field holds non-blank text.

Definition at line 188 of file importkeydialog.cpp.

◆ on_pasteButton_clicked

void ImportKeyDialog::on_pasteButton_clicked ( )
privateslot

"From clipboard" button: replaces the input field with the clipboard text, unless the clipboard is empty or the text fails fitsTheEditor().

Definition at line 161 of file importkeydialog.cpp.

Here is the call graph for this function:

◆ parseGpgImportOutput()

auto ImportKeyDialog::parseGpgImportOutput ( const QString & output) -> QString
static

Parse GPG import output to extract a key id or fingerprint.

Prefers locale-independent status lines ([GNUPG:] IMPORT_OK and [GNUPG:] IMPORTED) when present, then falls back to the human-readable "gpg: key ...: imported" line (English locale only).

Parameters
outputCombined stdout/stderr from gpg --import --status-fd 1.
Returns
The imported key id or fingerprint, or empty if not found.

Definition at line 294 of file importkeydialog.cpp.

Here is the caller graph for this function:

◆ runGpg()

auto ImportKeyDialog::runGpg ( const QStringList & args,
const QString & input,
QString * stdOut,
QString * stdErr,
const QElapsedTimer & timer ) -> bool
private

Runs gpg with args on input within what is left of m_timeoutMs since timer started.

Parameters
argsgpg's arguments.
inputText for gpg's stdin.
stdOutReceives gpg's stdout.
stdErrReceives gpg's stderr.
timerStarted when the import began.
Returns
true when gpg exited with 0; false after showError() otherwise.

Definition at line 255 of file importkeydialog.cpp.

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

◆ showError()

void ImportKeyDialog::showError ( const QString & message)
private

Shows message in a plain-text warning box (gpg's stderr quotes user IDs, which must not be read as markup).

Parameters
messageThe error text.

Definition at line 345 of file importkeydialog.cpp.

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

◆ showSuccess()

void ImportKeyDialog::showSuccess ( const QString & keyId)
private

Shows the "imported, now verify the fingerprint" information box.

Parameters
keyIdThe imported key id or fingerprint.

Definition at line 351 of file importkeydialog.cpp.

Here is the caller graph for this function:

Member Data Documentation

◆ kImportTimeoutMs

int ImportKeyDialog::kImportTimeoutMs = 60000
staticconstexpr

How long gpg may take to import before it is stopped: the import runs on the GUI thread, and a key crafted with many user IDs kept gpg busy for minutes, the window and its autoclear timers frozen with it.

Definition at line 38 of file importkeydialog.h.

◆ kMaxKeyLine

qsizetype ImportKeyDialog::kMaxKeyLine = 1024
staticconstexpr

Longest line fitsTheEditor() lets through, in characters.

Definition at line 93 of file importkeydialog.h.

◆ kMaxKeyLines

qsizetype ImportKeyDialog::kMaxKeyLines = 16 * 1024 * 1024 / 64
staticconstexpr

Most lines fitsTheEditor() lets through: a 16 MiB key file in armor's 64-character lines.

Definition at line 96 of file importkeydialog.h.

◆ m_gpgExe

QString ImportKeyDialog::m_gpgExe
private

Path of the gpg binary to run; empty means plain gpg on PATH.

Definition at line 151 of file importkeydialog.h.

◆ m_importedKeyId

QString ImportKeyDialog::m_importedKeyId
private

Key id or fingerprint of the last successful import; empty until then. Read back through importedKeyId().

Definition at line 149 of file importkeydialog.h.

◆ m_timeoutMs

int ImportKeyDialog::m_timeoutMs
private

Most milliseconds gpg may take to import (kImportTimeoutMs).

Definition at line 153 of file importkeydialog.h.

◆ ui

QScopedPointer<Ui::ImportKeyDialog> ImportKeyDialog::ui
private

Generated form (importkeydialog.ui); owned.

Definition at line 146 of file importkeydialog.h.


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