Make QFileSystemWatcherEngines children of QFileSystemWatcher
authorBradley T. Hughes <bradley.hughes@nokia.com>
Thu, 12 Jan 2012 10:40:51 +0000 (11:40 +0100)
committerQt by Nokia <qt-info@nokia.com>
Thu, 12 Jan 2012 13:38:18 +0000 (14:38 +0100)
To support moving QFileSystemWatcher to another thread, the engines need
to follow when the watcher is moved. The easiest way to do this is by
parenting the engines to the watcher.

Change-Id: Ie2bb701c0c148da9cc2302d4de23286b8ef42c4d
Reviewed-by: Robin Burchell <robin+qt@viroteck.net>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
src/corelib/io/qfilesystemwatcher.cpp
src/corelib/io/qfilesystemwatcher_inotify.cpp
src/corelib/io/qfilesystemwatcher_inotify_p.h
src/corelib/io/qfilesystemwatcher_kqueue.cpp
src/corelib/io/qfilesystemwatcher_kqueue_p.h
src/corelib/io/qfilesystemwatcher_p.h
src/corelib/io/qfilesystemwatcher_polling.cpp
src/corelib/io/qfilesystemwatcher_polling_p.h
src/corelib/io/qfilesystemwatcher_win_p.h

index b6eb5ed..4abb4f3 100644 (file)
 
 QT_BEGIN_NAMESPACE
 
-QFileSystemWatcherEngine *QFileSystemWatcherPrivate::createNativeEngine()
+QFileSystemWatcherEngine *QFileSystemWatcherPrivate::createNativeEngine(QObject *parent)
 {
 #if defined(Q_OS_WIN)
-    return new QWindowsFileSystemWatcherEngine;
+    return new QWindowsFileSystemWatcherEngine(parent);
 #elif defined(Q_OS_LINUX)
     // there is a chance that inotify may fail on Linux pre-2.6.13 (August
     // 2005), so we can't just new inotify directly.
-    return QInotifyFileSystemWatcherEngine::create();
+    return QInotifyFileSystemWatcherEngine::create(parent);
 #elif defined(Q_OS_FREEBSD) || defined(Q_OS_MAC)
-    return QKqueueFileSystemWatcherEngine::create();
+    return QKqueueFileSystemWatcherEngine::create(parent);
 #else
     return 0;
 #endif
@@ -86,7 +86,7 @@ QFileSystemWatcherPrivate::QFileSystemWatcherPrivate()
 void QFileSystemWatcherPrivate::init()
 {
     Q_Q(QFileSystemWatcher);
-    native = createNativeEngine();
+    native = createNativeEngine(q);
     if (native) {
         QObject::connect(native,
                          SIGNAL(fileChanged(QString,bool)),
@@ -105,7 +105,7 @@ void QFileSystemWatcherPrivate::initPollerEngine()
         return;
 
     Q_Q(QFileSystemWatcher);
-    poller = new QPollingFileSystemWatcherEngine; // that was a mouthful
+    poller = new QPollingFileSystemWatcherEngine(q); // that was a mouthful
     QObject::connect(poller,
                      SIGNAL(fileChanged(QString,bool)),
                      q,
@@ -216,17 +216,7 @@ QFileSystemWatcher::QFileSystemWatcher(const QStringList &paths, QObject *parent
     Destroys the file system watcher.
 */
 QFileSystemWatcher::~QFileSystemWatcher()
-{
-    Q_D(QFileSystemWatcher);
-    if (d->native) {
-        delete d->native;
-        d->native = 0;
-    }
-    if (d->poller) {
-        delete d->poller;
-        d->poller = 0;
-    }
-}
+{ }
 
 /*!
     Adds \a path to the file system watcher if \a path exists. The
index 49299e2..ff732bc 100644 (file)
@@ -210,7 +210,7 @@ QT_END_NAMESPACE
 
 QT_BEGIN_NAMESPACE
 
-QInotifyFileSystemWatcherEngine *QInotifyFileSystemWatcherEngine::create()
+QInotifyFileSystemWatcherEngine *QInotifyFileSystemWatcherEngine::create(QObject *parent)
 {
     register int fd = -1;
 #ifdef IN_CLOEXEC
@@ -221,12 +221,13 @@ QInotifyFileSystemWatcherEngine *QInotifyFileSystemWatcherEngine::create()
         if (fd == -1)
             return 0;
     }
-    return new QInotifyFileSystemWatcherEngine(fd);
+    return new QInotifyFileSystemWatcherEngine(fd, parent);
 }
 
-QInotifyFileSystemWatcherEngine::QInotifyFileSystemWatcherEngine(int fd)
-    : inotifyFd(fd)
-    , notifier(fd, QSocketNotifier::Read, this)
+QInotifyFileSystemWatcherEngine::QInotifyFileSystemWatcherEngine(int fd, QObject *parent)
+    : QFileSystemWatcherEngine(parent),
+      inotifyFd(fd),
+      notifier(fd, QSocketNotifier::Read, this)
 {
     fcntl(inotifyFd, F_SETFD, FD_CLOEXEC);
     connect(&notifier, SIGNAL(activated(int)), SLOT(readFromInotify()));
index daedb01..8b3ce62 100644 (file)
@@ -70,7 +70,7 @@ class QInotifyFileSystemWatcherEngine : public QFileSystemWatcherEngine
 public:
     ~QInotifyFileSystemWatcherEngine();
 
-    static QInotifyFileSystemWatcherEngine *create();
+    static QInotifyFileSystemWatcherEngine *create(QObject *parent);
 
     QStringList addPaths(const QStringList &paths, QStringList *files, QStringList *directories);
     QStringList removePaths(const QStringList &paths, QStringList *files, QStringList *directories);
@@ -79,7 +79,7 @@ private Q_SLOTS:
     void readFromInotify();
 
 private:
-    QInotifyFileSystemWatcherEngine(int fd);
+    QInotifyFileSystemWatcherEngine(int fd, QObject *parent);
     int inotifyFd;
     QHash<QString, int> pathToID;
     QHash<int, QString> idToPath;
index 8070af5..fd7bfa2 100644 (file)
@@ -67,17 +67,18 @@ QT_BEGIN_NAMESPACE
 #  define DEBUG if(false)qDebug
 #endif
 
-QKqueueFileSystemWatcherEngine *QKqueueFileSystemWatcherEngine::create()
+QKqueueFileSystemWatcherEngine *QKqueueFileSystemWatcherEngine::create(QObject *parent)
 {
     int kqfd = kqueue();
     if (kqfd == -1)
         return 0;
-    return new QKqueueFileSystemWatcherEngine(kqfd);
+    return new QKqueueFileSystemWatcherEngine(kqfd, parent);
 }
 
-QKqueueFileSystemWatcherEngine::QKqueueFileSystemWatcherEngine(int kqfd)
-    : kqfd(kqfd)
-    , notifier(kqfd, QSocketNotifier::Read, this)
+QKqueueFileSystemWatcherEngine::QKqueueFileSystemWatcherEngine(int kqfd, QObject *parent)
+    : QFileSystemWatcherEngine(parent),
+      kqfd(kqfd),
+      notifier(kqfd, QSocketNotifier::Read, this)
 {
     connect(&notifier, SIGNAL(activated(int)), SLOT(readFromKqueue()));
 
index 41afbad..9bd9378 100644 (file)
@@ -72,7 +72,7 @@ class QKqueueFileSystemWatcherEngine : public QFileSystemWatcherEngine
 public:
     ~QKqueueFileSystemWatcherEngine();
 
-    static QKqueueFileSystemWatcherEngine *create();
+    static QKqueueFileSystemWatcherEngine *create(QObject *parent);
 
     QStringList addPaths(const QStringList &paths, QStringList *files, QStringList *directories);
     QStringList removePaths(const QStringList &paths, QStringList *files, QStringList *directories);
@@ -81,7 +81,7 @@ private Q_SLOTS:
     void readFromKqueue();
 
 private:
-    QKqueueFileSystemWatcherEngine(int kqfd);
+    QKqueueFileSystemWatcherEngine(int kqfd, QObject *parent);
 
     int kqfd;
 
index 0f1032c..9f403b3 100644 (file)
@@ -68,7 +68,8 @@ class QFileSystemWatcherEngine : public QObject
     Q_OBJECT
 
 protected:
-    inline QFileSystemWatcherEngine()
+    inline QFileSystemWatcherEngine(QObject *parent)
+        : QObject(parent)
     {
     }
 
@@ -94,7 +95,7 @@ class QFileSystemWatcherPrivate : public QObjectPrivate
 {
     Q_DECLARE_PUBLIC(QFileSystemWatcher)
 
-    static QFileSystemWatcherEngine *createNativeEngine();
+    static QFileSystemWatcherEngine *createNativeEngine(QObject *parent);
 
 public:
     QFileSystemWatcherPrivate();
index ef106ac..23dca14 100644 (file)
@@ -44,8 +44,9 @@
 
 QT_BEGIN_NAMESPACE
 
-QPollingFileSystemWatcherEngine::QPollingFileSystemWatcherEngine()
-    : timer(this)
+QPollingFileSystemWatcherEngine::QPollingFileSystemWatcherEngine(QObject *parent)
+    : QFileSystemWatcherEngine(parent),
+      timer(this)
 {
     connect(&timer, SIGNAL(timeout()), SLOT(timeout()));
 }
index 717d438..3b3272a 100644 (file)
@@ -108,7 +108,7 @@ class QPollingFileSystemWatcherEngine : public QFileSystemWatcherEngine
     QHash<QString, FileInfo> files, directories;
 
 public:
-    QPollingFileSystemWatcherEngine();
+    QPollingFileSystemWatcherEngine(QObject *parent);
 
     QStringList addPaths(const QStringList &paths, QStringList *files, QStringList *directories);
     QStringList removePaths(const QStringList &paths, QStringList *files, QStringList *directories);
index 4da29bb..8e6b779 100644 (file)
@@ -79,6 +79,9 @@ class QWindowsFileSystemWatcherEngine : public QFileSystemWatcherEngine
 {
     Q_OBJECT
 public:
+    inline QWindowsFileSystemWatcherEngine(QObject *parent)
+        : QFileSystemWatcherEngine(parent)
+    { }
     ~QWindowsFileSystemWatcherEngine();
 
     QStringList addPaths(const QStringList &paths, QStringList *files, QStringList *directories);