2011-02-17 02:21:45 +03:00
|
|
|
|
2011-03-16 16:50:34 +03:00
|
|
|
// event masks
|
|
|
|
#include <sys/inotify.h>
|
|
|
|
|
2011-02-17 02:21:45 +03:00
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QFileSystemWatcher>
|
|
|
|
#include <QFlags>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QMutexLocker>
|
|
|
|
#include <QStringList>
|
|
|
|
|
2011-03-16 16:50:34 +03:00
|
|
|
#include "mirall/inotify.h"
|
2011-02-17 02:21:45 +03:00
|
|
|
#include "mirall/folderwatcher.h"
|
|
|
|
|
2011-03-16 16:50:34 +03:00
|
|
|
static const uint32_t standard_event_mask =
|
|
|
|
IN_ATTRIB | IN_CLOSE_WRITE | IN_CREATE |
|
|
|
|
IN_DELETE | IN_DELETE_SELF | IN_MOVED_FROM |
|
|
|
|
IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR;
|
|
|
|
|
2011-02-17 02:21:45 +03:00
|
|
|
namespace Mirall {
|
|
|
|
|
|
|
|
enum SubFolderListOption {
|
|
|
|
SubFolderNoOptions = 0x0,
|
|
|
|
SubFolderRecursive = 0x1,
|
|
|
|
};
|
|
|
|
Q_DECLARE_FLAGS(SubFolderListOptions, SubFolderListOption)
|
|
|
|
Q_DECLARE_OPERATORS_FOR_FLAGS(SubFolderListOptions)
|
|
|
|
|
|
|
|
// Forgive me using a bool as a flag
|
|
|
|
static QStringList subFoldersList(QString folder,
|
|
|
|
SubFolderListOptions options = SubFolderNoOptions )
|
|
|
|
{
|
|
|
|
QDir dir(folder);
|
|
|
|
dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
|
|
|
|
|
|
|
|
QFileInfoList list = dir.entryInfoList();
|
|
|
|
QStringList dirList;
|
|
|
|
|
|
|
|
for (int i = 0; i < list.size(); ++i) {
|
|
|
|
QFileInfo fileInfo = list.at(i);
|
|
|
|
dirList << fileInfo.absoluteFilePath();
|
|
|
|
if (options & SubFolderRecursive )
|
|
|
|
dirList << subFoldersList(fileInfo.absoluteFilePath(), options);
|
|
|
|
}
|
|
|
|
return dirList;
|
|
|
|
}
|
|
|
|
|
|
|
|
FolderWatcher::FolderWatcher(const QString &path, QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
{
|
|
|
|
_watcher = new QFileSystemWatcher(this);
|
2011-03-17 09:13:30 +03:00
|
|
|
_inotify = new INotify(standard_event_mask);
|
|
|
|
//_inotify->addPath(path);
|
2011-02-17 02:21:45 +03:00
|
|
|
|
|
|
|
// watch the path and all subdirectories
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&_mutex);
|
|
|
|
|
|
|
|
QStringList subfolders(subFoldersList(path, SubFolderRecursive));
|
|
|
|
qDebug() << "adding watchers for " << subfolders;
|
|
|
|
|
|
|
|
QStringListIterator subfoldersIt(subfolders);
|
|
|
|
while (subfoldersIt.hasNext()) {
|
|
|
|
_watcher->addPath(subfoldersIt.next());
|
2011-03-17 09:13:30 +03:00
|
|
|
_inotify->addPath(subfoldersIt.next());
|
2011-02-17 02:21:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2011-03-17 09:13:30 +03:00
|
|
|
// QObject::connect(_watcher, SIGNAL(directoryChanged(const QString &)),
|
|
|
|
// SLOT(slotDirectoryChanged(const QString &)));
|
|
|
|
QObject::connect(_inotify, SIGNAL(notifyEvent(int, const QString &)),
|
|
|
|
SLOT(slotDirectoryChanged(int, const QString &)));
|
2011-02-17 02:21:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
FolderWatcher::~FolderWatcher()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-03-17 09:13:30 +03:00
|
|
|
void FolderWatcher::slotDirectoryChanged(int mask, const QString &path)
|
2011-02-17 02:21:45 +03:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&_mutex);
|
|
|
|
|
2011-03-17 09:13:30 +03:00
|
|
|
qDebug() << mask << " : changed: " << path;
|
2011-02-17 02:21:45 +03:00
|
|
|
|
|
|
|
qDebug() << "updating subdirectories";
|
|
|
|
|
2011-03-17 09:13:30 +03:00
|
|
|
QStringList watchedFolders(_inotify->directories());
|
2011-02-17 02:21:45 +03:00
|
|
|
QStringListIterator watchedFoldersIt(watchedFolders);
|
|
|
|
|
|
|
|
while (watchedFoldersIt.hasNext()) {
|
|
|
|
QDir folder (watchedFoldersIt.next());
|
|
|
|
if (!folder.exists()){
|
|
|
|
qDebug() << "Removing " << folder.path();
|
2011-03-17 09:13:30 +03:00
|
|
|
_inotify->removePath(folder.path());
|
2011-02-17 02:21:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringListIterator subfoldersIt(subFoldersList(path, SubFolderRecursive));
|
|
|
|
while (subfoldersIt.hasNext()) {
|
|
|
|
QDir folder (subfoldersIt.next());
|
|
|
|
if (folder.exists() && !watchedFolders.contains(folder.path())) {
|
|
|
|
qDebug() << "Adding " << folder.path();
|
2011-03-17 09:13:30 +03:00
|
|
|
_inotify->addPath(folder.path());
|
2011-02-17 02:21:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Look if some of the subdirectories disappeared
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
emit folderChanged(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "folderwatcher.moc"
|