Exemplo n.º 1
0
void FileWatcher::checkFiles()
{
	m_stateFlags |= AddRemoveBlocked;
	m_stateFlags |= FileCheckRunning;
	m_watchTimer->stop();
	m_stateFlags |= TimerStopped;
	QDateTime time;
	QStringList toRemove;
	
	QMap<QString, fileMod>::Iterator it;
//	qDebug()<<files();
	for ( it = m_watchedFiles.begin(); !(m_stateFlags & FileCheckMustStop) && it != m_watchedFiles.end(); ++it )
	{
		it.value().info.refresh();
		if (!it.value().info.exists())
		{
			if (m_stateFlags & FileCheckMustStop)
				break;
			if (!it.value().pending)
			{
				if (it.value().fast)
				{
					if (it.value().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;
				}
				it.value().pendingCount = 5;
				it.value().pending = true;
				emit statePending(it.key());
				continue;
			}
			if (it.value().pendingCount != 0)
			{
				it.value().pendingCount--;
				continue;
			}
			it.value().pending = false;
			if (it.value().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;
		}
		//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().isDir)
			{
				//					qDebug()<<"dir, ignoring"<<it.key();
				it.value().timeInfo = time;
				if (!(m_stateFlags & FileCheckMustStop))
					emit dirChanged(it.key());
			}
			else
			{
				qint64 sizeo = it.value().info.size();
				std::this_thread::sleep_for(std::chrono::milliseconds(1));
				it.value().info.refresh();
				qint64 sizen = it.value().info.size();
				//					qDebug()<<"Size comparison"<<sizeo<<sizen<<it.key();
				while (sizen != sizeo)
				{
					sizeo = sizen;
					std::this_thread::sleep_for(std::chrono::milliseconds(1));
					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)
		m_watchedFiles.clear();
	else
	{
		for (int i=0; i<toRemove.count(); ++i)
			m_watchedFiles.remove(toRemove[i]);
		m_stateFlags &= ~AddRemoveBlocked;
		m_stateFlags &= ~TimerStopped;
		m_watchTimer->start(m_timeOut);
	}
	m_stateFlags &= ~FileCheckRunning;
}
Exemplo n.º 2
0
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)
			{
				if (it.value().fast)
				{
					if (it.value().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
				{
					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().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
		 {
			it.value().pending = false;
			time = it.value().info.lastModified();
			if (time != it.value().timeInfo)
			{
				if (it.value().isDir)
				{
					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();
					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;
}