[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    [patch] Break limitation of file watchers for KDirWatch on Windows
From:       Jaroslaw Staniek <js () iidea ! pl>
Date:       2008-07-09 20:13:00
Message-ID: 48751BCC.6020005 () iidea ! pl
[Download RAW message or body]

For review (trunk).

Maximum number of wait objects per single QFileSystemWatcher is 
MAXIMUM_WAIT_OBJECTS (64) on Windows.

Attached implementation of KFileSystemWatcher. It's a typedef for 
QFileSystemWatcher, except on Windows, where we allocate more 
QFileSystemWatcher instances on demand (and deallocate them too).

-- 
regards / pozdrawiam, Jaroslaw Staniek
  Sponsored by OpenOffice Polska (http://www.openoffice.com.pl/en) to work on
  Kexi & KOffice (http://www.kexi.pl/en, http://www.koffice.org/kexi)
  KDE Libraries for MS Windows (http://windows.kde.org)

["kdirwatch_win.cpp" (text/x-c++src)]

/* This file is part of the KDE libraries
   Copyright (C) 2008 Jaroslaw Staniek <js@iidea.pl>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "kdirwatch_p.h"

#include <windows.h>

KFileSystemWatcher::KFileSystemWatcher()
 : QObject(), m_recentWatcher(0)
{
}

KFileSystemWatcher::~KFileSystemWatcher()
{
  qDeleteAll(m_watchers);
}

QFileSystemWatcher* KFileSystemWatcher::availableWatcher()
{
  QFileSystemWatcher* watcher = m_recentWatcher;
  if (!watcher || m_usedObjects.value(watcher) >= MAXIMUM_WAIT_OBJECTS) {
    uint i = 0;
    watcher = 0;
    for (QList<QFileSystemWatcher*>::ConstIterator watchersIt(m_watchers.constBegin());
      watchersIt!=m_watchers.constEnd(); ++watchersIt, i++)
    {
      if (m_usedObjects.value(*watchersIt) < MAXIMUM_WAIT_OBJECTS) {
        watcher = *watchersIt;
        m_recentWatcher = watcher;
        return watcher;
      }
    }
  }
  if (!watcher) { //new one needed
    watcher = new QFileSystemWatcher();
    connect(watcher, SIGNAL(directoryChanged(QString)), this, SIGNAL(directoryChanged(QString)));
    connect(watcher, SIGNAL(fileChanged(QString)), this, SIGNAL(fileChanged(QString)));
    m_watchers.append( watcher );
    m_usedObjects.insert(watcher, 0);
    m_recentWatcher = watcher;
  }
  return watcher;
}

void KFileSystemWatcher::addPath(const QString &file)
{
  QFileSystemWatcher* watcher = availableWatcher();
  watcher->addPath(file);
  m_usedObjects[watcher]++;
  m_paths.insert(file, watcher);
}

void KFileSystemWatcher::removePath(const QString &file)
{
  QFileSystemWatcher* watcher = m_paths.value(file);
  if (!watcher)
    return;
  watcher->removePath(file);
  m_usedObjects[watcher]--;
  if (m_recentWatcher != watcher)
    m_recentWatcher = 0;
}

["kdirwatch.patch" (text/plain)]

Index: kio/kio/kdirwatch.cpp
===================================================================
--- kio/kio/kdirwatch.cpp	(wersja 829875)
+++ kio/kio/kdirwatch.cpp	(kopia robocza)
@@ -51,9 +51,6 @@
 #include <QtCore/QFile>
 #include <QtCore/QSocketNotifier>
 #include <QtCore/QTimer>
-#ifdef HAVE_QFILESYSTEMWATCHER
-#include <QtCore/QFileSystemWatcher>
-#endif
 
 #include <kapplication.h>
 #include <kdebug.h>
@@ -202,7 +199,7 @@
 #endif
 #ifdef HAVE_QFILESYSTEMWATCHER
   availableMethods << "QFileSystemWatcher";
-  fsWatcher = new QFileSystemWatcher();
+  fsWatcher = new KFileSystemWatcher();
   connect(fsWatcher, SIGNAL(directoryChanged(QString)), this, SLOT(fswEventReceived(QString)));
   connect(fsWatcher, SIGNAL(fileChanged(QString)),      this, SLOT(fswEventReceived(QString)));
 #endif
Index: kio/CMakeLists.txt
===================================================================
--- kio/CMakeLists.txt	(wersja 829875)
+++ kio/CMakeLists.txt	(kopia robocza)
@@ -128,6 +128,12 @@
    )
 endif(UNIX)
 
+if(WIN32)
+   set(kiocore_STAT_SRCS ${kiocore_STAT_SRCS}
+       kio/kdirwatch_win.cpp
+   )
+endif(WIN32)
+
 set(kbookmarks_STAT_SRCS
   bookmarks/kbookmark.cc
   bookmarks/kbookmarkimporter.cc
Index: kio/kio/kdirwatch_p.h
===================================================================
--- kio/kio/kdirwatch_p.h	(wersja 829875)
+++ kio/kio/kdirwatch_p.h	(kopia robocza)
@@ -7,6 +7,7 @@
  * Copyright (C) 1998 Sven Radej <sven@lisa.exp.univie.ac.at>
  * Copyright (C) 2006 Dirk Mueller <mueller@kde.org>
  * Copyright (C) 2007 Flavio Castelli <flavio.castelli@gmail.com>
+ * Copyright (C) 2008 Jaroslaw Staniek <js@iidea.pl>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -63,6 +64,40 @@
 
 #define invalid_ctime ((time_t)-1)
 
+#ifdef HAVE_QFILESYSTEMWATCHER
+#include <QtCore/QFileSystemWatcher>
+
+#if defined Q_WS_WIN
+/* Maximum number of wait objects per single QFileSystemWatcher
+ * is MAXIMUM_WAIT_OBJECTS (64) on Windows. 
+ * KFileSystemWatcher allocates more QFileSystemWatcher instances on demand
+ * (and deallocates them).
+ */
+class KFileSystemWatcher : public QObject
+{
+  Q_OBJECT
+public:
+  KFileSystemWatcher();
+  ~KFileSystemWatcher();
+  void addPath(const QString &file);
+  void removePath(const QString &file);
+
+Q_SIGNALS:
+    void fileChanged(const QString &path);
+    void directoryChanged(const QString &path);
+
+private:
+  QFileSystemWatcher* availableWatcher();
+  QFileSystemWatcher* m_recentWatcher;
+  QList<QFileSystemWatcher*> m_watchers;
+  QHash<QFileSystemWatcher*, uint> m_usedObjects;
+  QHash<QString,QFileSystemWatcher*> m_paths;
+};
+#else
+typedef KFileSystemWatcher QFileSystemWatcher;
+#endif
+#endif
+
 /* KDirWatchPrivate is a singleton and does the watching
  * for every KDirWatch instance in the application.
  */
@@ -194,7 +229,7 @@
   bool useINotify(Entry*);
 #endif
 #ifdef HAVE_QFILESYSTEMWATCHER
-  QFileSystemWatcher *fsWatcher;
+  KFileSystemWatcher *fsWatcher;
   bool useQFSWatch(Entry* e);
 #endif
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic