]> git.lyx.org Git - lyx.git/blobdiff - src/support/FileMonitor.h
Keep permissions of the saved files intact. (Backporting 04fe818b2239).
[lyx.git] / src / support / FileMonitor.h
index e2bbb1b1feba5310a321a9c5cbeeb7097c677376..07a8dc532c93335007ab4f9b79e5b24afa445dd6 100644 (file)
@@ -17,6 +17,7 @@
 #define FILEMONITOR_H
 
 #include "support/FileName.h"
+#include "support/signals.h"
 
 #include <memory>
 
@@ -24,8 +25,6 @@
 #include <QObject>
 #include <QPointer>
 
-#include <boost/signals2.hpp>
-
 
 namespace lyx {
 namespace support {
@@ -61,26 +60,16 @@ typedef std::unique_ptr<ActiveFileMonitor> ActiveFileMonitorPtr;
 ///   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
+       /// as described above
        static FileMonitorPtr monitor(FileName const & filename);
        /// same but with an ActiveFileMonitor
        static ActiveFileMonitorPtr activeMonitor(FileName const & filename,
                                                  int interval = 10000);
-       // Output whether the paths tracked by qwatcher_ and the active
-       // FileMonitorGuards are in correspondence.
+       /// Output whether the paths tracked by qwatcher_ and the active
+       /// FileMonitorGuards are in correspondence.
        static void debug();
 private:
        FileSystemWatcher();
@@ -88,9 +77,9 @@ private:
        static FileSystemWatcher & instance();
        ///
        std::shared_ptr<FileMonitorGuard> getGuard(FileName const & filename);
-       // Caches the monitor guards but allow them to be destroyed
+       /// 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
+       /// This class is a wrapper for QFileSystemWatcher
        std::unique_ptr<QFileSystemWatcher> const qwatcher_;
 };
 
@@ -109,18 +98,16 @@ public:
        ~FileMonitorGuard();
        /// absolute path being tracked
        std::string const & filename() { return filename_; }
-       /// if false, emit fileChanged() when we notice the existence of the file
-       void setExists(bool exists) { exists_ = exists; }
 
 public Q_SLOTS:
        /// Make sure it is being monitored, after e.g. a deletion. See
        /// <https://bugreports.qt.io/browse/QTBUG-46483>. This is called
        /// automatically.
-       void refresh();
+       void refresh(bool emit = true);
 
 Q_SIGNALS:
        /// Connect to this to be notified when the file changes
-       void fileChanged() const;
+       void fileChanged(bool exists) const;
 
 private Q_SLOTS:
        /// Receive notifications from the QFileSystemWatcher
@@ -129,72 +116,45 @@ private Q_SLOTS:
 private:
        std::string const filename_;
        QFileSystemWatcher * qwatcher_;
+       /// for emitting fileChanged() when the file is created or deleted
        bool exists_;
 };
 
 
-class FileMonitorBlockerGuard : public QObject
-{
-       Q_OBJECT
-       QPointer<FileMonitor> monitor_;
-       int delay_;
-
-public:
-       FileMonitorBlockerGuard(FileMonitor * monitor);
-       ~FileMonitorBlockerGuard();
-       void setDelay(int delay);
-};
-
-
-typedef std::shared_ptr<FileMonitorBlockerGuard> FileMonitorBlocker;
-
-
 /// Main class
 class FileMonitor : public QObject
 {
        Q_OBJECT
-       friend class FileMonitorBlockerGuard;
 
 public:
        FileMonitor(std::shared_ptr<FileMonitorGuard> monitor);
 
-       typedef boost::signals2::signal<void()> sig;
+       typedef signals2::signal<void(bool)> sig;
+       typedef sig::slot_type slot;
        /// Connect and you'll be informed when the file has changed.
-       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();
+       signals2::connection connect(slot const &);
        /// 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(); }
+       void refresh() { monitor_->refresh(); }
 
 Q_SIGNALS:
        /// Connect to this to be notified when the file changes
-       void fileChanged() const;
+       void fileChanged(bool exists) const;
 
 protected Q_SLOTS:
        /// Receive notifications from the FileMonitorGuard
-       void changed();
+       void changed(bool exists);
        ///
-       void reconnectToFileMonitorGuard();
+       void connectToFileMonitorGuard();
 
 private:
-       // boost signal
+       /// boost signal
        sig fileChanged_;
-       // the unique watch for our file
+       /// the unique watch for our file
        std::shared_ptr<FileMonitorGuard> const monitor_;
-       //
-       std::weak_ptr<FileMonitorBlockerGuard> blocker_;
 };