]> vaikene.ee Git - evaf/blob - src/apps/FileFinder/Engine/engine.h
Added the FileFinder application.
[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 /**
185 * Starts a new search.
186 * @param dir Directory where to search
187 * @param recursive If true, searches recurively in sub-directories
188 * @param filter Search filter
189 *
190 * The search() function starts a new search. If a search is already ongoing,
191 * cancels it and starts a new one.
192 *
193 * Search results are reported back with the found() signal and the finished()
194 * signal indicates that the search is done.
195 */
196 void search(QString const & dir, bool recursive, Filter const & filter);
197
198 /**
199 * Indicates that a search is ongoing and the worker thread busy.
200 */
201 bool busy() const;
202
203 /**
204 * Cancels an ongoing search.
205 */
206 void cancel();
207
208 /**
209 * Stops and terminates the worker thread.
210 */
211 void stop();
212
213
214 signals:
215
216 /**
217 * A file was found during the search.
218 * @param fileName Name of the file with a path relative to the search directory
219 * @param dir Search directory
220 */
221 void found(QString const & fileName, QString const & dir);
222
223 /**
224 * Signal indicating that a search is finished.
225 * @param canceled True if the search was canceled instead of finished
226 */
227 void finished(bool canceled);
228
229
230 protected:
231
232 virtual void run();
233
234
235 private: // Members
236
237 /// Size of the read buffer
238 static int const ReadBufferSize;
239
240 /// RW access lock
241 mutable QMutex mLock;
242
243 /// Wait for something to do
244 QWaitCondition mSomethingToDo;
245
246 QString mNewDir;
247 QDir mDir;
248
249 bool mNewRecursive;
250 bool mRecursive;
251
252 Filter mNewFilter;
253 RegExpChain mRxIncludeNames;
254 RegExpChain mRxExcludeNames;
255 QRegExp mRxIncludeContent;
256 QRegExp mRxExcludeContent;
257
258 bool mDoSearch;
259
260 bool mDoTerminate;
261
262 bool mDoCancel;
263
264 bool mBusy;
265
266
267 private: // Methods
268
269 /**
270 * Searches for files in the given directory
271 * @param path Path to the directory
272 *
273 * The recursiveSearch() function searches for files in the given directory and emits found()
274 * signals for every file found. If the mRecursive flag is set to true, searches recurively
275 * in all the sub-directories.
276 */
277 void recursiveSearch(QString const & path);
278
279 };
280
281 } // namespace eVaf::FileFinder::Engine::Internal
282 } // namespace eVaf::FileFinder::Engine
283 } // namespace eVaf::FileFinder
284 } // namespace eVaf
285
286 #endif // engine.h