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

PageSettingsDownloads.cpp

Go to the documentation of this file.
00001 //
00002 // PageSettingsDownloads.cpp
00003 //
00004 //      Date:                   "$Date: 2005/10/09 16:34:00 $"
00005 //      Revision:               "$Revision: 1.20 $"
00006 //  Last change by:     "$Author: mogthecat $"
00007 //
00008 // Copyright (c) Shareaza Development Team, 2002-2005.
00009 // This file is part of SHAREAZA (www.shareaza.com)
00010 //
00011 // Shareaza is free software; you can redistribute it
00012 // and/or modify it under the terms of the GNU General Public License
00013 // as published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Shareaza is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU General Public License
00022 // along with Shareaza; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024 //
00025 
00026 #include "StdAfx.h"
00027 #include "Shareaza.h"
00028 #include "Settings.h"
00029 #include "Library.h"
00030 #include "LibraryFolders.h"
00031 #include "SharedFolder.h"
00032 #include "Skin.h"
00033 #include "PageSettingsDownloads.h"
00034 
00035 #ifdef _DEBUG
00036 #define new DEBUG_NEW
00037 #undef THIS_FILE
00038 static char THIS_FILE[] = __FILE__;
00039 #endif
00040 
00041 IMPLEMENT_DYNCREATE(CDownloadsSettingsPage, CSettingsPage)
00042 
00043 BEGIN_MESSAGE_MAP(CDownloadsSettingsPage, CSettingsPage)
00044         //{{AFX_MSG_MAP(CDownloadsSettingsPage)
00045         ON_BN_CLICKED(IDC_DOWNLOADS_BROWSE, OnDownloadsBrowse)
00046         ON_BN_CLICKED(IDC_INCOMPLETE_BROWSE, OnIncompleteBrowse)
00047         //}}AFX_MSG_MAP
00048         ON_WM_SHOWWINDOW()
00049 END_MESSAGE_MAP()
00050 
00051 
00053 // CDownloadsSettingsPage property page
00054 
00055 CDownloadsSettingsPage::CDownloadsSettingsPage() : CSettingsPage(CDownloadsSettingsPage::IDD)
00056 {
00057         //{{AFX_DATA_INIT(CDownloadsSettingsPage)
00058         m_sDownloadsPath = _T("");
00059         m_sIncompletePath = _T("");
00060         m_nMaxDownFiles = 0;
00061         m_nMaxFileTransfers = 0;
00062         m_nMaxDownTransfers = 0;
00063         m_sBandwidthLimit = _T("");
00064         m_sQueueLimit = _T("");
00065         m_bRequireConnect = FALSE;
00066         //}}AFX_DATA_INIT
00067 }
00068 
00069 CDownloadsSettingsPage::~CDownloadsSettingsPage()
00070 {
00071 }
00072 
00073 void CDownloadsSettingsPage::DoDataExchange(CDataExchange* pDX)
00074 {
00075         CSettingsPage::DoDataExchange(pDX);
00076         //{{AFX_DATA_MAP(CDownloadsSettingsPage)
00077         DDX_Control(pDX, IDC_MAX_TRANSFERS_SPIN, m_wndMaxDownTransfers);
00078         DDX_Control(pDX, IDC_MAX_TPF_SPIN, m_wndMaxFileTransfers);
00079         DDX_Control(pDX, IDC_MAX_FILES_SPIN, m_wndMaxDownFiles);
00080         DDX_Control(pDX, IDC_INCOMPLETE_BROWSE, m_wndIncompletePath);
00081         DDX_Control(pDX, IDC_DOWNLOADS_BROWSE, m_wndDownloadsPath);
00082         DDX_Control(pDX, IDC_DOWNLOADS_BANDWIDTH_LIMIT, m_wndBandwidthLimit);
00083         DDX_Control(pDX, IDC_DOWNLOADS_QUEUE_LIMIT, m_wndQueueLimit);
00084         DDX_Text(pDX, IDC_DOWNLOADS_FOLDER, m_sDownloadsPath);
00085         DDX_Text(pDX, IDC_INCOMPLETE_FOLDER, m_sIncompletePath);
00086         DDX_Text(pDX, IDC_MAX_FILES, m_nMaxDownFiles);
00087         DDX_Text(pDX, IDC_MAX_TPF, m_nMaxFileTransfers);
00088         DDX_Text(pDX, IDC_MAX_TRANSFERS, m_nMaxDownTransfers);
00089         DDX_CBString(pDX, IDC_DOWNLOADS_BANDWIDTH_LIMIT, m_sBandwidthLimit);
00090         DDX_CBString(pDX, IDC_DOWNLOADS_QUEUE_LIMIT, m_sQueueLimit);
00091         DDX_Check(pDX, IDC_REQUIRE_CONNECT, m_bRequireConnect);
00092         //}}AFX_DATA_MAP
00093         
00094 }
00095 
00097 // CDownloadsSettingsPage message handlers
00098 
00099 BOOL CDownloadsSettingsPage::OnInitDialog() 
00100 {
00101         CSettingsPage::OnInitDialog();
00102         
00103         m_sDownloadsPath                = Settings.Downloads.CompletePath;
00104         m_sIncompletePath               = Settings.Downloads.IncompletePath;
00105         m_nMaxDownFiles                 = Settings.Downloads.MaxFiles;
00106         m_nMaxDownTransfers             = Settings.Downloads.MaxTransfers;
00107         m_nMaxFileTransfers             = Settings.Downloads.MaxFileTransfers;
00108         m_bRequireConnect               = Settings.Connection.RequireForTransfers;
00109         
00110 
00111         // Apply range limits to spin control
00112         if ( ! theApp.m_bNT )
00113         {
00114                 m_wndMaxDownFiles.SetRange( 1, 80 );
00115                 m_wndMaxDownTransfers.SetRange( 1, 80 );
00116                 m_wndMaxFileTransfers.SetRange( 1, 80 );
00117         }
00118         else
00119         {
00120                 m_wndMaxDownFiles.SetRange( 1, 100 );
00121                 if ( Settings.GetOutgoingBandwidth() >= 16 ) 
00122                         m_wndMaxDownTransfers.SetRange( 1, 250 );
00123                 else 
00124                         m_wndMaxDownTransfers.SetRange( 1, 200 );
00125                 m_wndMaxFileTransfers.SetRange( 1, 100 );
00126         }
00127         
00128         m_wndDownloadsPath.SetIcon( IDI_BROWSE );
00129         m_wndIncompletePath.SetIcon( IDI_BROWSE );
00130 
00131         if ( Settings.Downloads.QueueLimit )
00132                 m_sQueueLimit.Format( _T("%d"), Settings.Downloads.QueueLimit );
00133         else
00134                 m_sQueueLimit = _T("MAX");
00135 
00136         m_bDownloadsChanged = FALSE;
00137 
00138         
00139         // Update the text in the bandwidth limit combo
00140         if ( Settings.Bandwidth.Downloads )
00141         {
00142                 m_sBandwidthLimit = Settings.SmartVolume( Settings.Bandwidth.Downloads * 8, FALSE, TRUE );
00143         }
00144         else
00145         {
00146                 m_sBandwidthLimit       = Settings.SmartVolume( 0, FALSE, TRUE );
00147                 int nSpace                      = m_sBandwidthLimit.Find( ' ' );
00148                 m_sBandwidthLimit       = _T("MAX") + m_sBandwidthLimit.Mid( nSpace );
00149         }
00150         
00151         UpdateData( FALSE );
00152         
00153         return TRUE;
00154 }
00155 
00156 void CDownloadsSettingsPage::OnDownloadsBrowse() 
00157 {
00158         TCHAR szPath[MAX_PATH];
00159         LPITEMIDLIST pPath;
00160         LPMALLOC pMalloc;
00161         BROWSEINFO pBI;
00162                 
00163         ZeroMemory( &pBI, sizeof(pBI) );
00164         pBI.hwndOwner           = AfxGetMainWnd()->GetSafeHwnd();
00165         pBI.pszDisplayName      = szPath;
00166         pBI.lpszTitle           = _T("Select folder for downloads:");
00167         pBI.ulFlags                     = BIF_RETURNONLYFSDIRS;
00168         
00169         pPath = SHBrowseForFolder( &pBI );
00170 
00171         if ( pPath == NULL ) return;
00172 
00173         SHGetPathFromIDList( pPath, szPath );
00174         SHGetMalloc( &pMalloc );
00175         pMalloc->Free( pPath );
00176         pMalloc->Release();
00177         
00178         // Warn user about a path that's too long
00179         if ( _tcslen( szPath ) > 256 - 33 )
00180         {
00181                 CString strMessage;
00182                 LoadString( strMessage, IDS_SETTINGS_FILEPATH_TOO_LONG );
00183                 AfxMessageBox( strMessage, MB_ICONEXCLAMATION );
00184                 return;
00185         }
00186 
00187         // Make sure download/incomplete folders aren't the same
00188         if ( _tcsicmp( szPath, m_sIncompletePath ) == 0 )
00189         {
00190                 CString strMessage;
00191                 LoadString( strMessage, IDS_SETTINGS_FILEPATH_NOT_SAME );
00192                 AfxMessageBox( strMessage, MB_ICONEXCLAMATION );
00193                 return;
00194         }
00195         
00196         UpdateData( TRUE );
00197         m_sDownloadsPath = szPath;
00198         m_bDownloadsChanged = TRUE;
00199         UpdateData( FALSE );
00200 }
00201 
00202 void CDownloadsSettingsPage::OnIncompleteBrowse() 
00203 {
00204         TCHAR szPath[MAX_PATH];
00205         LPITEMIDLIST pPath;
00206         LPMALLOC pMalloc;
00207         BROWSEINFO pBI;
00208                 
00209         ZeroMemory( &pBI, sizeof(pBI) );
00210         pBI.hwndOwner           = AfxGetMainWnd()->GetSafeHwnd();
00211         pBI.pszDisplayName      = szPath;
00212         pBI.lpszTitle           = _T("Select folder for incomplete files:");
00213         pBI.ulFlags                     = BIF_RETURNONLYFSDIRS;
00214         
00215         pPath = SHBrowseForFolder( &pBI );
00216 
00217         if ( pPath == NULL ) return;
00218 
00219         SHGetPathFromIDList( pPath, szPath );
00220         SHGetMalloc( &pMalloc );
00221         pMalloc->Free( pPath );
00222         pMalloc->Release();
00223 
00224         // Warn user about a path that's too long
00225         if ( _tcslen( szPath ) > MAX_PATH - 60 )
00226         {
00227                 CString strMessage;
00228                 LoadString( strMessage, IDS_SETTINGS_FILEPATH_TOO_LONG );
00229                 AfxMessageBox( strMessage, MB_ICONEXCLAMATION );
00230                 return;
00231         }
00232 
00233         // Make sure download/incomplete folders aren't the same
00234         if ( _tcsicmp( szPath, m_sDownloadsPath ) == 0 )
00235         {
00236                 CString strMessage;
00237                 LoadString( strMessage, IDS_SETTINGS_FILEPATH_NOT_SAME );
00238                 AfxMessageBox( strMessage, MB_ICONEXCLAMATION );
00239                 return;
00240         }
00241 
00242         // Warn user about an incomplete folder in the library
00243         if ( LibraryFolders.IsFolderShared( szPath ) )
00244         {
00245                 CString strMessage;
00246                 LoadString( strMessage, IDS_SETTINGS_INCOMPLETE_LIBRARY );
00247                 AfxMessageBox( strMessage, MB_ICONEXCLAMATION );
00248                 return;
00249         }
00250 
00251         UpdateData( TRUE );
00252         m_sIncompletePath = szPath;
00253         UpdateData( FALSE );
00254 }
00255 
00256 BOOL CDownloadsSettingsPage::OnKillActive()
00257 {
00258         UpdateData();
00259         
00260         if ( ( ! IsNotLimited( m_sBandwidthLimit ) ) && Settings.ParseVolume( m_sBandwidthLimit, TRUE ) == 0 )
00261         {
00262                 CString strMessage;
00263                 LoadString( strMessage, IDS_SETTINGS_NEED_BANDWIDTH );
00264                 AfxMessageBox( strMessage, MB_ICONEXCLAMATION );
00265                 GetDlgItem( IDC_DOWNLOADS_BANDWIDTH_LIMIT )->SetFocus();
00266                 return FALSE;
00267         }
00268         
00269         return CSettingsPage::OnKillActive();
00270 }
00271 
00272 void CDownloadsSettingsPage::OnOK() 
00273 {
00274         DWORD nQueueLimit = 0;
00275         UpdateData( TRUE );
00276 
00277         // Figure out what the text in the queue limit box means
00278         if ( IsNotLimited( m_sQueueLimit ) )
00279         {
00280                 // Max queue is not limited
00281                 nQueueLimit = 0;
00282         }
00283         else
00284         {
00285                 // Max queue is limited, calculate number
00286                 int nPosition = 1, nCount = m_sQueueLimit.GetLength();
00287                 while ( nCount-- )
00288                 {
00289                         TCHAR cCharacter = m_sQueueLimit.GetAt( nCount );
00290                         if ( ( cCharacter >= '0' ) &&
00291                                  ( cCharacter <= '9' ) )
00292                         {
00293                                 nQueueLimit += ( ( cCharacter - '0') * nPosition );
00294                                 nPosition *= 10;
00295                         }
00296                 }
00297         }
00298 
00299         // Check the queue limit value is okay
00300         if ( ( nQueueLimit > 0 ) && ( nQueueLimit < 2000 ) && ( ! Settings.Live.QueueLimitWarning ) &&
00301                  ( Settings.eDonkey.EnableToday || Settings.eDonkey.EnableAlways ) && ( Settings.Downloads.QueueLimit != nQueueLimit ) )
00302         {
00303                 // Warn the user about setting the max queue wait limit too low
00304                 CString strMessage;
00305                 LoadString( strMessage, IDS_SETTINGS_WARN_QUEUELIMIT );
00306                                         
00307                 if ( AfxMessageBox( strMessage, MB_ICONQUESTION|MB_YESNO ) == IDNO )
00308                 {
00309                         nQueueLimit = 0;
00310                 }
00311                 else
00312                 {
00313                         // Don't need to warn the user again.
00314                         Settings.Live.QueueLimitWarning = TRUE;
00315                 }
00316         }
00317 
00318         // Redraw the text in the queue limit box (in case the limit changed)
00319         if ( nQueueLimit > 0 )
00320                 m_sQueueLimit.Format( _T("%d"), nQueueLimit );
00321         else
00322                 m_sQueueLimit = _T("MAX");
00323 
00324         // Apply limits to display
00325         if ( ! theApp.m_bNT )
00326         {
00327                 // Win9x is unable to handle high numbers of connections
00328                 m_nMaxDownFiles = min ( m_nMaxDownFiles, 80 );
00329                 m_nMaxDownTransfers = min ( m_nMaxDownTransfers, 80 );
00330                 m_nMaxFileTransfers = min ( m_nMaxFileTransfers, 80 );
00331         }
00332         else
00333         {
00334                 // For other systems we can guestimate a good value based on available bandwidth
00335         m_nMaxDownFiles = min ( m_nMaxDownFiles, 100 );
00336         if ( Settings.GetOutgoingBandwidth() < 16 )
00337                 m_nMaxDownTransfers = min ( m_nMaxDownTransfers, 200 );
00338                 else if ( Settings.GetOutgoingBandwidth() <= 32 )
00339                 m_nMaxDownTransfers = min ( m_nMaxDownTransfers, 250 );
00340                 else if ( Settings.GetOutgoingBandwidth() <= 64 )
00341                         m_nMaxDownTransfers = min ( m_nMaxDownTransfers, 400 );
00342                 else if ( Settings.GetOutgoingBandwidth() <= 128 )
00343                         m_nMaxDownTransfers = min ( m_nMaxDownTransfers, 600 );
00344         else
00345                 m_nMaxDownTransfers = min ( m_nMaxDownTransfers, 800 );
00346         m_nMaxFileTransfers = min ( m_nMaxFileTransfers, 100 );
00347         }
00348 
00349         // Display any data changes
00350         UpdateData( FALSE );
00351 
00352         // Put new values in the settings.
00353         Settings.Downloads.CompletePath                 = m_sDownloadsPath;
00354         Settings.Downloads.IncompletePath               = m_sIncompletePath;
00355         Settings.Downloads.MaxFiles                             = m_nMaxDownFiles;
00356         Settings.Downloads.MaxTransfers                 = m_nMaxDownTransfers;
00357         Settings.Downloads.MaxFileTransfers             = m_nMaxFileTransfers;
00358         Settings.Downloads.QueueLimit                   = nQueueLimit;
00359         Settings.Bandwidth.Downloads                    = (DWORD)Settings.ParseVolume( m_sBandwidthLimit, TRUE ) / 8;
00360         Settings.Connection.RequireForTransfers = m_bRequireConnect;
00361         
00362 
00363 
00364         CreateDirectory( m_sDownloadsPath, NULL );
00365         CreateDirectory( m_sIncompletePath, NULL );
00366         //CreateDirectory( m_sTorrentPath, NULL );
00367         
00368         if ( m_bDownloadsChanged )
00369         {
00370                 if ( LibraryFolders.GetFolderCount() == 0 )
00371                 {
00372                         LibraryFolders.AddFolder( m_sDownloadsPath );
00373                 }
00374                 else if ( ! LibraryFolders.IsFolderShared( m_sDownloadsPath ) )
00375                 {
00376                         CString strFormat, strMessage;
00377                         
00378                         LoadString( strFormat, IDS_LIBRARY_DOWNLOADS_ADD );
00379                         strMessage.Format( strFormat, (LPCTSTR)m_sDownloadsPath );
00380                         
00381                         if ( AfxMessageBox( strMessage, MB_ICONQUESTION|MB_YESNO ) == IDYES )
00382                         {
00383                                 CLibraryFolder* pFolder = LibraryFolders.AddFolder( m_sDownloadsPath );
00384                                 
00385                                 if ( pFolder )
00386                                 {
00387                                         LoadString( strMessage, IDS_LIBRARY_DOWNLOADS_SHARE );
00388                                         
00389                                         BOOL bShare = AfxMessageBox( strMessage, MB_ICONQUESTION|MB_YESNO ) == IDYES;
00390                                         
00391                                         CQuickLock oLock( Library.m_pSection );
00392                                         if ( LibraryFolders.CheckFolder( pFolder, TRUE ) )
00393                                                 pFolder->m_bShared = bShare ? TS_TRUE : TS_FALSE;
00394                                         Library.Update();
00395                                 }
00396                         }
00397                 }
00398         }
00399         
00400         CSettingsPage::OnOK();
00401 }
00402 
00403 void CDownloadsSettingsPage::OnShowWindow(BOOL bShow, UINT nStatus)
00404 {
00405         CSettingsPage::OnShowWindow(bShow, nStatus);
00406         if ( bShow )
00407         {
00408                 // Update the bandwidth limit combo values
00409 
00410                 // Remove any existing strings
00411                 while ( m_wndBandwidthLimit.GetCount() ) m_wndBandwidthLimit.DeleteString( 0 );
00412                 // Add the new ones
00413                 m_wndBandwidthLimit.AddString( Settings.SmartVolume( Settings.Connection.InSpeed / 4, TRUE, TRUE ) );
00414                 m_wndBandwidthLimit.AddString( Settings.SmartVolume( Settings.Connection.InSpeed / 2, TRUE, TRUE ) );
00415                 m_wndBandwidthLimit.AddString( Settings.SmartVolume( (Settings.Connection.InSpeed/2)+(Settings.Connection.InSpeed/4), TRUE, TRUE ) );
00416                 m_wndBandwidthLimit.AddString( Settings.SmartVolume( Settings.Connection.InSpeed, TRUE, TRUE ) );
00417                 m_wndBandwidthLimit.AddString( _T("MAX") );
00418 
00419                 // Update the queue limit combo values
00420 
00421                 // Remove any existing strings
00422                 while ( m_wndQueueLimit.GetCount() ) m_wndQueueLimit.DeleteString( 0 );
00423                 // Add the new ones
00424                 if ( Settings.eDonkey.EnableToday || Settings.eDonkey.EnableAlways )
00425                 {
00426                         m_wndQueueLimit.AddString( _T("2000") );
00427                         m_wndQueueLimit.AddString( _T("5000") );
00428                         m_wndQueueLimit.AddString( _T("10000") );
00429                         m_wndQueueLimit.AddString( _T("MAX") );
00430                 }
00431                 else
00432                 {
00433                         m_wndQueueLimit.AddString( _T("5") );
00434                         m_wndQueueLimit.AddString( _T("10") );
00435                         m_wndQueueLimit.AddString( _T("20") );
00436                         m_wndQueueLimit.AddString( _T("MAX") );
00437                 }
00438 
00439                 UpdateData( FALSE );
00440         }
00441 }
00442 
00443 BOOL CDownloadsSettingsPage::IsNotLimited(LPCTSTR pText)
00444 {
00445         if ( ( _tcslen( pText ) == 0 ) ||
00446                  ( _tcsistr( pText, _T("MAX") ) != NULL ) || 
00447                  ( _tcsistr( pText, _T("NONE") ) != NULL ) )
00448                 return TRUE;
00449         else
00450                 return FALSE;
00451 }

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