Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qfilesystemwatcher_polling.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtCore/qlatin1stringview.h>
7#include <QtCore/qscopeguard.h>
8#include <QtCore/qtimer.h>
9
10#include <chrono>
11
12using namespace std::chrono_literals;
13
15
16using namespace Qt::StringLiterals;
17
18static constexpr auto PollingInterval = 1s;
19
24
27 QStringList *directories)
28{
29 QStringList unhandled;
30 for (const QString &path : paths) {
31 auto sg = qScopeGuard([&]{ unhandled.push_back(path); });
32 QFileInfo fi(path);
33 if (!fi.exists())
34 continue;
35 if (fi.isDir()) {
36 if (directories->contains(path))
37 continue;
38 directories->append(path);
39 if (!path.endsWith(u'/'))
40 fi = QFileInfo(path + u'/');
41 this->directories.insert(path, fi);
42 } else {
43 if (files->contains(path))
44 continue;
45 files->append(path);
46 this->files.insert(path, fi);
47 }
48 sg.dismiss();
49 }
50
51 std::chrono::milliseconds interval = PollingInterval;
52#ifdef QT_BUILD_INTERNAL
53 if (Q_UNLIKELY(parent()->objectName().startsWith("_qt_autotest_force_engine_"_L1))) {
54 interval = 10ms; // Special case to speed up the unittests
55 }
56#endif
57
58 if ((!this->files.isEmpty() ||
59 !this->directories.isEmpty()) &&
60 !timer.isActive()) {
61 timer.start(interval, this);
62 }
63
64 return unhandled;
65}
66
69 QStringList *directories)
70{
71 QStringList unhandled;
72 for (const QString &path : paths) {
73 if (this->directories.remove(path)) {
74 directories->removeAll(path);
75 } else if (this->files.remove(path)) {
76 files->removeAll(path);
77 } else {
78 unhandled.push_back(path);
79 }
80 }
81
82 if (this->files.isEmpty() &&
83 this->directories.isEmpty()) {
84 timer.stop();
85 }
86
87 return unhandled;
88}
89
91{
92 if (e->timerId() != timer.timerId())
94
95 for (auto it = files.begin(), end = files.end(); it != end; /*erasing*/) {
96 QString path = it.key();
97 QFileInfo fi(path);
98 if (!fi.exists()) {
99 it = files.erase(it);
100 emit fileChanged(path, true);
101 continue;
102 } else if (it.value() != fi) {
103 it.value() = fi;
104 emit fileChanged(path, false);
105 }
106 ++it;
107 }
108
109 for (auto it = directories.begin(), end = directories.end(); it != end; /*erasing*/) {
110 QString path = it.key();
111 QFileInfo fi(path);
112 if (!path.endsWith(u'/'))
113 fi = QFileInfo(path + u'/');
114 if (!fi.exists()) {
115 it = directories.erase(it);
117 continue;
118 } else if (it.value() != fi) {
119 fi.refresh();
120 if (!fi.exists()) {
121 it = directories.erase(it);
123 continue;
124 } else {
125 it.value() = fi;
126 emit directoryChanged(path, false);
127 }
128 }
129 ++it;
130 }
131}
132
134
135#include "moc_qfilesystemwatcher_polling_p.cpp"
void start(int msec, QObject *obj)
\obsolete Use chrono overload instead.
int timerId() const noexcept
Returns the timer's ID.
Definition qbasictimer.h:35
void stop()
Stops the timer.
bool isActive() const noexcept
Returns true if the timer is running and has not been stopped; otherwise returns false.
Definition qbasictimer.h:34
void directoryChanged(const QString &path, bool removed)
void fileChanged(const QString &path, bool removed)
iterator begin()
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
Definition qhash.h:1212
iterator erase(const_iterator it)
Definition qhash.h:1233
iterator end() noexcept
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last ...
Definition qhash.h:1216
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
QString objectName
the name of this object
Definition qobject.h:107
virtual void timerEvent(QTimerEvent *event)
This event handler can be reimplemented in a subclass to receive timer events for the object.
Definition qobject.cpp:1470
void timerEvent(QTimerEvent *) final
This event handler can be reimplemented in a subclass to receive timer events for the object.
QStringList addPaths(const QStringList &paths, QStringList *files, QStringList *directories) override
QStringList removePaths(const QStringList &paths, QStringList *files, QStringList *directories) override
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qcoreevent.h:366
int timerId() const
Returns the unique timer identifier, which is the same identifier as returned from QObject::startTime...
Definition qcoreevent.h:370
#define this
Definition dialogs.cpp:9
QSet< QString >::iterator it
Combined button and popup list for selecting options.
#define Q_UNLIKELY(x)
static constexpr auto PollingInterval
GLuint GLuint end
GLsizei const GLuint * paths
GLdouble s
[6]
Definition qopenglext.h:235
GLsizei const GLchar *const * path
QScopeGuard< typename std::decay< F >::type > qScopeGuard(F &&f)
[qScopeGuard]
Definition qscopeguard.h:60
#define emit
QStringList files
[8]