| 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(&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([&]{ unhandled.push_back(path); }); | 
|---|
| 60 | QFileInfo fi(path); | 
|---|
| 61 | if (!fi.exists()) | 
|---|
| 62 | continue; | 
|---|
| 63 | if (fi.isDir()) { | 
|---|
| 64 | if (directories->contains(path)) | 
|---|
| 65 | continue; | 
|---|
| 66 | directories->append(path); | 
|---|
| 67 | if (!path.endsWith(QLatin1Char('/'))) | 
|---|
| 68 | fi = QFileInfo(path + QLatin1Char('/')); | 
|---|
| 69 | this->directories.insert(path, fi); | 
|---|
| 70 | } else { | 
|---|
| 71 | if (files->contains(path)) | 
|---|
| 72 | continue; | 
|---|
| 73 | files->append(path); | 
|---|
| 74 | this->files.insert(path, fi); | 
|---|
| 75 | } | 
|---|
| 76 | sg.dismiss(); | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | if ((!this->files.isEmpty() || | 
|---|
| 80 | !this->directories.isEmpty()) && | 
|---|
| 81 | !timer.isActive()) { | 
|---|
| 82 | timer.start(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(path)) { | 
|---|
| 95 | directories->removeAll(path); | 
|---|
| 96 | } else if (this->files.remove(path)) { | 
|---|
| 97 | files->removeAll(path); | 
|---|
| 98 | } else { | 
|---|
| 99 | unhandled.push_back(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 | QString path = it.key(); | 
|---|
| 115 | QFileInfo fi(path); | 
|---|
| 116 | if (!fi.exists()) { | 
|---|
| 117 | it = files.erase(it); | 
|---|
| 118 | emit fileChanged(path, true); | 
|---|
| 119 | continue; | 
|---|
| 120 | } else if (it.value() != fi) { | 
|---|
| 121 | it.value() = fi; | 
|---|
| 122 | emit fileChanged(path, false); | 
|---|
| 123 | } | 
|---|
| 124 | ++it; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | for (auto it = directories.begin(), end = directories.end(); it != end; /*erasing*/) { | 
|---|
| 128 | QString path = it.key(); | 
|---|
| 129 | QFileInfo fi(path); | 
|---|
| 130 | if (!path.endsWith(QLatin1Char('/'))) | 
|---|
| 131 | fi = QFileInfo(path + QLatin1Char('/')); | 
|---|
| 132 | if (!fi.exists()) { | 
|---|
| 133 | it = directories.erase(it); | 
|---|
| 134 | emit directoryChanged(path, true); | 
|---|
| 135 | continue; | 
|---|
| 136 | } else if (it.value() != fi) { | 
|---|
| 137 | fi.refresh(); | 
|---|
| 138 | if (!fi.exists()) { | 
|---|
| 139 | it = directories.erase(it); | 
|---|
| 140 | emit directoryChanged(path, true); | 
|---|
| 141 | continue; | 
|---|
| 142 | } else { | 
|---|
| 143 | it.value() = fi; | 
|---|
| 144 | emit directoryChanged(path, false); | 
|---|
| 145 | } | 
|---|
| 146 | } | 
|---|
| 147 | ++it; | 
|---|
| 148 | } | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | QT_END_NAMESPACE | 
|---|
| 152 |  | 
|---|
| 153 | #include "moc_qfilesystemwatcher_polling_p.cpp" | 
|---|
| 154 |  | 
|---|