Integrated file selection dialogue with the main code. Improved the
[openvpnui.git] / src / filebrowse / searchengine.cpp
1 #include "searchengine.h"
2 #include <QDateTime>
3 #include "searchworker.h"
4 #include "globals.h"
5
6 SearchEngine::SearchEngine(QObject *parent) :
7 QObject(parent)
8 {
9 m_dir = "";
10 m_searchWorker = new SearchWorker;
11 connect(m_searchWorker, SIGNAL(matchFound(QString)), this, SLOT(emitMatchFound(QString)));
12
13 // pass worker end signals to QML
14 connect(m_searchWorker, SIGNAL(progressChanged(QString)),
15 this, SIGNAL(progressChanged(QString)));
16 connect(m_searchWorker, SIGNAL(done()), this, SIGNAL(workerDone()));
17 connect(m_searchWorker, SIGNAL(errorOccurred(QString, QString)),
18 this, SIGNAL(workerErrorOccurred(QString, QString)));
19
20 connect(m_searchWorker, SIGNAL(started()), this, SIGNAL(runningChanged()));
21 connect(m_searchWorker, SIGNAL(finished()), this, SIGNAL(runningChanged()));
22 }
23
24 SearchEngine::~SearchEngine()
25 {
26 // is this the way to force stop the worker thread?
27 m_searchWorker->cancel(); // stop possibly running background thread
28 m_searchWorker->wait(); // wait until thread stops
29 delete m_searchWorker; // delete it
30 }
31
32 void SearchEngine::setDir(QString dir)
33 {
34 if (m_dir == dir)
35 return;
36
37 m_dir = dir;
38
39 emit dirChanged();
40 }
41
42 bool SearchEngine::running() const
43 {
44 return m_searchWorker->isRunning();
45 }
46
47 void SearchEngine::search(QString searchTerm)
48 {
49 // if search term is not empty, then restart search
50 if (!searchTerm.isEmpty()) {
51 m_searchWorker->cancel();
52 m_searchWorker->wait();
53 m_searchWorker->startSearch(m_dir, searchTerm);
54 }
55 }
56
57 void SearchEngine::cancel()
58 {
59 m_searchWorker->cancel();
60 }
61
62 void SearchEngine::emitMatchFound(QString fullpath)
63 {
64 QFileInfo info(fullpath);
65 emit matchFound(fullpath, info.fileName(), info.absoluteDir().absolutePath(),
66 infoToIconName(info), infoToFileKind(info));
67 }