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 "SharedFile.h"
00026 #include "SchemaCache.h"
00027 #include "Schema.h"
00028 #include "XML.h"
00029 #include "SHA.h"
00030 #include "PageFileMetadata.h"
00031
00032 #ifdef _DEBUG
00033 #define new DEBUG_NEW
00034 #undef THIS_FILE
00035 static char THIS_FILE[] = __FILE__;
00036 #endif
00037
00038 IMPLEMENT_DYNCREATE(CFileMetadataPage, CFilePropertiesPage)
00039
00040 BEGIN_MESSAGE_MAP(CFileMetadataPage, CFilePropertiesPage)
00041
00042 ON_CBN_SELCHANGE(IDC_SCHEMAS, OnSelChangeSchemas)
00043 ON_CBN_CLOSEUP(IDC_SCHEMAS, OnCloseUpSchemas)
00044
00045 END_MESSAGE_MAP()
00046
00047
00049
00050
00051 CFileMetadataPage::CFileMetadataPage() : CFilePropertiesPage(CFileMetadataPage::IDD)
00052 {
00053
00054
00055 }
00056
00057 CFileMetadataPage::~CFileMetadataPage()
00058 {
00059 }
00060
00061 void CFileMetadataPage::DoDataExchange(CDataExchange* pDX)
00062 {
00063 CFilePropertiesPage::DoDataExchange(pDX);
00064
00065 DDX_Control(pDX, IDC_SCHEMAS, m_wndSchemas);
00066
00067 }
00068
00070
00071
00072 BOOL CFileMetadataPage::OnInitDialog()
00073 {
00074 CFilePropertiesPage::OnInitDialog();
00075
00076 CLibraryList* pFiles = GetList();
00077
00078 CRect rcClient, rcCombo;
00079 CString strText;
00080 GetClientRect( &rcClient );
00081
00082 m_wndSchemas.GetWindowRect( &rcCombo );
00083 ScreenToClient( &rcCombo );
00084 rcCombo.top = rcCombo.bottom + 8;
00085 rcCombo.bottom = rcClient.bottom - 8;
00086
00087 m_wndData.Create( WS_CHILD|WS_VISIBLE|WS_BORDER|WS_TABSTOP, rcCombo, this, IDC_METADATA );
00088 LoadString ( strText, IDS_SEARCH_NO_METADATA );
00089 m_wndSchemas.m_sNoSchemaText = strText;
00090
00091 BOOL bCollection = FALSE;
00092 CSchema* pSchema = NULL;
00093
00094 {
00095 CQuickLock oLock( Library.m_pSection );
00096
00097 for ( POSITION pos = pFiles->GetIterator() ; pos ; )
00098 {
00099 if ( CLibraryFile* pFile = pFiles->GetNextFile( pos ) )
00100 {
00101 CSchema* pThisSchema = pFile->m_pSchema;
00102
00103 if ( pThisSchema != NULL && pThisSchema->m_nType == CSchema::stFolder ) bCollection = TRUE;
00104
00105 if ( pSchema == NULL )
00106 {
00107 pSchema = pThisSchema;
00108 }
00109 else if ( pSchema != pThisSchema )
00110 {
00111 pSchema = NULL;
00112 break;
00113 }
00114 }
00115 }
00116 }
00117
00118 m_wndSchemas.Load( pSchema != NULL ? pSchema->m_sURI : _T(""), bCollection ? -1 : 0 );
00119 OnSelChangeSchemas();
00120
00121 if ( pSchema != NULL )
00122 {
00123 CXMLElement* pContainer = pSchema->Instantiate( TRUE );
00124 CXMLElement* pXML = pContainer->AddElement( pSchema->m_sSingular );
00125
00126 {
00127 CQuickLock oLock( Library.m_pSection );
00128
00129 for ( POSITION pos1 = pFiles->GetIterator() ; pos1 ; )
00130 {
00131 if ( CLibraryFile* pFile = pFiles->GetNextFile( pos1 ) )
00132 {
00133 if ( pFile->m_pMetadata != NULL && pSchema->Equals( pFile->m_pSchema ) )
00134 {
00135 for ( POSITION pos2 = pSchema->GetMemberIterator() ; pos2 ; )
00136 {
00137 CSchemaMember* pMember = pSchema->GetNextMember( pos2 );
00138 CString strOld = pMember->GetValueFrom( pXML, _T("(~ns~)") );
00139 CString strNew = pMember->GetValueFrom( pFile->m_pMetadata );
00140
00141 if ( strNew != _T("(~ns~)") && strOld != _T("(~mt~)") )
00142 {
00143 if ( strOld == _T("(~ns~)") )
00144 {
00145 pXML->AddAttribute( pMember->m_sName, strNew );
00146 }
00147 else if ( strOld != strNew )
00148 {
00149 pXML->AddAttribute( pMember->m_sName, _T("(~mt~)") );
00150 }
00151 }
00152 }
00153 }
00154 }
00155 }
00156 }
00157
00158 m_wndData.UpdateData( pXML, FALSE );
00159 delete pContainer;
00160 }
00161
00162 return TRUE;
00163 }
00164
00165 void CFileMetadataPage::OnSelChangeSchemas()
00166 {
00167 CSchema* pSchema = m_wndSchemas.GetSelected();
00168 m_wndData.SetSchema( pSchema );
00169 }
00170
00171 void CFileMetadataPage::OnCloseUpSchemas()
00172 {
00173 if ( CSchema* pSchema = m_wndSchemas.GetSelected() ) PostMessage( WM_KEYDOWN, VK_TAB );
00174 }
00175
00176 BOOL CFileMetadataPage::PreTranslateMessage(MSG* pMsg)
00177 {
00178 if ( pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_TAB )
00179 {
00180 if ( m_wndData.OnTab() ) return TRUE;
00181 }
00182
00183 return CPropertyPage::PreTranslateMessage( pMsg );
00184 }
00185
00186 void CFileMetadataPage::OnOK()
00187 {
00188 CLibraryList* pFiles = GetList();
00189 if ( pFiles == NULL ) return;
00190
00191 if ( pFiles->GetCount() >= 10 )
00192 {
00193 CString strFormat, strMessage;
00194 LoadString( strFormat, IDS_LIBRARY_METADATA_MANY );
00195 strMessage.Format( strFormat, pFiles->GetCount() );
00196 if ( AfxMessageBox( strMessage, MB_YESNO|MB_ICONQUESTION ) != IDYES ) return;
00197 }
00198
00199 if ( CSchema* pSchema = m_wndSchemas.GetSelected() )
00200 {
00201 CQuickLock oLock( Library.m_pSection );
00202
00203 for ( POSITION pos1 = pFiles->GetIterator() ; pos1 ; )
00204 {
00205 if ( CLibraryFile* pFile = pFiles->GetNextFile( pos1 ) )
00206 {
00207 if ( pSchema->Equals( pFile->m_pSchema ) && pFile->m_pMetadata != NULL )
00208 {
00209 CXMLElement* pContainer = pSchema->Instantiate( TRUE );
00210 CXMLElement* pXML = pContainer->AddElement( pFile->m_pMetadata->Clone() );
00211 m_wndData.UpdateData( pXML, TRUE );
00212 if ( pContainer ) pFile->SetMetadata( pContainer );
00213 delete pContainer;
00214 }
00215 else
00216 {
00217 CXMLElement* pContainer = pSchema->Instantiate( TRUE );
00218 CXMLElement* pXML = pContainer->AddElement( pSchema->m_sSingular );
00219 m_wndData.UpdateData( pXML, TRUE );
00220 if ( pContainer ) pFile->SetMetadata( pContainer );
00221 delete pContainer;
00222 }
00223 }
00224 }
00225 }
00226 else
00227 {
00228 CQuickLock oLock( Library.m_pSection );
00229
00230 for ( POSITION pos1 = pFiles->GetIterator() ; pos1 ; )
00231 {
00232 if ( CLibraryFile* pFile = pFiles->GetNextFile( pos1 ) )
00233 {
00234 pFile->SetMetadata( NULL );
00235 }
00236 }
00237
00238 Library.Update();
00239 }
00240
00241 CFilePropertiesPage::OnOK();
00242 }