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

DlgTorrentTracker.cpp

Go to the documentation of this file.
00001 //
00002 // DlgTorrentTracker.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 "ShellIcons.h"
00026 #include "DlgTorrentTracker.h"
00027 #include "BENode.h"
00028 #include "Skin.h"
00029 
00030 
00031 #ifdef _DEBUG
00032 #define new DEBUG_NEW
00033 #undef THIS_FILE
00034 static char THIS_FILE[] = __FILE__;
00035 #endif
00036 
00037 BEGIN_MESSAGE_MAP(CTorrentTrackerDlg, CSkinDialog)
00038         //{{AFX_MSG_MAP(CTorrentTrackerDlg)
00039         ON_CBN_SELCHANGE(IDC_TORRENT_VIEW, OnSelChangeTorrentView)
00040         ON_BN_CLICKED(IDC_TORRENT_REFRESH, OnTorrentRefresh)
00041         ON_WM_TIMER()
00042         ON_WM_DESTROY()
00043         //}}AFX_MSG_MAP
00044 END_MESSAGE_MAP()
00045 
00046 
00048 // CTorrentTrackerDlg dialog
00049 
00050 CTorrentTrackerDlg::CTorrentTrackerDlg(CBTInfo* pInfo, int* pStart, CWnd* pParent) : CSkinDialog(CTorrentTrackerDlg::IDD, pParent)
00051 {
00052         //{{AFX_DATA_INIT(CTorrentTrackerDlg)
00053         m_sName = _T("");
00054         m_sTracker = _T("");
00055         m_sComment = _T("");
00056         m_sCreationDate = _T("");
00057         m_sCreatedBy = _T("");
00058         //}}AFX_DATA_INIT
00059 
00060         m_pInfo.Copy( pInfo );
00061         m_pInfo.m_bValid = FALSE;
00062 
00063         m_pStartTorrentDownloads = pStart;
00064 }
00065 
00066 void CTorrentTrackerDlg::DoDataExchange(CDataExchange* pDX)
00067 {
00068         CSkinDialog::DoDataExchange(pDX);
00069         //{{AFX_DATA_MAP(CTorrentTrackerDlg)
00070         DDX_Control(pDX, IDC_TORRENT_VIEW, m_wndView);
00071         DDX_Control(pDX, IDC_TORRENT_REFRESH, m_wndRefresh);
00072         DDX_Control(pDX, IDC_TORRENT_FILES, m_wndFiles);
00073         DDX_Control(pDX, IDC_TORRENT_COMPLETED, m_wndComplete);
00074         DDX_Control(pDX, IDC_TORRENT_INCOMPLETE, m_wndIncomplete);
00075         DDX_Text(pDX, IDC_TORRENT_NAME, m_sName);
00076         DDX_Text(pDX, IDC_TORRENT_TRACKER, m_sTracker);
00077         DDX_Text(pDX, IDC_TORRENT_COMMENTS, m_sComment);
00078         DDX_Text(pDX, IDC_TORRENT_CREATEDBY, m_sCreatedBy );
00079         DDX_Text(pDX, IDC_TORRENT_CREATIONDATE, m_sCreationDate );
00080         DDX_Control(pDX, IDC_TORRENT_STARTDOWNLOADS, m_wndStartDownloads);
00081         //}}AFX_DATA_MAP
00082 
00083 
00084 }
00085 
00087 // CTorrentTrackerDlg message handlers
00088 
00089 BOOL CTorrentTrackerDlg::OnInitDialog() 
00090 {
00091         CSkinDialog::OnInitDialog();
00092         
00093         SetIcon( theApp.LoadIcon( IDR_MAINFRAME ), TRUE );
00094         SkinMe( _T("CTorrentTrackerDlg") );
00095         
00096         m_sName                 = m_pInfo.m_sName;
00097         m_sTracker              = m_pInfo.m_sTracker;
00098         m_sComment              = m_pInfo.m_sComment;
00099         m_sCreatedBy    = m_pInfo.m_sCreatedBy;
00100         if ( m_pInfo.m_tCreationDate > 0 )
00101         {
00102                 CTime pTime( (time_t)m_pInfo.m_tCreationDate );
00103                 m_sCreationDate = pTime.Format( _T("%Y-%m-%d  %H:%M") );
00104         }
00105         
00106         
00107         CRect rc;
00108         m_wndFiles.GetClientRect( &rc );
00109         rc.right -= GetSystemMetrics( SM_CXVSCROLL );
00110         m_wndFiles.SetImageList( ShellIcons.GetObject( 16 ), LVSIL_SMALL );
00111         m_wndFiles.InsertColumn( 0, _T("Filename"), LVCFMT_LEFT, rc.right - 80, -1 );
00112         m_wndFiles.InsertColumn( 1, _T("Size"), LVCFMT_RIGHT, 80, 0 );
00113         Skin.Translate( _T("CTorrentTrackerList"), m_wndFiles.GetHeaderCtrl() );
00114 
00115         for ( int nFile = 0 ; nFile < m_pInfo.m_nFiles ; nFile++ )
00116         {
00117                 CBTInfo::CBTFile* pFile = m_pInfo.m_pFiles + nFile;
00118                 LV_ITEM pItem;
00119                 
00120                 ZeroMemory( &pItem, sizeof(pItem) );
00121                 pItem.mask              = LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM;
00122                 pItem.iItem             = m_wndFiles.GetItemCount();
00123                 pItem.lParam    = (LPARAM)nFile;
00124                 pItem.iImage    = ShellIcons.Get( pFile->m_sPath, 16 );
00125                 pItem.pszText   = (LPTSTR)(LPCTSTR)pFile->m_sPath;
00126                 pItem.iItem             = m_wndFiles.InsertItem( &pItem );
00127                 
00128                 m_wndFiles.SetItemText( pItem.iItem, 1, Settings.SmartVolume( pFile->m_nSize, FALSE ) );
00129         }
00130 
00131         m_wndStartDownloads.SetItemData( 0, dtAlways );
00132         m_wndStartDownloads.SetItemData( 1, dtWhenRatio );
00133         m_wndStartDownloads.SetItemData( 2, dtNever );
00134 
00135         m_wndStartDownloads.SetCurSel( *m_pStartTorrentDownloads );
00136         
00137         UpdateData( FALSE );
00138         m_hThread = NULL;
00139         
00140         PostMessage( WM_COMMAND, MAKELONG( IDC_TORRENT_REFRESH, BN_CLICKED ), (LPARAM)m_wndRefresh.GetSafeHwnd() );
00141 
00142         return TRUE;
00143 }
00144 
00145 void CTorrentTrackerDlg::OnDestroy() 
00146 {
00147         if ( m_hThread != NULL ) 
00148         {
00149                 m_pRequest.Cancel();
00150                 CHttpRequest::CloseThread( &m_hThread, _T("CTorrentTrackerDlg") );
00151         }
00152         
00153         CSkinDialog::OnDestroy();
00154 }
00155 
00156 void CTorrentTrackerDlg::OnSelChangeTorrentView() 
00157 {
00158 }
00159 
00160 void CTorrentTrackerDlg::OnTorrentRefresh() 
00161 {
00162         if ( m_wndRefresh.IsWindowEnabled() == FALSE ) return;
00163         
00164         if ( m_hThread != NULL ) 
00165         {
00166                 m_pRequest.Cancel();
00167                 CHttpRequest::CloseThread( &m_hThread, _T("CTorrentTrackerDlg") );
00168         }
00169         
00170         m_wndRefresh.EnableWindow( FALSE );
00171         m_hThread = AfxBeginThread( ThreadStart, this )->m_hThread;
00172 }
00173 
00174 void CTorrentTrackerDlg::OnTimer(UINT nIDEvent) 
00175 {
00176         if ( nIDEvent == 1 )
00177         {
00178                 m_wndRefresh.EnableWindow( TRUE );
00179                 KillTimer( 1 );
00180         }
00181         else
00182         {
00183                 CHttpRequest::CloseThread( &m_hThread, _T("CTorrentTrackerDlg") );
00184                 SetTimer( 1, 60000, NULL );
00185                 
00186                 if ( nIDEvent == 3 )
00187                 {
00188                         CString str;
00189                         str.Format( _T("%i"), m_nComplete );
00190                         m_wndComplete.SetWindowText( str );
00191                         str.Format( _T("%i"), m_nIncomplete );
00192                         m_wndIncomplete.SetWindowText( str );
00193                 }
00194         }
00195 }
00196 
00197 void CTorrentTrackerDlg::OnOK() 
00198 {
00199         UpdateData();
00200         
00201         //Check if tracker has been changed
00202         if ( m_pInfo.m_sTracker != m_sTracker )
00203         {
00204                 CString strMessage;
00205                 LoadString( strMessage, IDS_BT_TRACK_CHANGE );
00206                 
00207                 //Display warning
00208                 if ( AfxMessageBox( strMessage, MB_ICONQUESTION|MB_YESNO ) == IDYES )
00209                 {
00210                         m_pInfo.m_sTracker = m_sTracker;
00211                         m_pInfo.m_bValid = TRUE;
00212                 }
00213         }
00214 
00215         //Update the starting of torrent transfers
00216         *m_pStartTorrentDownloads = m_wndStartDownloads.GetCurSel();
00217         
00218         CSkinDialog::OnOK();
00219 }
00220 
00221 UINT CTorrentTrackerDlg::ThreadStart(LPVOID pParam)
00222 {
00223         CTorrentTrackerDlg* pObject = (CTorrentTrackerDlg*)pParam;
00224         pObject->OnRun();
00225         return 0;
00226 }
00227 
00228 void CTorrentTrackerDlg::OnRun()
00229 {
00230         m_pRequest.Clear();
00231         m_pRequest.AddHeader( _T("Accept-Encoding"), _T("deflate, gzip") );
00232         
00233         CString strURL = m_pInfo.m_sTracker;
00234         Replace( strURL, _T("/announce"), _T("/scrape") );
00235 
00236         // Skip obviously invalid trackers
00237         if ( m_pInfo.m_sTracker.GetLength() > 7 ) 
00238         {
00239                 m_pRequest.SetURL( strURL );
00240                 
00241                 if ( m_pRequest.Execute( FALSE ) && m_pRequest.InflateResponse() )
00242                 {
00243                         CBuffer* pResponse = m_pRequest.GetResponseBuffer();
00244                         
00245                         if ( CBENode* pNode = CBENode::Decode( pResponse ) )
00246                         {
00247                                 if ( OnTree( pNode ) )
00248                                 {
00249                                         delete pNode;
00250                                         PostMessage( WM_TIMER, 3 );
00251                                         return;
00252                                 }
00253                                 
00254                                 delete pNode;
00255                         }
00256                 }
00257         }
00258         
00259         m_pRequest.Clear();
00260         PostMessage( WM_TIMER, 2 );
00261 }
00262 
00263 BOOL CTorrentTrackerDlg::OnTree(CBENode* pNode)
00264 {
00265         if ( ! pNode->IsType( CBENode::beDict ) ) return FALSE;
00266         
00267         CBENode* pFiles = pNode->GetNode( "files" );
00268         if ( ! pFiles->IsType( CBENode::beDict ) ) return FALSE;
00269         
00270         SHA1* pSHA1 = &m_pInfo.m_pInfoSHA1;
00271         
00272         CBENode* pFile = pFiles->GetNode( (LPBYTE)pSHA1, sizeof(SHA1) );
00273         if ( ! pFile->IsType( CBENode::beDict ) ) return FALSE; 
00274         
00275         m_nComplete             = 0;
00276         m_nIncomplete   = 0;
00277         
00278         if ( CBENode* pComplete = pFile->GetNode( "complete" ) )
00279         {
00280                 if ( ! pComplete->IsType( CBENode::beInt ) ) return FALSE;      
00281                 m_nComplete = (int)pComplete->GetInt();
00282         }
00283         
00284         if ( CBENode* pIncomplete = pFile->GetNode( "incomplete" ) )
00285         {
00286                 if ( ! pIncomplete->IsType( CBENode::beInt ) ) return FALSE;    
00287                 m_nIncomplete = (int)pIncomplete->GetInt();
00288         }
00289         
00290         return TRUE;
00291 }

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