28
28
#if DEBUG_EXT_FS_WATCHER
29
29
# define DEBUG_FSWATCHER() \
30
30
qDebug() << "[extFsWatcher]" << QDateTime::currentDateTime().toString("hh:mm:ss.zzz") \
31
<< Q_FUNC_INFO << "m_setPath:" << m_setPath \
31
<< Q_FUNC_INFO << "m_setPath:" << m_setPaths \
32
32
<< "m_changedPath:" << m_changedPath \
33
33
<< "m_waitingEmit:" << m_waitingEmitCounter
40
40
QFileSystemWatcher(parent)
41
41
, m_waitingEmitCounter(0)
42
42
, m_msWaitTime(DEFAULT_NOTICATION_PERIOD)
43
, m_lastChangedIndex(-1)
44
45
connect(this, SIGNAL(directoryChanged(QString)),
45
46
this, SLOT(slotDirChanged(QString)));
49
50
void ExternalFSWatcher::setCurrentPath(const QString &curPath)
51
if (!curPath.isEmpty())
52
if (!curPath.isEmpty() && (m_setPaths.count() != 1 || m_setPaths.at(0) != curPath))
53
if (m_setPath != curPath)
55
if (!m_setPath.isEmpty())
57
removePath(m_setPath);
56
m_setPaths.append(curPath);
57
QFileSystemWatcher::addPath(curPath);
63
void ExternalFSWatcher::setCurrentPaths(const QStringList &paths)
65
QStringList myPaths(paths);
69
QFileSystemWatcher::addPaths(paths);
73
void ExternalFSWatcher::clearPaths()
75
QStringList existentPaths = QFileSystemWatcher::directories();
76
if (existentPaths.count() > 0)
78
QFileSystemWatcher::removePaths(existentPaths);
84
* \brief ExternalFSWatcher::slotDirChanged() schedules a Disk change to be notified
86
* Once path that belongs to \a m_setPaths is modified in the Disk it becomes the \a m_changedPath and
87
* its change is scheculed to notified later. This path is taken out from QFileSystemWatcher to avoid
88
* lots of continuous notifications from QFileSystemWatcher when having hevy disk io.
90
* \param dir directory changed in the File System
67
92
void ExternalFSWatcher::slotDirChanged(const QString &dir)
70
if ( (m_setPath == dir)
71
&& ( m_waitingEmitCounter == 0 || m_setPath != m_changedPath )
95
int index = m_setPaths.indexOf(dir);
96
if (index != -1 && (m_waitingEmitCounter == 0 || dir != m_changedPath))
74
removePath(m_setPath);
98
m_lastChangedIndex = index;
99
//changed path is taken from the QFileSystemWatcher and it becomes the current changed
100
//in this case there will not be slotDirChanged() for this path until slotFireChanges()
101
//restores the path in the QFileSystemWatcher
102
removePath(m_setPaths.at(m_lastChangedIndex));
75
103
++m_waitingEmitCounter;
76
m_changedPath = m_setPath;
77
105
QTimer::singleShot(m_msWaitTime, this, SLOT(slotFireChanges()));
83
111
* \brief ExternalFSWatcher::slotFireChanges() emits \ref pathModified() only when it is sure
84
* that the current path was changed.
86
* A change for the current path (the last current) MUST be notified at least once.
112
* that the LAST current path was changed.
114
* The notification will be sent out only for the LAST modified path (if more than one) from the \a m_setPaths
116
* \sa \ref ExternalFSWatcher class
88
118
void ExternalFSWatcher::slotFireChanges()
90
if (--m_waitingEmitCounter == 0)
93
if (m_setPath == m_changedPath)
120
if ( --m_waitingEmitCounter == 0
121
&& m_lastChangedIndex != -1
122
&& m_lastChangedIndex < m_setPaths.count() )
124
if (m_setPaths.at(m_lastChangedIndex) == m_changedPath)
126
emit pathModified(m_changedPath);
96
127
#if DEBUG_EXT_FS_WATCHER
97
128
DEBUG_FSWATCHER() << "emit pathModified()";
131
//restore the original list in QFileSystemWatcher
133
QFileSystemWatcher::addPaths(m_setPaths);