Line data Source code
1 : // SPDX-FileCopyrightText: 2018 Anne Jan Brouwer
2 : // SPDX-License-Identifier: GPL-3.0-or-later
3 : /**
4 : * @file
5 : * @brief QtPass: UI-neutral glue that turns backend completion signals into
6 : * formatted output, status messages and clipboard handling.
7 : */
8 : #ifndef SRC_QTPASS_H_
9 : #define SRC_QTPASS_H_
10 :
11 : #include "clipboardmanager.h"
12 : #include <QDialog>
13 : #include <QObject>
14 : #include <QPixmap>
15 : #include <QPointer>
16 :
17 : class Pass;
18 :
19 : /**
20 : * @class QtPass
21 : * @ingroup support
22 : * @brief Application-level glue between the pass backends and whatever shows
23 : * their results: turns the backends' completion signals into UI-neutral
24 : * signals (formatted output, status messages, "the operation is over"), owns
25 : * the ClipboardManager, migrates the settings of older versions on start-up
26 : * and renders QR codes. It knows nothing about the main window; the window
27 : * connects to the signals below.
28 : */
29 : class QtPass : public QObject {
30 7 : Q_OBJECT
31 :
32 : public:
33 : /**
34 : * @brief Construct the glue and connect it to both pass backends.
35 : * @param parent Owner.
36 : */
37 : explicit QtPass(QObject *parent = nullptr);
38 : /**
39 : * @brief Destroy the QtPass instance and clean up signal connections.
40 : */
41 : ~QtPass() override;
42 :
43 : /**
44 : * @brief Resolve the password store and the executables and migrate the
45 : * settings of an older version (fresh-install defaults, the native-OTP
46 : * switch). Whether the resulting configuration is usable is the caller's
47 : * question: see Util::configIsValid().
48 : */
49 : void init();
50 :
51 : /**
52 : * @brief The clipboard handling for this application instance.
53 : * @return The manager; lives as long as this object.
54 : */
55 906 : auto clipboard() -> ClipboardManager & { return m_clipboard; }
56 :
57 : /**
58 : * @brief Turn a process' plain-text output into the HTML the text browser
59 : * shows: http(s) URLs become links, everything else is escaped, line breaks
60 : * become `<br />` and all other white space is kept as it is (a lone
61 : * carriage return becomes a space).
62 : * @param output Plain text from a process.
63 : * @param prefix HTML placed before the converted output.
64 : * @param postfix HTML placed after it.
65 : * @param links Make launchable http(s) URLs links; off for text that
66 : * holds the password (the entry shown as it is).
67 : * @return The HTML.
68 : */
69 : static auto formatOutput(QString output, const QString &prefix = QString(),
70 : const QString &postfix = QString(),
71 : bool links = true) -> QString;
72 :
73 : /**
74 : * @brief Create a frameless popup dialog, placed at the cursor, showing the
75 : * given QR code. Split out of showTextAsQRCode() so tests can inspect
76 : * the dialog without running qrencode.
77 : * @param image Pixmap containing the rendered QR code.
78 : * @return Pointer to a QDialog showing the image; it deletes itself on
79 : * close, the caller shows it.
80 : */
81 : static QDialog *createQRCodePopup(const QPixmap &image);
82 :
83 : public slots:
84 : /**
85 : * @brief Render text as a QR code with qrencode and show it in a popup at
86 : * the cursor; failures are reported through statusMessage().
87 : * @param text Text to convert into a QR code.
88 : */
89 : void showTextAsQRCode(const QString &text);
90 : /**
91 : * @brief Close the QR code popup showTextAsQRCode() has open, if any: the
92 : * autoclear of the panel takes the code it shows off the screen too.
93 : */
94 : void closeQRCode();
95 :
96 : signals:
97 : /**
98 : * @brief Formatted output (see formatOutput()) or an error rendered in
99 : * colour, ready for the text browser.
100 : * @param html The HTML to show.
101 : */
102 : void outputReady(const QString &html);
103 : /**
104 : * @brief A backend operation is over, successfully or not; the interface
105 : * can be enabled again.
106 : */
107 : void operationFinished();
108 : /**
109 : * @brief A short message for the status bar.
110 : * @param message The text.
111 : * @param timeout Milliseconds to keep it, as QStatusBar::showMessage().
112 : */
113 : void statusMessage(const QString &message, int timeout);
114 : /**
115 : * @brief The store changed and the settings ask for an automatic push.
116 : */
117 : void pushRequested();
118 : /**
119 : * @brief An entry was written; the view of the current entry is stale.
120 : */
121 : void entryInserted();
122 :
123 : private:
124 : /**
125 : * @brief The clipboard handling for this application instance, exposed
126 : * through clipboard(); parented to this object.
127 : */
128 : ClipboardManager m_clipboard;
129 : /// The QR code popup while showTextAsQRCode() has it open.
130 : QPointer<QDialog> m_qrPopup;
131 :
132 : /**
133 : * @brief Connect the completion signals of one backend to the slots that
134 : * turn them into output, status and "finished" signals. Both
135 : * backends are wired up at construction because the active one can
136 : * change through the configuration dialog.
137 : * @param pass The backend to listen to.
138 : */
139 : void connectPassSignalHandlers(Pass *pass);
140 :
141 : /**
142 : * @brief Show a process' stderr in colour through outputReady(): red for a
143 : * failure, dark grey for the chatter a successful command prints
144 : * (see issue #111). Nothing is shown for empty output.
145 : * @param exitCode The process exit code; 0 selects grey.
146 : * @param error The stderr text.
147 : */
148 : void reportError(int exitCode, const QString &error);
149 :
150 : private slots:
151 : /**
152 : * @brief Slot for Pass::processErrorExit: report the error in red and emit
153 : * operationFinished().
154 : * @param exitCode The exit code of the failed process.
155 : * @param error Its stderr.
156 : */
157 : void processErrorExit(int exitCode, const QString &error);
158 :
159 : /**
160 : * @brief Slot for Pass::finishedGitPull / finishedGitPush and the common
161 : * tail of the other slots: emit the stdout as formatted output (if
162 : * any), the stderr as grey chatter, then operationFinished().
163 : * @param output stdout of the process.
164 : * @param errout stderr of the process.
165 : */
166 : void processFinished(const QString &output, const QString &errout);
167 :
168 : /**
169 : * @brief Slot for the backend signals that change the store on disk (git
170 : * init, remove, init, move, copy): processFinished() followed by
171 : * doGitPush().
172 : * @param output stdout of the process.
173 : * @param errout stderr of the process.
174 : */
175 : void passStoreChanged(const QString &output, const QString &errout);
176 :
177 : /** @brief Emit pushRequested() when the settings ask for automatic push. */
178 : void doGitPush();
179 :
180 : /**
181 : * @brief Slot for Pass::finishedInsert: like passStoreChanged(), then emit
182 : * entryInserted() so the window refreshes the shown entry.
183 : * @param output stdout of the process.
184 : * @param errout stderr of the process.
185 : */
186 : void finishedInsert(const QString &output, const QString &errout);
187 :
188 : /**
189 : * @brief Slot for Pass::finishedGenerateGPGKeys: a status message followed
190 : * by processFinished(). The KeygenDialog listens to the same signal
191 : * and closes itself.
192 : * @param output stdout of the key generation process.
193 : * @param errout stderr of the key generation process.
194 : */
195 : void onKeyGenerationComplete(const QString &output, const QString &errout);
196 : };
197 :
198 : #endif // SRC_QTPASS_H_
|