Qt 6.x
The Qt SDK
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#include <QtCore/qscopeguard.h>
6#include <QtCore/qtimer.h>
7
9
12 timer(this)
13{
14 connect(&timer, SIGNAL(timeout()), SLOT(timeout()));
15}
16
19 QStringList *directories)
20{
21 QStringList unhandled;
22 for (const QString &path : paths) {
23 auto sg = qScopeGuard([&]{ unhandled.push_back(path); });
25 if (!fi.exists())
26 continue;
27 if (fi.isDir()) {
28 if (directories->contains(path))
29 continue;
30 directories->append(path);
31 if (!path.endsWith(u'/'))
32 fi = QFileInfo(path + u'/');
33 this->directories.insert(path, fi);
34 } else {
35 if (files->contains(path))
36 continue;
37 files->append(path);
38 this->files.insert(path, fi);
39 }
40 sg.dismiss();
41 }
42
43 if ((!this->files.isEmpty() ||
44 !this->directories.isEmpty()) &&
45 !timer.isActive()) {
47 }
48
49 return unhandled;
50}
51
54 QStringList *directories)
55{
56 QStringList unhandled;
57 for (const QString &path : paths) {
58 if (this->directories.remove(path)) {
59 directories->removeAll(path);
60 } else if (this->files.remove(path)) {
61 files->removeAll(path);
62 } else {
63 unhandled.push_back(path);
64 }
65 }
66
67 if (this->files.isEmpty() &&
68 this->directories.isEmpty()) {
69 timer.stop();
70 }
71
72 return unhandled;
73}
74
75void QPollingFileSystemWatcherEngine::timeout()
76{
77 for (auto it = files.begin(), end = files.end(); it != end; /*erasing*/) {
78 QString path = it.key();
80 if (!fi.exists()) {
81 it = files.erase(it);
82 emit fileChanged(path, true);
83 continue;
84 } else if (it.value() != fi) {
85 it.value() = fi;
86 emit fileChanged(path, false);
87 }
88 ++it;
89 }
90
91 for (auto it = directories.begin(), end = directories.end(); it != end; /*erasing*/) {
92 QString path = it.key();
94 if (!path.endsWith(u'/'))
95 fi = QFileInfo(path + u'/');
96 if (!fi.exists()) {
97 it = directories.erase(it);
99 continue;
100 } else if (it.value() != fi) {
101 fi.refresh();
102 if (!fi.exists()) {
103 it = directories.erase(it);
105 continue;
106 } else {
107 it.value() = fi;
108 emit directoryChanged(path, false);
109 }
110 }
111 ++it;
112 }
113}
114
116
117#include "moc_qfilesystemwatcher_polling_p.cpp"
\inmodule QtCore \reentrant
Definition qfileinfo.h:22
void refresh()
Refreshes the information about the file, i.e.
bool isDir() const
Returns true if this object points to a directory or to a symbolic link to a directory.
bool exists() const
Returns true if the file exists; otherwise returns false.
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:1202
iterator erase(const_iterator it)
Definition qhash.h:1223
iterator end() noexcept
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last ...
Definition qhash.h:1206
\inmodule QtCore
Definition qobject.h:90
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
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:127
void start(int msec)
Starts or restarts the timer with a timeout interval of msec milliseconds.
Definition qtimer.cpp:208
bool isActive() const
Returns true if the timer is running (pending); otherwise returns false.
Definition qtimer.cpp:156
void stop()
Stops the timer.
Definition qtimer.cpp:226
#define this
Definition dialogs.cpp:9
QSet< QString >::iterator it
Combined button and popup list for selecting options.
#define SLOT(a)
Definition qobjectdefs.h:51
#define SIGNAL(a)
Definition qobjectdefs.h:52
GLuint GLuint end
GLbitfield GLuint64 timeout
[4]
GLsizei const GLuint * paths
GLsizei const GLchar *const * path
QScopeGuard< typename std::decay< F >::type > qScopeGuard(F &&f)
[qScopeGuard]
Definition qscopeguard.h:60
#define emit
QFileInfo fi("c:/temp/foo")
[newstuff]
QTimer * timer
[3]
QStringList files
[8]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent