void CDspVmAutoTaskManagerBase::timerEvent(QTimerEvent* te)
{
	if (!IsInitialized())
	{
		WRITE_TRACE( DBG_WARNING, "Timer event on unitialized %s manager", getManagerName() );
		return;
	}

// Looking for proper VM ident.

	bool bRestartTimer = false;
	TimerInfo timerInfo;
	CVmIdent vmIdent;
	{
		QMutexLocker locker(&m_lockVmIdents);

		QMap<CVmIdent, TimerInfo >::iterator it;
		for(it = m_mapVmIdents.begin(); it != m_mapVmIdents.end(); ++it)
		{
			timerInfo = it.value();
			if (timerInfo.iTimerId == te->timerId())
				break;
		}

		if ( it == m_mapVmIdents.end() || timerInfo.bTaskInUse)
			return;

		vmIdent = it.key();

		bRestartTimer = (timerInfo.iPeriod != timerInfo.iOriginalPeriod);

		if (bRestartTimer)
		{
			emit killVmTimerSignal(timerInfo.iTimerId);

			m_mapVmIdents.remove(vmIdent);
		}
	}

	if (bRestartTimer)
	{
		emit startVmTimerSignal(vmIdent, timerInfo.iOriginalPeriod, timerInfo.iOriginalPeriod);
	}

// Store timestamp
	{
		CDspLockedPointer<QSettings> pQSettings = CDspService::instance()->getQSettings();

		QString qsGroup = getSettingsGroupKey(vmIdent);
		pQSettings->beginGroup(qsGroup);

		pQSettings->setValue(getSettingsKeyTimestamp(), QVariant(QDateTime::currentDateTime()));

		pQSettings->endGroup();
	}

// Get client

	QHash< IOSender::Handle, SmartPtr<CDspClient> > hashClients =
		CDspService::instance()->getClientManager().getSessionListByVm(
		vmIdent.second, vmIdent.first, CDspAccessManager::VmAccessRights::makeModeRWX() );
	if ( hashClients.empty() )
	{
		WRITE_TRACE(DBG_FATAL, "No clients connect to VM %s with required permissions",
								QSTR2UTF8(vmIdent.first) );
		return;
	}

// Start auto task

	if ( ! updateTaskState(vmIdent, true) )
		return;

	// Fake client
	SmartPtr<CDspClient> pClient( new CDspClient(IOSender::Handle()) );
	pClient->getAuthHelper().AuthUserBySelfProcessOwner();
	pClient->setVmDirectoryUuid(vmIdent.second);

	startTask( pClient, vmIdent );
}
PRL_RESULT CDspVmAutoTaskManagerBase::tryToRegisterVm(const SmartPtr<CVmConfiguration>& pVmConfig,
													  const QString& qsVmDirUuid,
													  int iRecommendedNextTime)
{
	if ( ! pVmConfig )
		return PRL_ERR_FAILURE;

	CVmIdent vmIdent = MakeVmIdent(pVmConfig->getVmIdentification()->getVmUuid(), qsVmDirUuid);

	// Fake client (calling at starting dispatcher from Init())
	SmartPtr<CDspClient> pClient( new CDspClient(IOSender::Handle()) );
	pClient->getAuthHelper().AuthUserBySelfProcessOwner();
	pClient->setVmDirectoryUuid(vmIdent.second);

	PRL_RESULT res = prepareRegistration( vmIdent, pClient );
	if ( PRL_FAILED( res ) )
		return res;

	if ( !isEnabled( pVmConfig, false ) )
		return unregisterVm(vmIdent);

// Check period
	int iPeriod;
	res = getPeriod( pVmConfig, iPeriod );
	if ( PRL_FAILED( res ) )
		return res;

// Register VM and start timer
	{
		QMutexLocker locker(&m_lockVmIdents);

		if (m_mapVmIdents.contains(vmIdent))
		{
			TimerInfo timerInfo = m_mapVmIdents.value(vmIdent);

			if ( timerInfo.bAutoTask )
			{
				int iOldPeriod = timerInfo.iPeriod;
				if (iOldPeriod && iOldPeriod != iPeriod)
				{
					emit killVmTimerSignal(timerInfo.iTimerId);

					m_mapVmIdents.remove(vmIdent);
				}
				else
				{
					return PRL_ERR_SUCCESS;
				}
			}
		}
	}

// Check timestamp

	QDateTime dtNow = QDateTime::currentDateTime();

	QDateTime dtTimestamp;
	{
		CDspLockedPointer<QSettings> pQSettings = CDspService::instance()->getQSettings();

		QString qsGroup = getSettingsGroupKey(vmIdent);
		pQSettings->beginGroup(qsGroup);

		QString qsKeyTimeStamp = getSettingsKeyTimestamp();
		dtTimestamp = pQSettings->value(qsKeyTimeStamp, QVariant(dtNow)).toDateTime();

		if ( ! pQSettings->contains(qsKeyTimeStamp) )
			pQSettings->setValue(qsKeyTimeStamp, QVariant(dtNow));

		pQSettings->endGroup();
	}

	int iOriginalPeriod = iPeriod;

	int iSkippedInterval = dtTimestamp.secsTo( dtNow );
	if ( iSkippedInterval > 0 )
	{
		iPeriod = (iSkippedInterval > iPeriod ? 0 : iPeriod - iSkippedInterval);
	}
	if ( iRecommendedNextTime )
		iPeriod = qMin(iPeriod, iRecommendedNextTime);

	WRITE_TRACE( DBG_FATAL, "%s manager: register VM %s with period %d (%d)",
				getManagerName(), QSTR2UTF8( pVmConfig->getVmIdentification()->getVmName() ),
				iPeriod, iOriginalPeriod );
	emit startVmTimerSignal(vmIdent, iPeriod, iOriginalPeriod);

	return PRL_ERR_SUCCESS;
}