Delay watching of config file changes until an event loop is running due to limitatio... delay_watch
authorHong Jen Yee (PCMan) <pcman.tw@gmail.com>
Fri, 27 Jun 2014 19:15:48 +0000 (03:15 +0800)
committerHong Jen Yee (PCMan) <pcman.tw@gmail.com>
Fri, 27 Jun 2014 19:15:48 +0000 (03:15 +0800)
lxqtsettings.cpp
lxqtsettings.h

index b37da52..a265cb1 100644 (file)
@@ -34,6 +34,7 @@
 #include <QMutex>
 #include <QFileSystemWatcher>
 #include <QSharedData>
+#include <QTimer>
 
 #include <XdgIcon>
 #include <XdgDirs>
@@ -44,16 +45,29 @@ class LxQt::SettingsPrivate
 {
 public:
     SettingsPrivate(Settings* parent):
-        mParent(parent)
+        mParent(parent),
+        mWatcher(NULL)
+    {
+    }
+    
+    ~SettingsPrivate()
     {
+        if(mWatcher)
+            delete mWatcher;
     }
 
     QString localizedKey(const QString& key) const;
 
-    QFileSystemWatcher mWatcher;
+    QFileSystemWatcher* watcher()
+    {
+        if(!mWatcher)
+            mWatcher = new QFileSystemWatcher();
+        return mWatcher;
+    }
 
 private:
     Settings* mParent;
+    QFileSystemWatcher* mWatcher;
 };
 
 
@@ -105,8 +119,7 @@ Settings::Settings(const QString& module, QObject* parent) :
         setValue("__userfile__", true);
         sync();
     }
-    d_ptr->mWatcher.addPath(this->fileName());
-    connect(&(d_ptr->mWatcher), SIGNAL(fileChanged(QString)), this, SLOT(fileChanged()));
+    QTimer::singleShot(0, this, SLOT(initWatch()));
 }
 
 
@@ -124,8 +137,7 @@ Settings::Settings(const QString &fileName, QSettings::Format format, QObject *p
         setValue("__userfile__", true);
         sync();
     }
-    d_ptr->mWatcher.addPath(this->fileName());
-    connect(&(d_ptr->mWatcher), SIGNAL(fileChanged(QString)), this, SLOT(fileChanged()));
+    QTimer::singleShot(0, this, SLOT(initWatch()));
 }
 
 
@@ -164,6 +176,12 @@ Settings::~Settings()
     delete d_ptr;
 }
 
+void Settings::initWatch()
+{
+    d_ptr->watcher()->addPath(this->fileName());
+    connect(d_ptr->watcher(), SIGNAL(fileChanged(QString)), this, SLOT(fileChanged()));
+}
+
 bool Settings::event(QEvent *event)
 {
     if (event->type() == QEvent::UpdateRequest)
index e7c838a..242095d 100644 (file)
@@ -86,6 +86,7 @@ protected:
     bool event(QEvent *event);
 
 protected slots:
+    void initWatch();
     virtual void fileChanged();
 
 private: