]> git.lyx.org Git - lyx.git/commitdiff
Blocking signals also blocks the notification of file creation (#10595)
authorGuillaume Munch <gm@lyx.org>
Sat, 18 Mar 2017 19:39:34 +0000 (20:39 +0100)
committerGuillaume Munch <gm@lyx.org>
Sun, 19 Mar 2017 13:41:17 +0000 (14:41 +0100)
src/support/FileMonitor.cpp
src/support/FileMonitor.h

index c14d3d6e3a3e11d8b574f54d6e3d7bf6d0a94cb7..ced1188667beae5ef6bb86b678375bbcaaa46605 100644 (file)
@@ -82,7 +82,7 @@ void FileSystemWatcher::debug()
 
 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 &)));
@@ -99,7 +99,7 @@ FileMonitorGuard::~FileMonitorGuard()
 }
 
 
-void FileMonitorGuard::refresh(bool new_file)
+void FileMonitorGuard::refresh()
 {
        QString const qfilename = toqstr(filename_);
        if(!qwatcher_->files().contains(qfilename)) {
@@ -117,16 +117,10 @@ void FileMonitorGuard::refresh(bool new_file)
                                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);
        }
 }
 
@@ -146,13 +140,15 @@ void FileMonitorGuard::notifyChange(QString const & path)
 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()));
 }
index 768e61f8733f91254895efca25e6259ab8818de2..51dbd7c72d8d1ecfd6458547653a0a2934792e2f 100644 (file)
@@ -101,12 +101,14 @@ 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.
-       /// \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
@@ -116,17 +118,10 @@ private Q_SLOTS:
        /// 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_;
 };
 
 
@@ -183,7 +178,7 @@ private Q_SLOTS:
        /// Receive notifications from the FileMonitorGuard
        void changed();
        ///
-       void connectToFileMonitorGuard();
+       void reconnectToFileMonitorGuard();
 
 private:
        // boost signal