kdelibs/kdecore/io/kdirwatch.cpp

150 lines
4 KiB
C++
Raw Normal View History

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>
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>
#include <QStringList>
#include <QDir>
2014-11-13 01:04:59 +02:00
KDirWatchPrivate::KDirWatchPrivate()
: watcher(new QFileSystemWatcher())
2014-11-13 01:04:59 +02:00
{
}
KDirWatchPrivate::~KDirWatchPrivate()
{
watcher->deleteLater();
2014-11-13 01:04:59 +02:00
}
Q_GLOBAL_STATIC(KDirWatch, globalWatch)
2014-11-13 01:04:59 +02:00
KDirWatch* KDirWatch::self()
{
return globalWatch();
2014-11-13 01:04:59 +02:00
}
KDirWatch::KDirWatch (QObject* parent)
: QObject(parent), d(new KDirWatchPrivate())
2014-11-13 01:04:59 +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()
{
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;
}
void KDirWatch::addDir(const QString& path, WatchModes watchModes)
2014-11-13 01:04:59 +02:00
{
if (path.isEmpty() || path.startsWith(QLatin1String("/dev"))) {
return; // Don't even go there.
}
if (watchModes & WatchDirOnly || watchModes == WatchDirOnly) {
d->watcher->addPath(path);
return;
2014-11-13 01:04:59 +02:00
}
kDebug(7001) << "listing" << path << watchModes;
QDir::Filters filters = QDir::NoDotAndDotDot;
if (watchModes & WatchFiles) {
filters |= QDir::Files;
2014-11-13 01:04:59 +02:00
}
if (watchModes & WatchSubDirs) {
filters |= QDir::Dirs;
}
QDir dir(path);
if (!dir.exists()) {
// try to watch the parent directory
dir.cdUp();
}
foreach(const QFileInfo info, dir.entryInfoList(filters)) {
const QString fullpath = info.absoluteFilePath();
if (info.isDir()) {
addDir(fullpath, watchModes);
} else {
d->watcher->addPath(fullpath);
}
}
2014-11-13 01:04:59 +02:00
}
void KDirWatch::addFile(const QString& path)
2014-11-13 01:04:59 +02:00
{
if (path.isEmpty() || path.startsWith(QLatin1String("/dev"))) {
return; // Don't even go there.
} else if (d->watcher->files().contains(path)) {
return;
}
QFileInfo info(path);
if (!info.exists() && !info.isDir()) {
// try to watch the parent directory
d->watcher->addPath(info.path());
return;
}
d->watcher->addPath(path);
2014-11-13 01:04:59 +02:00
}
void KDirWatch::removeDir(const QString& path)
2014-11-13 01:04:59 +02:00
{
d->watcher->removePath(path);
2014-11-13 01:04:59 +02:00
}
void KDirWatch::removeFile(const QString &path)
2014-11-13 01:04:59 +02:00
{
d->watcher->removePath(path);
2014-11-13 01:04:59 +02:00
}
bool KDirWatch::contains(const QString &path) const
2014-11-13 01:04:59 +02:00
{
return (d->watcher->files().contains(path) || d->watcher->directories().contains(path));
2014-11-13 01:04:59 +02:00
}
void KDirWatch::setCreated(const QString &file)
2014-11-13 01:04:59 +02:00
{
kDebug(7001) << objectName() << "emitting created" << file;
emit created(file);
2014-11-13 01:04:59 +02:00
}
void KDirWatch::setDirty(const QString &file)
2014-11-13 01:04:59 +02:00
{
kDebug(7001) << objectName() << "emitting dirty" << file;
emit dirty(file);
2014-11-13 01:04:59 +02:00
}
void KDirWatch::setDeleted(const QString &file)
2014-11-13 01:04:59 +02:00
{
kDebug(7001) << objectName() << "emitting deleted" << file;
emit deleted(file);
2014-11-13 01:04:59 +02:00
}
#include "moc_kdirwatch.cpp"
2014-11-13 01:04:59 +02:00
//sven