00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00042
00043 CDecodeMetadataDlg::CDecodeMetadataDlg(CWnd* pParent) : CSkinDialog(CDecodeMetadataDlg::IDD, pParent)
00044 {
00045
00046
00047 }
00048
00049 void CDecodeMetadataDlg::DoDataExchange(CDataExchange* pDX)
00050 {
00051 CSkinDialog::DoDataExchange(pDX);
00052
00053 DDX_Control(pDX, IDC_CODEPAGES, m_wndCodepages);
00054
00055 }
00056
00058
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
00076
00077
00078 void CDecodeMetadataDlg::OnOK()
00079 {
00080 UpdateData();
00081 static const unsigned codePages[ 13 ] =
00082 {
00083
00084 1256, 1257, 1250, 936, 950,
00085
00086 1251, 1253, 1255, 932, 949, 874, 1254, 1258
00087 };
00088 unsigned nCodePage = m_wndCodepages.GetCurSel();
00089 nCodePage = nCodePage < 13 ? codePages[ nCodePage ] : 1252;
00090
00091
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
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
00145 CXMLElement* pContainer = pFile->m_pSchema->Instantiate( TRUE );
00146 if ( pContainer )
00147 {
00148
00149 CXMLElement* pMetadata = pContainer->AddElement( pXML );
00150
00151 pFile->SetMetadata( pContainer );
00152 delete pContainer;
00153 }
00154 }
00155 }