2022-12-21 00:19:19 +02:00
|
|
|
/* This file is part of the KDE libraries
|
|
|
|
Copyright (C) 2019 Ivailo Monev <xakepa10@gmail.com>
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License version 2, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public License
|
|
|
|
along with this library; see the file COPYING.LIB. If not, write to
|
|
|
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA.
|
2014-11-13 01:04:59 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kdirwatch.h"
|
|
|
|
#include "kdirwatch_p.h"
|
2021-07-07 01:58:06 +03:00
|
|
|
#include "kde_file.h"
|
2014-11-13 01:04:59 +02:00
|
|
|
|
|
|
|
#include <kdebug.h>
|
2019-05-29 12:18:09 +00:00
|
|
|
#include <QDir>
|
2014-11-13 01:04:59 +02:00
|
|
|
|
|
|
|
KDirWatchPrivate::KDirWatchPrivate()
|
2019-05-29 12:18:09 +00:00
|
|
|
: watcher(new QFileSystemWatcher())
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
KDirWatchPrivate::~KDirWatchPrivate()
|
|
|
|
{
|
2019-05-29 12:18:09 +00:00
|
|
|
watcher->deleteLater();
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2019-05-29 12:18:09 +00:00
|
|
|
Q_GLOBAL_STATIC(KDirWatch, globalWatch)
|
2014-11-13 01:04:59 +02:00
|
|
|
|
|
|
|
KDirWatch* KDirWatch::self()
|
|
|
|
{
|
2022-12-21 00:19:19 +02:00
|
|
|
return globalWatch();
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2022-12-21 00:19:19 +02:00
|
|
|
KDirWatch::KDirWatch(QObject* parent)
|
|
|
|
: QObject(parent),
|
|
|
|
d(new KDirWatchPrivate())
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2021-03-19 10:28:57 +02:00
|
|
|
connect(d->watcher, SIGNAL(directoryChanged(QString)), this, SLOT(setDirty(QString)));
|
|
|
|
connect(d->watcher, SIGNAL(fileChanged(QString)), this, SLOT(setDirty(QString)));
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
KDirWatch::~KDirWatch()
|
|
|
|
{
|
2021-03-19 10:28:57 +02:00
|
|
|
disconnect(d->watcher, SIGNAL(directoryChanged(QString)), this, SLOT(setDirty(QString)));
|
|
|
|
disconnect(d->watcher, SIGNAL(fileChanged(QString)), this, SLOT(setDirty(QString)));
|
2014-11-13 01:04:59 +02:00
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2022-12-21 00:19:19 +02:00
|
|
|
void KDirWatch::addDir(const QString &path)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2019-06-10 22:55:26 +00:00
|
|
|
if (path.isEmpty() || path.startsWith(QLatin1String("/dev"))) {
|
2019-05-29 12:18:09 +00:00
|
|
|
return; // Don't even go there.
|
2021-11-14 04:05:32 +02:00
|
|
|
} else if (d->watcher->directories().contains(path)) {
|
|
|
|
return;
|
2019-06-10 22:55:26 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 00:19:19 +02:00
|
|
|
kDebug(7001) << "watching directory" << path;
|
|
|
|
// watching non-existing directory requires a trailing slash
|
|
|
|
if (!path.endsWith(QDir::separator())) {
|
|
|
|
const QString dirpath = path + QDir::separator();
|
|
|
|
d->watcheddirs.append(dirpath);
|
|
|
|
d->watcher->addPath(dirpath);
|
2019-05-29 12:18:09 +00:00
|
|
|
return;
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
2022-12-21 00:19:19 +02:00
|
|
|
d->watcheddirs.append(path);
|
|
|
|
d->watcher->addPath(path);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2022-12-21 00:19:19 +02:00
|
|
|
void KDirWatch::addFile(const QString &path)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2019-06-11 15:39:08 +00:00
|
|
|
if (path.isEmpty() || path.startsWith(QLatin1String("/dev"))) {
|
2019-05-29 12:18:09 +00:00
|
|
|
return; // Don't even go there.
|
2019-07-12 14:52:57 +00:00
|
|
|
} else if (d->watcher->files().contains(path)) {
|
|
|
|
return;
|
2019-06-11 15:39:08 +00:00
|
|
|
}
|
2019-06-10 22:55:26 +00:00
|
|
|
|
2022-12-21 00:19:19 +02:00
|
|
|
if (QDir(path).exists()) {
|
|
|
|
// trying to add dir as file, huh?
|
|
|
|
addDir(path);
|
2019-06-10 22:55:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-12-21 00:19:19 +02:00
|
|
|
|
|
|
|
kDebug(7001) << "watching file" << path;
|
|
|
|
d->watchedfiles.append(path);
|
2019-05-29 12:18:09 +00:00
|
|
|
d->watcher->addPath(path);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2022-12-21 00:19:19 +02:00
|
|
|
void KDirWatch::removeDir(const QString &path)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2019-05-29 12:18:09 +00:00
|
|
|
d->watcher->removePath(path);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2019-05-29 12:18:09 +00:00
|
|
|
void KDirWatch::removeFile(const QString &path)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2019-05-29 12:18:09 +00:00
|
|
|
d->watcher->removePath(path);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2019-05-29 12:18:09 +00:00
|
|
|
bool KDirWatch::contains(const QString &path) const
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2019-05-29 12:18:09 +00:00
|
|
|
return (d->watcher->files().contains(path) || d->watcher->directories().contains(path));
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2019-05-29 12:18:09 +00:00
|
|
|
void KDirWatch::setDirty(const QString &file)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2022-12-21 00:19:19 +02:00
|
|
|
kDebug(7001) << "emitting dirty" << file;
|
|
|
|
|
|
|
|
// QFileSystemWatcher removes the file/dir from the watched list when it is deleted, put it
|
|
|
|
// back so that events are emited in case it is created
|
|
|
|
if (d->watcheddirs.contains(file)) {
|
|
|
|
addDir(file);
|
|
|
|
} else {
|
2021-06-20 14:22:31 +03:00
|
|
|
addFile(file);
|
|
|
|
}
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2022-12-21 00:19:19 +02:00
|
|
|
emit dirty(file);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2015-02-27 07:40:26 +00:00
|
|
|
#include "moc_kdirwatch.cpp"
|