//
// SchemaChild.h
//
// Copyright (c) Shareaza Development Team, 2002-2005.
// This file is part of SHAREAZA (www.shareaza.com)
//
// Shareaza is free software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Shareaza is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Shareaza; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#if !defined(AFX_SCHEMACHILD_H__8CDBBB11_8165_478F_9E52_1B1E298CA72E__INCLUDED_)
#define AFX_SCHEMACHILD_H__8CDBBB11_8165_478F_9E52_1B1E298CA72E__INCLUDED_
#pragma once
class CSchema;
class CSchemaChildMap;
class CXMLElement;
class CSchemaChild
{
// Construction
public:
CSchemaChild(CSchema* pSchema);
virtual ~CSchemaChild();
// Attributes
public:
CSchema* m_pSchema;
int m_nType;
CString m_sURI;
public:
CPtrList m_pMap;
// Operations
public:
BOOL Load(CXMLElement* pXML);
void Clear();
BOOL MemberCopy(CXMLElement* pLocal, CXMLElement* pRemote,
BOOL bToRemote = FALSE, BOOL bAggressive = FALSE);
};
class CSchemaChildMap
{
// Construction
public:
CSchemaChildMap();
virtual ~CSchemaChildMap();
// Attributes
public:
BOOL m_bIdentity;
CString m_sLocal;
CString m_sRemote;
// Operations
public:
BOOL Load(CXMLElement* pXML);
};
#endif // !defined(AFX_SCHEMACHILD_H__8CDBBB11_8165_478F_9E52_1B1E298CA72E__INCLUDED_)
//
// SchemaChild.cpp
//
// Copyright (c) Shareaza Development Team, 2002-2005.
// This file is part of SHAREAZA (www.shareaza.com)
//
// Shareaza is free software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Shareaza is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Shareaza; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#include "StdAfx.h"
#include "Shareaza.h"
#include "Settings.h"
#include "Schema.h"
#include "SchemaChild.h"
#include "XML.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//
// CSchemaChild construction
CSchemaChild::CSchemaChild(CSchema* pSchema)
{
m_pSchema = pSchema;
m_nType = CSchema::stFile;
}
CSchemaChild::~CSchemaChild()
{
Clear();
}
//
// CSchemaChild load
//根据参数xml来设置各个属性的值,建立SchemaChild对象,加载参数xml的各个element到m_pMap中
BOOL CSchemaChild::Load(CXMLElement* pXML)
{
//根据参数pXML的"location"属性来设置SchemaChild的m_sURI
m_sURI = pXML->GetAttributeValue( _T("location") );
if ( m_sURI.IsEmpty() )
{
return FALSE;
}
//根据xml的type属性来设置SchemaChild的m_nType
//只能是文件和文件夹两种类型
CString strType = pXML->GetAttributeValue( _T("type") );
if ( strType == _T("folder") )
{
m_nType = CSchema::stFolder;
}
else if ( strType == _T("file") )
{
m_nType = CSchema::stFile;
}
else
{
return FALSE;
}
//如果xml中的Element是identity或者shared,则创建一个SChemaChildMap
//根据element对象设置ChildMap的各种属性,然后加入到m_pMap中
for ( POSITION pos = pXML->GetElementIterator() ; pos ; )
{
CXMLElement* pElement = pXML->GetNextElement( pos );
if ( pElement->IsNamed( _T("identity") ) ||
pElement->IsNamed( _T("shared") ) )
{
CSchemaChildMap* pMap = new CSchemaChildMap();
if ( pMap->Load( pElement ) )
{
m_pMap.AddTail( pMap );
}
else
{
delete pMap;
return FALSE;
}
}
}
return TRUE;
}
//
// CSchemaChild clear
//删除m_pMap中的各个CSchemaChildMap对象,释放空间
void CSchemaChild::Clear()
{
for ( POSITION pos = m_pMap.GetHeadPosition() ; pos ; )
{
delete (CSchemaChildMap*)m_pMap.GetNext( pos );
}
m_pMap.RemoveAll();
}
//
// CSchemaChild member copy
//根据参数bToRemote,将m_pMap中的各个ChildMap的local或者remote属性
//复制加入到参数plocal或者pRemote中
BOOL CSchemaChild::MemberCopy(CXMLElement* pLocal, CXMLElement* pRemote,
BOOL bToRemote, BOOL bAggressive)
{
//pLocal和pRemote至少得有一个为true
if ( ! pLocal || ! pRemote )
{
return FALSE;
}
BOOL bChanged = FALSE;
for ( POSITION pos = m_pMap.GetHeadPosition() ; pos ; )
{
CSchemaChildMap* pMap = (CSchemaChildMap*)m_pMap.GetNext( pos );
CXMLAttribute* pAttribute1 = NULL;
CXMLAttribute* pAttribute2 = NULL;
if ( bToRemote )
{
pAttribute1 = pLocal->GetAttribute( pMap->m_sLocal );
pAttribute2 = pRemote->GetAttribute( pMap->m_sRemote );
}
else
{
pAttribute1 = pRemote->GetAttribute( pMap->m_sRemote );
pAttribute2 = pLocal->GetAttribute( pMap->m_sLocal );
}
if ( pAttribute1 && ( ! pAttribute2 || bAggressive ) )
{
//如果是toRemote,则将(map的remote,local的attribute local)加入到pRemote中
//,否则将(map的local,remote的attribute remote)加入到pLocal中
CString strValue( pAttribute1->GetValue() );
if ( pMap->m_bIdentity )
{
CXMLNode::UniformString( strValue );
}
if ( bToRemote )
{
pRemote->AddAttribute( pMap->m_sRemote, strValue );
}
else
{
pLocal->AddAttribute( pMap->m_sLocal, strValue );
}
bChanged = TRUE;
}
}
return bChanged;
}
//
// CSchemaChildMap construction
CSchemaChildMap::CSchemaChildMap()
{
}
CSchemaChildMap::~CSchemaChildMap()
{
}
//
// CSchemaChildMap operation
//根据参数pXML来设置CSchemaChildMap的各种属性如m_sRemote等的值
BOOL CSchemaChildMap::Load(CXMLElement* pXML)
{
//参数pXML的名字只能是 identity和shared两种情况
if ( pXML->IsNamed( _T("identity") ) )
{
m_bIdentity = TRUE;
}
else if ( pXML->IsNamed( _T("shared") ) )
{
m_bIdentity = FALSE;
}
else
{
return FALSE;
}
//将m_sLocal 和 m_sRemote设置为pXML的local属性和remote属性
m_sLocal = pXML->GetAttributeValue( _T("local") );
m_sRemote = pXML->GetAttributeValue( _T("remote") );
if ( m_sLocal.IsEmpty() || m_sRemote.IsEmpty() )
{
return FALSE;
}
return TRUE;
}