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