Changeset 374
- Timestamp:
- Dec 3, 2009, 2:17:13 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/corelib/io/qfilesystemwatcher.cpp
r2 r374 51 51 #include <qmutex.h> 52 52 #include <qset.h> 53 #include <q timer.h>53 #include <q.h> 54 54 55 55 #if defined(Q_OS_WIN) … … 64 64 QT_BEGIN_NAMESPACE 65 65 66 enum { PollingInterval = 1000 };67 68 66 class QPollingFileSystemWatcherEngine : public QFileSystemWatcherEngine 69 67 { 70 Q_OBJECT 68 enum { 69 SmallPollingInterval = 50, // time to wait after checking each watched 70 // item, ms 71 BigPollingInterval = 1000, // time to wait after checking all watched 72 // items, ms 73 }; 71 74 72 75 class FileInfo … … 84 87 permissions(fileInfo.permissions()), 85 88 lastModified(fileInfo.lastModified()) 86 { 89 { 87 90 if (fileInfo.isDir()) { 88 91 entries = fileInfo.absoluteDir().entryList(QDir::AllEntries); … … 119 122 void stop(); 120 123 121 private slots: 122 void timeout(); 124 private: 125 QWaitCondition waitCond; 126 bool askedToFinish; 127 bool startOver; 123 128 }; 124 129 125 130 QPollingFileSystemWatcherEngine::QPollingFileSystemWatcherEngine() 131 126 132 { 127 133 #ifndef QT_NO_THREAD … … 132 138 void QPollingFileSystemWatcherEngine::run() 133 139 { 134 QTimer timer; 135 connect(&timer, SIGNAL(timeout()), SLOT(timeout())); 136 timer.start(PollingInterval); 137 (void) exec(); 140 mutex.lock(); 141 142 forever { 143 startOver = false; 144 QMutableHashIterator<QString, FileInfo> fit(files); 145 while (!startOver && fit.hasNext()) { 146 QHash<QString, FileInfo>::iterator x = fit.next(); 147 QString path = x.key(); 148 QFileInfo fi(path); 149 if (!fi.exists()) { 150 fit.remove(); 151 emit fileChanged(path, true); 152 } else if (x.value() != fi) { 153 x.value() = fi; 154 emit fileChanged(path, false); 155 } 156 waitCond.wait(&mutex, SmallPollingInterval); 157 } 158 QMutableHashIterator<QString, FileInfo> dit(directories); 159 while (!startOver && dit.hasNext()) { 160 QHash<QString, FileInfo>::iterator x = dit.next(); 161 QString path = x.key(); 162 QFileInfo fi(path); 163 if (!path.endsWith(QLatin1Char('/'))) 164 fi = QFileInfo(path + QLatin1Char('/')); 165 if (!fi.exists()) { 166 dit.remove(); 167 emit directoryChanged(path, true); 168 } else if (x.value() != fi) { 169 x.value() = fi; 170 emit directoryChanged(path, false); 171 } 172 waitCond.wait(&mutex, SmallPollingInterval); 173 } 174 if (!startOver && !askedToFinish) { 175 waitCond.wait(&mutex, BigPollingInterval); 176 } 177 if (askedToFinish) { 178 askedToFinish = false; 179 break; 180 } 181 } 182 183 mutex.unlock(); 138 184 } 139 185 … … 156 202 fi = QFileInfo(path + QLatin1Char('/')); 157 203 this->directories.insert(path, fi); 204 158 205 } else { 159 206 if (!files->contains(path)) 160 207 files->append(path); 161 208 this->files.insert(path, fi); 209 162 210 } 163 211 it.remove(); 164 212 } 165 start( );213 start(); 166 214 return p; 167 215 } … … 179 227 directories->removeAll(path); 180 228 it.remove(); 229 181 230 } else if (this->files.remove(path)) { 182 231 files->removeAll(path); 183 232 it.remove(); 233 184 234 } 185 235 } 186 236 if (this->files.isEmpty() && this->directories.isEmpty()) { 237 238 187 239 locker.unlock(); 188 stop();189 240 wait(); 190 241 } … … 194 245 void QPollingFileSystemWatcherEngine::stop() 195 246 { 196 QMetaObject::invokeMethod(this, "quit");197 }198 199 void QPollingFileSystemWatcherEngine::timeout()200 {201 247 QMutexLocker locker(&mutex); 202 QMutableHashIterator<QString, FileInfo> fit(files); 203 while (fit.hasNext()) { 204 QHash<QString, FileInfo>::iterator x = fit.next(); 205 QString path = x.key(); 206 QFileInfo fi(path); 207 if (!fi.exists()) { 208 fit.remove(); 209 emit fileChanged(path, true); 210 } else if (x.value() != fi) { 211 x.value() = fi; 212 emit fileChanged(path, false); 213 } 214 } 215 QMutableHashIterator<QString, FileInfo> dit(directories); 216 while (dit.hasNext()) { 217 QHash<QString, FileInfo>::iterator x = dit.next(); 218 QString path = x.key(); 219 QFileInfo fi(path); 220 if (!path.endsWith(QLatin1Char('/'))) 221 fi = QFileInfo(path + QLatin1Char('/')); 222 if (!fi.exists()) { 223 dit.remove(); 224 emit directoryChanged(path, true); 225 } else if (x.value() != fi) { 226 x.value() = fi; 227 emit directoryChanged(path, false); 228 } 229 230 } 231 } 232 233 248 startOver = true; 249 askedToFinish = true; 250 waitCond.wakeAll(); 251 } 234 252 235 253 … … 615 633 #include "moc_qfilesystemwatcher.cpp" 616 634 617 #include "qfilesystemwatcher.moc"618 619 635 #endif // QT_NO_FILESYSTEMWATCHER 620 636
Note:
See TracChangeset
for help on using the changeset viewer.