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 lyx::make_unique<FileMonitor>(instance().getGuard(filename));
67 ActiveFileMonitorPtr FileSystemWatcher::activeMonitor(FileName const & filename,
70 return lyx::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(bool const emit)
123 if (filename_.empty())
125 QString const qfilename = toqstr(filename_);
126 if (!qwatcher_->files().contains(qfilename)) {
127 bool const existed = exists_;
128 exists_ = QFile(qfilename).exists();
129 if (exists_ && !qwatcher_->addPath(qfilename))
132 "Could not add path to QFileSystemWatcher: " << filename_);
133 QTimer::singleShot(5000, this, SLOT(refresh()));
136 // The standard way to overwrite a file is to delete it and
137 // create a new file with the same name. Therefore if the file
138 // has just been deleted, it is smart to check not too long
139 // after whether it has been recreated.
140 QTimer::singleShot(existed ? 100 : 2000, this, SLOT(refresh()));
141 if (existed != exists_ && emit)
142 Q_EMIT fileChanged(exists_);
148 void FileMonitorGuard::notifyChange(QString const & path)
150 if (path == toqstr(filename_)) {
151 // If the file has been modified by delete-move, we are notified of the
152 // deletion but we no longer track the file. See
153 // <https://bugreports.qt.io/browse/QTBUG-46483> (not a bug).
154 // Therefore we must refresh.
156 Q_EMIT fileChanged(exists_);
161 FileMonitor::FileMonitor(std::shared_ptr<FileMonitorGuard> monitor)
164 connectToFileMonitorGuard();
169 void FileMonitor::connectToFileMonitorGuard()
171 // Connections need to be asynchronous because the receiver can own this
172 // object and therefore is allowed to delete it.
174 QObject::connect(monitor_.get(), SIGNAL(fileChanged(bool)),
175 this, SIGNAL(fileChanged(bool)),
176 Qt::QueuedConnection);
178 QObject::connect(this, SIGNAL(fileChanged(bool)),
179 this, SLOT(changed(bool)));
183 connection FileMonitor::connect(slot const & slot)
185 return fileChanged_.connect(slot);
189 void FileMonitor::changed(bool const exists)
192 fileChanged_(exists);
196 ActiveFileMonitor::ActiveFileMonitor(std::shared_ptr<FileMonitorGuard> monitor,
197 FileName const & filename, int interval)
198 : FileMonitor(monitor), filename_(filename), interval_(interval),
199 timestamp_(0), checksum_(0), cooldown_(true)
201 QObject::connect(this, SIGNAL(fileChanged(bool)), this, SLOT(setCooldown()));
202 QTimer::singleShot(interval_, this, SLOT(clearCooldown()));
204 if (!filename_.exists())
206 timestamp_ = filename_.lastModified();
207 checksum_ = filename_.checksum();
211 void ActiveFileMonitor::checkModified()
217 bool changed = false;
219 bool exists = filename_.exists();
221 changed = timestamp_ || checksum_;
225 time_t const new_timestamp = filename_.lastModified();
227 if (new_timestamp != timestamp_) {
228 timestamp_ = new_timestamp;
230 unsigned long const new_checksum = filename_.checksum();
231 if (new_checksum != checksum_) {
232 checksum_ = new_checksum;
238 Q_EMIT FileMonitor::fileChanged(exists);
239 QTimer::singleShot(interval_, this, SLOT(clearCooldown()));
243 void ActiveFileMonitor::checkModifiedAsync()
246 QTimer::singleShot(0, this, SLOT(checkModified()));
251 } // namespace support
254 #include "moc_FileMonitor.cpp"