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

RelatedSearch.cpp

Go to the documentation of this file.
00001 //
00002 // RelatedSearch.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 "Library.h"
00025 #include "MatchObjects.h"
00026 #include "SharedFile.h"
00027 #include "RelatedSearch.h"
00028 #include "QuerySearch.h"
00029 #include "QueryHit.h"
00030 #include "Schema.h"
00031 #include "SchemaCache.h"
00032 #include "XML.h"
00033 
00034 #ifdef _DEBUG
00035 #undef THIS_FILE
00036 static char THIS_FILE[]=__FILE__;
00037 #define new DEBUG_NEW
00038 #endif
00039 
00040 #define MIN_LENGTH              4
00041 
00042 
00044 // CRelatedSearch construction
00045 
00046 CRelatedSearch::CRelatedSearch(CMatchFile* pFile)
00047 {
00048         if ( pFile != NULL )
00049         {
00050                 m_bSHA1         = pFile->m_bSHA1;
00051                 m_pSHA1         = pFile->m_pSHA1;
00052                 m_bTiger        = pFile->m_bTiger;
00053                 m_pTiger        = pFile->m_pTiger;
00054                 m_bED2K         = pFile->m_bED2K;
00055                 m_pED2K         = pFile->m_pED2K;
00056                 m_sName         = pFile->m_pBest->m_sName;
00057 
00058                 m_pSchema       = SchemaCache.Get( pFile->m_pBest->m_sSchemaURI );
00059                 m_pXML          = NULL;
00060                 m_bXML          = FALSE;
00061 
00062                 if ( m_pSchema != NULL )
00063                 {
00064                         m_pXML = new CXMLElement( NULL, m_pSchema->m_sSingular );
00065                         m_bXML = TRUE;
00066 
00067                         for ( CQueryHit* pHit = pFile->m_pHits ; pHit ; pHit = pHit->m_pNext )
00068                         {
00069                                 if ( pHit->m_pXML != NULL )
00070                                 {
00071                                         for ( POSITION pos = pHit->m_pXML->GetAttributeIterator() ; pos ; )
00072                                         {
00073                                                 CXMLAttribute* pAttribute = pHit->m_pXML->GetNextAttribute( pos );
00074                                                 m_pXML->AddAttribute( pAttribute->GetName(), pAttribute->GetValue() );
00075                                         }
00076                                 }
00077                         }
00078                 }
00079         }
00080         else
00081         {
00082                 m_bSHA1 = m_bTiger = m_bED2K = m_bXML = FALSE;
00083                 m_pSchema = NULL;
00084                 m_pXML = NULL;
00085         }
00086 }
00087 
00088 CRelatedSearch::CRelatedSearch(CLibraryFile* pFile)
00089 {
00090         if ( pFile != NULL )
00091         {
00092                 m_bSHA1         = pFile->m_bSHA1;
00093                 m_pSHA1         = pFile->m_pSHA1;
00094                 m_bTiger        = pFile->m_bTiger;
00095                 m_pTiger        = pFile->m_pTiger;
00096                 m_bED2K         = pFile->m_bED2K;
00097                 m_pED2K         = pFile->m_pED2K;
00098                 m_sName         = pFile->m_sName;
00099                 m_pSchema       = pFile->m_pSchema;
00100                 m_bXML          = ( pFile->m_pMetadata != NULL );
00101                 m_pXML          = m_bXML ? pFile->m_pMetadata->Clone() : NULL;
00102         }
00103         else
00104         {
00105                 m_bSHA1 = m_bTiger = m_bED2K = m_bXML = FALSE;
00106                 m_pSchema = NULL;
00107                 m_pXML = NULL;
00108         }
00109 }
00110 
00111 CRelatedSearch::~CRelatedSearch()
00112 {
00113         if ( m_bXML ) delete m_pXML;
00114 }
00115 
00117 // CRelatedSearch search for this
00118 
00119 BOOL CRelatedSearch::CanSearchForThis()
00120 {
00121         return m_bSHA1 || m_bTiger || m_bED2K;
00122 }
00123 
00124 BOOL CRelatedSearch::RunSearchForThis()
00125 {
00126         if ( ! CanSearchForThis() ) return FALSE;
00127         CQuerySearch* pSearch = new CQuerySearch();
00128         pSearch->m_bSHA1        = m_bSHA1;
00129         pSearch->m_pSHA1        = m_pSHA1;
00130         pSearch->m_bTiger       = m_bTiger;
00131         pSearch->m_pTiger       = m_pTiger;
00132         pSearch->m_bED2K        = m_bED2K;
00133         pSearch->m_pED2K        = m_pED2K;
00134         return pSearch->OpenWindow() != NULL;
00135 }
00136 
00138 // CRelatedSearch search for similar
00139 
00140 BOOL CRelatedSearch::CanSearchForSimilar()
00141 {
00142         return m_sName.GetLength() >= MIN_LENGTH;
00143 }
00144 
00145 BOOL CRelatedSearch::RunSearchForSimilar()
00146 {
00147         if ( ! CanSearchForSimilar() ) return FALSE;
00148         CQuerySearch* pSearch = new CQuerySearch();
00149         pSearch->m_sSearch = Tokenise( m_sName );
00150 
00151         // Support "Related Search" in ed2k
00152         if ( m_bED2K )
00153         {
00154                 pSearch->m_bSimilarSearch = TRUE;
00155                 pSearch->m_pSimilarED2K = m_pED2K;
00156         }
00157 
00158         return pSearch->OpenWindow() != NULL;
00159 }
00160 
00162 // CRelatedSearch search for artist
00163 
00164 BOOL CRelatedSearch::CanSearchForArtist()
00165 {
00166         if ( m_pSchema == NULL || m_pXML == NULL ) return FALSE;
00167         CString str = m_pXML->GetAttributeValue( _T("artist") );
00168         return str.GetLength() >= MIN_LENGTH;
00169 }
00170 
00171 BOOL CRelatedSearch::RunSearchForArtist()
00172 {
00173         if ( ! CanSearchForArtist() ) return FALSE;
00174         CQuerySearch* pSearch = new CQuerySearch();
00175         pSearch->m_pSchema      = m_pSchema ? m_pSchema : SchemaCache.Get( CSchema::uriAudio );
00176         pSearch->m_pXML         = pSearch->m_pSchema->Instantiate();
00177         CXMLElement* pXML       = pSearch->m_pXML->AddElement( pSearch->m_pSchema->m_sSingular );
00178         pXML->AddAttribute( _T("artist"), Tokenise( m_pXML->GetAttributeValue( _T("artist") ) ) );
00179         return pSearch->OpenWindow() != NULL;
00180 }
00181 
00183 // CRelatedSearch search for album
00184 
00185 BOOL CRelatedSearch::CanSearchForAlbum()
00186 {
00187         if ( m_pSchema == NULL || m_pXML == NULL ) return FALSE;
00188         CString str = m_pXML->GetAttributeValue( _T("album") );
00189         return str.GetLength() >= MIN_LENGTH;
00190 }
00191 
00192 BOOL CRelatedSearch::RunSearchForAlbum()
00193 {
00194         if ( ! CanSearchForAlbum() ) return FALSE;
00195         CQuerySearch* pSearch = new CQuerySearch();
00196         pSearch->m_pSchema      = m_pSchema ? m_pSchema : SchemaCache.Get( CSchema::uriAudio );
00197         pSearch->m_pXML         = pSearch->m_pSchema->Instantiate();
00198         CXMLElement* pXML       = pSearch->m_pXML->AddElement( pSearch->m_pSchema->m_sSingular );
00199         pXML->AddAttribute( _T("album"), Tokenise( m_pXML->GetAttributeValue( _T("album") ) ) );
00200         return pSearch->OpenWindow() != NULL;
00201 }
00202 
00204 // CRelatedSearch search for series
00205 
00206 BOOL CRelatedSearch::CanSearchForSeries()
00207 {
00208         if ( m_pSchema == NULL || m_pXML == NULL ) return FALSE;
00209         CString str = m_pXML->GetAttributeValue( _T("series") );
00210         return str.GetLength() >= MIN_LENGTH;
00211 }
00212 
00213 BOOL CRelatedSearch::RunSearchForSeries()
00214 {
00215         if ( ! CanSearchForSeries() ) return FALSE;
00216         CQuerySearch* pSearch = new CQuerySearch();
00217         pSearch->m_pSchema      = m_pSchema ? m_pSchema : SchemaCache.Get( CSchema::uriVideo );
00218         pSearch->m_pXML         = pSearch->m_pSchema->Instantiate();
00219         CXMLElement* pXML       = pSearch->m_pXML->AddElement( pSearch->m_pSchema->m_sSingular );
00220         pXML->AddAttribute( _T("series"), Tokenise( m_pXML->GetAttributeValue( _T("series") ) ) );
00221         return pSearch->OpenWindow() != NULL;
00222 }
00223 
00225 // CRelatedSearch string token processing
00226 
00227 CString CRelatedSearch::Tokenise(LPCTSTR psz)
00228 {
00229         int nChars = 0;
00230         CString str, strTemp(psz);
00231 
00232         // remove diacritics; supported for NT systems only
00233         if ( theApp.m_bNT )
00234         {
00235                 int nSource = FoldString( MAP_COMPOSITE, psz, -1, NULL, 0 ); //_tcslen( psz );
00236                 FoldString( MAP_COMPOSITE, psz, -1, strTemp.GetBuffer( nSource ), nSource );
00237                 strTemp.ReleaseBuffer( nSource );
00238                 psz = strTemp.GetBuffer( nSource );
00239         }
00240 
00241         int nLastPoint = strTemp.ReverseFind( '.' );
00242         if ( nLastPoint > 0 )
00243                 nLastPoint = strTemp.GetLength() - nLastPoint - 1;
00244 
00245         for ( ; *psz ; psz++ )
00246         {
00247                 if ( *psz == '.' && _tcslen( psz ) == nLastPoint )
00248                 {
00249                         break;
00250                 }
00251                 else if ( _istalnum( *psz ) )
00252                 {
00253                         str += *psz;
00254                         nChars ++;
00255                 }
00256                 else if ( nChars > 1 && !( *psz >= 0x02B0 && *psz <= 0x036F ) )
00257                 {
00258                         str += ' ';
00259                         nChars = 0;
00260                 }
00261                 else if ( nChars == 1 && !( *psz >= 0x02B0 && *psz <= 0x036F ) )
00262                 {
00263                         str = str.Left( str.GetLength() - 1 );
00264                         nChars = 0;
00265                 }
00266         }
00267 
00268         return str;
00269 }

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