merge QFileSystemWatcherEngineUnix into QFileSystemWatcherPrivate

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-12-21 16:05:49 +02:00
parent fa121a8f02
commit ab46f5347a
6 changed files with 115 additions and 254 deletions

View file

@ -126,7 +126,6 @@ set(CORE_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/io/qstandardpaths.h
${CMAKE_CURRENT_SOURCE_DIR}/io/qfilesystemwatcher.h
${CMAKE_CURRENT_SOURCE_DIR}/io/qfilesystemwatcher_p.h
${CMAKE_CURRENT_SOURCE_DIR}/io/qfilesystemwatcher_unix_p.h
${CMAKE_CURRENT_SOURCE_DIR}/io/qfilesystementry_p.h
${CMAKE_CURRENT_SOURCE_DIR}/io/qfilesystemengine_p.h
${CMAKE_CURRENT_SOURCE_DIR}/io/qfilesystemmetadata_p.h
@ -243,7 +242,6 @@ set(CORE_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/io/qsettings.cpp
${CMAKE_CURRENT_SOURCE_DIR}/io/qstandardpaths.cpp
${CMAKE_CURRENT_SOURCE_DIR}/io/qfilesystemwatcher.cpp
${CMAKE_CURRENT_SOURCE_DIR}/io/qfilesystemwatcher_unix.cpp
${CMAKE_CURRENT_SOURCE_DIR}/io/qfilesystementry.cpp
${CMAKE_CURRENT_SOURCE_DIR}/io/qfilesystemengine_unix.cpp
${CMAKE_CURRENT_SOURCE_DIR}/io/qprocess_unix.cpp

View file

@ -25,16 +25,89 @@
#ifndef QT_NO_FILESYSTEMWATCHER
#include "qdebug.h"
#include "qfilesystemwatcher_unix_p.h"
QT_BEGIN_NAMESPACE
enum { PollingInterval = 1000 };
QFileSystemWatcherPrivate::QFileSystemWatcherPrivate()
: watcher(new QFileSystemWatcherEngineUnix())
: timer(q_ptr)
{
}
QStringList QFileSystemWatcherPrivate::addPaths(const QStringList &paths)
{
QStringList p = paths;
foreach (const QString &path, paths) {
QStatInfo fi(path, true);
if (fi.isDir() || path.endsWith(QLatin1Char('/'))) {
if (!path.endsWith(QLatin1Char('/')))
fi = QStatInfo(path + QLatin1Char('/'), true);
directories.insert(path, fi);
} else {
files.insert(path, fi);
}
p.removeAll(path);
}
if ((!files.isEmpty() || !directories.isEmpty()) && !timer.isActive()) {
timer.start(PollingInterval);
}
return p;
}
QStringList QFileSystemWatcherPrivate::removePaths(const QStringList &paths)
{
QStringList p = paths;
foreach (const QString &path, paths) {
if (directories.remove(path)) {
p.removeAll(path);
} else if (files.remove(path)) {
p.removeAll(path);
}
}
if (files.isEmpty() && directories.isEmpty()) {
timer.stop();
}
return p;
}
void QFileSystemWatcherPrivate::_q_timeout()
{
Q_Q(QFileSystemWatcher);
QMutableHashIterator<QString, QStatInfo> fit(files);
while (fit.hasNext()) {
QHash<QString, QStatInfo>::iterator x = fit.next();
QString path = x.key();
QStatInfo fi(path);
if (x.value() != fi) {
if (!fi.exists()) {
fit.remove();
} else {
x.value() = fi;
}
emit q->fileChanged(path);
}
}
QMutableHashIterator<QString, QStatInfo> dit(directories);
while (dit.hasNext()) {
QHash<QString, QStatInfo>::iterator x = dit.next();
QString path = x.key();
QStatInfo fi(path, true);
if (!path.endsWith(QLatin1Char('/')))
fi = QStatInfo(path + QLatin1Char('/'), true);
if (!fi.dirEquals(x.value())) {
if (!fi.exists()) {
dit.remove();
} else {
x.value() = fi;
}
emit q->directoryChanged(path);
}
}
}
/*!
\class QFileSystemWatcher
\brief The QFileSystemWatcher class provides an interface for monitoring files and directories for modifications.
@ -71,14 +144,7 @@ QFileSystemWatcher::QFileSystemWatcher(QObject *parent)
: QObject(*new QFileSystemWatcherPrivate, parent)
{
Q_D(QFileSystemWatcher);
connect(
d->watcher, SIGNAL(fileChanged(QString)),
this, SIGNAL(fileChanged(QString))
);
connect(
d->watcher, SIGNAL(directoryChanged(QString)),
this, SIGNAL(directoryChanged(QString))
);
connect(&d->timer, SIGNAL(timeout()), this, SLOT(_q_timeout()));
}
/*!
@ -89,15 +155,8 @@ QFileSystemWatcher::QFileSystemWatcher(const QStringList &paths, QObject *parent
: QObject(*new QFileSystemWatcherPrivate, parent)
{
Q_D(QFileSystemWatcher);
connect(
d->watcher, SIGNAL(fileChanged(QString)),
this, SIGNAL(fileChanged(QString))
);
connect(
d->watcher, SIGNAL(directoryChanged(QString)),
this, SIGNAL(directoryChanged(QString))
);
addPaths(paths);
connect(&d->timer, SIGNAL(timeout()), this, SLOT(_q_timeout()));
}
/*!
@ -106,10 +165,7 @@ QFileSystemWatcher::QFileSystemWatcher(const QStringList &paths, QObject *parent
QFileSystemWatcher::~QFileSystemWatcher()
{
Q_D(QFileSystemWatcher);
if (d->watcher) {
delete d->watcher;
d->watcher = nullptr;
}
d->timer.stop();
}
/*!
@ -157,10 +213,7 @@ void QFileSystemWatcher::addPaths(const QStringList &paths)
return;
}
QStringList p = paths;
if (Q_LIKELY(d->watcher))
p = d->watcher->addPaths(paths);
QStringList p = d->addPaths(paths);
if (Q_UNLIKELY(!p.isEmpty())) {
qWarning("QFileSystemWatcher: failed to add paths: %s",
qPrintable(p.join(QLatin1String(", "))));
@ -193,8 +246,29 @@ void QFileSystemWatcher::removePaths(const QStringList &paths)
return;
}
Q_D(QFileSystemWatcher);
if (Q_LIKELY(d->watcher))
d->watcher->removePaths(paths);
d->removePaths(paths);
}
/*!
Returns a list of paths to directories that are being watched.
\sa files()
*/
QStringList QFileSystemWatcher::directories() const
{
Q_D(const QFileSystemWatcher);
return d->directories.keys();
}
/*!
Returns a list of paths to files that are being watched.
\sa directories()
*/
QStringList QFileSystemWatcher::files() const
{
Q_D(const QFileSystemWatcher);
return d->files.keys();
}
/*!
@ -219,34 +293,6 @@ void QFileSystemWatcher::removePaths(const QStringList &paths)
\sa fileChanged()
*/
/*!
\fn QStringList QFileSystemWatcher::directories() const
Returns a list of paths to directories that are being watched.
\sa files()
*/
/*!
\fn QStringList QFileSystemWatcher::files() const
Returns a list of paths to files that are being watched.
\sa directories()
*/
QStringList QFileSystemWatcher::directories() const
{
Q_D(const QFileSystemWatcher);
return d->watcher->directories.keys();
}
QStringList QFileSystemWatcher::files() const
{
Q_D(const QFileSystemWatcher);
return d->watcher->files.keys();
}
QT_END_NAMESPACE
#include "moc_qfilesystemwatcher.h"

View file

@ -53,6 +53,9 @@ public:
Q_SIGNALS:
void fileChanged(const QString &path);
void directoryChanged(const QString &path);
private:
Q_PRIVATE_SLOT(d_func(), void _q_timeout())
};
QT_END_NAMESPACE

View file

@ -38,9 +38,9 @@
#ifndef QT_NO_FILESYSTEMWATCHER
#include "qobject_p.h"
#include "qfilesystemwatcher_unix_p.h"
#include <QtCore/qstringlist.h>
#include "qtimer.h"
#include "qcore_unix_p.h"
#include "qstringlist.h"
QT_BEGIN_NAMESPACE
@ -51,7 +51,13 @@ class QFileSystemWatcherPrivate : public QObjectPrivate
public:
QFileSystemWatcherPrivate();
QFileSystemWatcherEngineUnix *watcher;
QStringList addPaths(const QStringList &paths);
QStringList removePaths(const QStringList &paths);
QTimer timer;
QHash<QString, QStatInfo> files, directories;
void _q_timeout();
};
QT_END_NAMESPACE

View file

@ -1,113 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Copyright (C) 2016 Ivailo Monev
**
** This file is part of the QtCore module of the Katie Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
**
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qfilesystemwatcher_unix_p.h"
#if !defined(QT_NO_FILESYSTEMWATCHER)
#include "qdebug.h"
#include "qfile.h"
QT_BEGIN_NAMESPACE
enum { PollingInterval = 1000 };
QFileSystemWatcherEngineUnix::QFileSystemWatcherEngineUnix()
: timer(this)
{
connect(&timer, SIGNAL(timeout()), this, SLOT(timeout()));
}
QStringList QFileSystemWatcherEngineUnix::addPaths(const QStringList &paths)
{
QStringList p = paths;
foreach (const QString &path, paths) {
QStatInfo fi(path, true);
if (fi.isDir() || path.endsWith(QLatin1Char('/'))) {
if (!path.endsWith(QLatin1Char('/')))
fi = QStatInfo(path + QLatin1Char('/'), true);
directories.insert(path, fi);
} else {
files.insert(path, fi);
}
p.removeAll(path);
}
if ((!files.isEmpty() || !directories.isEmpty()) && !timer.isActive()) {
timer.start(PollingInterval);
}
return p;
}
QStringList QFileSystemWatcherEngineUnix::removePaths(const QStringList &paths)
{
QStringList p = paths;
foreach (const QString &path, paths) {
if (directories.remove(path)) {
p.removeAll(path);
} else if (files.remove(path)) {
p.removeAll(path);
}
}
if (files.isEmpty() && directories.isEmpty()) {
timer.stop();
}
return p;
}
void QFileSystemWatcherEngineUnix::timeout()
{
QMutableHashIterator<QString, QStatInfo> fit(files);
while (fit.hasNext()) {
QHash<QString, QStatInfo>::iterator x = fit.next();
QString path = x.key();
QStatInfo fi(path);
if (x.value() != fi) {
if (!fi.exists()) {
fit.remove();
} else {
x.value() = fi;
}
emit fileChanged(path);
}
}
QMutableHashIterator<QString, QStatInfo> dit(directories);
while (dit.hasNext()) {
QHash<QString, QStatInfo>::iterator x = dit.next();
QString path = x.key();
QStatInfo fi(path, true);
if (!path.endsWith(QLatin1Char('/')))
fi = QStatInfo(path + QLatin1Char('/'), true);
if (!fi.dirEquals(x.value())) {
if (!fi.exists()) {
dit.remove();
} else {
x.value() = fi;
}
emit directoryChanged(path);
}
}
}
#include "moc_qfilesystemwatcher_unix_p.h"
QT_END_NAMESPACE
#endif // QT_NO_FILESYSTEMWATCHER

View file

@ -1,79 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Copyright (C) 2016 Ivailo Monev
**
** This file is part of the QtCore module of the Katie Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
**
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QFILESYSTEMWATCHER_UNIX_P_H
#define QFILESYSTEMWATCHER_UNIX_P_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Katie API. It exists for the convenience
// of the QLibrary class. This header file may change from
// version to version without notice, or even be removed.
//
// We mean it.
//
#include "qglobal.h"
#ifndef QT_NO_FILESYSTEMWATCHER
#include "qhash.h"
#include "qfile.h"
#include "qdir.h"
#include "qfileinfo.h"
#include "qdatetime.h"
#include "qtimer.h"
#include "qcore_unix_p.h"
QT_BEGIN_NAMESPACE
class QFileSystemWatcherEngineUnix : public QObject
{
Q_OBJECT
public:
QFileSystemWatcherEngineUnix();
QStringList addPaths(const QStringList &paths);
QStringList removePaths(const QStringList &paths);
QHash<QString, QStatInfo> files, directories;
Q_SIGNALS:
void fileChanged(const QString &path);
void directoryChanged(const QString &path);
private Q_SLOTS:
void timeout();
private:
QTimer timer;
};
QT_END_NAMESPACE
#endif // QT_NO_FILESYSTEMWATCHER
#endif // QFILESYSTEMWATCHER_UNIX_P_H