| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qfilesystemwatcher_polling_p.h" |
| 41 | #include <QtCore/qscopeguard.h> |
| 42 | #include <QtCore/qtimer.h> |
| 43 | |
| 44 | QT_BEGIN_NAMESPACE |
| 45 | |
| 46 | QPollingFileSystemWatcherEngine::QPollingFileSystemWatcherEngine(QObject *parent) |
| 47 | : QFileSystemWatcherEngine(parent), |
| 48 | timer(this) |
| 49 | { |
| 50 | connect(asender: &timer, SIGNAL(timeout()), SLOT(timeout())); |
| 51 | } |
| 52 | |
| 53 | QStringList QPollingFileSystemWatcherEngine::addPaths(const QStringList &paths, |
| 54 | QStringList *files, |
| 55 | QStringList *directories) |
| 56 | { |
| 57 | QStringList unhandled; |
| 58 | for (const QString &path : paths) { |
| 59 | auto sg = qScopeGuard(f: [&]{ unhandled.push_back(t: path); }); |
| 60 | QFileInfo fi(path); |
| 61 | if (!fi.exists()) |
| 62 | continue; |
| 63 | if (fi.isDir()) { |
| 64 | if (directories->contains(str: path)) |
| 65 | continue; |
| 66 | directories->append(t: path); |
| 67 | if (!path.endsWith(c: QLatin1Char('/'))) |
| 68 | fi = QFileInfo(path + QLatin1Char('/')); |
| 69 | this->directories.insert(akey: path, avalue: fi); |
| 70 | } else { |
| 71 | if (files->contains(str: path)) |
| 72 | continue; |
| 73 | files->append(t: path); |
| 74 | this->files.insert(akey: path, avalue: fi); |
| 75 | } |
| 76 | sg.dismiss(); |
| 77 | } |
| 78 | |
| 79 | if ((!this->files.isEmpty() || |
| 80 | !this->directories.isEmpty()) && |
| 81 | !timer.isActive()) { |
| 82 | timer.start(msec: PollingInterval); |
| 83 | } |
| 84 | |
| 85 | return unhandled; |
| 86 | } |
| 87 | |
| 88 | QStringList QPollingFileSystemWatcherEngine::removePaths(const QStringList &paths, |
| 89 | QStringList *files, |
| 90 | QStringList *directories) |
| 91 | { |
| 92 | QStringList unhandled; |
| 93 | for (const QString &path : paths) { |
| 94 | if (this->directories.remove(akey: path)) { |
| 95 | directories->removeAll(t: path); |
| 96 | } else if (this->files.remove(akey: path)) { |
| 97 | files->removeAll(t: path); |
| 98 | } else { |
| 99 | unhandled.push_back(t: path); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if (this->files.isEmpty() && |
| 104 | this->directories.isEmpty()) { |
| 105 | timer.stop(); |
| 106 | } |
| 107 | |
| 108 | return unhandled; |
| 109 | } |
| 110 | |
| 111 | void QPollingFileSystemWatcherEngine::timeout() |
| 112 | { |
| 113 | for (auto it = files.begin(), end = files.end(); it != end; /*erasing*/) { |
| 114 | auto x = it++; |
| 115 | QString path = x.key(); |
| 116 | QFileInfo fi(path); |
| 117 | if (!fi.exists()) { |
| 118 | files.erase(it: x); |
| 119 | emit fileChanged(path, removed: true); |
| 120 | } else if (x.value() != fi) { |
| 121 | x.value() = fi; |
| 122 | emit fileChanged(path, removed: false); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | for (auto it = directories.begin(), end = directories.end(); it != end; /*erasing*/) { |
| 127 | auto x = it++; |
| 128 | QString path = x.key(); |
| 129 | QFileInfo fi(path); |
| 130 | if (!path.endsWith(c: QLatin1Char('/'))) |
| 131 | fi = QFileInfo(path + QLatin1Char('/')); |
| 132 | if (!fi.exists()) { |
| 133 | directories.erase(it: x); |
| 134 | emit directoryChanged(path, removed: true); |
| 135 | } else if (x.value() != fi) { |
| 136 | fi.refresh(); |
| 137 | if (!fi.exists()) { |
| 138 | directories.erase(it: x); |
| 139 | emit directoryChanged(path, removed: true); |
| 140 | } else { |
| 141 | x.value() = fi; |
| 142 | emit directoryChanged(path, removed: false); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | QT_END_NAMESPACE |
| 149 | |
| 150 | #include "moc_qfilesystemwatcher_polling_p.cpp" |
| 151 | |