-
Notifications
You must be signed in to change notification settings - Fork 0
/
filewatcher.cpp
105 lines (87 loc) · 2.45 KB
/
filewatcher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <QDebug>
#include <QString>
#include "filewatcher.h"
/**
* @brief FileWatcher::FileWatcher
* This class can monitor file changes in a directory and calls a callback
* in response
* @param callback
* The callback function to be executed whenever changes are detected
*/
FileWatcher::FileWatcher(QObject *parent) : QObject(parent)
{
QObject::connect(&mWatcher, &QFileSystemWatcher::directoryChanged, this, &FileWatcher::directoryChanged);
QObject::connect(&mWatcher, &QFileSystemWatcher::fileChanged, this, &FileWatcher::fileChanged);
// configure the timer to signal the changes to the callback
mTimer.setInterval(200);
mTimer.setSingleShot(true);
// configure directory filters
mDir.setFilter(QDir::Files | QDir::NoSymLinks);
mDir.setNameFilters(QStringList() << "*.qml");
// connect timer to callback function
QObject::connect(&mTimer, &QTimer::timeout, [&](){
emit qmlChanged();
});
}
/**
* @brief FileWatcher::addPaths
* Adds the paths of the files to be monitored in the current directory
*/
void FileWatcher::addPaths()
{
QStringList entries = mDir.entryList();
for (int i=0; i<entries.size(); i++)
{
entries[i].prepend(mDir.absolutePath() + "/");
}
mWatcher.addPaths(entries);
//mTimer.start();
}
/**
* @brief FileWatcher::setDirectory
* Set the directory to watch for changes
* @param path
* Directory path
*/
void FileWatcher::setDirectory(const QString &path)
{
qDebug() << "added directory:" << path;
if (!mWatcher.addPath(path))
{
qWarning() << "Could not add path: " << path;
}
else
{
mDir.setPath(path);
addPaths();
}
emit qmlChanged();
}
/**
* @brief FileWatcher::directoryChanged
* Slot connected to the directoryChanged signal from internal QFileSystemWatcher
* @param path
* Path of the directory where the change was detected
*/
void FileWatcher::directoryChanged(const QString &path)
{
Q_UNUSED(path);
addPaths();
}
QString FileWatcher::directory() const {
return mDir.path();
}
/**
* @brief FileWatcher::fileChanged
* Slot connected to the fileChanged signal from internal QFileSystemWatcher
* @param path
* Path of the file where the change was detected
*/
void FileWatcher::fileChanged(const QString &path)
{
qDebug() << "changed in dir:" << path;
Q_UNUSED(path);
if (!mTimer.isActive()) { //prevent called twice ( on UT )
mTimer.start();
}
}