nextcloud-desktop/src/gui/folderwatcher.h

135 lines
3.6 KiB
C
Raw Normal View History

2011-04-06 13:48:02 +04:00
/*
2014-01-13 19:16:19 +04:00
* Copyright (C) by Klaas Freitag <freitag@owncloud.com>
2011-04-06 13:48:02 +04:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
2011-04-06 13:48:02 +04:00
*
* This program 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 General Public License
* for more details.
*/
2011-02-17 02:21:45 +03:00
#ifndef MIRALL_FOLDERWATCHER_H
#define MIRALL_FOLDERWATCHER_H
#include "config.h"
#include <QList>
#include <QLoggingCategory>
2011-02-17 02:21:45 +03:00
#include <QObject>
#include <QString>
#include <QStringList>
#include <QElapsedTimer>
2012-02-16 01:36:52 +04:00
#include <QHash>
#include <QScopedPointer>
#include <QSet>
#include <QDir>
2011-02-17 02:21:45 +03:00
class QTimer;
2012-05-21 18:48:49 +04:00
2014-11-10 00:34:07 +03:00
namespace OCC {
2012-05-21 18:48:49 +04:00
Q_DECLARE_LOGGING_CATEGORY(lcFolderWatcher)
2012-12-04 21:15:37 +04:00
class FolderWatcherPrivate;
class Folder;
2011-02-17 02:21:45 +03:00
2015-06-29 19:56:09 +03:00
/**
* @brief Monitors a directory recursively for changes
*
* Folder Watcher monitors a directory and its sub directories
* for changes in the local file system. Changes are signalled
* through the pathChanged() signal.
2014-02-03 14:58:37 +04:00
*
2015-06-29 19:56:09 +03:00
* @ingroup gui
2011-03-21 00:17:23 +03:00
*/
2011-02-17 02:21:45 +03:00
class FolderWatcher : public QObject
{
Q_OBJECT
2011-02-17 02:21:45 +03:00
public:
// Construct, connect signals, call init()
explicit FolderWatcher(Folder *folder = nullptr);
~FolderWatcher() override;
2011-03-21 00:17:23 +03:00
/**
* @param root Path of the root of the folder
*/
void init(const QString &root);
2011-03-21 00:18:38 +03:00
/* Check if the path is ignored. */
2017-05-17 11:55:42 +03:00
bool pathIsIgnored(const QString &path);
/**
* Returns false if the folder watcher can't be trusted to capture all
* notifications.
*
* For example, this can happen on linux if the inotify user limit from
* /proc/sys/fs/inotify/max_user_watches is exceeded.
*/
[[nodiscard]] bool isReliable() const;
/**
* Triggers a change in the path and verifies a notification arrives.
*
* If no notification is seen, the folderwatcher marks itself as unreliable.
* The path must be ignored by the watcher.
*/
void startNotificatonTest(const QString &path);
/// For testing linux behavior only
[[nodiscard]] int testLinuxWatchCount() const;
2011-02-17 02:21:45 +03:00
signals:
/** Emitted when one of the watched directories or one
* of the contained files is changed. */
void pathChanged(const QString &path);
/**
* Emitted if some notifications were lost.
*
* Would happen, for example, if the number of pending notifications
* exceeded the allocated buffer size on Windows. Note that the folder
* watcher could still be able to capture all future notifications -
* i.e. isReliable() is orthogonal to losing changes occasionally.
*/
void lostChanges();
2011-03-21 00:17:23 +03:00
/**
* Signals when the watcher became unreliable. The string is a translated
* message that can be shown to users.
*/
void becameUnreliable(const QString &message);
2011-02-17 02:21:45 +03:00
protected slots:
// called from the implementations to indicate a change in path
2017-05-17 11:55:42 +03:00
void changeDetected(const QString &path);
void changeDetected(const QStringList &paths);
private slots:
void startNotificationTestWhenReady();
protected:
QHash<QString, int> _pendingPathes;
2012-12-04 20:46:44 +04:00
2011-02-17 02:21:45 +03:00
private:
QScopedPointer<FolderWatcherPrivate> _d;
QElapsedTimer _timer;
QSet<QString> _lastPaths;
2017-05-17 11:55:42 +03:00
Folder *_folder;
bool _isReliable = true;
void appendSubPaths(QDir dir, QStringList& subPaths);
/** Path of the expected test notification */
QString _testNotificationPath;
friend class FolderWatcherPrivate;
2011-02-17 02:21:45 +03:00
};
}
#endif