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

DlgDecodeMetadata.cpp

Go to the documentation of this file.
00001 //
00002 // DlgDecodeMetadata.cpp
00003 //
00004 //      Date:                   "$Date: 2005/03/11 16:28:40 $"
00005 //      Revision:               "$Revision: 1.2 $"
00006 //  Last change by:     "$Author: thetruecamper $"
00007 //
00008 // Copyright (c) Shareaza Development Team, 2002-2005.
00009 // This file is part of SHAREAZA (www.shareaza.com)
00010 //
00011 // Shareaza is free software; you can redistribute it
00012 // and/or modify it under the terms of the GNU General Public License
00013 // as published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Shareaza is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU General Public License
00022 // along with Shareaza; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024 //
00025 
00026 #include "StdAfx.h"
00027 #include "Shareaza.h"
00028 #include "Library.h"
00029 #include "SharedFile.h"
00030 #include "XML.h"
00031 #include "Schema.h"
00032 #include "DlgDecodeMetadata.h"
00033 
00034 #ifdef _DEBUG
00035 #define new DEBUG_NEW
00036 #undef THIS_FILE
00037 static char THIS_FILE[] = __FILE__;
00038 #endif
00039 
00041 // CDecodeMetadataDlg dialog
00042 
00043 CDecodeMetadataDlg::CDecodeMetadataDlg(CWnd* pParent) : CSkinDialog(CDecodeMetadataDlg::IDD, pParent)
00044 {
00045         //{{AFX_DATA_INIT(CDecodeMetadataDlg)
00046         //}}AFX_DATA_INIT
00047 }
00048 
00049 void CDecodeMetadataDlg::DoDataExchange(CDataExchange* pDX)
00050 {
00051         CSkinDialog::DoDataExchange(pDX);
00052         //{{AFX_DATA_MAP(CDecodeMetadataDlg)
00053         DDX_Control(pDX, IDC_CODEPAGES, m_wndCodepages);
00054         //}}AFX_DATA_MAP
00055 }
00056 
00058 // CDecodeMetadataDlg message handlers
00059 
00060 BOOL CDecodeMetadataDlg::OnInitDialog() 
00061 {
00062         CSkinDialog::OnInitDialog();
00063         
00064         SkinMe( _T("CDecodeMetadataDlg"), IDI_WORLD );
00065         UpdateData( FALSE );
00066 
00067         return TRUE;
00068 }
00069 
00070 void CDecodeMetadataDlg::AddFile(CLibraryFile* pFile)
00071 {
00072         m_pFiles.AddTail( (LPVOID)pFile->m_nIndex );
00073 }
00074 
00075 // TODO: Allow to restore initial metadata from backup 
00076 // if bad encoding was applied by mistake.
00077 //
00078 void CDecodeMetadataDlg::OnOK() 
00079 {
00080         UpdateData();
00081         static const unsigned codePages[ 13 ] =
00082         {
00083                 // arabic, baltic, centr. european, chinese simplified, chinese traditional
00084                 1256, 1257, 1250, 936,  950,
00085                 // cyrillic, greek, hebrew, japanese, korean, thai, turkish, vietnamese
00086                 1251, 1253, 1255,  932,  949, 874, 1254, 1258
00087         };
00088         unsigned nCodePage = m_wndCodepages.GetCurSel();
00089         nCodePage = nCodePage < 13 ? codePages[ nCodePage ] : 1252; // english
00090 
00091         // close dialog and perform decoding in background
00092         CSkinDialog::OnOK();
00093 
00094         for ( POSITION pos = m_pFiles.GetHeadPosition() ; pos ; )
00095         {
00096                 DWORD nIndex = (DWORD)m_pFiles.GetNext( pos );
00097                 
00098                 CXMLElement* pXML;
00099                 CLibraryFile* pFile;
00100 
00101                 {
00102                         CQuickLock oLock( Library.m_pSection );
00103 
00104                         if ( m_pFiles.IsEmpty() ) break;
00105 
00106                         pFile = Library.LookupFile( nIndex );
00107                         if ( !pFile || !pFile->m_pMetadata || !pFile->m_pSchema ) continue;
00108 
00109                         pXML = pFile->m_pMetadata->Clone();
00110                 }
00111 
00112                 for ( POSITION pos = pXML->GetAttributeIterator() ; pos ; )
00113                 {
00114                         CXMLAttribute* pAttribute = pXML->GetNextAttribute( pos );
00115                         // decode only these attributes
00116                         if ( !pAttribute->IsNamed( _T("artist") ) && 
00117                                  !pAttribute->IsNamed( _T("album") ) &&
00118                                  !pAttribute->IsNamed( _T("title") ) &&
00119                                  !pAttribute->IsNamed( _T("description") ) ) continue;
00120                         CString strAttribute = pAttribute->GetValue();
00121 
00122                         int nLength = strAttribute.GetLength();
00123                         LPTSTR pszSource = strAttribute.GetBuffer( nLength );
00124                         CHAR* pszDest = new CHAR[ nLength + 1 ];
00125 
00126                         {
00127                                 const TCHAR* source = pszSource;
00128                                 CHAR* dest = pszDest;
00129                                 while ( *dest++ = static_cast< CHAR >( *source ), *source++ );
00130                         }
00131 
00132                         int nWide = MultiByteToWideChar( nCodePage, 0, pszDest, nLength, NULL, 0 );
00133                         LPTSTR pszOutput = strAttribute.GetBuffer( nWide + 1 );
00134                         MultiByteToWideChar( nCodePage, 0, pszDest, nLength, pszOutput, nWide );
00135                         pszOutput[ nWide ] = 0;
00136 
00137                         delete pszDest;
00138                         strAttribute.ReleaseBuffer();
00139 
00140                         pAttribute->SetValue( strAttribute );
00141                 }
00142 
00143                 CQuickLock oLock( Library.m_pSection );
00144                 // make a clean copy of schema with namespace included
00145                 CXMLElement* pContainer = pFile->m_pSchema->Instantiate( TRUE );
00146                 if ( pContainer )
00147                 {
00148                         // append modified metadata
00149                         CXMLElement* pMetadata  = pContainer->AddElement( pXML );
00150                         // save metadata by creating XML file
00151                         pFile->SetMetadata( pContainer );
00152                         delete pContainer;
00153                 }
00154         }
00155 }

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