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

DlgURLAction.cpp

Go to the documentation of this file.
00001 //
00002 // DlgURLAction.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 "ShareazaURL.h"
00026 #include "Download.h"
00027 #include "Downloads.h"
00028 #include "Transfers.h"
00029 #include "Network.h"
00030 #include "Library.h"
00031 #include "SharedFile.h"
00032 #include "HostCache.h"
00033 #include "DiscoveryServices.h"
00034 #include "Skin.h"
00035 #include "DlgURLAction.h"
00036 #include "WndMain.h"
00037 #include "WndSearch.h"
00038 #include "WndDownloads.h"
00039 #include "WndBrowseHost.h"
00040 
00041 #include "TigerTree.h"
00042 #include "SHA.h"
00043 #include "MD5.h"
00044 #include "ED2K.h"
00045 
00046 #ifdef _DEBUG
00047 #define new DEBUG_NEW
00048 #undef THIS_FILE
00049 static char THIS_FILE[] = __FILE__;
00050 #endif
00051 
00052 BEGIN_MESSAGE_MAP(CURLActionDlg, CSkinDialog)
00053         //{{AFX_MSG_MAP(CURLActionDlg)
00054         ON_BN_CLICKED(IDC_URL_DOWNLOAD, OnUrlDownload)
00055         ON_BN_CLICKED(IDC_URL_SEARCH, OnUrlSearch)
00056         //}}AFX_MSG_MAP
00057 END_MESSAGE_MAP()
00058 
00059 
00061 // CURLActionDlg construction
00062 
00063 CURLActionDlg::CURLActionDlg(CWnd* pParent, CShareazaURL* pURL, BOOL bMultiple) : CSkinDialog(CURLActionDlg::IDD, pParent)
00064 {
00065         //{{AFX_DATA_INIT(CURLActionDlg)
00066         m_sNameTitle = _T("");
00067         m_sNameValue = _T("");
00068         m_sHashTitle = _T("");
00069         m_sHashValue = _T("");
00070         m_bNewWindow = FALSE;
00071         m_bAlwaysOpen = FALSE;
00072         //}}AFX_DATA_INIT
00073 
00074         m_pURLs.AddTail( pURL );
00075         m_bMultiple = bMultiple;
00076 }
00077 
00078 CURLActionDlg::~CURLActionDlg()
00079 {
00080         for ( POSITION pos = m_pURLs.GetHeadPosition() ; pos ; )
00081         {
00082                 delete (CShareazaURL*)m_pURLs.GetNext( pos );
00083         }
00084 }
00085 
00086 void CURLActionDlg::DoDataExchange(CDataExchange* pDX)
00087 {
00088         CSkinDialog::DoDataExchange(pDX);
00089         //{{AFX_DATA_MAP(CURLActionDlg)
00090         DDX_Control(pDX, IDC_MESSAGE_4, m_wndMessage4);
00091         DDX_Control(pDX, IDC_MESSAGE_3, m_wndMessage3);
00092         DDX_Control(pDX, IDC_NEW_WINDOW, m_wndNewWindow);
00093         DDX_Control(pDX, IDCANCEL, m_wndCancel);
00094         DDX_Control(pDX, IDC_MESSAGE_2, m_wndMessage2);
00095         DDX_Control(pDX, IDC_MESSAGE_1, m_wndMessage1);
00096         DDX_Control(pDX, IDC_URL_SEARCH, m_wndSearch);
00097         DDX_Control(pDX, IDC_URL_DOWNLOAD, m_wndDownload);
00098         DDX_Text(pDX, IDC_URL_NAME_TITLE, m_sNameTitle);
00099         DDX_Text(pDX, IDC_URL_NAME_VALUE, m_sNameValue);
00100         DDX_Text(pDX, IDC_URL_URN_TITLE, m_sHashTitle);
00101         DDX_Text(pDX, IDC_URL_URN_VALUE, m_sHashValue);
00102         DDX_Check(pDX, IDC_NEW_WINDOW, m_bNewWindow);
00103         DDX_Check(pDX, IDC_ALWAYS_OPEN, m_bAlwaysOpen);
00104         //}}AFX_DATA_MAP
00105 }
00106 
00108 // CURLActionDlg message handlers
00109 
00110 BOOL CURLActionDlg::OnInitDialog()
00111 {
00112         CSkinDialog::OnInitDialog();
00113 
00114         SkinMe( _T("CURLActionDlg"), IDR_MAINFRAME );
00115 
00116         m_bAlwaysOpen   = Settings.General.AlwaysOpenURLs;
00117         m_bNewWindow    = Settings.Downloads.ShowMonitorURLs;
00118 
00119         Update();
00120 
00121         if ( m_bAlwaysOpen )
00122         {
00123                 if ( m_wndDownload.IsWindowEnabled() )
00124                         PostMessage( WM_COMMAND, IDC_URL_DOWNLOAD );
00125                 else
00126                         PostMessage( WM_COMMAND, IDC_URL_SEARCH );
00127         }
00128         else
00129         {
00130                 m_bMultiple = FALSE;
00131 
00132                 if ( CWnd* pWnd = AfxGetMainWnd() )
00133                 {
00134                         if ( pWnd->IsWindowVisible() && ! pWnd->IsIconic() )
00135                         {
00136                                 pWnd->BringWindowToTop();
00137                                 pWnd->SetForegroundWindow();
00138                         }
00139                 }
00140         }
00141 
00142         return FALSE;
00143 }
00144 
00145 void CURLActionDlg::AddURL(CShareazaURL* pURL)
00146 {
00147         if ( IsWindowVisible() && m_pURLs.GetCount() > 0 )
00148         {
00149                 CShareazaURL* pFirst = (CShareazaURL*)m_pURLs.GetHead();
00150 
00151                 if ( pFirst->m_nAction == pURL->m_nAction )
00152                 {
00153                         m_pURLs.AddTail( pURL );
00154                         Update();
00155                         return;
00156                 }
00157         }
00158 
00159         delete pURL;
00160 }
00161 
00162 void CURLActionDlg::Update()
00163 {
00164         CShareazaURL* pURL = (CShareazaURL*)m_pURLs.GetHead();
00165 
00166         CString strMessage;
00167 
00168         if ( pURL->m_nAction == CShareazaURL::uriHost ||
00169                  pURL->m_nAction == CShareazaURL::uriDonkeyServer )
00170         {
00171                 LoadString(m_sNameTitle, IDS_URL_HOST );
00172                 LoadString(m_sHashTitle, IDS_URL_PORT );
00173 
00174                 m_sNameValue = pURL->m_sName;
00175                 m_sHashValue.Format( _T("%lu"), pURL->m_nPort );
00176 
00177                 m_wndMessage2.ShowWindow( SW_SHOW );
00178                 m_wndNewWindow.ShowWindow( SW_HIDE );
00179 
00180                 LoadString( strMessage, IDS_URL_CONNECT );
00181                 m_wndDownload.SetWindowText( strMessage );
00182                 m_wndDownload.SetFocus();
00183 
00184                 if ( pURL->m_nAction == CShareazaURL::uriHost )
00185                 {
00186                         LoadString(strMessage, IDS_URL_BROWSE );
00187                         m_wndSearch.SetWindowText( strMessage );
00188                 }
00189                 else
00190                         m_wndSearch.ShowWindow( SW_HIDE );
00191         }
00192         else if ( pURL->m_nAction == CShareazaURL::uriBrowse )
00193         {
00194                 LoadString(m_sNameTitle, IDS_URL_HOST );
00195                 LoadString(m_sHashTitle, IDS_URL_PORT );
00196 
00197                 m_sNameValue = pURL->m_sName;
00198                 m_sHashValue.Format( _T("%lu"), pURL->m_nPort );
00199 
00200                 m_wndMessage3.ShowWindow( SW_SHOW );
00201                 m_wndNewWindow.ShowWindow( SW_HIDE );
00202 
00203                 LoadString(strMessage, IDS_URL_BROWSE );
00204                 m_wndDownload.SetWindowText( strMessage );
00205                 m_wndDownload.SetFocus();
00206                 LoadString(strMessage, IDS_URL_CONNECT );
00207                 m_wndSearch.SetWindowText( strMessage );
00208         }
00209         else if ( pURL->m_nAction == CShareazaURL::uriDiscovery )
00210         {
00211                 LoadString(m_sNameTitle, IDS_URL_URL );
00212                 LoadString(m_sHashTitle, IDS_URL_TYPE );
00213 
00214                 if ( m_pURLs.GetCount() == 1 )
00215                 {
00216                         m_sNameValue = pURL->m_sURL;
00217                 }
00218                 else
00219                 {
00220                         m_sNameValue.Format( _T("%i URL(s)"), m_pURLs.GetCount() );
00221                 }
00222 
00223                 switch ( pURL->m_nSize )
00224                 {
00225                 case CDiscoveryService::dsWebCache:
00226                         m_sHashValue = _T("GWebCache");
00227                         break;
00228                 case CDiscoveryService::dsServerMet:
00229                         m_sHashValue = _T("Server.met URL");
00230                         break;
00231                 }
00232 
00233                 m_wndMessage4.ShowWindow( SW_SHOW );
00234                 LoadString(strMessage, IDS_URL_ADD );
00235                 m_wndDownload.SetWindowText( strMessage );
00236                 m_wndSearch.ShowWindow( SW_HIDE );
00237                 m_wndNewWindow.ShowWindow( SW_HIDE );
00238         }
00239         else if ( pURL->m_nAction == CShareazaURL::uriSource )
00240         {
00241                 LoadString(m_sNameTitle, IDS_URL_URL );
00242 
00243                 if ( m_pURLs.GetCount() == 1 )
00244                 {
00245                         m_sNameValue = pURL->m_sURL;
00246                 }
00247                 else
00248                 {
00249                         m_sNameValue.Format( _T("%i URL(s)"), m_pURLs.GetCount() );
00250                 }
00251 
00252                 m_wndMessage1.ShowWindow( SW_SHOW );
00253                 m_wndSearch.ShowWindow( SW_HIDE );
00254         }
00255         else
00256         {
00257                 LoadString(m_sNameTitle, IDS_URL_FILENAME );
00258                 m_sHashTitle = _T("URN:");
00259 
00260                 if ( m_pURLs.GetCount() > 1 )
00261                 {
00262                         m_sNameValue.Format( _T("%i file(s)"), m_pURLs.GetCount() );
00263                 }
00264                 else if ( pURL->m_sName.GetLength() )
00265                 {
00266                         m_sNameValue = pURL->m_sName;
00267 
00268                         if ( pURL->m_bSize )
00269                         {
00270                                 m_sNameValue += _T(" (");
00271                                 m_sNameValue += Settings.SmartVolume( pURL->m_nSize, FALSE );
00272                                 m_sNameValue += _T(")");
00273                         }
00274                 }
00275                 else
00276                 {
00277                         LoadString(m_sNameValue, IDS_URL_UNSPECIFIED );
00278                 }
00279 
00280                 if ( m_pURLs.GetCount() > 1 )
00281                 {
00282                         m_sHashValue.Format( _T("%i file(s)"), m_pURLs.GetCount() );
00283                 }
00284                 else if ( pURL->m_bTiger && pURL->m_bSHA1 )
00285                 {
00286                         m_sHashValue    = _T("bitprint:")
00287                                                         + CSHA::HashToString( &pURL->m_pSHA1 ) + _T(".")
00288                                                         + CTigerNode::HashToString( &pURL->m_pTiger );
00289                 }
00290                 else if ( pURL->m_bTiger )
00291                 {
00292                         m_sHashValue = _T("tree:tiger/:") + CTigerNode::HashToString( &pURL->m_pTiger );
00293                 }
00294                 else if ( pURL->m_bSHA1 )
00295                 {
00296                         m_sHashValue = _T("sha1:") + CSHA::HashToString( &pURL->m_pSHA1 );
00297                 }
00298                 else if ( pURL->m_bED2K )
00299                 {
00300                         m_sHashValue = _T("ed2k:") + CED2K::HashToString( &pURL->m_pED2K );
00301                 }
00302                 else
00303                 {
00304                         LoadString(m_sHashValue, IDS_URL_UNSPECIFIED );
00305                 }
00306 
00307                 m_wndMessage1.ShowWindow( SW_SHOW );
00308 
00309                 if ( pURL->m_nAction == CShareazaURL::uriDownload )
00310                 {
00311                         m_wndDownload.SetFocus();
00312                 }
00313                 else if ( pURL->m_nAction == CShareazaURL::uriSearch )
00314                 {
00315                         m_wndDownload.EnableWindow( FALSE );
00316                         m_wndDownload.ModifyStyle( BS_DEFPUSHBUTTON, 0 );
00317                         m_wndSearch.ModifyStyle( 0, BS_DEFPUSHBUTTON );
00318                         m_wndSearch.SetFocus();
00319                         m_wndNewWindow.ShowWindow( SW_HIDE );
00320                 }
00321         }
00322 
00323         UpdateData( FALSE );
00324 }
00325 
00326 BOOL CURLActionDlg::PreTranslateMessage(MSG* pMsg)
00327 {
00328         if ( pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN )
00329         {
00330                 if ( GetFocus() == &m_wndCancel )
00331                         PostMessage( WM_COMMAND, IDCANCEL );
00332                 else if ( m_wndDownload.IsWindowEnabled() )
00333                         PostMessage( WM_COMMAND, IDC_URL_DOWNLOAD );
00334                 else if ( m_wndSearch.IsWindowEnabled() )
00335                         PostMessage( WM_COMMAND, IDC_URL_SEARCH );
00336                 return TRUE;
00337         }
00338 
00339         return CSkinDialog::PreTranslateMessage( pMsg );
00340 }
00341 
00342 void CURLActionDlg::OnUrlDownload()
00343 {
00344         UpdateData();
00345 
00346         Settings.General.AlwaysOpenURLs         = m_bAlwaysOpen;
00347         Settings.Downloads.ShowMonitorURLs      = m_bNewWindow;
00348 
00349         for ( POSITION pos = m_pURLs.GetHeadPosition() ; pos ; )
00350         {
00351                 CShareazaURL* pURL = (CShareazaURL*)m_pURLs.GetNext( pos );
00352 
00353                 if ( pURL->m_nAction == CShareazaURL::uriDownload ||
00354                          pURL->m_nAction == CShareazaURL::uriSource )
00355                 {
00356                         CLibraryFile* pFile;
00357 
00358                         {
00359                                 CSingleLock oLock( &Library.m_pSection, TRUE );
00360                                 if ( ( pURL->m_bSHA1 && ( pFile = LibraryMaps.LookupFileBySHA1( &pURL->m_pSHA1 ) ) ) ||
00361                                         ( pURL->m_bED2K && ( pFile = LibraryMaps.LookupFileByED2K( &pURL->m_pED2K ) ) ) )
00362                                 {
00363                                         CString strFormat, strMessage;
00364 					::Skin.LoadString( strFormat, IDS_URL_ALREADY_HAVE );
00365                                         strMessage.Format( strFormat, (LPCTSTR)pFile->m_sName );
00366                                         oLock.Unlock();
00367 
00368                                         UINT nMBOX = AfxMessageBox( strMessage, MB_ICONINFORMATION|MB_YESNOCANCEL|MB_DEFBUTTON2 );
00369                                         if ( nMBOX == IDCANCEL ) return;
00370                                         if ( nMBOX == IDNO ) continue;
00371                                 }
00372                         }
00373 
00374                         CDownload* pDownload = Downloads.Add( pURL );
00375 
00376                         if ( pDownload == NULL ) continue;
00377 
00378                         if ( ( GetAsyncKeyState( VK_SHIFT ) & 0x8000 ) == 0 )
00379                         {
00380 //                              if ( pURL->m_bED2K && HostCache.eDonkey.GetNewest() != NULL )
00381 //                                      Settings.eDonkey.EnableToday = TRUE;
00382 
00383                                 if ( ! Network.IsWellConnected() ) Network.Connect( TRUE );
00384                         }
00385 
00386                         if ( m_bMultiple == FALSE )
00387                         {
00388                                 CMainWnd* pMainWnd = (CMainWnd*)AfxGetMainWnd();
00389                                 pMainWnd->m_pWindows.Open( RUNTIME_CLASS(CDownloadsWnd) );
00390 
00391                                 if ( Settings.Downloads.ShowMonitorURLs && m_pURLs.GetCount() == 1 )
00392                                 {
00393                                         CSingleLock pLock( &Transfers.m_pSection, TRUE );
00394                                         if ( Downloads.Check( pDownload ) ) pDownload->ShowMonitor( &pLock );
00395                                 }
00396                         }
00397                 }
00398                 else if ( pURL->m_nAction == CShareazaURL::uriHost )
00399                 {
00400                         Network.ConnectTo( pURL->m_sName, pURL->m_nPort );
00401                 }
00402                 else if ( pURL->m_nAction == CShareazaURL::uriDonkeyServer )
00403                 {
00404                         Network.ConnectTo( pURL->m_sName, pURL->m_nPort, PROTOCOL_ED2K );
00405                 }
00406                 else if ( pURL->m_nAction == CShareazaURL::uriBrowse )
00407                 {
00408                         SOCKADDR_IN pAddress;
00409 
00410                         if ( Network.Resolve( pURL->m_sName, pURL->m_nPort, &pAddress ) )
00411                         {
00412                                 new CBrowseHostWnd( &pAddress );
00413                         }
00414                 }
00415                 else if ( pURL->m_nAction == CShareazaURL::uriDiscovery )
00416                 {
00417                         DiscoveryServices.Add( pURL->m_sURL, (int)pURL->m_nSize );
00418                 }
00419         }
00420 
00421         CSkinDialog::OnOK();
00422 }
00423 
00424 void CURLActionDlg::OnUrlSearch()
00425 {
00426         Settings.General.AlwaysOpenURLs = m_bAlwaysOpen;
00427 
00428         for ( POSITION pos = m_pURLs.GetHeadPosition() ; pos ; )
00429         {
00430                 CShareazaURL* pURL = (CShareazaURL*)m_pURLs.GetNext( pos );
00431 
00432                 if ( pURL->m_nAction == CShareazaURL::uriHost )
00433                 {
00434                         SOCKADDR_IN pAddress;
00435 
00436                         if ( Network.Resolve( pURL->m_sName, pURL->m_nPort, &pAddress ) )
00437                         {
00438                                 new CBrowseHostWnd( &pAddress );
00439                         }
00440                 }
00441                 else if ( pURL->m_nAction == CShareazaURL::uriBrowse )
00442                 {
00443                         Network.ConnectTo( pURL->m_sName, pURL->m_nPort );
00444                 }
00445                 else if (       pURL->m_nAction == CShareazaURL::uriDownload ||
00446                                         pURL->m_nAction == CShareazaURL::uriSearch )
00447                 {
00448                         if ( ! Network.IsWellConnected() ) Network.Connect( TRUE );
00449 
00450                         CQuerySearch* pSearch = pURL->ToQuery();
00451 
00452                         new CSearchWnd( pSearch );
00453                 }
00454         }
00455 
00456         CSkinDialog::OnOK();
00457 }

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