1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qfilesystemwatcher_polling_p.h"
43 #include <QtCore/qtimer.h>
47 QPollingFileSystemWatcherEngine::QPollingFileSystemWatcherEngine(QObject *parent)
48 : QFileSystemWatcherEngine(parent),
51 connect(&timer, SIGNAL(timeout()), SLOT(timeout()));
54 QStringList QPollingFileSystemWatcherEngine::addPaths(const QStringList &paths,
56 QStringList *directories)
58 QStringList p = paths;
59 QMutableListIterator<QString> it(p);
60 while (it.hasNext()) {
61 QString path = it.next();
66 if (!directories->contains(path))
67 directories->append(path);
68 if (!path.endsWith(QLatin1Char('/')))
69 fi = QFileInfo(path + QLatin1Char('/'));
70 this->directories.insert(path, fi);
72 if (!files->contains(path))
74 this->files.insert(path, fi);
79 if ((!this->files.isEmpty() ||
80 !this->directories.isEmpty()) &&
82 timer.start(PollingInterval);
88 QStringList QPollingFileSystemWatcherEngine::removePaths(const QStringList &paths,
90 QStringList *directories)
92 QStringList p = paths;
93 QMutableListIterator<QString> it(p);
94 while (it.hasNext()) {
95 QString path = it.next();
96 if (this->directories.remove(path)) {
97 directories->removeAll(path);
99 } else if (this->files.remove(path)) {
100 files->removeAll(path);
105 if (this->files.isEmpty() &&
106 this->directories.isEmpty()) {
113 void QPollingFileSystemWatcherEngine::timeout()
115 QMutableHashIterator<QString, FileInfo> fit(files);
116 while (fit.hasNext()) {
117 QHash<QString, FileInfo>::iterator x = fit.next();
118 QString path = x.key();
122 emit fileChanged(path, true);
123 } else if (x.value() != fi) {
125 emit fileChanged(path, false);
128 QMutableHashIterator<QString, FileInfo> dit(directories);
129 while (dit.hasNext()) {
130 QHash<QString, FileInfo>::iterator x = dit.next();
131 QString path = x.key();
133 if (!path.endsWith(QLatin1Char('/')))
134 fi = QFileInfo(path + QLatin1Char('/'));
137 emit directoryChanged(path, true);
138 } else if (x.value() != fi) {
142 emit directoryChanged(path, true);
145 emit directoryChanged(path, false);