20#include <QDesktopServices>
32#include <QTextBrowser>
36 QBoxLayout *container,
37 QWidget *widgetParent,
43 while (
m_grid->count() > 0) {
44 QLayoutItem *item =
m_grid->takeAt(0);
45 if (QWidget *widget = item->widget()) {
54 const QList<QDrag *> drags = widget->findChildren<QDrag *>();
55 if (drags.isEmpty()) {
59 connect(drags.last(), &QObject::destroyed, widget,
60 [widget]() { widget->deleteLater(); });
70 for (
int i = 0; i <
m_grid->count(); ++i) {
71 QWidget *widget =
m_grid->itemAt(i)->widget();
72 if (widget ==
nullptr) {
85 const QString &otpConfig) {
95 addField(position, QObject::tr(
"Password"), password, s);
98 bool otpRendered =
false;
108 if (!otpRendered && !otpConfig.isEmpty()) {
115 addField(position, nv.name, nv.value, s);
119 if (!otpConfig.isEmpty() && !otpRendered) {
130constexpr const char *kButtonStyle =
131 "QPushButton { border-style: none; background: transparent; padding: 0; "
132 "margin: 0; icon-size: 16px; color: inherit; }";
135constexpr int kFieldHeight = 26;
142auto valueBrowser(
const QString &field,
const QString &value,
145 browser->setOpenExternalLinks(
true);
146 browser->setOpenLinks(
true);
147 browser->setMaximumHeight(kFieldHeight);
148 browser->setMinimumHeight(kFieldHeight);
149 browser->setSizePolicy(
150 QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum));
151 browser->setObjectName(field);
156 const QString shown =
161 browser->setWordWrapMode(QTextOption::WrapAnywhere);
164 const QString linkedText =
170 browser->setHtml(QStringLiteral(
"<span style=\"white-space: pre-wrap;\">") +
171 linkedText + QStringLiteral(
"</span>"));
175 browser->setPlainText(shown);
177 browser->setReadOnly(
true);
178 browser->setStyleSheet(lineStyle);
179 browser->setContentsMargins(0, 0, 0, 0);
184auto urlButton(
const QString &url, QWidget *parent) -> QPushButton * {
185 auto *button =
new QPushButton(parent);
186 button->setIcon(QIcon::fromTheme(QStringLiteral(
"applications-internet"),
187 QIcon(
":/icons/open-url.svg")));
194 QStringLiteral(
"<qt style=\"white-space:nowrap\">%1</qt>")
195 .arg(QObject::tr(
"Open %1 in browser").arg(url.toHtmlEscaped())));
196 button->setStyleSheet(QString::fromLatin1(kButtonStyle));
197 button->setCursor(Qt::PointingHandCursor);
198 QObject::connect(button, &QPushButton::clicked, button, [url]() {
201 QDesktopServices::openUrl(QUrl(url));
214 for (
const QString &name : names) {
221 return s.hidePassword &&
222 (s.hideAllFields || field == QObject::tr(
"Password") ||
228 if (!s.hidePassword) {
232 return marked || (s.hideSecretFields &&
237 const QString &field,
238 const QString &value,
240 const QString buttonStyle = QString::fromLatin1(kButtonStyle);
245 copy->setStyleSheet(buttonStyle);
246 layout->addWidget(copy);
252 qr->setStyleSheet(buttonStyle);
253 layout->addWidget(qr);
258 if (field != QObject::tr(
"Password") && !
hides(field, s) &&
265 const QString &field,
266 const QString &value,
267 const QString &lineStyle) {
273 passwordLineEdit->setObjectName(field);
276 passwordLineEdit->setStoredText(value);
277 passwordLineEdit->setReadOnly(
true);
278 passwordLineEdit->setStyleSheet(lineStyle);
279 passwordLineEdit->setContentsMargins(0, 0, 0, 0);
280 passwordLineEdit->setEchoMode(QLineEdit::Password);
283 showButton->setStyleSheet(QString::fromLatin1(kButtonStyle));
284 showButton->setContentsMargins(0, 0, 0, 0);
285 layout->addWidget(showButton);
286 layout->addWidget(passwordLineEdit);
290 const QString &value,
292 const QString trimmedField = field.trimmed();
300 const QString displayValue =
301 trimmedField == QObject::tr(
"Password") ? value : value.trimmed();
304 const QString lineStyle =
306 ?
"QLineEdit, QTextBrowser { border-style: none; background: "
307 "transparent; font-family: monospace; }"
308 :
"QLineEdit, QTextBrowser { border-style: none; background: "
312 if (
hides(trimmedField, s)) {
321 const bool secret = trimmedField == QObject::tr(
"Password") ||
324 valueBrowser(trimmedField, displayValue, lineStyle, !secret);
327 frame->layout()->addWidget(browser);
331 auto *label =
new QLabel();
332 label->setTextFormat(Qt::PlainText);
335 addRow(position, label, frame);
339 m_grid->addWidget(label, position, 0);
340 m_grid->addWidget(frame, position, 1);
341 label->installEventFilter(
this);
342 frame->installEventFilter(
this);
344 for (QWidget *child : frame->findChildren<QWidget *>()) {
345 child->installEventFilter(
this);
351 if (event->type() == QEvent::MouseButtonDblClick &&
352 static_cast<QMouseEvent *
>(event)->button() == Qt::LeftButton) {
356 return QObject::eventFilter(watched, event);
362 const QString borderColor =
364 frame->setStyleSheet(QStringLiteral(
".QFrame{border: 1px solid %1; "
365 "border-radius: 5px;}")
370 for (
int i = 0; i <
m_grid->count(); ++i) {
371 if (
auto *frame = qobject_cast<QFrame *>(
m_grid->itemAt(i)->widget())) {
380 auto *frame =
new QFrame();
381 auto *frameLayout =
new QHBoxLayout();
382 frameLayout->setContentsMargins(5, 2, 2, 2);
383 frameLayout->setSpacing(0);
384 frame->setLayout(frameLayout);
396 const std::optional<Totp::Settings> settings =
Totp::parse(otpConfig, &why);
397 if (!settings.has_value()) {
407 addField(position, QObject::tr(
"OTP code"),
409 ? QObject::tr(
"No OTP code found in this password entry")
421 frame->layout()->addWidget(otpWidget);
423 addRow(position,
new QLabel(QObject::tr(
"OTP code")), frame);
AppSettings, the value object holding every flat QtPass setting.
static auto isOtpFieldName(const QString &name) -> bool
Whether a field name designates the one-time password field.
static auto isSecretFieldName(const QString &name, const QStringList &secrets) -> bool
Whether a field name names a secret: one of secrets occurs in it as whole words, compared case-insens...
static auto isOtpPasswordLine(const QString &line) -> bool
Whether an entry's first line holds a TOTP shared secret rather than a password.
static auto carriesOtpUri(const QString &text) -> bool
Whether text holds an otpauth URI anywhere: after a list dash, in quotes, behind other text or a seco...
The NamedValues class is mostly a list of NamedValue but also has a method to take a specific NamedVa...
auto isSecret(const QString &field, const AppSettings &s) const -> bool
Whether a field is a secret "Hide password" hides: one a template marks (setMarkedSecrets()),...
void qrRequested(const QString &text)
Emitted when a field's QR button is clicked.
QWidget * m_widgetParent
Parent for the buttons the panel creates and source of the palette used by applyFrameStyle(); not own...
void refreshPalette()
Re-derive palette-dependent styling after a runtime theme change.
void setMarkedSecrets(const QStringList &names)
Set the field names a template marks as secret.
void displayFields(const QString &password, const NamedValues &namedValues, const AppSettings &s, const QString &otpConfig={})
Render the password and template fields of a decrypted entry.
auto eventFilter(QObject *watched, QEvent *event) -> bool override
Turn a left double-click on a field row into editRequested().
QGridLayout * m_grid
The grid the rows go into; owned by MainWindow's .ui.
void addActionButtons(QLayout *layout, const QString &field, const QString &value, const AppSettings &s)
Add the copy, QR and open-in-browser buttons a field gets, per the settings.
QBoxLayout * m_container
Layout around the grid whose spacing is 0 while empty and 6 once rows are shown; owned by MainWindow'...
void addField(int position, const QString &field, const QString &value, const AppSettings &s)
Render one name: value row: action buttons, then the value either behind the eye button (see hides())...
void clear()
Remove all field rows and collapse the container spacing.
auto createFieldFrame() -> QFrame *
New bordered frame with a tight horizontal layout, styled by applyFrameStyle(); shared by addField() ...
PasswordDisplayPanel(QGridLayout *grid, QBoxLayout *container, QWidget *widgetParent, QObject *parent=nullptr)
Construct a panel that renders into the given layouts.
void textCopied(const QString &text)
Text was selected and copied out of a field's value (Ctrl+C, the context menu, a drag,...
QSet< QString > m_markedSecrets
Field names the current template marks as secret, case-folded, so isSecret() looks a name up in any c...
void addRow(int position, QLabel *label, QFrame *frame)
Put a row's label and value frame into the grid, listening for a double-click on either (and on the f...
void copyRequested(const QString &text)
Emitted when a field's copy button is clicked.
void addOtpField(int position, const QString &otpConfig, const AppSettings &s)
Render the live OTP row: an OtpCodeWidget in a field frame under an "OTP code" label....
void addHiddenPassword(QLayout *layout, const QString &field, const QString &value, const QString &lineStyle)
Add the password, hidden, with a button that shows it.
auto currentOtpCode() const -> QString
The one-time password currently on display, if there is one.
void editRequested()
Emitted on a double-click anywhere on a field row: the user wants to change what they are looking at.
auto hides(const QString &field, const AppSettings &s) const -> bool
Whether a field is shown behind the eye button: the password, a field a template marks,...
void applyFrameStyle(QFrame *frame) const
Give a field frame its rounded border in the current QPalette::Mid colour. The colour is baked into t...
void copied(const QString &text)
Selected text was copied out of the line edit.
SecretLineEdit(QWidget *parent=nullptr)
Constructs the line edit.
A QTextBrowser for decrypted entry text: whatever is copied out of it is marked as a secret and repor...
SecretTextBrowser(QWidget *parent=nullptr)
Constructs the browser.
void copied(const QString &text)
Selected text was copied out of the browser.
static auto parse(const QString &config, Refusal *why=nullptr) -> std::optional< Settings >
Parse an OTP configuration string.
static auto describe(Refusal why) -> QString
One sentence for the user on why a configuration gives no code.
Refusal
Why parse() gave no settings, so the user can be told more than "invalid".
@ None
Parsed: there was nothing to refuse.
static auto hasLongUnbrokenRun(QStringView text) -> bool
Whether text holds more than kMaxUnbrokenRun characters in a row without white space Qt wraps at (a n...
static auto withoutOtherBlockBreaks(QString text) -> QString
text with every block break other than a line feed (isOtherBlockBreak()) shown as a space,...
static auto boundSpaceRuns(const QString &text) -> QString
text with every run of white space other than tabs and line feeds cut to kMaxSpaceRun characters,...
static auto isLaunchableWebUrl(const QString &value) -> bool
Check whether a value is a safe, launchable web URL.
static auto shownName(const QString &name, qsizetype max=kMaxShownName) -> QString
A name or path from the store, as a label or list shows it: one line, at most max characters.
static auto linkifyUrls(const QString &text, bool *linked=nullptr) -> QString
Turn plain text into HTML with clickable links for safe web URLs.
PasswordDisplayPanel: renders the decrypted fields of an entry (password, template fields,...
QLineEdit whose copies, cuts and primary-selection fills carry the secret clipboard hints and are rep...
QTextBrowser for showing decrypted entry text that marks everything copied out of it as a secret.
Plain value-object holding all flat QtPass configuration.
Enums::ClipBoardType clipBoardType
Clipboard copy behaviour.
bool useMonospace
Render content in a monospace font.
bool useQrencode
Enable qrencode support.
A name/value pair parsed from a password file field.
RFC 6238 TOTP: otpauth URI parsing, code generation and canonical re-serialisation.
Util: static helpers for the process environment, binary discovery, store paths, URL safety and the l...