~ubuntu-branches/ubuntu/maverick/scribus-ng/maverick-backports

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
/*
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 "filewatcher.h"
#include "filewatcher.moc"

#include "scconfig.h"

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef _WIN32
#include <windows.h>
#endif

FileWatcher::FileWatcher( QObject* parent) : QObject(parent)
{
	m_timeOut=10000;
	watchedFiles.clear();
	watchTimer = new QTimer(this);
	connect(watchTimer, SIGNAL(timeout()), this, SLOT(checkFiles()));
	watchTimer->start(m_timeOut, true);
	blockAddRemove = false;
	stopped = false;
}

FileWatcher::~FileWatcher()
{
	watchTimer->stop();
	disconnect(watchTimer, SIGNAL(timeout()), this, SLOT(checkFiles()));
	watchedFiles.clear();
	delete watchTimer;
}

void FileWatcher::setTimeOut(const int newTimeOut, const bool restartTimer)
{
	m_timeOut=newTimeOut;
	if (restartTimer)
		start();
}

const int FileWatcher::timeOut()
{
	return m_timeOut;
}

void FileWatcher::addFile(QString fileName)
{
	watchTimer->stop();
	if (!watchedFiles.contains(fileName))
	{
		struct fileMod fi;
		fi.info = QFileInfo(fileName);
		fi.timeInfo = fi.info.lastModified();
		fi.pendingCount = 0;
		fi.pending = false;
		watchedFiles.insert(fileName, fi);
	}
	if (!stopped)
		watchTimer->start(m_timeOut, true);
}

void FileWatcher::removeFile(QString fileName)
{
	watchTimer->stop();
	if (watchedFiles.contains(fileName))
		watchedFiles.remove(fileName);
	if (!stopped)
		watchTimer->start(m_timeOut, true);
}

void FileWatcher::addDir(QString fileName)
{
	addFile(fileName);
}

void FileWatcher::removeDir(QString fileName)
{
	removeFile(fileName);
}

void FileWatcher::start()
{
	watchTimer->stop();
	stopped = false;
	watchTimer->start(m_timeOut, true);
}

void FileWatcher::stop()
{
	watchTimer->stop();
	stopped = true;
}

void FileWatcher::forceScan()
{
	checkFiles();
}

bool FileWatcher::isActive()
{
	return blockAddRemove;
}

QValueList<QString> FileWatcher::files()
{
	return watchedFiles.keys();
}

void FileWatcher::checkFiles()
{
	blockAddRemove = true;
	watchTimer->stop();
	stopped = true;
	QDateTime time;
	QStringList toRemove;
	QMap<QString, fileMod>::Iterator it;
	for ( it = watchedFiles.begin(); it != watchedFiles.end(); ++it )
	{
		it.data().info.refresh();
		if (!it.data().info.exists())
		{
			if (!it.data().pending)
			{
				it.data().pendingCount = 5;
				it.data().pending = true;
				emit statePending(it.key());
				continue;
			}
			else
			{
				if (it.data().pendingCount != 0)
				{
					it.data().pendingCount--;
					continue;
				}
				else
				{
					it.data().pending = false;
					if (it.data().info.isDir())
						emit dirDeleted(it.key());
					else
						emit fileDeleted(it.key());
					toRemove.append(it.key());
					continue;
				}
			}
		}
		else
		 {
			it.data().pending = false;
			time = it.data().info.lastModified();
			if (time != it.data().timeInfo)
			{
				if (it.data().info.isDir())
					emit dirChanged(it.key());
				else
				{
					uint sizeo = it.data().info.size();
			#ifndef _WIN32
					usleep(100);
			#else
					Sleep(1);
			#endif
					it.data().info.refresh();
					uint sizen = it.data().info.size();
					while (sizen != sizeo)
					{
						sizeo = sizen;
				#ifndef _WIN32
						usleep(100);
				#else
						Sleep(1);
				#endif
						it.data().info.refresh();
						sizen = it.data().info.size();
					}
					it.data().timeInfo = time;
					emit fileChanged(it.key());
				}
			}
		}
	}
	for(unsigned int i=0; i<toRemove.count(); ++i)
		watchedFiles.remove(toRemove[i]);
	blockAddRemove = false;
	stopped = false;
	watchTimer->start(m_timeOut, true);
}