コード例 #1
0
CSecureRule* CSecureRule::fromXML(QXmlStreamReader& oXMLdocument, float nVersion)
{
	QXmlStreamAttributes attributes = oXMLdocument.attributes();

	const QString sType = attributes.value( "type" ).toString();

	if ( sType.isEmpty() )
		return NULL;

	CSecureRule* pRule = NULL;

	if ( sType.compare( "address", Qt::CaseInsensitive ) == 0 )
	{
		QString sAddress = attributes.value( "address" ).toString();

		pRule = new CIPRule();
		pRule->parseContent( sAddress );
	}
	else if ( sType.compare( "addressrange", Qt::CaseInsensitive ) == 0 )
	{
		QString sStartAddress = attributes.value( "startaddress" ).toString();
		QString sEndAddress = attributes.value( "endaddress" ).toString();

		pRule = new CIPRangeRule();
		pRule->parseContent( QString("%1-%2").arg(sStartAddress).arg(sEndAddress) );
	}
	else if ( sType.compare( "hash", Qt::CaseInsensitive ) == 0 )
	{
		CHashRule* rule = new CHashRule();
		if ( !rule->parseContent( attributes.value( "content" ).toString() ) )
		{
			delete rule;
			return NULL;
		}

		pRule = rule;
	}
	else if ( sType.compare( "regexp", Qt::CaseInsensitive ) == 0 )
	{
		CRegularExpressionRule* rule = new CRegularExpressionRule();
		if ( !rule->parseContent( attributes.value( "content" ).toString() ) )
		{
			delete rule;
			return NULL;
		}

		pRule = rule;
	}
	else if ( sType.compare( "content", Qt::CaseInsensitive ) == 0 )
	{
		const QString sMatch = attributes.value( "match" ).toString();
		const QString sContent = attributes.value( "content" ).toString();

		const QString sUrn = sContent.left( 4 );

		if ( nVersion < 2.0 )
		{
			// This handles "old style" Shareaza RegExp rules.
			if ( sMatch.compare( "regexp", Qt::CaseInsensitive ) == 0 )
			{
				CRegularExpressionRule* rule = new CRegularExpressionRule();
				if ( !rule->parseContent( sContent ) )
				{
					delete rule;
					return NULL;
				}

				pRule = rule;
			}
			// This handles "old style" Shareaza hash rules.
			else if ( sUrn.compare( "urn:", Qt::CaseInsensitive ) == 0 )
			{
				CHashRule* rule = new CHashRule();
				if ( !rule->parseContent( sContent ) )
				{
					delete rule;
					return NULL;
				}

				pRule = rule;
			}
		}

		if ( !pRule )
		{
			bool all = ( sMatch.compare( "all", Qt::CaseInsensitive ) == 0 );

			if ( all || sMatch.compare( "any", Qt::CaseInsensitive ) == 0 )
			{
				CContentRule* rule = new CContentRule();
				if ( !rule->parseContent( sContent ) )
				{
					delete rule;
					return NULL;
				}

				rule->setAll( all );
				pRule = rule;
			}
			else
			{
				return NULL;
			}
		}
	}
	else
	{
		return NULL;
	}

	const QString sAction = attributes.value( "action" ).toString();

	if ( sAction.compare( "deny", Qt::CaseInsensitive ) == 0 || sAction.isEmpty() )
	{
		pRule->m_nAction = RuleAction::Deny;
	}
	else if ( sAction.compare( "accept", Qt::CaseInsensitive ) == 0 )
	{
		pRule->m_nAction = RuleAction::Accept;
	}
	else if ( sAction.compare( "null", Qt::CaseInsensitive ) == 0 )
	{
		pRule->m_nAction = RuleAction::None;
	}
	else
	{
		delete pRule;
		return NULL;
	}

	const QString sAutomatic = attributes.value( "automatic" ).toString();
	if(sAutomatic == "true")
		pRule->m_bAutomatic = true;
	else
		pRule->m_bAutomatic = false;

	pRule->m_sComment = attributes.value( "comment" ).toString().trimmed();

	QString sExpire = attributes.value( "expire" ).toString();
	if ( sExpire.compare( "forever", Qt::CaseInsensitive ) == 0 )
	{
		pRule->setForever(true);
	}
	else if ( sExpire.compare( "session", Qt::CaseInsensitive ) == 0 )
	{
		pRule->setForever(false);
		pRule->m_tExpire = RuleTime::Special;
	}
	else
	{
		pRule->m_tExpire = sExpire.toUInt();
	}

	QString sUUID = attributes.value( "uuid" ).toString();
	if ( sUUID.isEmpty() )
		sUUID = attributes.value( "guid" ).toString();

	if ( sUUID.isEmpty() )
	{
		pRule->m_oUUID = QUuid::createUuid();
	}
	else
	{
		pRule->m_oUUID = QUuid( sUUID );
	}

	return pRule;
}