]> vaikene.ee Git - evaf/blob - src/apps/FileFinder/Engine/engine.h
Warning fixes and copyright update.
[evaf] / src / apps / FileFinder / Engine / engine.h
1 /**
2 * @file FileFinder/Engine/engine.h
3 * @brief Module for the FileFinder application that searches for files
4 * @author Enar Vaikene
5 *
6 * Copyright (c) 2011-2019 Enar Vaikene
7 *
8 * This file is part of the eVaf C++ cross-platform application development framework.
9 *
10 * This file can be used under the terms of the GNU General Public License
11 * version 3.0 as published by the Free Software Foundation and appearing in
12 * the file LICENSE included in the packaging of this file. Please review the
13 * the following information to ensure the GNU General Public License version
14 * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
15 *
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
18 */
19
20 #ifndef __FILEFINDER_ENGINE_ENGINE_H
21 # define __FILEFINDER_ENGINE_ENGINE_H
22
23 #include "ifilefinder.h"
24 #include "version.h"
25
26 #include <Plugins/iPlugin>
27
28 #include <QDir>
29 #include <QRegExp>
30 #include <QVector>
31 #include <QThread>
32 #include <QMutex>
33 #include <QWaitCondition>
34 #include <QStringList>
35
36
37 namespace eVaf {
38
39 /**
40 * Application for searching files.
41 */
42 namespace FileFinder {
43
44 /**
45 * Module that implements the engine for searching files.
46 */
47 namespace Engine {
48
49 /**
50 * Internal implementation of the FileFinder engine.
51 */
52 namespace Internal {
53 class Engine;
54 } // namespace eVaf::FileFinder::Engine::Internal
55
56 /**
57 * Module for the FileFinder application that searches for files.
58 *
59 * The Module class implements the Plugins::iPlugin interface and
60 * creates the Internal::Engine object for file searching.
61 */
62 class Module : public Plugins::iPlugin
63 {
64 Q_OBJECT
65 Q_INTERFACES(eVaf::Plugins::iPlugin)
66 Q_PLUGIN_METADATA(IID VER_MODULE_NAME_STR)
67
68 public:
69
70 Module();
71
72 virtual ~Module();
73
74 virtual bool init(QString const & args);
75
76 virtual void done();
77
78 virtual bool isReady() const { return mReady; }
79
80
81 private: // Members
82
83 /// Flag indicating that the module is ready
84 bool mReady;
85
86 /// iFileFinder interface implementation
87 Internal::Engine * mEngine;
88
89 };
90
91
92 namespace Internal {
93
94 class Worker;
95
96 /**
97 * Implements the iFileFinder interface.
98 *
99 * The Internal::Engine class implements the iFileFinder interface.
100 * Searching for files is done in a separate worker thread Internal::Worker.
101 */
102 class Engine : public iFileFinder
103 {
104 Q_OBJECT
105 Q_INTERFACES(eVaf::FileFinder::iFileFinder)
106
107 public:
108
109 Engine();
110
111 virtual ~Engine();
112
113 bool init();
114
115 void done();
116
117 virtual void search(QString const & dir, bool recursive, Filter const & filter);
118
119 virtual void cancel();
120
121 virtual bool busy() const;
122
123
124 private: // Members
125
126 /// Worker thread
127 Worker * mWorker;
128
129 };
130
131 /**
132 * A chain of QRegExp patterns.
133 */
134 class RegExpChain
135 {
136 public:
137
138 RegExpChain()
139 : mValid(false)
140 {}
141
142 ~RegExpChain();
143
144 void setPattern(QString const & pattern);
145
146 void clear();
147
148 bool isEmpty() const { return mPatterns.isEmpty(); }
149
150 bool isValid() const { return mValid; }
151
152 bool exactMatch(QString const & str) const;
153
154
155 private:
156
157 QVector<QRegExp *> mPatterns;
158
159 bool mValid;
160
161 /**
162 * Splits the string str into individual patterns separated by commas
163 * @param str Input string
164 * @return List of individual patterns
165 *
166 * The split() function splits the string str into individual patterns. Patterns are
167 * separated with commas. To use the comma as part of the pattern, escape it with '\'.
168 */
169 QStringList split(QString const & str);
170 };
171
172 /**
173 * Worker thread searching for files.
174 *
175 * The Worker class is a separate thread that searches for files and then emits
176 * the found() signal on every file found.
177 */
178 class Worker : public QThread
179 {
180 Q_OBJECT
181
182 public:
183
184 Worker(QObject * parent = nullptr);
185
186 virtual ~Worker();
187
188 /**
189 * Starts a new search.
190 * @param dir Directory where to search
191 * @param recursive If true, searches recurively in sub-directories
192 * @param filter Search filter
193 *
194 * The search() function starts a new search. If a search is already ongoing,
195 * cancels it and starts a new one.
196 *
197 * Search results are reported back with the found() signal and the finished()
198 * signal indicates that the search is done.
199 */
200 void search(QString const & dir, bool recursive, Filter const & filter);
201
202 /**
203 * Indicates that a search is ongoing and the worker thread busy.
204 */
205 bool busy() const;
206
207 /**
208 * Cancels an ongoing search.
209 */
210 void cancel();
211
212 /**
213 * Stops and terminates the worker thread.
214 */
215 void stop();
216
217
218 signals:
219
220 /**
221 * A file was found during the search.
222 * @param fileName Name of the file with a path relative to the search directory
223 * @param dir Search directory
224 */
225 void found(QString const & fileName, QString const & dir);
226
227 /**
228 * Signal indicating that a search is finished.
229 * @param canceled True if the search was canceled instead of finished
230 */
231 void finished(bool canceled);
232
233
234 protected:
235
236 virtual void run();
237
238
239 private: // Members
240
241 /// Size of the read buffer
242 static int const ReadBufferSize;
243
244 /// RW access lock
245 mutable QMutex mLock;
246
247 /// Wait for something to do
248 QWaitCondition mSomethingToDo;
249
250 QString mNewDir;
251 QDir mDir;
252
253 bool mNewRecursive;
254 bool mRecursive;
255
256 Filter mNewFilter;
257 RegExpChain mRxIncludeNames;
258 RegExpChain mRxExcludeNames;
259 QRegExp mRxIncludeContent;
260 QRegExp mRxExcludeContent;
261
262 bool mDoSearch;
263
264 bool mDoTerminate;
265
266 bool mDoCancel;
267
268 bool mBusy;
269
270
271 private: // Methods
272
273 /**
274 * Searches for files in the given directory
275 * @param path Path to the directory
276 *
277 * The recursiveSearch() function searches for files in the given directory and emits found()
278 * signals for every file found. If the mRecursive flag is set to true, searches recurively
279 * in all the sub-directories.
280 */
281 void recursiveSearch(QString const & path);
282
283 };
284
285 } // namespace eVaf::FileFinder::Engine::Internal
286 } // namespace eVaf::FileFinder::Engine
287 } // namespace eVaf::FileFinder
288 } // namespace eVaf
289
290 #endif // engine.h