QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
nativegrep.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
2// SPDX-License-Identifier: GPL-3.0-or-later
8#ifndef SRC_NATIVEGREP_H_
9#define SRC_NATIVEGREP_H_
10
11#include <QList>
12#include <QObject>
13#include <QPair>
14#include <QProcessEnvironment>
15#include <QString>
16#include <QStringList>
17#include <atomic>
18#include <memory>
19
20class QRegularExpression;
21class QThread;
22
31class NativeGrep : public QObject {
32 Q_OBJECT
33
34public:
39 explicit NativeGrep(QObject *parent = nullptr);
40
48 static constexpr int kMaxMatchesPerEntry = 100;
49
55 static constexpr int kMaxMatches = 10000;
56
68 static constexpr qsizetype kMaxPreviewLength = 200;
69
80 [[nodiscard]] static auto isMoreMarker(const QString &line) -> bool {
81 return line.isEmpty();
82 }
83
95 [[nodiscard]] static auto isOtpMarker(const QString &line) -> bool {
96 return line == QLatin1String("\n");
97 }
98
108 [[nodiscard]] static auto isPasswordMatch(const QString &line) -> bool {
109 return line.size() > 1 && line.front() == QLatin1Char('\n');
110 }
111
117 [[nodiscard]] static auto passwordMatchText(const QString &line) -> QString {
118 return line.mid(1);
119 }
120
130 [[nodiscard]] static auto
131 isStoppedMarker(const QPair<QString, QStringList> &result) -> bool {
132 return result.first.isEmpty();
133 }
134
137 ~NativeGrep() override;
138
149 void search(const QString &pattern, bool caseInsensitive,
150 const QString &gpgExe, const QString &storeDir,
151 const QProcessEnvironment &env);
152
158 void cancel();
159
180 static auto matchFile(const QProcessEnvironment &env, const QString &gpgExe,
181 const QString &filePath, const QRegularExpression &rx,
182 const std::atomic_bool *cancel = nullptr,
183 const QString &storeDir = QString()) -> QStringList;
198 static auto scanStore(const QProcessEnvironment &env, const QString &gpgExe,
199 const QString &storeDir, const QRegularExpression &rx,
200 const std::atomic_bool *cancel = nullptr)
201 -> QList<QPair<QString, QStringList>>;
202
203signals:
208 void finished(const QList<QPair<QString, QStringList>> &results);
209
210private:
214 struct Worker {
216 QThread *thread;
218 std::shared_ptr<std::atomic_bool> cancel;
219 };
220
224 int m_seq = 0;
226 QList<Worker> m_workers;
227};
228
229#endif // SRC_NATIVEGREP_H_
Full-text search over a password store without pass grep: every .gpg file is decrypted with gpg on a ...
Definition nativegrep.h:31
void finished(const QList< QPair< QString, QStringList > > &results)
The newest search is over.
void search(const QString &pattern, bool caseInsensitive, const QString &gpgExe, const QString &storeDir, const QProcessEnvironment &env)
Start a search; finished() follows asynchronously, also for an empty or invalid pattern (with no resu...
static auto isPasswordMatch(const QString &line) -> bool
Whether a line in a result is a match in the entry's first line, the password: passwordMatchText() is...
Definition nativegrep.h:108
static constexpr int kMaxMatches
Most matching lines a search collects over the whole store.
Definition nativegrep.h:55
static auto isMoreMarker(const QString &line) -> bool
Whether a line in a result is the marker for "more matches inthis entry" rather than a match.
Definition nativegrep.h:80
static constexpr qsizetype kMaxPreviewLength
Longest preview of a matching line, in UTF-16 code units.
Definition nativegrep.h:68
static constexpr int kMaxMatchesPerEntry
Most matching lines taken from one entry.
Definition nativegrep.h:48
static auto passwordMatchText(const QString &line) -> QString
The text of a match isPasswordMatch() accepts.
Definition nativegrep.h:117
static auto scanStore(const QProcessEnvironment &env, const QString &gpgExe, const QString &storeDir, const QRegularExpression &rx, const std::atomic_bool *cancel=nullptr) -> QList< QPair< QString, QStringList > >
Walk storeDir, decrypt every .gpg file and collect the matches.
static auto isStoppedMarker(const QPair< QString, QStringList > &result) -> bool
Whether a result is the marker for "the search stopped atkMaxMatches, more entries were not searched"...
Definition nativegrep.h:131
int m_seq
Number of the newest search. A worker reports through finished() only when its own number still equal...
Definition nativegrep.h:224
QList< Worker > m_workers
Searches still running, superseded ones included.
Definition nativegrep.h:226
static auto matchFile(const QProcessEnvironment &env, const QString &gpgExe, const QString &filePath, const QRegularExpression &rx, const std::atomic_bool *cancel=nullptr, const QString &storeDir=QString()) -> QStringList
Decrypt one .gpg file and return its trimmed lines matching rx.
static auto isOtpMarker(const QString &line) -> bool
Whether a line in a result stands for a match in a line holding a one-time password secret (FileConte...
Definition nativegrep.h:95
void cancel()
Ask running searches to stop without waiting for them: the walk ends at the next file and a gpg that ...
NativeGrep(QObject *parent=nullptr)
Create a searcher.
QThread * thread
The thread the search runs on.
Definition nativegrep.h:216
std::shared_ptr< std::atomic_bool > cancel
Set to ask the search to stop; polled by its gpg runs.
Definition nativegrep.h:218