2 * \file FileMonitor.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
7 * \author Guillaume Munch
9 * Full author contact details are available in file CREDITS.
14 #include "support/FileMonitor.h"
16 #include "support/debug.h"
17 #include "support/FileName.h"
18 #include "support/qstring_helpers.h"
19 #include "support/unique_ptr.h"
22 #include <QStringList>
33 FileSystemWatcher & FileSystemWatcher::instance()
35 // This is thread-safe because QFileSystemWatcher is thread-safe.
36 static FileSystemWatcher f;
41 FileSystemWatcher::FileSystemWatcher()
42 : qwatcher_(make_unique<QFileSystemWatcher>())
46 shared_ptr<FileMonitorGuard>
47 FileSystemWatcher::getGuard(FileName const & filename)
49 string const absfilename = filename.absFileName();
50 weak_ptr<FileMonitorGuard> & wptr = store_[absfilename];
51 if (shared_ptr<FileMonitorGuard> mon = wptr.lock())
53 auto mon = make_shared<FileMonitorGuard>(absfilename, qwatcher_.get());
60 FileMonitorPtr FileSystemWatcher::monitor(FileName const & filename)
62 return make_unique<FileMonitor>(instance().getGuard(filename));
67 ActiveFileMonitorPtr FileSystemWatcher::activeMonitor(FileName const & filename,
70 return make_unique<ActiveFileMonitor>(instance().getGuard(filename),
76 void FileSystemWatcher::debug()
78 FileSystemWatcher & f = instance();
79 QStringList q_files = f.qwatcher_->files();
80 for (pair<string, weak_ptr<FileMonitorGuard>> pair : f.store_) {
81 string const & name = pair.first;
82 if (!pair.second.expired()) {
83 if (!q_files.contains(toqstr(name)))
84 LYXERR0("Monitored but not QFileSystemWatched (bad): " << name);
86 //LYXERR0("Monitored and QFileSystemWatched (good): " << name);
90 for (QString const & qname : q_files) {
91 string const name = fromqstr(qname);
92 weak_ptr<FileMonitorGuard> & wptr = f.store_[name];
94 LYXERR0("QFileSystemWatched but not monitored (bad): " << name);
99 FileMonitorGuard::FileMonitorGuard(string const & filename,
100 QFileSystemWatcher * qwatcher)
101 : filename_(filename), qwatcher_(qwatcher), exists_(true)
103 if (filename.empty())
105 QObject::connect(qwatcher, SIGNAL(fileChanged(QString const &)),
106 this, SLOT(notifyChange(QString const &)));
107 if (qwatcher_->files().contains(toqstr(filename)))
108 LYXERR0("This file is already being QFileSystemWatched: " << filename
109 << ". This should not happen.");
114 FileMonitorGuard::~FileMonitorGuard()
116 if (!filename_.empty())
117 qwatcher_->removePath(toqstr(filename_));
121 void FileMonitorGuard::refresh()
123 if (filename_.empty())
125 QString const qfilename = toqstr(filename_);
126 if(!qwatcher_->files().contains(qfilename)) {
127 bool exists = QFile(qfilename).exists();
128 #if (QT_VERSION >= 0x050000)
129 if (!exists || !qwatcher_->addPath(qfilename))
131 auto add_path = [&]() {
132 qwatcher_->addPath(qfilename);
133 return qwatcher_->files().contains(qfilename);
135 if (!exists || !add_path())
140 "Could not add path to QFileSystemWatcher: "
142 QTimer::singleShot(2000, this, SLOT(refresh()));
143 } else if (exists && !exists_)
144 Q_EMIT fileChanged();
150 void FileMonitorGuard::notifyChange(QString const & path)
152 if (path == toqstr(filename_)) {
153 Q_EMIT fileChanged();
154 // If the file has been modified by delete-move, we are notified of the
155 // deletion but we no longer track the file. See
156 // <https://bugreports.qt.io/browse/QTBUG-46483> (not a bug).
162 FileMonitor::FileMonitor(std::shared_ptr<FileMonitorGuard> monitor)
165 QObject::connect(monitor_.get(), SIGNAL(fileChanged()),
166 this, SLOT(changed()));
171 void FileMonitor::reconnectToFileMonitorGuard()
173 monitor_->setExists(true);
174 QObject::connect(monitor_.get(), SIGNAL(fileChanged()),
175 this, SLOT(changed()));
179 boost::signals2::connection
180 FileMonitor::connect(sig::slot_type const & slot)
182 return fileChanged_.connect(slot);
186 void FileMonitor::disconnect()
188 fileChanged_.disconnect_all_slots();
189 QObject::disconnect(this, SIGNAL(fileChanged()));
193 void FileMonitor::changed()
197 Q_EMIT fileChanged();
201 FileMonitorBlocker FileMonitor::block(int delay)
203 FileMonitorBlocker blocker = blocker_.lock();
205 blocker_ = blocker = make_shared<FileMonitorBlockerGuard>(this);
206 blocker->setDelay(delay);
211 FileMonitorBlockerGuard::FileMonitorBlockerGuard(FileMonitor * monitor)
212 : monitor_(monitor), delay_(0)
214 QObject::disconnect(monitor->monitor_.get(), SIGNAL(fileChanged()),
215 monitor, SLOT(changed()));
219 void FileMonitorBlockerGuard::setDelay(int delay)
221 delay_ = max(delay_, delay);
225 FileMonitorBlockerGuard::~FileMonitorBlockerGuard()
229 // Even if delay_ is 0, the QTimer is necessary. Indeed, the notifications
230 // from QFileSystemWatcher that we meant to ignore are not going to be
231 // treated immediately, so we must yield to give us the opportunity to
233 QTimer::singleShot(delay_, monitor_, SLOT(reconnectToFileMonitorGuard()));
237 ActiveFileMonitor::ActiveFileMonitor(std::shared_ptr<FileMonitorGuard> monitor,
238 FileName const & filename, int interval)
239 : FileMonitor(monitor), filename_(filename), interval_(interval),
240 timestamp_(0), checksum_(0), cooldown_(true)
242 QObject::connect(this, SIGNAL(fileChanged()), this, SLOT(setCooldown()));
243 QTimer::singleShot(interval_, this, SLOT(clearCooldown()));
244 if (!filename_.exists())
246 timestamp_ = filename_.lastModified();
247 checksum_ = filename_.checksum();
251 void ActiveFileMonitor::checkModified()
257 bool changed = false;
258 if (!filename_.exists()) {
259 changed = timestamp_ || checksum_;
263 time_t const new_timestamp = filename_.lastModified();
265 if (new_timestamp != timestamp_) {
266 timestamp_ = new_timestamp;
268 unsigned long const new_checksum = filename_.checksum();
269 if (new_checksum != checksum_) {
270 checksum_ = new_checksum;
276 FileMonitor::changed();
277 QTimer::singleShot(interval_, this, SLOT(clearCooldown()));
281 void ActiveFileMonitor::checkModifiedAsync()
284 QTimer::singleShot(0, this, SLOT(checkModified()));
289 } // namespace support
292 #include "moc_FileMonitor.cpp"