2 * @file FileFinder/Engine/engine.h
3 * @brief Module for the FileFinder application that searches for files
6 * Copyright (c) 2011-2019 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"
26 #include <Plugins/iPlugin>
33 #include <QWaitCondition>
34 #include <QStringList>
40 * Application for searching files.
42 namespace FileFinder
{
45 * Module that implements the engine for searching files.
50 * Internal implementation of the FileFinder engine.
54 } // namespace eVaf::FileFinder::Engine::Internal
57 * Module for the FileFinder application that searches for files.
59 * The Module class implements the Plugins::iPlugin interface and
60 * creates the Internal::Engine object for file searching.
62 class Module
: public Plugins::iPlugin
65 Q_INTERFACES(eVaf::Plugins::iPlugin
)
66 Q_PLUGIN_METADATA(IID VER_MODULE_NAME_STR
)
74 virtual bool init(QString
const & args
);
78 virtual bool isReady() const { return mReady
; }
83 /// Flag indicating that the module is ready
86 /// iFileFinder interface implementation
87 Internal::Engine
* mEngine
;
97 * Implements the iFileFinder interface.
99 * The Internal::Engine class implements the iFileFinder interface.
100 * Searching for files is done in a separate worker thread Internal::Worker.
102 class Engine
: public iFileFinder
105 Q_INTERFACES(eVaf::FileFinder::iFileFinder
)
117 virtual void search(QString
const & dir
, bool recursive
, Filter
const & filter
);
119 virtual void cancel();
121 virtual bool busy() const;
132 * A chain of QRegExp patterns.
144 void setPattern(QString
const & pattern
);
148 bool isEmpty() const { return mPatterns
.isEmpty(); }
150 bool isValid() const { return mValid
; }
152 bool exactMatch(QString
const & str
) const;
157 QVector
<QRegExp
*> mPatterns
;
162 * Splits the string str into individual patterns separated by commas
163 * @param str Input string
164 * @return List of individual patterns
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 '\'.
169 QStringList
split(QString
const & str
);
173 * Worker thread searching for files.
175 * The Worker class is a separate thread that searches for files and then emits
176 * the found() signal on every file found.
178 class Worker
: public QThread
184 Worker(QObject
* parent
= nullptr);
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
194 * The search() function starts a new search. If a search is already ongoing,
195 * cancels it and starts a new one.
197 * Search results are reported back with the found() signal and the finished()
198 * signal indicates that the search is done.
200 void search(QString
const & dir
, bool recursive
, Filter
const & filter
);
203 * Indicates that a search is ongoing and the worker thread busy.
208 * Cancels an ongoing search.
213 * Stops and terminates the worker thread.
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
225 void found(QString
const & fileName
, QString
const & dir
);
228 * Signal indicating that a search is finished.
229 * @param canceled True if the search was canceled instead of finished
231 void finished(bool canceled
);
241 /// Size of the read buffer
242 static int const ReadBufferSize
;
245 mutable QMutex mLock
;
247 /// Wait for something to do
248 QWaitCondition mSomethingToDo
;
257 RegExpChain mRxIncludeNames
;
258 RegExpChain mRxExcludeNames
;
259 QRegExp mRxIncludeContent
;
260 QRegExp mRxExcludeContent
;
274 * Searches for files in the given directory
275 * @param path Path to the directory
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.
281 void recursiveSearch(QString
const & path
);
285 } // namespace eVaf::FileFinder::Engine::Internal
286 } // namespace eVaf::FileFinder::Engine
287 } // namespace eVaf::FileFinder