blob: 7b9c9cf2af06a904cc779c103489731ff35a1d17 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
/*
For general Scribus (>=1.3.2) copyright and licensing information please refer
to the COPYING file provided with the program. Following this notice may exist
a copyright and/or license notice that predates the release of Scribus 1.3.2
for which a new license (GPL+exception) is in place.
*/
#include "scconfig.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#if defined(_WIN32) && !defined(usleep)
#include <windows.h>
#define usleep(t) Sleep((t > 1000) ? (t / 1000) : 1)
#endif
#include <QDebug>
#include <QStringList>
#include "filewatcher.h"
FileWatcher::FileWatcher( QObject* parent) : QObject(parent)
{
m_stateFlags = 0;
m_timeOut = 10000;
watchedFiles.clear();
watchTimer = new QTimer(this);
watchTimer->setSingleShot(true);
connect(watchTimer, SIGNAL(timeout()), this, SLOT(checkFiles()));
watchTimer->start(m_timeOut);
}
FileWatcher::~FileWatcher()
{
m_stateFlags |= Dying;
this->stop();
disconnect(watchTimer, SIGNAL(timeout()), this, SLOT(checkFiles()));
if (!(m_stateFlags & AddRemoveBlocked))
watchedFiles.clear();
delete watchTimer;
}
void FileWatcher::setTimeOut(const int newTimeOut, const bool restartTimer)
{
m_timeOut=newTimeOut;
if (restartTimer)
start();
}
int FileWatcher::timeOut() const
{
return m_timeOut;
}
void FileWatcher::addFile(QString fileName)
{
if (fileName.isEmpty())
{
qDebug()<<"Attempt to add empty filename to filewatcher";
return;
}
watchTimer->stop();
if (!watchedFiles.contains(fileName))
{
struct fileMod fi;
fi.info = QFileInfo(fileName);
fi.timeInfo = fi.info.lastModified();
fi.pendingCount = 0;
fi.pending = false;
fi.refCount = 1;
watchedFiles.insert(fileName, fi);
}
else
watchedFiles[fileName].refCount++;
if (!(m_stateFlags & TimerStopped))
watchTimer->start(m_timeOut);
}
void FileWatcher::removeFile(QString fileName)
{
watchTimer->stop();
if (watchedFiles.contains(fileName))
{
watchedFiles[fileName].refCount--;
if (watchedFiles[fileName].refCount == 0)
watchedFiles.remove(fileName);
}
if (!(m_stateFlags & TimerStopped))
watchTimer->start(m_timeOut);
}
void FileWatcher::addDir(QString fileName)
{
addFile(fileName);
}
void FileWatcher::removeDir(QString fileName)
{
removeFile(fileName);
}
void FileWatcher::start()
{
watchTimer->stop();
m_stateFlags &= ~TimerStopped;
watchTimer->start(m_timeOut);
}
void FileWatcher::stop()
{
watchTimer->stop();
m_stateFlags |= StopRequested;
while ((m_stateFlags & FileCheckRunning))
usleep(100);
m_stateFlags &= ~StopRequested;
m_stateFlags |= TimerStopped;
}
void FileWatcher::forceScan()
{
checkFiles();
}
bool FileWatcher::isActive()
{
return (m_stateFlags & AddRemoveBlocked);
}
QList<QString> FileWatcher::files()
{
return watchedFiles.keys();
}
void FileWatcher::checkFiles()
{
m_stateFlags |= AddRemoveBlocked;
m_stateFlags |= FileCheckRunning;
watchTimer->stop();
m_stateFlags |= TimerStopped;
QDateTime time;
QStringList toRemove;
QMap<QString, fileMod>::Iterator it;
for ( it = watchedFiles.begin(); !(m_stateFlags & FileCheckMustStop) && it != watchedFiles.end(); ++it )
{
it.value().info.refresh();
if (!it.value().info.exists())
{
if (m_stateFlags & FileCheckMustStop)
break;
if (!it.value().pending)
{
it.value().pendingCount = 5;
it.value().pending = true;
emit statePending(it.key());
continue;
}
else
{
if (it.value().pendingCount != 0)
{
it.value().pendingCount--;
continue;
}
else
{
it.value().pending = false;
if (it.value().info.isDir())
emit dirDeleted(it.key());
else
emit fileDeleted(it.key());
if (m_stateFlags & FileCheckMustStop)
break;
it.value().refCount--;
if (it.value().refCount == 0)
toRemove.append(it.key());
continue;
}
}
}
else
{
//qDebug()<<it.key();
it.value().pending = false;
time = it.value().info.lastModified();
if (time != it.value().timeInfo)
{
//qDebug()<<"Times different: last modified:"<<time<<"\t recorded time:"<<it.value().timeInfo;
if (it.value().info.isDir())
{
//qDebug()<<"dir, ignoring"<<it.key();
it.value().timeInfo = time;
if (!(m_stateFlags & FileCheckMustStop))
emit dirChanged(it.key());
}
else
{
uint sizeo = it.value().info.size();
usleep(100);
it.value().info.refresh();
uint sizen = it.value().info.size();
//qDebug()<<"Size comparison"<<sizeo<<sizen<<it.key();
while (sizen != sizeo)
{
sizeo = sizen;
usleep(100);
it.value().info.refresh();
sizen = it.value().info.size();
}
it.value().timeInfo = time;
if (m_stateFlags & FileCheckMustStop)
break;
emit fileChanged(it.key());
}
}
}
}
if (m_stateFlags & Dying)
watchedFiles.clear();
else
{
for( int i=0; i<toRemove.count(); ++i)
watchedFiles.remove(toRemove[i]);
m_stateFlags &= ~AddRemoveBlocked;
m_stateFlags &= ~TimerStopped;
watchTimer->start(m_timeOut);
}
m_stateFlags &= ~FileCheckRunning;
}
|