|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtCore module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "qfilesystemwatcher.h" |
|
43 #include "qfilesystemwatcher_symbian_p.h" |
|
44 #include "qfileinfo.h" |
|
45 #include "qdebug.h" |
|
46 #include "private/qcore_symbian_p.h" |
|
47 #include <QDir> |
|
48 |
|
49 #ifndef QT_NO_FILESYSTEMWATCHER |
|
50 |
|
51 |
|
52 QT_BEGIN_NAMESPACE |
|
53 |
|
54 QNotifyChangeEvent::QNotifyChangeEvent(RFs &fs, const TDesC &file, |
|
55 QSymbianFileSystemWatcherEngine *e, bool aIsDir, |
|
56 TInt aPriority) : |
|
57 CActive(aPriority), |
|
58 isDir(aIsDir), |
|
59 fsSession(fs), |
|
60 watchedPath(file), |
|
61 engine(e), |
|
62 failureCount(0) |
|
63 { |
|
64 if (isDir) { |
|
65 fsSession.NotifyChange(ENotifyEntry, iStatus, file); |
|
66 } else { |
|
67 fsSession.NotifyChange(ENotifyAll, iStatus, file); |
|
68 } |
|
69 CActiveScheduler::Add(this); |
|
70 SetActive(); |
|
71 } |
|
72 |
|
73 QNotifyChangeEvent::~QNotifyChangeEvent() |
|
74 { |
|
75 Cancel(); |
|
76 } |
|
77 |
|
78 void QNotifyChangeEvent::RunL() |
|
79 { |
|
80 if(iStatus.Int() == KErrNone) { |
|
81 failureCount = 0; |
|
82 } else { |
|
83 qWarning("QNotifyChangeEvent::RunL() - Failed to order change notifications: %d", iStatus.Int()); |
|
84 failureCount++; |
|
85 } |
|
86 |
|
87 // Re-request failed notification once, but if it won't start working, |
|
88 // we can't do much besides just not request any more notifications. |
|
89 if (failureCount < 2) { |
|
90 if (isDir) { |
|
91 fsSession.NotifyChange(ENotifyEntry, iStatus, watchedPath); |
|
92 } else { |
|
93 fsSession.NotifyChange(ENotifyAll, iStatus, watchedPath); |
|
94 } |
|
95 SetActive(); |
|
96 |
|
97 if (!failureCount) { |
|
98 QT_TRYCATCH_LEAVING(engine->emitPathChanged(this)); |
|
99 } |
|
100 } |
|
101 } |
|
102 |
|
103 void QNotifyChangeEvent::DoCancel() |
|
104 { |
|
105 fsSession.NotifyChangeCancel(iStatus); |
|
106 } |
|
107 |
|
108 QSymbianFileSystemWatcherEngine::QSymbianFileSystemWatcherEngine() : |
|
109 errorCode(KErrNone), watcherStarted(false) |
|
110 { |
|
111 moveToThread(this); |
|
112 } |
|
113 |
|
114 QSymbianFileSystemWatcherEngine::~QSymbianFileSystemWatcherEngine() |
|
115 { |
|
116 stop(); |
|
117 } |
|
118 |
|
119 QStringList QSymbianFileSystemWatcherEngine::addPaths(const QStringList &paths, QStringList *files, |
|
120 QStringList *directories) |
|
121 { |
|
122 QMutexLocker locker(&mutex); |
|
123 QStringList p = paths; |
|
124 |
|
125 if (!startWatcher()) { |
|
126 qWarning("Could not start QSymbianFileSystemWatcherEngine thread"); |
|
127 |
|
128 return p; |
|
129 } |
|
130 |
|
131 QMutableListIterator<QString> it(p); |
|
132 while (it.hasNext()) { |
|
133 QString path = it.next(); |
|
134 QFileInfo fi(path); |
|
135 if (!fi.exists()) |
|
136 continue; |
|
137 |
|
138 bool isDir = fi.isDir(); |
|
139 if (isDir) { |
|
140 if (directories->contains(path)) |
|
141 continue; |
|
142 } else { |
|
143 if (files->contains(path)) |
|
144 continue; |
|
145 } |
|
146 |
|
147 // Use absolute filepath as relative paths seem to have some issues. |
|
148 QString filePath = fi.absoluteFilePath(); |
|
149 if (isDir && filePath.at(filePath.size() - 1) != QChar(L'/')) { |
|
150 filePath += QChar(L'/'); |
|
151 } |
|
152 |
|
153 currentEvent = NULL; |
|
154 QMetaObject::invokeMethod(this, |
|
155 "addNativeListener", |
|
156 Qt::QueuedConnection, |
|
157 Q_ARG(QString, filePath)); |
|
158 |
|
159 syncCondition.wait(&mutex); |
|
160 |
|
161 if (currentEvent) { |
|
162 currentEvent->isDir = isDir; |
|
163 |
|
164 activeObjectToPath.insert(currentEvent, path); |
|
165 it.remove(); |
|
166 |
|
167 if (isDir) |
|
168 directories->append(path); |
|
169 else |
|
170 files->append(path); |
|
171 } |
|
172 } |
|
173 |
|
174 return p; |
|
175 } |
|
176 |
|
177 QStringList QSymbianFileSystemWatcherEngine::removePaths(const QStringList &paths, |
|
178 QStringList *files, |
|
179 QStringList *directories) |
|
180 { |
|
181 QMutexLocker locker(&mutex); |
|
182 |
|
183 QStringList p = paths; |
|
184 QMutableListIterator<QString> it(p); |
|
185 while (it.hasNext()) { |
|
186 QString path = it.next(); |
|
187 |
|
188 currentEvent = activeObjectToPath.key(path); |
|
189 if (!currentEvent) |
|
190 continue; |
|
191 activeObjectToPath.remove(currentEvent); |
|
192 |
|
193 QMetaObject::invokeMethod(this, |
|
194 "removeNativeListener", |
|
195 Qt::QueuedConnection); |
|
196 |
|
197 syncCondition.wait(&mutex); |
|
198 |
|
199 it.remove(); |
|
200 |
|
201 files->removeAll(path); |
|
202 directories->removeAll(path); |
|
203 } |
|
204 |
|
205 if (activeObjectToPath.size() == 0) |
|
206 stop(); |
|
207 |
|
208 return p; |
|
209 } |
|
210 |
|
211 void QSymbianFileSystemWatcherEngine::emitPathChanged(QNotifyChangeEvent *e) |
|
212 { |
|
213 QMutexLocker locker(&mutex); |
|
214 |
|
215 QString path = activeObjectToPath.value(e); |
|
216 QFileInfo fi(path); |
|
217 |
|
218 if (e->isDir) |
|
219 emit directoryChanged(path, !fi.exists()); |
|
220 else |
|
221 emit fileChanged(path, !fi.exists()); |
|
222 } |
|
223 |
|
224 void QSymbianFileSystemWatcherEngine::stop() |
|
225 { |
|
226 QMetaObject::invokeMethod(this, "quit"); |
|
227 wait(); |
|
228 } |
|
229 |
|
230 // This method must be called inside mutex |
|
231 bool QSymbianFileSystemWatcherEngine::startWatcher() |
|
232 { |
|
233 bool retval = true; |
|
234 |
|
235 if (!watcherStarted) { |
|
236 setStackSize(0x5000); |
|
237 start(); |
|
238 syncCondition.wait(&mutex); |
|
239 |
|
240 if (errorCode != KErrNone) { |
|
241 retval = false; |
|
242 } else { |
|
243 watcherStarted = true; |
|
244 } |
|
245 } |
|
246 return retval; |
|
247 } |
|
248 |
|
249 |
|
250 void QSymbianFileSystemWatcherEngine::run() |
|
251 { |
|
252 // Initialize file session |
|
253 |
|
254 mutex.lock(); |
|
255 syncCondition.wakeOne(); |
|
256 mutex.unlock(); |
|
257 |
|
258 if (errorCode == KErrNone) { |
|
259 exec(); |
|
260 |
|
261 foreach(QNotifyChangeEvent *e, activeObjectToPath.keys()) { |
|
262 e->Cancel(); |
|
263 delete e; |
|
264 } |
|
265 |
|
266 activeObjectToPath.clear(); |
|
267 watcherStarted = false; |
|
268 } |
|
269 } |
|
270 |
|
271 void QSymbianFileSystemWatcherEngine::addNativeListener(const QString &directoryPath) |
|
272 { |
|
273 QMutexLocker locker(&mutex); |
|
274 QString nativeDir(QDir::toNativeSeparators(directoryPath)); |
|
275 TPtrC ptr(qt_QString2TPtrC(nativeDir)); |
|
276 currentEvent = new QNotifyChangeEvent(qt_s60GetRFs(), ptr, this, directoryPath.endsWith(QChar(L'/'), Qt::CaseSensitive)); |
|
277 syncCondition.wakeOne(); |
|
278 } |
|
279 |
|
280 void QSymbianFileSystemWatcherEngine::removeNativeListener() |
|
281 { |
|
282 QMutexLocker locker(&mutex); |
|
283 currentEvent->Cancel(); |
|
284 delete currentEvent; |
|
285 currentEvent = NULL; |
|
286 syncCondition.wakeOne(); |
|
287 } |
|
288 |
|
289 |
|
290 QT_END_NAMESPACE |
|
291 #endif // QT_NO_FILESYSTEMWATCHER |