2014-11-13 01:04:59 +02:00
|
|
|
/* This file is part of the KDE libraries
|
|
|
|
Copyright (C) 1998 Sven Radej <sven@lisa.exp.univie.ac.at>
|
|
|
|
Copyright (C) 2006 Dirk Mueller <mueller@kde.org>
|
|
|
|
Copyright (C) 2007 Flavio Castelli <flavio.castelli@gmail.com>
|
|
|
|
Copyright (C) 2008 Rafal Rzepecki <divided.mind@gmail.com>
|
|
|
|
Copyright (C) 2010 David Faure <faure@kde.org>
|
2019-05-29 12:18:09 +00:00
|
|
|
Copyright (C) 2019 Ivailo Monev <xakepa10@laimg.moc>
|
2014-11-13 01:04:59 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kdirwatch.h"
|
|
|
|
#include "kdirwatch_p.h"
|
|
|
|
|
|
|
|
#include <kdebug.h>
|
2019-05-29 12:18:09 +00:00
|
|
|
#include <QStringList>
|
|
|
|
#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()
|
|
|
|
{
|
2019-05-29 12:18:09 +00:00
|
|
|
return globalWatch();
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
KDirWatch::KDirWatch (QObject* parent)
|
2019-05-29 12:18:09 +00:00
|
|
|
: 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;
|
|
|
|
}
|
|
|
|
|
2019-05-29 12:18:09 +00:00
|
|
|
void KDirWatch::addDir(const QString& path, WatchModes watchModes)
|
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.
|
2019-06-10 22:55:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 16:48:33 +00:00
|
|
|
if (watchModes & WatchDirOnly || watchModes == WatchDirOnly) {
|
2019-05-29 12:18:09 +00:00
|
|
|
d->watcher->addPath(path);
|
|
|
|
return;
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
2019-05-29 16:48:33 +00:00
|
|
|
|
|
|
|
kDebug(7001) << "listing" << path << watchModes;
|
|
|
|
QDir::Filters filters = QDir::NoDotAndDotDot;
|
2019-05-29 12:18:09 +00:00
|
|
|
if (watchModes & WatchFiles) {
|
|
|
|
filters |= QDir::Files;
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
2019-05-29 12:18:09 +00:00
|
|
|
if (watchModes & WatchSubDirs) {
|
|
|
|
filters |= QDir::Dirs;
|
|
|
|
}
|
|
|
|
QDir dir(path);
|
2019-06-10 22:55:26 +00:00
|
|
|
if (!dir.exists()) {
|
|
|
|
// try to watch the parent directory
|
|
|
|
dir.cdUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(const QFileInfo info, dir.entryInfoList(filters)) {
|
2019-06-12 00:18:59 +00:00
|
|
|
const QString fullpath = info.absoluteFilePath();
|
2019-06-10 22:55:26 +00:00
|
|
|
if (info.isDir()) {
|
|
|
|
addDir(fullpath, watchModes);
|
|
|
|
} else {
|
|
|
|
d->watcher->addPath(fullpath);
|
|
|
|
}
|
2019-05-29 16:48:33 +00:00
|
|
|
}
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2019-05-29 12:18:09 +00: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
|
|
|
|
|
|
|
QFileInfo info(path);
|
2019-06-11 15:39:08 +00:00
|
|
|
if (!info.exists() && !info.isDir()) {
|
2019-06-10 22:55:26 +00:00
|
|
|
// try to watch the parent directory
|
|
|
|
d->watcher->addPath(info.path());
|
|
|
|
return;
|
|
|
|
}
|
2019-05-29 12:18:09 +00:00
|
|
|
d->watcher->addPath(path);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2019-05-29 12:18:09 +00: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::setCreated(const QString &file)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2019-05-29 12:18:09 +00:00
|
|
|
kDebug(7001) << objectName() << "emitting created" << file;
|
|
|
|
emit created(file);
|
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
|
|
|
{
|
2019-05-29 12:18:09 +00:00
|
|
|
kDebug(7001) << objectName() << "emitting dirty" << file;
|
|
|
|
emit dirty(file);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2019-05-29 12:18:09 +00:00
|
|
|
void KDirWatch::setDeleted(const QString &file)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2019-05-29 12:18:09 +00:00
|
|
|
kDebug(7001) << objectName() << "emitting deleted" << file;
|
|
|
|
emit deleted(file);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2015-02-27 07:40:26 +00:00
|
|
|
#include "moc_kdirwatch.cpp"
|
2014-11-13 01:04:59 +02:00
|
|
|
|
|
|
|
//sven
|