10#include <QApplication>
11#include <QCoreApplication>
16constexpr qsizetype kMaxLines = 40;
18constexpr qsizetype kMaxChars = 4000;
20constexpr qsizetype kMaxDetailChars = qsizetype{256} * 1024;
31auto slice(
const QString &text, qsizetype chars,
bool fromEnd) -> QString {
32 if (chars >= text.size()) {
36 const qsizetype from = text.size() - chars;
37 return text.right(text.at(from).isLowSurrogate() ? chars - 1 : chars);
39 return text.left(text.at(chars - 1).isHighSurrogate() ? chars - 1 : chars);
51auto part(
const QString &text, qsizetype lines, qsizetype chars,
bool fromEnd)
53 QString piece = slice(text, chars, 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);
62 for (qsizetype i = 0; i < piece.size(); ++i) {
63 if (piece.at(i) == QLatin1Char(
'\n') && ++seen == lines) {
84auto bounded(
const QString &text,
bool *cut) -> QString {
85 *cut = text.size() > kMaxChars || text.count(QLatin1Char(
'\n')) >= kMaxLines;
89 return part(text, kMaxLines / 2, kMaxChars / 2,
false) +
90 QStringLiteral(
"\n\u2026\n") +
91 part(text, kMaxLines / 2, kMaxChars / 2,
true);
96 const QString &title,
const QString &text,
97 QMessageBox::StandardButtons buttons)
98 -> QMessageBox::StandardButton {
99 QMessageBox box(parent);
106 box.setTextFormat(Qt::PlainText);
109 box.setText(bounded(shown, &cut));
116 box.setDetailedText(shown.size() <= kMaxDetailChars
118 : slice(shown, kMaxDetailChars / 2,
false) +
119 QStringLiteral(
"\n\u2026\n") +
120 slice(shown, kMaxDetailChars / 2,
true));
122 box.setStandardButtons(buttons);
123 return static_cast<QMessageBox::StandardButton
>(box.exec());
127 const QString &text) ->
bool {
128 return show(parent, QMessageBox::Question, title, text,
129 QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes;
133 const QString &text) {
134 show(parent, QMessageBox::Warning, title, text);
138 const QString &text) {
139 show(parent, QMessageBox::Critical, title, text);
143 const QStringList &entries) ->
bool {
145 for (
const QString &entry : entries) {
146 names << QDir(store).relativeFilePath(entry);
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));
158 QCoreApplication::translate(
"PlainMessageBox",
159 "Give these entries new readers?"),
160 QCoreApplication::translate(
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'))));
170 const std::function<QStringList()> &check)
172 QApplication::setOverrideCursor(Qt::WaitCursor);
173 const QStringList entries = check();
174 QApplication::restoreOverrideCursor();
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,...
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...