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 "Library.h"
00025 #include "LibraryFolders.h"
00026 #include "AlbumFolder.h"
00027 #include "SharedFile.h"
00028 #include "Schema.h"
00029 #include "XML.h"
00030
00031 #include "SHA.h"
00032 #include "MD5.h"
00033 #include "ED2K.h"
00034 #include "TigerTree.h"
00035
00036 #include "DlgCollectionExport.h"
00037
00038 IMPLEMENT_DYNAMIC(CCollectionExportDlg, CSkinDialog)
00039
00040 BEGIN_MESSAGE_MAP(CCollectionExportDlg, CSkinDialog)
00041 END_MESSAGE_MAP()
00042
00043
00044 CCollectionExportDlg::CCollectionExportDlg(CAlbumFolder* pFolder, CWnd* pParent) : CSkinDialog(CCollectionExportDlg::IDD, pParent)
00045 {
00046 m_pFolder = pFolder;
00047 }
00048
00049 CCollectionExportDlg::~CCollectionExportDlg()
00050 {
00051 }
00052
00053 void CCollectionExportDlg::DoDataExchange(CDataExchange* pDX)
00054 {
00055 CSkinDialog::DoDataExchange( pDX );
00056 }
00057
00058 BOOL CCollectionExportDlg::OnInitDialog()
00059 {
00060 CSkinDialog::OnInitDialog();
00061
00062 SkinMe( NULL );
00063
00064 return TRUE;
00065 }
00066
00067 void CCollectionExportDlg::OnOK()
00068 {
00069 CString strPath = BrowseForFolder();
00070 if ( strPath.IsEmpty() ) return;
00071
00072 CSingleLock pLock( &Library.m_pSection, TRUE );
00073
00074 if ( LibraryFolders.CheckAlbum( m_pFolder ) )
00075 {
00076 CXMLElement* pXML = CreateXML();
00077 CString strXML = pXML->ToString( TRUE, TRUE );
00078 delete pXML;
00079
00080 CString strFile = strPath + _T("\\Collection.xml");
00081 CFile pFile;
00082
00083 if ( pFile.Open( strFile, CFile::modeWrite|CFile::modeCreate ) )
00084 {
00085 USES_CONVERSION;
00086 LPCSTR pszXML = T2CA( (LPCTSTR)strXML );
00087 pFile.Write( pszXML, strlen(pszXML) );
00088 pFile.Close();
00089 CSkinDialog::OnOK();
00090 }
00091 else
00092 {
00093 pLock.Unlock();
00094 AfxMessageBox( _T("TODO: Can't write to ") + strFile );
00095 }
00096 }
00097 else
00098 {
00099 pLock.Unlock();
00100 AfxMessageBox( _T("TODO: Folder disappeared.") );
00101 }
00102 }
00103
00104 CString CCollectionExportDlg::BrowseForFolder()
00105 {
00106 TCHAR szPath[MAX_PATH];
00107 LPITEMIDLIST pPath;
00108 LPMALLOC pMalloc;
00109 BROWSEINFO pBI;
00110 CString str;
00111
00112 ZeroMemory( &pBI, sizeof(pBI) );
00113 pBI.hwndOwner = GetSafeHwnd();
00114 pBI.pszDisplayName = szPath;
00115 pBI.lpszTitle = _T("Choose output folder:");
00116 pBI.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
00117
00118 pPath = SHBrowseForFolder( &pBI );
00119
00120 if ( pPath == NULL ) return str;
00121
00122 SHGetPathFromIDList( pPath, szPath );
00123 SHGetMalloc( &pMalloc );
00124 pMalloc->Free( pPath );
00125
00126 str = szPath;
00127 return str;
00128 }
00129
00130 CXMLElement* CCollectionExportDlg::CreateXML()
00131 {
00132 CXMLElement* pRoot = new CXMLElement( NULL, _T("collection") );
00133 pRoot->AddAttribute( _T("xmlns"), _T("http://www.shareaza.com/schemas/Collection.xsd") );
00134
00135 CXMLElement* pProperties = pRoot->AddElement( _T("properties") );
00136
00137 pProperties->AddElement( _T("title") )->SetValue( m_pFolder->m_sName );
00138
00139 if ( m_pFolder->m_pXML != NULL && m_pFolder->m_sSchemaURI.GetLength() > 0 )
00140 {
00141 CXMLElement* pMeta = pProperties->AddElement( _T("metadata") );
00142 pMeta->AddAttribute( _T("xmlns:s"), m_pFolder->m_sSchemaURI );
00143 pMeta->AddElement( CopyMetadata( m_pFolder->m_pXML ) );
00144 }
00145
00146 CXMLElement* pContents = pRoot->AddElement( _T("contents") );
00147
00148 for ( POSITION pos = m_pFolder->GetFileIterator() ; pos ; )
00149 {
00150 CLibraryFile* pFile = m_pFolder->GetNextFile( pos );
00151 if ( pFile == NULL ) continue;
00152
00153 CXMLElement* pFileRoot = pContents->AddElement( _T("file") );
00154
00155 if ( pFile->m_bSHA1 && pFile->m_bTiger )
00156 {
00157 pFileRoot->AddElement( _T("id") )->SetValue(
00158 _T("urn:bitprint:") + CSHA::HashToString( &pFile->m_pSHA1 ) + '.' +
00159 CTigerNode::HashToString( &pFile->m_pTiger ) );
00160 }
00161 else if ( pFile->m_bSHA1 )
00162 {
00163 pFileRoot->AddElement( _T("id") )->SetValue(
00164 CSHA::HashToString( &pFile->m_pSHA1, TRUE ) );
00165 }
00166 else if ( pFile->m_bTiger )
00167 {
00168 pFileRoot->AddElement( _T("id") )->SetValue(
00169 CTigerNode::HashToString( &pFile->m_pTiger, TRUE ) );
00170 }
00171 if ( pFile->m_bMD5 )
00172 {
00173 pFileRoot->AddElement( _T("id") )->SetValue(
00174 CMD5::HashToString( &pFile->m_pMD5, TRUE ) );
00175 }
00176 if ( pFile->m_bED2K )
00177 {
00178 pFileRoot->AddElement( _T("id") )->SetValue(
00179 CED2K::HashToString( &pFile->m_pED2K, TRUE ) );
00180 }
00181
00182 CXMLElement* pDescription = pFileRoot->AddElement( _T("description") );
00183 pDescription->AddElement( _T("name") )->SetValue( pFile->m_sName );
00184 CString str;
00185 str.Format( _T("%I64i"), pFile->GetSize() );
00186 pDescription->AddElement( _T("size") )->SetValue( str );
00187
00188 if ( pFile->m_pMetadata != NULL && pFile->m_bMetadataAuto == FALSE && pFile->m_pSchema != NULL )
00189 {
00190 CXMLElement* pMetadata = pFileRoot->AddElement( _T("metadata") );
00191 pMetadata->AddAttribute( _T("xmlns:s"), pFile->m_pSchema->m_sURI );
00192 pMetadata->AddElement( CopyMetadata( pFile->m_pMetadata ) );
00193 }
00194 }
00195
00196 return pRoot;
00197 }
00198
00199 CXMLElement* CCollectionExportDlg::CopyMetadata(CXMLElement* pMetadata)
00200 {
00201 pMetadata = pMetadata->Clone();
00202 pMetadata->SetName( _T("s:") + pMetadata->GetName() );
00203
00204 for ( POSITION pos = pMetadata->GetElementIterator() ; pos ; )
00205 {
00206 CXMLNode* pNode = pMetadata->GetNextElement( pos );
00207 pNode->SetName( _T("s:") + pNode->GetName() );
00208 }
00209
00210 for ( POSITION pos = pMetadata->GetAttributeIterator() ; pos ; )
00211 {
00212 CXMLNode* pNode = pMetadata->GetNextAttribute( pos );
00213 pNode->SetName( _T("s:") + pNode->GetName() );
00214 }
00215
00216 return pMetadata;
00217 }