Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

LibraryBuilderPlugins.cpp

Go to the documentation of this file.
00001 //
00002 // LibraryBuilderPlugins.cpp
00003 //
00004 // Copyright (c) Shareaza Development Team, 2002-2005.
00005 // This file is part of SHAREAZA (www.shareaza.com)
00006 //
00007 // Shareaza is free software; you can redistribute it
00008 // and/or modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2 of
00010 // the License, or (at your option) any later version.
00011 //
00012 // Shareaza is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with Shareaza; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "LibraryBuilder.h"
00025 #include "LibraryBuilderPlugins.h"
00026 #include "Plugins.h"
00027 #include "XML.h"
00028 #include "XMLCOM.h"
00029 
00030 #ifdef _DEBUG
00031 #undef THIS_FILE
00032 static char THIS_FILE[]=__FILE__;
00033 #define new DEBUG_NEW
00034 #endif
00035 
00036 
00038 // CLibraryBuilderPlugins construction
00039 
00040 CLibraryBuilderPlugins::CLibraryBuilderPlugins(CLibraryBuilder* pBuilder)
00041 {
00042         m_pBuilder      = pBuilder;
00043         m_bCOM          = FALSE;
00044 }
00045 
00046 CLibraryBuilderPlugins::~CLibraryBuilderPlugins()
00047 {
00048         Cleanup();
00049 }
00050 
00052 // CLibraryBuilderPlugins extract
00053 
00054 BOOL CLibraryBuilderPlugins::ExtractMetadata(CString& strPath, HANDLE hFile)
00055 {
00056         CString strType;
00057         
00058         int nExtPos = strPath.ReverseFind( '.' );
00059         if ( nExtPos > 0 ) strType = strPath.Mid( nExtPos );
00060         
00061         CharLower( strType.GetBuffer() );
00062         strType.ReleaseBuffer();
00063         
00064         ILibraryBuilderPlugin* pPlugin = NULL;
00065         
00066         if ( m_pMap.Lookup( strType, (void*&)pPlugin ) )
00067         {
00068                 if ( pPlugin == NULL ) return FALSE;
00069         }
00070         else
00071         {
00072                 pPlugin = LoadPlugin( strType );
00073                 if ( pPlugin == NULL ) return FALSE;
00074         }
00075         
00076         CXMLElement* pXML       = new CXMLElement();
00077         ISXMLElement* ppXML     = (ISXMLElement*)CXMLCOM::Wrap( pXML, IID_ISXMLElement );
00078         
00079         BSTR bsFile = strPath.AllocSysString();
00080         
00081         HRESULT hResult = pPlugin->Process( hFile, bsFile, ppXML );
00082         
00083         SysFreeString( bsFile );
00084         
00085         ppXML->Release();
00086         
00087         BOOL bSuccess = FALSE;
00088         
00089         if ( hResult == S_OK )
00090         {
00091                 if ( CXMLElement* pOuter = pXML->GetFirstElement() )
00092                 {
00093                         CXMLElement* pInner             = pOuter->GetFirstElement();
00094                         CString strSchemaURI    = pOuter->GetAttributeValue( CXMLAttribute::schemaName );
00095                         
00096                         if ( pInner && strSchemaURI.GetLength() )
00097                         {
00098                                 pInner = pInner->Detach();
00099                                 bSuccess = m_pBuilder->SubmitMetadata( strSchemaURI, pInner );
00100                         }
00101                 }
00102         }
00103         else if ( hResult == E_UNEXPECTED )
00104         {
00105                 bSuccess = m_pBuilder->SubmitCorrupted();
00106         }
00107         
00108         delete pXML;
00109         
00110         return bSuccess;
00111 }
00112 
00114 // CLibraryBuilderPlugins cleanup
00115 
00116 void CLibraryBuilderPlugins::Cleanup()
00117 {
00118         for ( POSITION pos = m_pMap.GetStartPosition() ; pos ; )
00119         {
00120                 ILibraryBuilderPlugin* pPlugin = NULL;
00121                 CString strType;
00122                 
00123                 m_pMap.GetNextAssoc( pos, strType, (void*&)pPlugin );
00124                 if ( pPlugin ) pPlugin->Release();
00125         }
00126         
00127         if ( m_bCOM )
00128         {
00129                 m_bCOM = FALSE;
00130                 CoUninitialize();
00131         }
00132         
00133         m_pMap.RemoveAll();
00134 }
00135 
00137 // CLibraryBuilderPlugins load plugin
00138 
00139 ILibraryBuilderPlugin* CLibraryBuilderPlugins::LoadPlugin(LPCTSTR pszType)
00140 {
00141         CLSID pCLSID;
00142         
00143         if ( ! Plugins.LookupCLSID( _T("LibraryBuilder"), pszType, pCLSID ) )
00144         {
00145                 m_pMap.SetAt( pszType, NULL );
00146                 return NULL;
00147         }
00148         
00149         if ( ! m_bCOM )
00150         {
00151                 if ( FAILED( CoInitializeEx( NULL, COINIT_MULTITHREADED ) ) )
00152                 {
00153                         m_pMap.SetAt( pszType, NULL );
00154                         return NULL;
00155                 }
00156                 
00157                 m_bCOM = TRUE;
00158         }
00159         
00160         ILibraryBuilderPlugin* pPlugin;
00161         
00162         HRESULT hResult = CoCreateInstance( pCLSID, NULL, CLSCTX_INPROC_SERVER,
00163                 IID_ILibraryBuilderPlugin, (void**)&pPlugin );
00164         
00165         m_pMap.SetAt( pszType, pPlugin );
00166         
00167         return pPlugin;
00168 }
00169 

Generated on Thu Dec 15 10:39:44 2005 for Shareaza 2.2.1.0 by  doxygen 1.4.2