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
106
107
108
109
110
111
112
113
114
115
116
117
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qfilesystemwatcher_polling_p.h"
#include <QtCore/qscopeguard.h>
#include <QtCore/qtimer.h>
QT_BEGIN_NAMESPACE
QPollingFileSystemWatcherEngine::QPollingFileSystemWatcherEngine(QObject *parent)
: QFileSystemWatcherEngine(parent),
timer(this)
{
connect(&timer, SIGNAL(timeout()), SLOT(timeout()));
}
QStringList QPollingFileSystemWatcherEngine::addPaths(const QStringList &paths,
QStringList *files,
QStringList *directories)
{
QStringList unhandled;
for (const QString &path : paths) {
auto sg = qScopeGuard([&]{ unhandled.push_back(path); });
QFileInfo fi(path);
if (!fi.exists())
continue;
if (fi.isDir()) {
if (directories->contains(path))
continue;
directories->append(path);
if (!path.endsWith(u'/'))
fi = QFileInfo(path + u'/');
this->directories.insert(path, fi);
} else {
if (files->contains(path))
continue;
files->append(path);
this->files.insert(path, fi);
}
sg.dismiss();
}
if ((!this->files.isEmpty() ||
!this->directories.isEmpty()) &&
!timer.isActive()) {
timer.start(PollingInterval);
}
return unhandled;
}
QStringList QPollingFileSystemWatcherEngine::removePaths(const QStringList &paths,
QStringList *files,
QStringList *directories)
{
QStringList unhandled;
for (const QString &path : paths) {
if (this->directories.remove(path)) {
directories->removeAll(path);
} else if (this->files.remove(path)) {
files->removeAll(path);
} else {
unhandled.push_back(path);
}
}
if (this->files.isEmpty() &&
this->directories.isEmpty()) {
timer.stop();
}
return unhandled;
}
void QPollingFileSystemWatcherEngine::timeout()
{
for (auto it = files.begin(), end = files.end(); it != end; /*erasing*/) {
QString path = it.key();
QFileInfo fi(path);
if (!fi.exists()) {
it = files.erase(it);
emit fileChanged(path, true);
continue;
} else if (it.value() != fi) {
it.value() = fi;
emit fileChanged(path, false);
}
++it;
}
for (auto it = directories.begin(), end = directories.end(); it != end; /*erasing*/) {
QString path = it.key();
QFileInfo fi(path);
if (!path.endsWith(u'/'))
fi = QFileInfo(path + u'/');
if (!fi.exists()) {
it = directories.erase(it);
emit directoryChanged(path, true);
continue;
} else if (it.value() != fi) {
fi.refresh();
if (!fi.exists()) {
it = directories.erase(it);
emit directoryChanged(path, true);
continue;
} else {
it.value() = fi;
emit directoryChanged(path, false);
}
}
++it;
}
}
QT_END_NAMESPACE
#include "moc_qfilesystemwatcher_polling_p.cpp"
|