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

DlgDonkeyServers.cpp

Go to the documentation of this file.
00001 //
00002 // DlgDonkeyServers.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 "HostCache.h"
00026 #include "DlgDonkeyServers.h"
00027 
00028 #ifdef _DEBUG
00029 #define new DEBUG_NEW
00030 #undef THIS_FILE
00031 static char THIS_FILE[] = __FILE__;
00032 #endif
00033 
00034 BEGIN_MESSAGE_MAP(CDonkeyServersDlg, CSkinDialog)
00035         //{{AFX_MSG_MAP(CDonkeyServersDlg)
00036         ON_EN_CHANGE(IDC_URL, OnChangeURL)
00037         ON_WM_TIMER()
00038         //}}AFX_MSG_MAP
00039 END_MESSAGE_MAP()
00040 
00041 
00043 // CDonkeyServersDlg dialog
00044 
00045 CDonkeyServersDlg::CDonkeyServersDlg(CWnd* pParent) : CSkinDialog(CDonkeyServersDlg::IDD, pParent)
00046 {
00047         //{{AFX_DATA_INIT(CDonkeyServersDlg)
00048         m_sURL = _T("");
00049         //}}AFX_DATA_INIT
00050         m_hInternet = NULL;
00051         m_hThread = NULL;
00052 }
00053 
00054 CDonkeyServersDlg::~CDonkeyServersDlg()
00055 {
00056         ASSERT( m_hThread == NULL );
00057         ASSERT( m_hInternet == NULL );
00058 }
00059 
00060 void CDonkeyServersDlg::DoDataExchange(CDataExchange* pDX)
00061 {
00062         CSkinDialog::DoDataExchange(pDX);
00063         //{{AFX_DATA_MAP(CDonkeyServersDlg)
00064         DDX_Control(pDX, IDC_URL, m_wndURL);
00065         DDX_Control(pDX, IDOK, m_wndOK);
00066         DDX_Control(pDX, IDC_PROGRESS, m_wndProgress);
00067         DDX_Text(pDX, IDC_URL, m_sURL);
00068         //}}AFX_DATA_MAP
00069 }
00070 
00072 // CDonkeyServersDlg message handlers
00073 
00074 BOOL CDonkeyServersDlg::OnInitDialog()
00075 {
00076         CSkinDialog::OnInitDialog();
00077 
00078         SkinMe( _T("CDonkeyServersDlg") );
00079 
00080         m_sURL = Settings.eDonkey.ServerListURL;
00081         UpdateData( FALSE );
00082 
00083         m_wndOK.EnableWindow( m_sURL.Find( _T("http://") ) == 0 );
00084         m_wndProgress.SetRange( 0, 100 );
00085         m_wndProgress.SetPos( 0 );
00086 
00087         return TRUE;
00088 }
00089 
00090 void CDonkeyServersDlg::OnChangeURL()
00091 {
00092         UpdateData();
00093         m_wndOK.EnableWindow( m_sURL.Find( _T("http://") ) == 0 );
00094 }
00095 
00096 void CDonkeyServersDlg::OnOK()
00097 {
00098         UpdateData();
00099 
00100         if ( m_sURL.Find( _T("http://") ) != 0 ) return;
00101 
00102         Settings.eDonkey.ServerListURL = m_sURL;
00103 
00104         CString strAgent = Settings.SmartAgent();
00105         m_hInternet = InternetOpen( strAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
00106         if ( m_hInternet == NULL ) return;
00107 
00108         CWinThread* pThread = AfxBeginThread( ThreadStart, this, THREAD_PRIORITY_NORMAL );
00109         m_hThread = pThread->m_hThread;
00110 
00111         m_wndOK.EnableWindow( FALSE );
00112         m_wndURL.EnableWindow( FALSE );
00113 }
00114 
00115 void CDonkeyServersDlg::OnCancel()
00116 {
00117         OnTimer( 2 );
00118         CSkinDialog::OnCancel();
00119 }
00120 
00121 void CDonkeyServersDlg::OnTimer(UINT nIDEvent)
00122 {
00123         if ( m_hInternet != NULL )
00124         {
00125                 InternetCloseHandle( m_hInternet );
00126                 m_hInternet = NULL;
00127         }
00128 
00129         if ( m_hThread != NULL )
00130         {
00131         int nAttempt = 5;
00132                 for ( ; nAttempt > 0 ; nAttempt-- )
00133                 {
00134                         DWORD nCode;
00135 
00136                         if ( ! GetExitCodeThread( m_hThread, &nCode ) ) break;
00137                         if ( nCode != STILL_ACTIVE ) break;
00138                         Sleep( 100 );
00139                 }
00140 
00141                 if ( nAttempt == 0 )
00142                 {
00143                         TerminateThread( m_hThread, 0 );
00144                         theApp.Message( MSG_DEBUG, _T("WARNING: Terminating CDonkeyServersDlg thread.") );
00145                         Sleep( 100 );
00146                 }
00147 
00148                 m_hThread = NULL;
00149         }
00150 
00151         if ( nIDEvent == 1 ) EndDialog( IDOK );
00152 }
00153 
00155 // CDonkeyServersDlg thread
00156 
00157 UINT CDonkeyServersDlg::ThreadStart(LPVOID pParam)
00158 {
00159         CDonkeyServersDlg* pDlg = (CDonkeyServersDlg*)pParam;
00160         pDlg->OnRun();
00161         return 0;
00162 }
00163 
00164 void CDonkeyServersDlg::OnRun()
00165 {
00166         if ( m_hInternet == NULL ) return;
00167 
00168         HINTERNET hRequest = InternetOpenUrl( m_hInternet, m_sURL, NULL, 0,
00169                 INTERNET_FLAG_RELOAD|INTERNET_FLAG_DONT_CACHE, 0 );
00170 
00171         if ( hRequest == NULL )
00172         {
00173                 InternetCloseHandle( m_hInternet );
00174                 m_hInternet = NULL;
00175                 PostMessage( WM_TIMER, FALSE );
00176                 return;
00177         }
00178 
00179         DWORD nLength, nlLength = 4;
00180         DWORD nRemaining = 0;
00181         BYTE pBuffer[1024];
00182         CMemFile pFile;
00183 
00184         if ( HttpQueryInfo( hRequest, HTTP_QUERY_CONTENT_LENGTH|HTTP_QUERY_FLAG_NUMBER,
00185                 &nLength, &nlLength, NULL ) )
00186         {
00187                 m_wndProgress.PostMessage( PBM_SETRANGE32, 0, nLength );
00188         }
00189 
00190         nLength = 0;
00191 
00192         while ( InternetQueryDataAvailable( hRequest, &nRemaining, 0, 0 ) && nRemaining > 0 )
00193         {
00194                 nLength += nRemaining;
00195                 m_wndProgress.PostMessage( PBM_SETPOS, nLength );
00196 
00197                 while ( nRemaining > 0 )
00198                 {
00199                         DWORD nBuffer = min( nRemaining, DWORD(1024) );
00200                         InternetReadFile( hRequest, pBuffer, nBuffer, &nBuffer );
00201                         pFile.Write( pBuffer, nBuffer );
00202                         nRemaining -= nBuffer;
00203                 }
00204         }
00205 
00206         pFile.Seek( 0, CFile::begin );
00207 
00208         BOOL bSuccess = HostCache.eDonkey.ImportMET( &pFile );
00209         if ( bSuccess ) HostCache.Save();
00210 
00211         InternetCloseHandle( m_hInternet );
00212         m_hInternet = NULL;
00213 
00214         PostMessage( WM_TIMER, bSuccess ? 1 : 0 );
00215 }
00216 

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