2 * @file FileFinder/Engine/engine.h
3 * @brief Module for the FileFinder application that searches for files
6 * Copyright (c) 2011 Enar Vaikene
8 * This file is part of the eVaf C++ cross-platform application development framework.
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.
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
20 #ifndef __FILEFINDER_ENGINE_ENGINE_H
21 # define __FILEFINDER_ENGINE_ENGINE_H
23 #include "ifilefinder.h"
25 #include <Plugins/iPlugin>
32 #include <QWaitCondition>
33 #include <QStringList>
39 * Application for searching files.
41 namespace FileFinder
{
44 * Module that implements the engine for searching files.
49 * Internal implementation of the FileFinder engine.
53 } // namespace eVaf::FileFinder::Engine::Internal
56 * Module for the FileFinder application that searches for files.
58 * The Module class implements the Plugins::iPlugin interface and
59 * creates the Internal::Engine object for file searching.
61 class Module
: public Plugins::iPlugin
64 Q_INTERFACES(eVaf::Plugins::iPlugin
)
72 virtual bool init(QString
const & args
);
76 virtual bool isReady() const { return mReady
; }
81 /// Flag indicating that the module is ready
84 /// iFileFinder interface implementation
85 Internal::Engine
* mEngine
;
95 * Implements the iFileFinder interface.
97 * The Internal::Engine class implements the iFileFinder interface.
98 * Searching for files is done in a separate worker thread Internal::Worker.
100 class Engine
: public iFileFinder
103 Q_INTERFACES(eVaf::FileFinder::iFileFinder
)
115 virtual void search(QString
const & dir
, bool recursive
, Filter
const & filter
);
117 virtual void cancel();
119 virtual bool busy() const;
130 * A chain of QRegExp patterns.
142 void setPattern(QString
const & pattern
);
146 bool isEmpty() const { return mPatterns
.isEmpty(); }
148 bool isValid() const { return mValid
; }
150 bool exactMatch(QString
const & str
) const;
155 QVector
<QRegExp
*> mPatterns
;
160 * Splits the string str into individual patterns separated by commas
161 * @param str Input string
162 * @return List of individual patterns
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 '\'.
167 QStringList
split(QString
const & str
);
171 * Worker thread searching for files.
173 * The Worker class is a separate thread that searches for files and then emits
174 * the found() signal on every file found.
176 class Worker
: public QThread
182 Worker(QObject
* parent
= 0);
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
190 * The search() function starts a new search. If a search is already ongoing,
191 * cancels it and starts a new one.
193 * Search results are reported back with the found() signal and the finished()
194 * signal indicates that the search is done.
196 void search(QString
const & dir
, bool recursive
, Filter
const & filter
);
199 * Indicates that a search is ongoing and the worker thread busy.
204 * Cancels an ongoing search.
209 * Stops and terminates the worker thread.
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
221 void found(QString
const & fileName
, QString
const & dir
);
224 * Signal indicating that a search is finished.
225 * @param canceled True if the search was canceled instead of finished
227 void finished(bool canceled
);
237 /// Size of the read buffer
238 static int const ReadBufferSize
;
241 mutable QMutex mLock
;
243 /// Wait for something to do
244 QWaitCondition mSomethingToDo
;
253 RegExpChain mRxIncludeNames
;
254 RegExpChain mRxExcludeNames
;
255 QRegExp mRxIncludeContent
;
256 QRegExp mRxExcludeContent
;
270 * Searches for files in the given directory
271 * @param path Path to the directory
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.
277 void recursiveSearch(QString
const & path
);
281 } // namespace eVaf::FileFinder::Engine::Internal
282 } // namespace eVaf::FileFinder::Engine
283 } // namespace eVaf::FileFinder