92d0a2b6b03a939c84e15bf8986dd514cecf607a
[profile/ivi/qtbase.git] / src / corelib / io / qfilesystemwatcher_polling.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qfilesystemwatcher_polling_p.h"
43 #include <QtCore/qtimer.h>
44
45 QT_BEGIN_NAMESPACE
46
47 QPollingFileSystemWatcherEngine::QPollingFileSystemWatcherEngine(QObject *parent)
48     : QFileSystemWatcherEngine(parent),
49       timer(this)
50 {
51     connect(&timer, SIGNAL(timeout()), SLOT(timeout()));
52 }
53
54 QStringList QPollingFileSystemWatcherEngine::addPaths(const QStringList &paths,
55                                                       QStringList *files,
56                                                       QStringList *directories)
57 {
58     QStringList p = paths;
59     QMutableListIterator<QString> it(p);
60     while (it.hasNext()) {
61         QString path = it.next();
62         QFileInfo fi(path);
63         if (!fi.exists())
64             continue;
65         if (fi.isDir()) {
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);
71         } else {
72             if (!files->contains(path))
73                 files->append(path);
74             this->files.insert(path, fi);
75         }
76         it.remove();
77     }
78
79     if ((!this->files.isEmpty() ||
80          !this->directories.isEmpty()) &&
81         !timer.isActive()) {
82         timer.start(PollingInterval);
83     }
84
85     return p;
86 }
87
88 QStringList QPollingFileSystemWatcherEngine::removePaths(const QStringList &paths,
89                                                          QStringList *files,
90                                                          QStringList *directories)
91 {
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);
98             it.remove();
99         } else if (this->files.remove(path)) {
100             files->removeAll(path);
101             it.remove();
102         }
103     }
104
105     if (this->files.isEmpty() &&
106         this->directories.isEmpty()) {
107         timer.stop();
108     }
109
110     return p;
111 }
112
113 void QPollingFileSystemWatcherEngine::timeout()
114 {
115     QMutableHashIterator<QString, FileInfo> fit(files);
116     while (fit.hasNext()) {
117         QHash<QString, FileInfo>::iterator x = fit.next();
118         QString path = x.key();
119         QFileInfo fi(path);
120         if (!fi.exists()) {
121             fit.remove();
122             emit fileChanged(path, true);
123         } else if (x.value() != fi) {
124             x.value() = fi;
125             emit fileChanged(path, false);
126         }
127     }
128     QMutableHashIterator<QString, FileInfo> dit(directories);
129     while (dit.hasNext()) {
130         QHash<QString, FileInfo>::iterator x = dit.next();
131         QString path = x.key();
132         QFileInfo fi(path);
133         if (!path.endsWith(QLatin1Char('/')))
134             fi = QFileInfo(path + QLatin1Char('/'));
135         if (!fi.exists()) {
136             dit.remove();
137             emit directoryChanged(path, true);
138         } else if (x.value() != fi) {
139             fi.refresh();
140             if (!fi.exists()) {
141                 dit.remove();
142                 emit directoryChanged(path, true);
143             } else {
144                 x.value() = fi;
145                 emit directoryChanged(path, false);
146             }
147         }
148     }
149 }
150
151 QT_END_NAMESPACE