00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "Settings.h"
00025 #include "VendorCache.h"
00026 #include "XML.h"
00027
00028 #ifdef _DEBUG
00029 #undef THIS_FILE
00030 static char THIS_FILE[]=__FILE__;
00031 #define new DEBUG_NEW
00032 #endif
00033
00034 CVendorCache VendorCache;
00035
00036
00038
00039
00040 CVendorCache::CVendorCache()
00041 {
00042 m_pNull = new CVendor();
00043 m_pNull->m_bAuto = TRUE;
00044
00045 m_pShareaza = m_pED2K = m_pNull;
00046 }
00047
00048 CVendorCache::~CVendorCache()
00049 {
00050 delete m_pNull;
00051 Clear();
00052 }
00053
00055
00056
00057 POSITION CVendorCache::GetIterator() const
00058 {
00059 return m_pMap.GetStartPosition();
00060 }
00061
00062 CVendor* CVendorCache::GetNext(POSITION& pos) const
00063 {
00064 CVendor* pItem = NULL;
00065 CString strCode;
00066 m_pMap.GetNextAssoc( pos, strCode, (void*&)pItem );
00067 return pItem;
00068 }
00069
00070 int CVendorCache::GetCount() const
00071 {
00072 return m_pMap.GetCount();
00073 }
00074
00076
00077
00078 CVendor* CVendorCache::Lookup(LPCSTR pszCode, BOOL bCreate)
00079 {
00080 WCHAR szCode[5] = { pszCode[0], pszCode[1], pszCode[2], pszCode[3], 0 };
00081 return Lookup( szCode, bCreate );
00082 }
00083
00084 CVendor* CVendorCache::Lookup(LPCWSTR pszCode, BOOL bCreate)
00085
00086 {
00087 CVendor* pVendor = NULL;
00088
00089 if ( m_pMap.Lookup( pszCode, (void*&)pVendor ) ) return pVendor;
00090 if ( ! bCreate ) return NULL;
00091
00092 if ( ! pszCode[0] || ! pszCode[1] || ! pszCode[2] || ! pszCode[3] ) return m_pNull;
00093 if ( ! _istalpha( pszCode[0] ) || ! _istalpha( pszCode[1] ) ) return m_pNull;
00094 if ( ! _istalpha( pszCode[2] ) || ! _istalpha( pszCode[3] ) ) return m_pNull;
00095
00096 pVendor = new CVendor( pszCode );
00097 m_pMap.SetAt( pszCode, pVendor );
00098
00099 return pVendor;
00100 }
00101
00102 CVendor* CVendorCache::LookupByName(LPCTSTR pszName) const
00103 {
00104 for ( POSITION pos = GetIterator() ; pos ; )
00105 {
00106 CVendor* pVendor = GetNext( pos );
00107
00108 if ( _tcsstr( pszName, pVendor->m_sName ) != NULL ) return pVendor;
00109 }
00110
00111 return NULL;
00112 }
00113
00115
00116
00117 void CVendorCache::Clear()
00118 {
00119 for ( POSITION pos = GetIterator() ; pos ; )
00120 {
00121 delete GetNext( pos );
00122 }
00123 m_pMap.RemoveAll();
00124 m_pShareaza = NULL;
00125 }
00126
00128
00129
00130 BOOL CVendorCache::Load()
00131 {
00132 CString strPath = Settings.General.Path + _T("\\Data\\Vendors.xml");
00133 CXMLElement* pXML = CXMLElement::FromFile( strPath, TRUE );
00134 BOOL bSuccess = FALSE;
00135
00136 if ( pXML != NULL )
00137 {
00138 bSuccess = LoadFrom( pXML );
00139 delete pXML;
00140 }
00141
00142 return bSuccess;
00143 }
00144
00146
00147
00148 BOOL CVendorCache::LoadFrom(CXMLElement* pXML)
00149 {
00150 if ( ! pXML->IsNamed( _T("vendorCache") ) ) return FALSE;
00151
00152 for ( POSITION pos = pXML->GetElementIterator() ; pos ; )
00153 {
00154 CXMLElement* pKey = pXML->GetNextElement( pos );
00155
00156 if ( pKey->IsNamed( _T("vendor") ) )
00157 {
00158 CVendor* pVendor = new CVendor();
00159
00160 if ( pVendor->LoadFrom( pKey ) )
00161 {
00162 CVendor* pOld = NULL;
00163
00164 if ( m_pMap.Lookup( pVendor->m_sCode, (void*&)pOld ) )
00165 {
00166 theApp.Message( MSG_ERROR, _T("Duplicate Vendors.xml key for \"%s\"."),
00167 (LPCTSTR)pVendor->m_sCode );
00168 delete pOld;
00169 }
00170
00171 m_pMap.SetAt( pVendor->m_sCode, pVendor );
00172
00173 if ( pVendor->m_sCode == _T("RAZA") ) m_pShareaza = pVendor;
00174 if ( pVendor->m_sCode == _T("ED2K") ) m_pED2K = pVendor;
00175 }
00176 else
00177 {
00178 delete pVendor;
00179 }
00180 }
00181 }
00182
00183 return GetCount() > 0;
00184 }
00185
00186
00188
00189
00190 CVendor::CVendor(LPCTSTR pszCode)
00191 {
00192 if ( m_bAuto = ( pszCode != NULL ) )
00193 {
00194 m_sCode = m_sName = pszCode;
00195 while ( m_sCode.GetLength() < 4 ) m_sCode += ' ';
00196 if ( m_sCode.GetLength() > 4 ) m_sCode = m_sCode.Left( 4 );
00197 }
00198
00199 m_bChatFlag = FALSE;
00200 m_bHTMLBrowse = FALSE;
00201 }
00202
00203 CVendor::~CVendor()
00204 {
00205 }
00206
00208
00209
00210 BOOL CVendor::LoadFrom(CXMLElement* pXML)
00211 {
00212 m_sCode = pXML->GetAttributeValue( _T("code") );
00213 if ( m_sCode.GetLength() != 4 ) return FALSE;
00214
00215 for ( POSITION pos = pXML->GetElementIterator() ; pos ; )
00216 {
00217 CXMLElement* pKey = pXML->GetNextElement( pos );
00218
00219 if ( pKey->IsNamed( _T("title") ) )
00220 {
00221 if ( m_sName.GetLength() ) return FALSE;
00222 m_sName = pKey->GetValue();
00223 }
00224 else if ( pKey->IsNamed( _T("link") ) )
00225 {
00226 if ( m_sLink.GetLength() ) return FALSE;
00227 m_sLink = pKey->GetValue();
00228 }
00229 else if ( pKey->IsNamed( _T("capability") ) )
00230 {
00231 CString strCap = pKey->GetAttributeValue( _T("name") );
00232 CharLower( strCap.GetBuffer() );
00233 strCap.ReleaseBuffer();
00234
00235 BOOL bValue = TRUE;
00236
00237 if ( strCap == _T("chatflag") )
00238 {
00239 m_bChatFlag = bValue;
00240 }
00241 else if ( strCap == _T("htmlhostbrowse") )
00242 {
00243 m_bHTMLBrowse = bValue;
00244 }
00245 }
00246 }
00247
00248 return m_sName.GetLength() > 0;
00249 }