QtPass Developer documentation
QtPass test coverage
Current view: top level - src/plainmessagebox.cpp (source / functions) Coverage Total Hit
Test: QtPass Lines: 96.9 % 64 62
Test Date: 2026-12-03 23:34:20 Functions: 100.0 % 9 9
Branches: 89.8 % 49 44

             Branch data     Line data    Source code
       1                 :             : // SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
       2                 :             : // SPDX-License-Identifier: GPL-3.0-or-later
       3                 :             : /**
       4                 :             :  * @file
       5                 :             :  * @brief Implements the PlainMessageBox helpers; the text format is set before
       6                 :             :  * the text so it is never laid out as rich text.
       7                 :             :  */
       8                 :             : #include "plainmessagebox.h"
       9                 :             : #include "util.h"
      10                 :             : #include <QApplication>
      11                 :             : #include <QCoreApplication>
      12                 :             : #include <QDir>
      13                 :             : 
      14                 :             : namespace {
      15                 :             : /// Lines and characters of a message a box shows whole.
      16                 :             : constexpr qsizetype kMaxLines = 40;
      17                 :             : /// See kMaxLines.
      18                 :             : constexpr qsizetype kMaxChars = 4000;
      19                 :             : /// Characters of a message kept whole under "Show Details...".
      20                 :             : constexpr qsizetype kMaxDetailChars = qsizetype{256} * 1024;
      21                 :             : 
      22                 :             : /**
      23                 :             :  * @brief The first @p chars UTF-16 units of @p text, or its last with
      24                 :             :  * @p fromEnd, one fewer where the cut would split a surrogate pair: half a
      25                 :             :  * pair shows as a broken glyph.
      26                 :             :  * @param text The text.
      27                 :             :  * @param chars The most UTF-16 units.
      28                 :             :  * @param fromEnd Take the end, not the start.
      29                 :             :  * @return That part of @p text, valid UTF-16 where @p text is.
      30                 :             :  */
      31                 :          24 : auto slice(const QString &text, qsizetype chars, bool fromEnd) -> QString {
      32         [ -  + ]:          24 :   if (chars >= text.size()) {
      33                 :             :     return text;
      34                 :             :   }
      35         [ +  + ]:          24 :   if (fromEnd) {
      36                 :          12 :     const qsizetype from = text.size() - chars;
      37         [ +  + ]:          12 :     return text.right(text.at(from).isLowSurrogate() ? chars - 1 : chars);
      38                 :             :   }
      39         [ +  + ]:          14 :   return text.left(text.at(chars - 1).isHighSurrogate() ? chars - 1 : chars);
      40                 :             : }
      41                 :             : 
      42                 :             : /**
      43                 :             :  * @brief At most @p lines lines and @p chars characters from the start of
      44                 :             :  * @p text, or from its end with @p fromEnd.
      45                 :             :  * @param text The text.
      46                 :             :  * @param lines The most lines.
      47                 :             :  * @param chars The most characters.
      48                 :             :  * @param fromEnd Take the end, not the start.
      49                 :             :  * @return That part of @p text.
      50                 :             :  */
      51                 :          18 : auto part(const QString &text, qsizetype lines, qsizetype chars, bool fromEnd)
      52                 :             :     -> QString {
      53                 :          18 :   QString piece = slice(text, chars, fromEnd);
      54                 :             :   qsizetype seen = 0;
      55         [ +  + ]:          18 :   if (fromEnd) {
      56         [ +  + ]:       16390 :     for (qsizetype i = piece.size() - 1; i >= 0; --i) {
      57   [ +  +  +  + ]:       16382 :       if (piece.at(i) == QLatin1Char('\n') && ++seen == lines) {
      58                 :           1 :         return piece.mid(i + 1);
      59                 :             :       }
      60                 :             :     }
      61                 :             :   } else {
      62         [ +  + ]:       16337 :     for (qsizetype i = 0; i < piece.size(); ++i) {
      63   [ +  +  +  + ]:       16329 :       if (piece.at(i) == QLatin1Char('\n') && ++seen == lines) {
      64                 :           1 :         return piece.left(i);
      65                 :             :       }
      66                 :             :     }
      67                 :             :   }
      68                 :             :   return piece;
      69                 :             : }
      70                 :             : 
      71                 :             : /**
      72                 :             :  * @brief @p text as a box shows it: whole up to kMaxLines lines and
      73                 :             :  * kMaxChars characters, else its start and its end with an ellipsis
      74                 :             :  * between.
      75                 :             :  *
      76                 :             :  * A message quoting git's or gpg's error output is as long as that output,
      77                 :             :  * and a pushed .gitattributes with many bad lines decides its length: laid
      78                 :             :  * out whole, the box froze the GUI. The end stays because that is where git
      79                 :             :  * puts what went wrong ("fatal: ..."), after its warnings.
      80                 :             :  * @param text The message.
      81                 :             :  * @param cut Set to whether anything was left out.
      82                 :             :  * @return The message to show.
      83                 :             :  */
      84                 :          67 : auto bounded(const QString &text, bool *cut) -> QString {
      85   [ +  +  -  + ]:          67 :   *cut = text.size() > kMaxChars || text.count(QLatin1Char('\n')) >= kMaxLines;
      86         [ +  + ]:          67 :   if (!*cut) {
      87                 :             :     return text;
      88                 :             :   }
      89                 :          18 :   return part(text, kMaxLines / 2, kMaxChars / 2, false) +
      90                 :          18 :          QStringLiteral("\n\u2026\n") +
      91                 :          27 :          part(text, kMaxLines / 2, kMaxChars / 2, true);
      92                 :             : }
      93                 :             : } // namespace
      94                 :             : 
      95                 :          67 : auto PlainMessageBox::show(QWidget *parent, QMessageBox::Icon icon,
      96                 :             :                            const QString &title, const QString &text,
      97                 :             :                            QMessageBox::StandardButtons buttons)
      98                 :             :     -> QMessageBox::StandardButton {
      99                 :          67 :   QMessageBox box(parent);
     100                 :          67 :   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                 :          67 :   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                 :          67 :   box.setTextFormat(Qt::PlainText);
     107                 :          67 :   const QString shown = Util::displayable(text);
     108                 :          67 :   bool cut = false;
     109                 :          67 :   box.setText(bounded(shown, &cut));
     110         [ +  + ]:          67 :   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                 :           9 :     box.setDetailedText(shown.size() <= kMaxDetailChars
     117      [ +  +  + ]:          12 :                             ? shown
     118                 :          15 :                             : slice(shown, kMaxDetailChars / 2, false) +
     119                 :          15 :                                   QStringLiteral("\n\u2026\n") +
     120   [ +  +  +  -  :          15 :                                   slice(shown, kMaxDetailChars / 2, true));
                      - ]
     121                 :             :   }
     122                 :          67 :   box.setStandardButtons(buttons);
     123                 :         134 :   return static_cast<QMessageBox::StandardButton>(box.exec());
     124                 :          67 : }
     125                 :             : 
     126                 :          17 : auto PlainMessageBox::askYes(QWidget *parent, const QString &title,
     127                 :             :                              const QString &text) -> bool {
     128                 :          17 :   return show(parent, QMessageBox::Question, title, text,
     129                 :          17 :               QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes;
     130                 :             : }
     131                 :             : 
     132                 :          39 : void PlainMessageBox::warning(QWidget *parent, const QString &title,
     133                 :             :                               const QString &text) {
     134                 :          39 :   show(parent, QMessageBox::Warning, title, text);
     135                 :          39 : }
     136                 :             : 
     137                 :           9 : void PlainMessageBox::critical(QWidget *parent, const QString &title,
     138                 :             :                                const QString &text) {
     139                 :           9 :   show(parent, QMessageBox::Critical, title, text);
     140                 :           9 : }
     141                 :             : 
     142                 :           4 : auto PlainMessageBox::askToGiveNewReaders(QWidget *parent, const QString &store,
     143                 :             :                                           const QStringList &entries) -> bool {
     144                 :           4 :   QStringList names;
     145         [ +  + ]:           8 :   for (const QString &entry : entries) {
     146                 :           8 :     names << QDir(store).relativeFilePath(entry);
     147                 :             :   }
     148                 :             :   names.sort();
     149                 :             :   constexpr int kMaxListed = 15;
     150                 :           4 :   QStringList listed = names.mid(0, kMaxListed);
     151         [ -  + ]:           4 :   if (names.size() > kMaxListed) {
     152                 :           0 :     listed << QCoreApplication::translate(
     153                 :             :         "PlainMessageBox", "... and %n more", nullptr,
     154                 :           0 :         static_cast<int>(names.size() - kMaxListed));
     155                 :             :   }
     156                 :           4 :   return askYes(
     157                 :             :       parent,
     158                 :           8 :       QCoreApplication::translate("PlainMessageBox",
     159                 :             :                                   "Give these entries new readers?"),
     160                 :           4 :       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         [ +  + ]:          12 :           .arg(listed.join(QLatin1Char('\n'))));
     167                 :             : }
     168                 :             : 
     169                 :          16 : auto PlainMessageBox::confirmPassRun(QWidget *parent, const QString &store,
     170                 :             :                                      const std::function<QStringList()> &check)
     171                 :             :     -> bool {
     172                 :          16 :   QApplication::setOverrideCursor(Qt::WaitCursor);
     173                 :          16 :   const QStringList entries = check();
     174                 :          16 :   QApplication::restoreOverrideCursor();
     175   [ +  +  +  +  :          32 :   return entries.isEmpty() || askToGiveNewReaders(parent, store, entries);
                      + ]
     176                 :             : }
        

Generated by: LCOV version 2.0-1