File: qreliablefilewatch.cpp

package info (click to toggle)
texstudio 2.11.2%2Bdebian-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 41,292 kB
  • ctags: 12,405
  • sloc: cpp: 93,072; xml: 10,217; ansic: 4,153; sh: 145; makefile: 56
file content (202 lines) | stat: -rw-r--r-- 4,748 bytes parent folder | download | duplicates (3)
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
/****************************************************************************
**
** Copyright (C) 2006-2009 fullmetalcoder <fullmetalcoder@hotmail.fr>
**
** This file is part of the Edyuk project <http://edyuk.org>
**
** This file may be used under the terms of the GNU General Public License
** version 3 as published by the Free Software Foundation and appearing in the
** file GPL.txt included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "qreliablefilewatch.h"

/*!
	\file qreliablefilewatch.cpp
	\brief Implementation of the QReliableFileWatch class.
*/

/*!
	\class QReliableFileWatch
	\brief A specialized file monitor that works around some issues in QFileSystemWatcher
*/

QReliableFileWatch::QReliableFileWatch(QObject *p)
 : QFileSystemWatcher(p)
{
	connect(this, SIGNAL( fileChanged(QString) ),
			this, SLOT  ( sourceChanged(QString) ) );

}

QReliableFileWatch::~QReliableFileWatch()
{

}

void QReliableFileWatch::addWatch(const QString& file, QObject *recipient)
{
	QHash<QString, Watch>::iterator it = m_targets.find(file);

	if ( it != m_targets.end() )
	{
		if (!it->recipients.contains(recipient))
			it->recipients << recipient;
	} else {
		QFile f(file);
		QFileInfo info(file);

		Watch w;
		w.state = Clean;
		w.size = f.size();
		w.lastModified=info.lastModified();
		w.recipients << recipient;
		m_targets[file] = w;

		addPath(file);
	}
}

void QReliableFileWatch::removeWatch(QObject *recipient)
{
	removeWatch(QString(), recipient);
}

void QReliableFileWatch::removeWatch(const QString& file, QObject *recipient)
{
	QHash<QString, Watch>::iterator it = m_targets.find(file);

	if ( it == m_targets.end() )
	{
		if ( !recipient )
			return;

		// given recipient stop watching any file

		it = m_targets.begin();

		while ( it != m_targets.end() )
		{
			int n = it->recipients.removeAll(recipient);

			if ( n && it->recipients.isEmpty() )
			{
				// no more recipients watching this file
				removePath(it.key());
				it = m_targets.erase(it);
			} else {
				++it;
			}
		}
	} else {
		if ( recipient )
		{
			// given recipient stops watching given file
			it->recipients.removeAll(recipient);

			if ( it->recipients.isEmpty() )
			{
				// no more recipients watching this file
				removePath(it.key());
				m_targets.erase(it);
			}
		} else {
			// stop watching given file at all
			removePath(it.key());
			m_targets.erase(it);
		}
	}
}

void QReliableFileWatch::timerEvent(QTimerEvent *e)
{
	if ( e->timerId() != m_timer.timerId() )
		return QFileSystemWatcher::timerEvent(e);

	int postponedEmissions = 0;
	QHash<QString, Watch> targets=m_targets; //copy targets, so m_targets can be modified from the invoken functions without crashing
	QHash<QString, Watch>::iterator it = targets.begin();

	while ( it != targets.end() )
	{
		if ( it->state & Duplicate )
		{
			// postpone signal emission...
			++postponedEmissions;
		} else if ( it->state & Recent ) {
//			// send signal
//			it->state = Clean;
/*
			QFile f(it.key());

			if ( f.size() == (unsigned int)(it->size) )
			{
				// TODO : avoid signal emission if checksum match
				// DO this in editor or here?
			}*/

			//qDebug("%s emission.", qPrintable(it.key()));

			it->recipients.removeAll(0);

			foreach ( QObject *r, it->recipients )
				QMetaObject::invokeMethod(r, "fileChanged", Q_ARG(QString, it.key()));

			//it = m_state.erase(it);
		}

		++it;
	}

	m_timer.stop();
	if ( postponedEmissions )
	{
		//remove duplicates from the real hash
		it = m_targets.begin();
		while (it != m_targets.end()) {
			if ( it->state & Duplicate ) 
				it->state = Recent;
			++it;
		}
		//qDebug("%i postponed emissions", postponedEmissions);
		m_timer.start(20, this);
	} else {
		//remove changes from the real hash
		it = m_targets.begin();
		while (it != m_targets.end()) {
			if ( it->state & Recent ) 
				it->state = Clean;
			++it;
		}
	} 
}

void QReliableFileWatch::sourceChanged(const QString& filepath)
{
	m_timer.stop();

	QHash<QString, Watch>::iterator it = m_targets.find(filepath);

	if ( it == m_targets.end() )
		return;

	qDebug("%s modified.", qPrintable(filepath));
	QFileInfo info(filepath);
	if(it->lastModified==info.lastModified()){
		qDebug("filtered");
		return;
	}

	if ( it->state )
	{
		it->state = Recent | Duplicate;
	} else {
		it->state = Recent;
	}

	m_timer.start(20, this);
}