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
93 [[nodiscard]] static auto
94 isStoppedMarker(const QPair<QString, QStringList> &result) -> bool {
95 return result.first.isEmpty();
96 }
97
100 ~NativeGrep() override;
101
112 void search(const QString &pattern, bool caseInsensitive,
113 const QString &gpgExe, const QString &storeDir,
114 const QProcessEnvironment &env);
115
121 void cancel();
122
135 static auto matchFile(const QProcessEnvironment &env, const QString &gpgExe,
136 const QString &filePath, const QRegularExpression &rx,
137 const std::atomic_bool *cancel = nullptr)
138 -> QStringList;
153 static auto scanStore(const QProcessEnvironment &env, const QString &gpgExe,
154 const QString &storeDir, const QRegularExpression &rx,
155 const std::atomic_bool *cancel = nullptr)
156 -> QList<QPair<QString, QStringList>>;
157
158signals:
163 void finished(const QList<QPair<QString, QStringList>> &results);
164
165private:
169 struct Worker {
171 QThread *thread;
173 std::shared_ptr<std::atomic_bool> cancel;
174 };
175
179 int m_seq = 0;
181 QList<Worker> m_workers;
182};
183
184#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 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 matchFile(const QProcessEnvironment &env, const QString &gpgExe, const QString &filePath, const QRegularExpression &rx, const std::atomic_bool *cancel=nullptr) -> QStringList
Decrypt one .gpg file and return its trimmed lines matching rx.
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:94
int m_seq
Number of the newest search. A worker reports through finished() only when its own number still equal...
Definition nativegrep.h:179
QList< Worker > m_workers
Searches still running, superseded ones included.
Definition nativegrep.h:181
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:171
std::shared_ptr< std::atomic_bool > cancel
Set to ask the search to stop; polled by its gpg runs.
Definition nativegrep.h:173