]> git.lyx.org Git - lyx.git/blob - src/support/FileMonitor.h
Remove support for gcc 4.6
[lyx.git] / src / support / FileMonitor.h
1 // -*- C++ -*-
2 /**
3  * \file FileMonitor.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Angus Leeming
8  * \author Guillaume Munch
9  *
10  * Full author contact details are available in file CREDITS.
11  *
12  * FileMonitor monitors a file and informs a listener when that file has
13  * changed.
14  */
15
16 #ifndef FILEMONITOR_H
17 #define FILEMONITOR_H
18
19 #include "support/FileName.h"
20 #include "support/signals.h"
21
22 #include <memory>
23
24 #include <QFileSystemWatcher>
25 #include <QObject>
26 #include <QPointer>
27
28
29 namespace lyx {
30 namespace support {
31
32 ///
33 ///  FileMonitor, a file monitor based on QFileSystemWatcher
34 ///
35
36 class FileMonitor;
37 class ActiveFileMonitor;
38 class FileMonitorGuard;
39 typedef std::unique_ptr<FileMonitor> FileMonitorPtr;
40 typedef std::unique_ptr<ActiveFileMonitor> ActiveFileMonitorPtr;
41
42 ///
43 /// Watch a file:
44 ///   FileMonitorPtr monitor = FileSystemWatcher::monitor(file_with_path);
45 ///   monitor.connect(...); //(using boost::signals2), or:
46 ///   connect(monitor, SIGNAL(fileChanged()),...); // (using Qt)
47 ///
48 /// Remember that a unique_ptr is automatically deleted at the end of a scope if
49 /// it has not been moved, or when assigned. When that happens, the signal
50 /// object is deleted and therefore all the connections are closed. The file
51 /// ceases being tracked when all the monitors for a file have been deleted.
52 ///
53 /// Stop watching:
54 ///   * as determined statically by the scope, or
55 ///   * dynamically, using:
56 ///       monitor = nullptr;
57 ///
58 /// Watch a different file:
59 ///   monitor = FileSystemWatcher::monitor(file_with_path2);
60 ///   monitor.connect(...);
61 /// (stops watching the first)
62 ///
63 class FileSystemWatcher
64 {
65 public:
66         /// as described above
67         static FileMonitorPtr monitor(FileName const & filename);
68         /// same but with an ActiveFileMonitor
69         static ActiveFileMonitorPtr activeMonitor(FileName const & filename,
70                                                   int interval = 10000);
71         /// Output whether the paths tracked by qwatcher_ and the active
72         /// FileMonitorGuards are in correspondence.
73         static void debug();
74 private:
75         FileSystemWatcher();
76         /// A global instance is created automatically on first call
77         static FileSystemWatcher & instance();
78         ///
79         std::shared_ptr<FileMonitorGuard> getGuard(FileName const & filename);
80         /// Caches the monitor guards but allow them to be destroyed
81         std::map<std::string, std::weak_ptr<FileMonitorGuard>> store_;
82         /// This class is a wrapper for QFileSystemWatcher
83         std::unique_ptr<QFileSystemWatcher> const qwatcher_;
84 };
85
86
87 /// Must be unique per path
88 /// Ends the watch when deleted
89 class FileMonitorGuard : public QObject
90 {
91         Q_OBJECT
92
93 public:
94         /// Start the watch
95         FileMonitorGuard(std::string const & filename,
96                          QFileSystemWatcher * qwatcher);
97         /// End the watch
98         ~FileMonitorGuard();
99         /// absolute path being tracked
100         std::string const & filename() { return filename_; }
101
102 public Q_SLOTS:
103         /// Make sure it is being monitored, after e.g. a deletion. See
104         /// <https://bugreports.qt.io/browse/QTBUG-46483>. This is called
105         /// automatically.
106         void refresh(bool emit = true);
107
108 Q_SIGNALS:
109         /// Connect to this to be notified when the file changes
110         void fileChanged(bool exists) const;
111
112 private Q_SLOTS:
113         /// Receive notifications from the QFileSystemWatcher
114         void notifyChange(QString const & path);
115
116 private:
117         std::string const filename_;
118         QFileSystemWatcher * qwatcher_;
119         /// for emitting fileChanged() when the file is created or deleted
120         bool exists_;
121 };
122
123
124 /// Main class
125 class FileMonitor : public QObject
126 {
127         Q_OBJECT
128
129 public:
130         FileMonitor(std::shared_ptr<FileMonitorGuard> monitor);
131
132         typedef signals2::signal<void(bool)> sig;
133         typedef sig::slot_type slot;
134         /// Connect and you'll be informed when the file has changed.
135         signals2::connection connect(slot const &);
136         /// absolute path being tracked
137         std::string const & filename() { return monitor_->filename(); }
138         /// Make sure the good file is being monitored, after e.g. a move or a
139         /// deletion. See <https://bugreports.qt.io/browse/QTBUG-46483>. This is
140         /// called automatically.
141         void refresh() { monitor_->refresh(); }
142
143 Q_SIGNALS:
144         /// Connect to this to be notified when the file changes
145         void fileChanged(bool exists) const;
146
147 protected Q_SLOTS:
148         /// Receive notifications from the FileMonitorGuard
149         void changed(bool exists);
150         ///
151         void connectToFileMonitorGuard();
152
153 private:
154         /// boost signal
155         sig fileChanged_;
156         /// the unique watch for our file
157         std::shared_ptr<FileMonitorGuard> const monitor_;
158 };
159
160
161 /// When a more active monitoring style is needed.
162 /// For instance because QFileSystemWatcher does not work for remote file
163 /// systems.
164 class ActiveFileMonitor : public FileMonitor
165 {
166         Q_OBJECT
167 public:
168         ActiveFileMonitor(std::shared_ptr<FileMonitorGuard> monitor,
169                           FileName const & filename, int interval);
170         /// call checkModified asynchronously
171         void checkModifiedAsync();
172
173 public Q_SLOTS:
174         /// Check explicitly for a modification, but not more than once every
175         /// interval ms.
176         void checkModified();
177
178 private Q_SLOTS:
179         void setCooldown() { cooldown_ = true; }
180         void clearCooldown() { cooldown_ = false; }
181
182 private:
183         FileName const filename_;
184         ///
185         int const interval_;
186         ///
187         time_t timestamp_;
188         ///
189         unsigned long checksum_;
190         ///
191         bool cooldown_;
192 };
193
194
195 } // namespace support
196 } // namespace lyx
197
198 #endif // FILEMONITOR_H