QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
plainmessagebox.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
2// SPDX-License-Identifier: GPL-3.0-or-later
8#include "plainmessagebox.h"
9#include "util.h"
10#include <QApplication>
11#include <QCoreApplication>
12#include <QDir>
13
14namespace {
16constexpr qsizetype kMaxLines = 40;
18constexpr qsizetype kMaxChars = 4000;
20constexpr qsizetype kMaxDetailChars = qsizetype{256} * 1024;
21
31auto slice(const QString &text, qsizetype chars, bool fromEnd) -> QString {
32 if (chars >= text.size()) {
33 return text;
34 }
35 if (fromEnd) {
36 const qsizetype from = text.size() - chars;
37 return text.right(text.at(from).isLowSurrogate() ? chars - 1 : chars);
38 }
39 return text.left(text.at(chars - 1).isHighSurrogate() ? chars - 1 : chars);
40}
41
51auto part(const QString &text, qsizetype lines, qsizetype chars, bool fromEnd)
52 -> QString {
53 QString piece = slice(text, chars, fromEnd);
54 qsizetype seen = 0;
55 if (fromEnd) {
56 for (qsizetype i = piece.size() - 1; i >= 0; --i) {
57 if (piece.at(i) == QLatin1Char('\n') && ++seen == lines) {
58 return piece.mid(i + 1);
59 }
60 }
61 } else {
62 for (qsizetype i = 0; i < piece.size(); ++i) {
63 if (piece.at(i) == QLatin1Char('\n') && ++seen == lines) {
64 return piece.left(i);
65 }
66 }
67 }
68 return piece;
69}
70
84auto bounded(const QString &text, bool *cut) -> QString {
85 *cut = text.size() > kMaxChars || text.count(QLatin1Char('\n')) >= kMaxLines;
86 if (!*cut) {
87 return text;
88 }
89 return part(text, kMaxLines / 2, kMaxChars / 2, false) +
90 QStringLiteral("\n\u2026\n") +
91 part(text, kMaxLines / 2, kMaxChars / 2, true);
92}
93} // namespace
94
95auto PlainMessageBox::show(QWidget *parent, QMessageBox::Icon icon,
96 const QString &title, const QString &text,
97 QMessageBox::StandardButtons buttons)
98 -> QMessageBox::StandardButton {
99 QMessageBox box(parent);
100 box.setIcon(icon);
101 // The text quotes names from the store: a bidi override in a folder name
102 // mirrored the rest of a warning, a C1 control cut it short.
103 box.setWindowTitle(Util::displayable(title, false));
104 // Before the text: set afterwards, the text would already have been laid
105 // out as rich text once.
106 box.setTextFormat(Qt::PlainText);
107 const QString shown = Util::displayable(text);
108 bool cut = false;
109 box.setText(bounded(shown, &cut));
110 if (cut) {
111 // All of it under "Show Details...": plain text in a scrolling field,
112 // which does not lay it out at once as the label did. Not all tool
113 // output reaches the Process Output panel (the set-up runs git while
114 // the window waits), so this is where the cause can be read. Past
115 // 256 KB its start and end, for the same reason as the box.
116 box.setDetailedText(shown.size() <= kMaxDetailChars
117 ? shown
118 : slice(shown, kMaxDetailChars / 2, false) +
119 QStringLiteral("\n\u2026\n") +
120 slice(shown, kMaxDetailChars / 2, true));
121 }
122 box.setStandardButtons(buttons);
123 return static_cast<QMessageBox::StandardButton>(box.exec());
124}
125
126auto PlainMessageBox::askYes(QWidget *parent, const QString &title,
127 const QString &text) -> bool {
128 return show(parent, QMessageBox::Question, title, text,
129 QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes;
130}
131
132void PlainMessageBox::warning(QWidget *parent, const QString &title,
133 const QString &text) {
134 show(parent, QMessageBox::Warning, title, text);
135}
136
137void PlainMessageBox::critical(QWidget *parent, const QString &title,
138 const QString &text) {
139 show(parent, QMessageBox::Critical, title, text);
140}
141
142auto PlainMessageBox::askToGiveNewReaders(QWidget *parent, const QString &store,
143 const QStringList &entries) -> bool {
144 QStringList names;
145 for (const QString &entry : entries) {
146 names << QDir(store).relativeFilePath(entry);
147 }
148 names.sort();
149 constexpr int kMaxListed = 15;
150 QStringList listed = names.mid(0, kMaxListed);
151 if (names.size() > kMaxListed) {
152 listed << QCoreApplication::translate(
153 "PlainMessageBox", "... and %n more", nullptr,
154 static_cast<int>(names.size() - kMaxListed));
155 }
156 return askYes(
157 parent,
158 QCoreApplication::translate("PlainMessageBox",
159 "Give these entries new readers?"),
160 QCoreApplication::translate(
161 "PlainMessageBox",
162 "pass re-encrypts every entry here, and these would become "
163 "readable by someone on their recipient list who cannot read them "
164 "now:\n\n%1\n\nAn entry copied in from another folder can end up "
165 "here this way. Go on anyway?")
166 .arg(listed.join(QLatin1Char('\n'))));
167}
168
169auto PlainMessageBox::confirmPassRun(QWidget *parent, const QString &store,
170 const std::function<QStringList()> &check)
171 -> bool {
172 QApplication::setOverrideCursor(Qt::WaitCursor);
173 const QStringList entries = check();
174 QApplication::restoreOverrideCursor();
175 return entries.isEmpty() || askToGiveNewReaders(parent, store, entries);
176}
static auto displayable(const QString &text, bool lineBreaks=true) -> QString
text with the characters that change how the text around them reads replaced by U+FFFD,...
Definition util.cpp:898
auto confirmPassRun(QWidget *parent, const QString &store, const std::function< QStringList()> &check) -> bool
Run check (which lists the entries a pass run would give new readers, one gpg call each) under a wait...
auto show(QWidget *parent, QMessageBox::Icon icon, const QString &title, const QString &text, QMessageBox::StandardButtons buttons=QMessageBox::Ok) -> QMessageBox::StandardButton
Show a modal message box with plain text and wait for it.
auto askYes(QWidget *parent, const QString &title, const QString &text) -> bool
Ask a yes/no question in plain text.
void warning(QWidget *parent, const QString &title, const QString &text)
Show a warning in plain text.
auto askToGiveNewReaders(QWidget *parent, const QString &store, const QStringList &entries) -> bool
Ask whether to go on although entries would gain readers: with the pass backend, pass re-encrypts eve...
void critical(QWidget *parent, const QString &title, const QString &text)
Show an error in plain text.
QMessageBox helpers that force Qt::PlainText so store-controlled names can never be read as rich-text...
Util: static helpers for the process environment, binary discovery, store paths, URL safety and the l...