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

DlgURLExport.cpp

Go to the documentation of this file.
00001 //
00002 // DlgURLExport.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 "Settings.h"
00025 #include "Library.h"
00026 #include "SharedFile.h"
00027 #include "SharedFolder.h"
00028 #include "Transfer.h"
00029 #include "Network.h"
00030 #include "TigerTree.h"
00031 #include "SHA.h"
00032 #include "MD5.h"
00033 #include "ED2K.h"
00034 #include "DlgURLExport.h"
00035 #include "DlgURLCopy.h"
00036 
00037 #ifdef _DEBUG
00038 #define new DEBUG_NEW
00039 #undef THIS_FILE
00040 static char THIS_FILE[] = __FILE__;
00041 #endif
00042 
00043 BEGIN_MESSAGE_MAP(CURLExportDlg, CSkinDialog)
00044         //{{AFX_MSG_MAP(CURLExportDlg)
00045         ON_CBN_CLOSEUP(IDC_URL_TOKEN, OnCloseUpUrlToken)
00046         ON_CBN_CLOSEUP(IDC_URL_PRESET, OnCloseUpUrlPreset)
00047         ON_BN_CLICKED(IDC_SAVE, OnSave)
00048         ON_BN_CLICKED(IDC_COPY, OnCopy)
00049         //}}AFX_MSG_MAP
00050 END_MESSAGE_MAP()
00051 
00052 
00054 // CURLExportDlg dialog
00055 
00056 CURLExportDlg::CURLExportDlg(CWnd* pParent) : CSkinDialog(CURLExportDlg::IDD, pParent)
00057 {
00058         //{{AFX_DATA_INIT(CURLExportDlg)
00059         m_sFormat = _T("");
00060         //}}AFX_DATA_INIT
00061 }
00062 
00063 void CURLExportDlg::DoDataExchange(CDataExchange* pDX)
00064 {
00065         CSkinDialog::DoDataExchange(pDX);
00066         //{{AFX_DATA_MAP(CURLExportDlg)
00067         DDX_Control(pDX, IDC_SAVE, m_wndSave);
00068         DDX_Control(pDX, IDC_COPY, m_wndCopy);
00069         DDX_Control(pDX, IDC_PROGRESS, m_wndProgress);
00070         DDX_Control(pDX, IDC_URL_TOKEN, m_wndToken);
00071         DDX_Control(pDX, IDC_URL_PRESET, m_wndPreset);
00072         DDX_Control(pDX, IDC_URL_FORMAT, m_wndFormat);
00073         DDX_Control(pDX, IDC_MESSAGE, m_wndMessage);
00074         DDX_Text(pDX, IDC_URL_FORMAT, m_sFormat);
00075         //}}AFX_DATA_MAP
00076 }
00077 
00079 // CURLExportDlg message handlers
00080 
00081 BOOL CURLExportDlg::OnInitDialog()
00082 {
00083         CSkinDialog::OnInitDialog();
00084 
00085         SkinMe( _T("CURLExportDlg"), IDI_WEB_URL );
00086 
00087         if ( theApp.m_bRTL ) m_wndProgress.ModifyStyleEx( WS_EX_LAYOUTRTL, 0, 0 );
00088         CString strFormat, strMessage;
00089         m_wndMessage.GetWindowText( strFormat );
00090         strMessage.Format( strFormat, m_pFiles.GetCount() );
00091         m_wndMessage.SetWindowText( strMessage );
00092 
00093         m_sFormat = theApp.GetProfileString( _T("Library"), _T("URLExportFormat"), _T("") );
00094 
00095         if ( m_sFormat.IsEmpty() )
00096                 m_sFormat = _T("<a href=\"magnet:?xt=[URN]&dn=[NameURI]\">[Name]</a><br>");
00097 
00098         UpdateData( FALSE );
00099 
00100         return TRUE;
00101 }
00102 
00103 void CURLExportDlg::AddFile(CLibraryFile* pFile)
00104 {
00105         if ( pFile->m_bSHA1 ) m_pFiles.AddTail( (LPVOID)pFile->m_nIndex );
00106 }
00107 
00108 void CURLExportDlg::OnCloseUpUrlToken()
00109 {
00110         int nToken = m_wndToken.GetCurSel();
00111         m_wndToken.SetCurSel( -1 );
00112         if ( nToken < 0 || nToken > 12 ) return;
00113 
00114         LPCTSTR pszTokens[] =
00115         {
00116                 _T("[TIGER]"), _T("[SHA1]"), _T("[MD5]"), _T("[ED2K]"),
00117                 _T("[Name]"), _T("[NameURI]"), _T("[FileBase]"),
00118                 _T("[FileExt]"), _T("[Size]"), _T("[ByteSize]"), _T("[Path]"),
00119                 _T("[LocalHost]"), _T("[LocalPort]")
00120         };
00121 
00122         m_wndFormat.ReplaceSel( pszTokens[ nToken ] );
00123         m_wndFormat.SetFocus();
00124 }
00125 
00126 void CURLExportDlg::OnCloseUpUrlPreset()
00127 {
00128         int nPreset = m_wndPreset.GetCurSel();
00129         m_wndPreset.SetCurSel( -1 );
00130         if ( nPreset < 0 || nPreset > 5 ) return;
00131 
00132         LPCTSTR pszPresets[] =
00133         {
00134                 _T("magnet:?xt=urn:bitprint:[SHA1].[TIGER]&dn=[NameURI]"),
00135                 _T("gnutella://urn:sha1:[SHA1]/[NameURI]/"),
00136                 _T("ed2k://|file|[NameURI]|[ByteSize]|[ED2K]|/"),
00137                 _T("<a href=\"magnet:?xt=urn:bitprint:[SHA1].[TIGER]&dn=[NameURI]\">[Name]</a><br>"),
00138                 _T("<a href=\"gnutella://urn:sha1:[SHA1]/[NameURI]/\">[Name]</a><br>"),
00139                 _T("<a href=\"ed2k://|file|[NameURI]|[ByteSize]|[ED2K]|/\">[Name]</a>"),
00140         };
00141 
00142         m_wndFormat.SetWindowText( pszPresets[ nPreset ] );
00143         m_wndFormat.SetFocus();
00144 }
00145 
00146 void CURLExportDlg::OnSave()
00147 {
00148         UpdateData();
00149 
00150         if ( m_sFormat.IsEmpty() ) return;
00151 
00152         theApp.WriteProfileString( _T("Library"), _T("URLExportFormat"), m_sFormat );
00153 
00154         LPCTSTR pszExt = ( m_sFormat.Find( '<' ) >= 0 ) ? _T("htm") : _T("txt");
00155         LPCTSTR pszFilter = ( m_sFormat.Find( '<' ) >= 0 ) ?
00156                 _T("HTML Files|*.htm;*.html|Text Files|*.txt|All Files|*.*||") :
00157                 _T("Text Files|*.txt|HTML Files|*.htm;*.html|All Files|*.*||");
00158 
00159         CFileDialog dlg( FALSE, pszExt, NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
00160                 pszFilter, this );
00161 
00162         if ( dlg.DoModal() != IDOK ) return;
00163 
00164         CFile pOutput;
00165 
00166         if ( ! pOutput.Open( dlg.GetPathName(), CFile::modeWrite|CFile::modeCreate ) )
00167                 return;
00168 
00169         CWaitCursor pCursor;
00170 
00171         m_wndProgress.SetRange( 0, m_pFiles.GetCount() );
00172         m_wndCopy.EnableWindow( FALSE );
00173         m_wndSave.EnableWindow( FALSE );
00174 
00175         for ( POSITION pos = m_pFiles.GetHeadPosition() ; pos ; )
00176         {
00177                 m_wndProgress.OffsetPos( 1 );
00178 
00179                 DWORD nIndex = (DWORD)m_pFiles.GetNext( pos );
00180 
00181                 CString strLine = m_sFormat;
00182                 {
00183                         CQuickLock oLock( Library.m_pSection );
00184                         CLibraryFile* pFile = Library.LookupFile( nIndex );
00185                         if ( ! pFile ) continue;
00186 
00187                         CString strItem;
00188 
00189                         Replace( strLine, _T("[Name]"), pFile->m_sName );
00190                         Replace( strLine, _T("[NameURI]"), CTransfer::URLEncode( pFile->m_sName ) );
00191                         Replace( strLine, _T("[Size]"), Settings.SmartVolume( pFile->m_nSize, FALSE ) );
00192                         if ( pFile->m_pFolder != NULL ) Replace( strLine, _T("[Path]"), pFile->m_pFolder->m_sPath );
00193 
00194                         strItem.Format( _T("%I64i"), pFile->m_nSize );
00195                         Replace( strLine, _T("[ByteSize]"), strItem );
00196 
00197                         strItem = CTigerNode::HashToString( &pFile->m_pTiger );
00198                         Replace( strLine, _T("[TIGER]"), strItem );
00199                         strItem = CSHA::HashToString( &pFile->m_pSHA1 );
00200                         Replace( strLine, _T("[SHA1]"), strItem );
00201                         strItem = CMD5::HashToString( &pFile->m_pMD5 );
00202                         Replace( strLine, _T("[MD5]"), strItem );
00203                         strItem = CED2K::HashToString( &pFile->m_pED2K );
00204                         Replace( strLine, _T("[ED2K]"), strItem );
00205 
00206                         int nDot = pFile->m_sName.ReverseFind( '.' );
00207 
00208                         if ( nDot > 0 )
00209                         {
00210                                 Replace( strLine, _T("[FileBase]"), pFile->m_sName.Left( nDot ) );
00211                                 Replace( strLine, _T("[FileExt]"), pFile->m_sName.Mid( nDot + 1 ) );
00212                         }
00213                         else
00214                         {
00215                                 Replace( strLine, _T("[FileBase]"), pFile->m_sName );
00216                                 Replace( strLine, _T("[FileExt]"), _T("") );
00217                         }
00218 
00219                         if ( Network.IsListening() )
00220                         {
00221                                 strItem = inet_ntoa( Network.m_pHost.sin_addr );
00222                                 Replace( strLine, _T("[LocalHost]"), strItem );
00223                                 strItem.Format( _T("%lu"), htons( Network.m_pHost.sin_port ) );
00224                                 Replace( strLine, _T("[LocalPort]"), strItem );
00225                         }
00226 
00227                         strLine += _T("\r\n");
00228                 }
00229 
00230                 int nBytes = WideCharToMultiByte( CP_ACP, 0, strLine, strLine.GetLength(), NULL, 0, NULL, NULL );
00231                 LPSTR pBytes = new CHAR[nBytes];
00232                 WideCharToMultiByte( CP_ACP, 0, strLine, strLine.GetLength(), pBytes, nBytes, NULL, NULL );
00233                 pOutput.Write( pBytes, nBytes );
00234                 delete [] pBytes;
00235         }
00236 
00237         pOutput.Close();
00238 
00239         EndDialog( IDOK );
00240 }
00241 
00242 void CURLExportDlg::OnCopy()
00243 {
00244         UpdateData();
00245 
00246         if ( m_sFormat.IsEmpty() ) return;
00247 
00248         theApp.WriteProfileString( _T("Library"), _T("URLExportFormat"), m_sFormat );
00249 
00250         CWaitCursor pCursor;
00251         CString strOutput;
00252 
00253         m_wndProgress.SetRange( 0, m_pFiles.GetCount() );
00254         m_wndCopy.EnableWindow( FALSE );
00255         m_wndSave.EnableWindow( FALSE );
00256 
00257         for ( POSITION pos = m_pFiles.GetHeadPosition() ; pos ; )
00258         {
00259                 m_wndProgress.OffsetPos( 1 );
00260 
00261                 DWORD nIndex = (DWORD)m_pFiles.GetNext( pos );
00262 
00263                 CString strLine = m_sFormat;
00264                 {
00265                         CQuickLock oLock( Library.m_pSection );
00266                         CLibraryFile* pFile = Library.LookupFile( nIndex );
00267                         if ( ! pFile ) continue;
00268 
00269                         CString strItem;
00270 
00271                         Replace( strLine, _T("[Name]"), pFile->m_sName );
00272                         Replace( strLine, _T("[NameURI]"), CTransfer::URLEncode( pFile->m_sName ) );
00273                         Replace( strLine, _T("[Size]"), Settings.SmartVolume( pFile->m_nSize, FALSE ) );
00274                         if ( pFile->m_pFolder != NULL ) Replace( strLine, _T("[Path]"), pFile->m_pFolder->m_sPath );
00275 
00276                         strItem.Format( _T("%I64i"), pFile->m_nSize );
00277                         Replace( strLine, _T("[ByteSize]"), strItem );
00278 
00279                         strItem = CTigerNode::HashToString( &pFile->m_pTiger );
00280                         Replace( strLine, _T("[TIGER]"), strItem );
00281                         strItem = CSHA::HashToString( &pFile->m_pSHA1 );
00282                         Replace( strLine, _T("[SHA1]"), strItem );
00283                         strItem = CMD5::HashToString( &pFile->m_pMD5 );
00284                         Replace( strLine, _T("[MD5]"), strItem );
00285                         strItem = CED2K::HashToString( &pFile->m_pED2K );
00286                         Replace( strLine, _T("[ED2K]"), strItem );
00287 
00288                         int nDot = pFile->m_sName.ReverseFind( '.' );
00289 
00290                         if ( nDot > 0 )
00291                         {
00292                                 Replace( strLine, _T("[FileBase]"), pFile->m_sName.Left( nDot ) );
00293                                 Replace( strLine, _T("[FileExt]"), pFile->m_sName.Mid( nDot + 1 ) );
00294                         }
00295                         else
00296                         {
00297                                 Replace( strLine, _T("[FileBase]"), pFile->m_sName );
00298                                 Replace( strLine, _T("[FileExt]"), _T("") );
00299                         }
00300 
00301                         if ( Network.IsListening() )
00302                         {
00303                                 strItem = inet_ntoa( Network.m_pHost.sin_addr );
00304                                 Replace( strLine, _T("[LocalHost]"), strItem );
00305                                 strItem.Format( _T("%lu"), htons( Network.m_pHost.sin_port ) );
00306                                 Replace( strLine, _T("[LocalPort]"), strItem );
00307                         }
00308                 }
00309 
00310                 strLine += _T("\r\n");
00311                 strOutput += strLine;
00312         }
00313 
00314         CURLCopyDlg::SetClipboardText( strOutput );
00315 
00316         EndDialog( IDOK );
00317 }

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