FileMonitorGuard::FileMonitorGuard(string const & filename,
QFileSystemWatcher * qwatcher)
- : filename_(filename), qwatcher_(qwatcher)
+ : filename_(filename), qwatcher_(qwatcher), exists_(true)
{
QObject::connect(qwatcher, SIGNAL(fileChanged(QString const &)),
this, SLOT(notifyChange(QString const &)));
}
-void FileMonitorGuard::refresh(bool new_file)
+void FileMonitorGuard::refresh()
{
QString const qfilename = toqstr(filename_);
if(!qwatcher_->files().contains(qfilename)) {
LYXERR(Debug::FILES,
"Could not add path to QFileSystemWatcher: "
<< filename_);
- if (new_file || !exists)
- QTimer::singleShot(1000, this, SLOT(refreshTrue()));
- else
- QTimer::singleShot(1000, this, SLOT(refreshFalse()));
- // Better (qt>=5.4):
- /*QTimer::singleShot(1000, this, [=](){
- refresh(new_file || !exists);
- });*/
- } else if (exists && new_file)
+ QTimer::singleShot(2000, this, SLOT(refresh()));
+ } else if (exists && !exists_)
Q_EMIT fileChanged();
+ setExists(exists);
}
}
FileMonitor::FileMonitor(std::shared_ptr<FileMonitorGuard> monitor)
: monitor_(monitor)
{
- connectToFileMonitorGuard();
+ QObject::connect(monitor_.get(), SIGNAL(fileChanged()),
+ this, SLOT(changed()));
refresh();
}
-void FileMonitor::connectToFileMonitorGuard()
+void FileMonitor::reconnectToFileMonitorGuard()
{
+ monitor_->setExists(true);
QObject::connect(monitor_.get(), SIGNAL(fileChanged()),
this, SLOT(changed()));
}
~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.
- /// \param new_file If true, emit fileChanged if the file exists and was
- /// successfully added.
- void refresh(bool new_file = false);
+ void refresh();
Q_SIGNALS:
/// Connect to this to be notified when the file changes
/// Receive notifications from the QFileSystemWatcher
void notifyChange(QString const & path);
- /// nonsense introduced for when QT_VERSION < 0x050000, cannot be placed
- /// between #ifdef
- void refreshTrue() { refresh(true); }
- /// nonsense introduced for when QT_VERSION < 0x050000, cannot be placed
- /// between #ifdef
- void refreshFalse() { refresh(false); }
-
-
private:
std::string const filename_;
QFileSystemWatcher * qwatcher_;
+ bool exists_;
};
/// Receive notifications from the FileMonitorGuard
void changed();
///
- void connectToFileMonitorGuard();
+ void reconnectToFileMonitorGuard();
private:
// boost signal