2002-07-18 14:01:42 +00:00
|
|
|
// -*- C++ -*-
|
2002-09-25 10:03:41 +00:00
|
|
|
/**
|
2002-07-18 14:01:42 +00:00
|
|
|
* \file FileMonitor.h
|
2002-09-25 10:03:41 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-07-18 14:01:42 +00:00
|
|
|
*
|
2017-03-10 21:41:48 +00:00
|
|
|
* \author Guillaume Munch
|
2002-09-25 10:03:41 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-07-18 14:01:42 +00:00
|
|
|
*
|
|
|
|
* FileMonitor monitors a file and informs a listener when that file has
|
|
|
|
* changed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FILEMONITOR_H
|
|
|
|
#define FILEMONITOR_H
|
|
|
|
|
2017-03-10 21:41:48 +00:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include <QFileSystemWatcher>
|
|
|
|
#include <QObject>
|
2017-03-18 19:37:55 +00:00
|
|
|
#include <QPointer>
|
2017-02-27 22:46:10 +00:00
|
|
|
|
2016-06-09 20:25:34 +00:00
|
|
|
#include <boost/signals2.hpp>
|
2002-07-18 14:01:42 +00:00
|
|
|
|
2017-03-10 21:41:48 +00:00
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace support {
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
class FileName;
|
|
|
|
|
2017-03-10 21:41:48 +00:00
|
|
|
///
|
|
|
|
/// FileMonitor, a file monitor based on QFileSystemWatcher
|
|
|
|
///
|
|
|
|
|
|
|
|
class FileMonitor;
|
|
|
|
class FileMonitorGuard;
|
2017-03-10 23:11:02 +00:00
|
|
|
typedef std::unique_ptr<FileMonitor> FileMonitorPtr;
|
2017-03-10 21:41:48 +00:00
|
|
|
|
|
|
|
///
|
|
|
|
/// Watch a file:
|
|
|
|
/// FileMonitorPtr monitor = FileSystemWatcher::monitor(file_with_path);
|
|
|
|
/// monitor.connect(...); //(using boost::signals2), or:
|
|
|
|
/// connect(monitor, SIGNAL(fileChanged()),...); // (using Qt)
|
|
|
|
///
|
|
|
|
/// Remember that a unique_ptr is automatically deleted at the end of a scope if
|
|
|
|
/// it has not been moved, or when assigned. When that happens, the signal
|
|
|
|
/// object is deleted and therefore all the connections are closed. The file
|
|
|
|
/// ceases being tracked when all the monitors for a file have been deleted.
|
|
|
|
///
|
|
|
|
/// Stop watching:
|
|
|
|
/// * as determined statically by the scope, or
|
|
|
|
/// * dynamically, using:
|
|
|
|
/// monitor = nullptr;
|
|
|
|
///
|
|
|
|
/// Watch a different file:
|
|
|
|
/// monitor = FileSystemWatcher::monitor(file_with_path2);
|
|
|
|
/// monitor.connect(...);
|
|
|
|
/// (stops watching the first)
|
|
|
|
///
|
|
|
|
/// Block notifications for the duration of a scope:
|
|
|
|
/// {
|
|
|
|
/// FileMonitorBlocker block = monitor.block();
|
|
|
|
/// ...
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// Reset connections:
|
|
|
|
/// monitor.disconnect();
|
|
|
|
/// or the disconnect method of the connection object for the boost signal.
|
|
|
|
///
|
|
|
|
class FileSystemWatcher
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// as described above
|
|
|
|
static FileMonitorPtr monitor(FileName const & file_with_path);
|
|
|
|
// Output whether the paths tracked by qwatcher_ and the active
|
|
|
|
// FileMonitorGuards are in correspondence.
|
|
|
|
static void debug();
|
|
|
|
private:
|
|
|
|
FileSystemWatcher();
|
|
|
|
// A global instance is created automatically on first call to monitor
|
|
|
|
static FileSystemWatcher & instance();
|
|
|
|
// Caches the monitor guards but allow them to be destroyed
|
|
|
|
std::map<std::string, std::weak_ptr<FileMonitorGuard>> store_;
|
|
|
|
// This class is a wrapper for QFileSystemWatcher
|
|
|
|
std::unique_ptr<QFileSystemWatcher> const qwatcher_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Must be unique per path
|
|
|
|
// Ends the watch when deleted
|
|
|
|
class FileMonitorGuard : public QObject
|
2007-11-29 07:11:08 +00:00
|
|
|
{
|
2017-03-10 21:41:48 +00:00
|
|
|
Q_OBJECT
|
|
|
|
|
2002-07-18 14:01:42 +00:00
|
|
|
public:
|
2017-03-10 21:41:48 +00:00
|
|
|
/// Start the watch
|
|
|
|
FileMonitorGuard(std::string const & filename,
|
|
|
|
QFileSystemWatcher * qwatcher);
|
|
|
|
/// End the watch
|
|
|
|
~FileMonitorGuard();
|
|
|
|
/// absolute path being tracked
|
|
|
|
std::string const & filename() { return filename_; }
|
2017-03-18 19:39:34 +00:00
|
|
|
/// if false, emit fileChanged() when we notice the existence of the file
|
|
|
|
void setExists(bool exists) { exists_ = exists; }
|
|
|
|
|
|
|
|
public Q_SLOTS:
|
2017-03-10 21:41:48 +00:00
|
|
|
/// Make sure it is being monitored, after e.g. a deletion. See
|
|
|
|
/// <https://bugreports.qt.io/browse/QTBUG-46483>. This is called
|
|
|
|
/// automatically.
|
2017-03-18 19:39:34 +00:00
|
|
|
void refresh();
|
2017-03-10 21:41:48 +00:00
|
|
|
|
|
|
|
Q_SIGNALS:
|
|
|
|
/// Connect to this to be notified when the file changes
|
|
|
|
void fileChanged() const;
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
/// Receive notifications from the QFileSystemWatcher
|
|
|
|
void notifyChange(QString const & path);
|
2002-07-18 14:01:42 +00:00
|
|
|
|
2017-03-10 21:41:48 +00:00
|
|
|
private:
|
|
|
|
std::string const filename_;
|
|
|
|
QFileSystemWatcher * qwatcher_;
|
2017-03-18 19:39:34 +00:00
|
|
|
bool exists_;
|
2017-03-10 21:41:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class FileMonitorBlockerGuard : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
2017-03-18 19:37:55 +00:00
|
|
|
QPointer<FileMonitor> monitor_;
|
2017-03-10 21:41:48 +00:00
|
|
|
int delay_;
|
|
|
|
|
|
|
|
public:
|
2017-03-18 19:37:55 +00:00
|
|
|
FileMonitorBlockerGuard(FileMonitor * monitor);
|
2017-03-10 21:41:48 +00:00
|
|
|
~FileMonitorBlockerGuard();
|
|
|
|
void setDelay(int delay);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-03-10 23:11:02 +00:00
|
|
|
typedef std::shared_ptr<FileMonitorBlockerGuard> FileMonitorBlocker;
|
2017-03-10 21:41:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// Main class
|
|
|
|
class FileMonitor : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
friend class FileMonitorBlockerGuard;
|
|
|
|
|
|
|
|
public:
|
|
|
|
FileMonitor(std::shared_ptr<FileMonitorGuard> monitor);
|
|
|
|
|
2017-03-10 23:11:02 +00:00
|
|
|
typedef boost::signals2::signal<void()> sig;
|
2002-07-18 14:01:42 +00:00
|
|
|
/// Connect and you'll be informed when the file has changed.
|
2017-03-10 21:41:48 +00:00
|
|
|
boost::signals2::connection connect(sig::slot_type const &);
|
|
|
|
/// disconnect all slots connected to the boost signal fileChanged_ or to
|
|
|
|
/// the qt signal fileChanged()
|
|
|
|
void disconnect();
|
|
|
|
/// absolute path being tracked
|
|
|
|
std::string const & filename() { return monitor_->filename(); }
|
|
|
|
/// Creates a guard that blocks notifications. Copyable. Notifications from
|
|
|
|
/// this monitor are blocked as long as there are copies around.
|
|
|
|
/// \param delay is the amount waited in ms after expiration of the guard
|
|
|
|
/// before reconnecting. This delay thing is to deal with asynchronous
|
|
|
|
/// notifications in a not so elegant fashion. But it can also be used to
|
|
|
|
/// slow down incoming events.
|
|
|
|
FileMonitorBlocker block(int delay = 0);
|
|
|
|
/// Make sure the good file is being monitored, after e.g. a move or a
|
|
|
|
/// deletion. See <https://bugreports.qt.io/browse/QTBUG-46483>. This is
|
|
|
|
/// called automatically.
|
|
|
|
void refresh() { return monitor_->refresh(); }
|
|
|
|
|
|
|
|
Q_SIGNALS:
|
|
|
|
/// Connect to this to be notified when the file changes
|
|
|
|
void fileChanged() const;
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
/// Receive notifications from the FileMonitorGuard
|
|
|
|
void changed();
|
2017-03-10 23:11:02 +00:00
|
|
|
///
|
2017-03-18 19:39:34 +00:00
|
|
|
void reconnectToFileMonitorGuard();
|
2002-07-18 14:01:42 +00:00
|
|
|
|
|
|
|
private:
|
2017-03-10 21:41:48 +00:00
|
|
|
// boost signal
|
|
|
|
sig fileChanged_;
|
|
|
|
// the unique watch for our file
|
|
|
|
std::shared_ptr<FileMonitorGuard> const monitor_;
|
|
|
|
//
|
|
|
|
std::weak_ptr<FileMonitorBlockerGuard> blocker_;
|
2002-07-18 14:01:42 +00:00
|
|
|
};
|
|
|
|
|
2017-03-10 21:41:48 +00:00
|
|
|
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
} // namespace support
|
|
|
|
} // namespace lyx
|
|
|
|
|
2002-07-18 14:01:42 +00:00
|
|
|
#endif // FILEMONITOR_H
|