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

PageSettingsBandwidth.cpp

Go to the documentation of this file.
00001 //
00002 // PageSettingsBandwidth.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 "PageSettingsBandwidth.h"
00026 #include "PageSettingsGnutella.h"
00027 #include "PageSettingsDownloads.h"
00028 
00029 #ifdef _DEBUG
00030 #define new DEBUG_NEW
00031 #undef THIS_FILE
00032 static char THIS_FILE[] = __FILE__;
00033 #endif
00034 
00035 IMPLEMENT_DYNCREATE(CBandwidthSettingsPage, CSettingsPage)
00036 
00037 BEGIN_MESSAGE_MAP(CBandwidthSettingsPage, CSettingsPage)
00038         ON_WM_TIMER()
00039         ON_WM_CTLCOLOR()
00040 END_MESSAGE_MAP()
00041 
00042 
00044 // CBandwidthSettingsPage property page
00045 
00046 CBandwidthSettingsPage::CBandwidthSettingsPage() : CSettingsPage(CBandwidthSettingsPage::IDD)
00047 {
00048         m_sUPInLimit = _T("");
00049         m_sUPInMax = _T("");
00050         m_sUPInTotal = _T("");
00051         m_sUPOutLimit = _T("");
00052         m_sUPOutMax = _T("");
00053         m_sUPOutTotal = _T("");
00054         m_sDLTotal = _T("");
00055         m_sInTotal = _T("");
00056         m_sOutTotal = _T("");
00057         m_sPInTotal = _T("");
00058         m_sPOutLimit = _T("");
00059         m_sPOutMax = _T("");
00060         m_sPOutTotal = _T("");
00061         m_sULTotal = _T("");
00062         m_sPInLimit = _T("");
00063         m_sPInMax = _T("");
00064         m_sLInLimit = _T("");
00065         m_sLInMax = _T("");
00066         m_sLInTotal = _T("");
00067         m_sLOutLimit = _T("");
00068         m_sLOutMax = _T("");
00069         m_sLOutTotal = _T("");
00070         m_sUDPTotal = _T("");
00071         m_bActive = FALSE;
00072 }
00073 
00074 CBandwidthSettingsPage::~CBandwidthSettingsPage()
00075 {
00076 }
00077 
00078 void CBandwidthSettingsPage::DoDataExchange(CDataExchange* pDX)
00079 {
00080         CSettingsPage::DoDataExchange( pDX );
00081         DDX_Control(pDX, IDC_HEAD_TRANSMIT, m_wndHeadTransmit);
00082         DDX_Control(pDX, IDC_HEAD_RECEIVE, m_wndHeadReceive);
00083         DDX_Text(pDX, IDC_UPIN_LIMIT, m_sUPInLimit);
00084         DDX_Text(pDX, IDC_UPIN_MAX, m_sUPInMax);
00085         DDX_Text(pDX, IDC_UPIN_TOTAL, m_sUPInTotal);
00086         DDX_Text(pDX, IDC_UPOUT_LIMIT, m_sUPOutLimit);
00087         DDX_Text(pDX, IDC_UPOUT_MAX, m_sUPOutMax);
00088         DDX_Text(pDX, IDC_UPOUT_TOTAL, m_sUPOutTotal);
00089         DDX_Text(pDX, IDC_DL_TOTAL, m_sDLTotal);
00090         DDX_Text(pDX, IDC_IN_TOTAL, m_sInTotal);
00091         DDX_Text(pDX, IDC_OUT_TOTAL, m_sOutTotal);
00092         DDX_Text(pDX, IDC_PIN_TOTAL, m_sPInTotal);
00093         DDX_Text(pDX, IDC_POUT_LIMIT, m_sPOutLimit);
00094         DDX_Text(pDX, IDC_POUT_MAX, m_sPOutMax);
00095         DDX_Text(pDX, IDC_POUT_TOTAL, m_sPOutTotal);
00096         DDX_Text(pDX, IDC_UL_TOTAL, m_sULTotal);
00097         DDX_Text(pDX, IDC_PIN_LIMIT, m_sPInLimit);
00098         DDX_Text(pDX, IDC_PIN_MAX, m_sPInMax);
00099         DDX_Text(pDX, IDC_LIN_LIMIT, m_sLInLimit);
00100         DDX_Text(pDX, IDC_LIN_MAX, m_sLInMax);
00101         DDX_Text(pDX, IDC_LIN_TOTAL, m_sLInTotal);
00102         DDX_Text(pDX, IDC_LOUT_LIMIT, m_sLOutLimit);
00103         DDX_Text(pDX, IDC_LOUT_MAX, m_sLOutMax);
00104         DDX_Text(pDX, IDC_LOUT_TOTAL, m_sLOutTotal);
00105         DDX_Text(pDX, IDC_UDP_TOTAL, m_sUDPTotal);
00106 }
00107 
00109 // CBandwidthSettingsPage message handlers
00110 
00111 BOOL CBandwidthSettingsPage::OnInitDialog()
00112 {
00113         CSettingsPage::OnInitDialog();
00114 
00115         /*
00116         for ( CWnd* pWnd = GetWindow( GW_CHILD ) ; pWnd ; pWnd = pWnd->GetNextWindow() )
00117         {
00118                 TCHAR szClass[64];
00119                 GetClassName( *pWnd, szClass, 64 );
00120 
00121                 if ( _tcsistr( szClass, _T("UPDOWN") ) )
00122                 {
00123                         theApp.Message( MSG_DEBUG, _T("Class : %s"), szClass );
00124                         CSpinButtonCtrl* pSpin = reinterpret_cast<CSpinButtonCtrl*>(pWnd);
00125                         pSpin->SetRange( 0, 0x7FFF );
00126                 }
00127         }
00128         */
00129 
00130         m_bBytes                = Settings.General.RatesInBytes;
00131         m_sUPInLimit    = ToString( Settings.Bandwidth.HubIn, TRUE, TRUE );
00132         m_sUPOutLimit   = ToString( Settings.Bandwidth.HubOut, TRUE, TRUE );
00133         m_sLInLimit             = ToString( Settings.Bandwidth.LeafIn, TRUE, TRUE );
00134         m_sLOutLimit    = ToString( Settings.Bandwidth.LeafOut, TRUE, TRUE );
00135         m_sPInLimit             = ToString( Settings.Bandwidth.PeerIn, TRUE, TRUE );
00136         m_sPOutLimit    = ToString( Settings.Bandwidth.PeerOut, TRUE, TRUE );
00137         m_sDLTotal              = ToString( Settings.Bandwidth.Downloads, TRUE, TRUE );
00138         m_sULTotal              = ToString( Settings.Bandwidth.Uploads, TRUE, TRUE );
00139         m_sUDPTotal             = ToString( Settings.Bandwidth.UdpOut, TRUE, TRUE );
00140 
00141         Calculate( TRUE );
00142 
00143         return TRUE;
00144 }
00145 
00146 BOOL CBandwidthSettingsPage::OnSetActive()
00147 {
00148         Calculate( TRUE );
00149         return CSettingsPage::OnSetActive();
00150 }
00151 
00152 void CBandwidthSettingsPage::Calculate(BOOL bForeward)
00153 {
00154         int nNumHubs    = Settings.Gnutella1.NumHubs  + Settings.Gnutella2.NumHubs;
00155         int nNumLeafs   = Settings.Gnutella1.NumLeafs + Settings.Gnutella2.NumLeafs;
00156         int nNumPeers   = Settings.Gnutella1.NumPeers + Settings.Gnutella2.NumPeers;
00157 
00158         CGnutellaSettingsPage* ppGnutella =
00159                 (CGnutellaSettingsPage*)GetPage( RUNTIME_CLASS(CGnutellaSettingsPage) );
00160 
00161         if ( ppGnutella->GetSafeHwnd() )
00162         {
00163                 ppGnutella->UpdateData();
00164                 nNumHubs        = min( ppGnutella->m_nG2Hubs, 3 ) + min( ppGnutella->m_nG1Hubs, 10 );
00165                 nNumLeafs       = ppGnutella->m_nG1Leafs + ppGnutella->m_nG2Leafs;
00166                 nNumPeers       = ppGnutella->m_nG1Peers + ppGnutella->m_nG2Peers;
00167         }
00168 
00169         Calculate( m_sUPInLimit, nNumHubs, m_sUPInMax, m_sUPInTotal, bForeward );
00170         Calculate( m_sLInLimit, nNumLeafs, m_sLInMax, m_sLInTotal, bForeward );
00171         Calculate( m_sPInLimit, nNumPeers, m_sPInMax, m_sPInTotal, bForeward );
00172         // Calculate( m_sDLLimit, nDownloads, m_sDLMax, m_sDLTotal, bForeward );
00173 
00174         m_sInTotal = ToString( AddString( m_sUPInTotal + ';' + m_sLInTotal + ';' + m_sPInTotal + ';' + m_sDLTotal ), TRUE, TRUE );
00175 
00176         Calculate( m_sUPOutLimit, nNumHubs, m_sUPOutMax, m_sUPOutTotal, bForeward );
00177         Calculate( m_sLOutLimit, nNumLeafs, m_sLOutMax, m_sLOutTotal, bForeward );
00178         Calculate( m_sPOutLimit, nNumPeers, m_sPOutMax, m_sPOutTotal, bForeward );
00179         // Calculate( m_sULLimit, nUploads, m_sULMax, m_sULTotal, bForeward );
00180 
00181         // CString strDummy;
00182         // Calculate( m_sUDPLimit, 1, strDummy, m_sUDPTotal, bForeward );
00183 
00184         m_sOutTotal = ToString( AddString( m_sUPOutTotal + ';' + m_sLOutTotal + ';' + m_sPOutTotal + ';' + m_sULTotal + ';' + m_sUDPTotal ), TRUE, TRUE );
00185 
00186         m_bActive = FALSE;
00187         UpdateData( FALSE );
00188         m_bActive = TRUE;
00189 }
00190 
00191 void CBandwidthSettingsPage::Calculate(CString& strLimit, int nCount, CString& strCount, CString& strTotal, BOOL bForeward)
00192 {
00193         strCount.Format( _T("%lu"), nCount );
00194 
00195         if ( bForeward )
00196         {
00197                 DWORD nSpeed = ToSpeed( strLimit ) * nCount;
00198                 strTotal = ToString( nSpeed, TRUE, TRUE );
00199         }
00200         else
00201         {
00202                 DWORD nSpeed = nCount ? ( ToSpeed( strTotal ) / nCount ) : 0;
00203                 strLimit = ToString( nSpeed, TRUE, TRUE );
00204         }
00205 }
00206 
00207 DWORD CBandwidthSettingsPage::ToSpeed(CString& str)
00208 {
00209         float nSpeed = 0;
00210         _stscanf( str, _T("%f"), &nSpeed );
00211         nSpeed *= 1024.0f;
00212         if ( ! m_bBytes ) nSpeed /= 8.0f;
00213         return (DWORD)nSpeed;
00214 }
00215 
00216 CString CBandwidthSettingsPage::ToString(DWORD nSpeed, BOOL bUnlimited, BOOL bUnit)
00217 {
00218         CString str;
00219 
00220         if ( nSpeed || ! bUnlimited )
00221         {
00222                 float nValue = (float)nSpeed / 1024.0f;
00223                 if ( ! m_bBytes ) nValue *= 8.0f;
00224                 str.Format( _T("%.1f"), double( nValue ) );
00225 
00226                 if ( bUnit ) str += ( m_bBytes ) ? _T(" KB/s") : _T(" Kb/s");
00227         }
00228         else
00229         {
00230                 str = _T("U/L");
00231         }
00232 
00233         return str;
00234 }
00235 
00236 DWORD CBandwidthSettingsPage::AddString(CString& str)
00237 {
00238         DWORD nTotal = 0;
00239 
00240         for ( str += ',' ; str.GetLength() ; )
00241         {
00242                 CString num = str.SpanExcluding( _T(";") );
00243                 str = str.Mid( num.GetLength() + 1 );
00244 
00245                 if ( num.GetLength() )
00246                 {
00247                         DWORD nSpeed = ToSpeed( num );
00248                         if ( ! nSpeed ) return 0;
00249                         nTotal += nSpeed;
00250                 }
00251         }
00252 
00253         return nTotal;
00254 }
00255 
00256 void CBandwidthSettingsPage::SwapBytesBits(CString& str)
00257 {
00258         m_bBytes = ! m_bBytes;
00259         DWORD nSpeed = ToSpeed( str );
00260         m_bBytes = ! m_bBytes;
00261         str = ToString( nSpeed, TRUE );
00262 }
00263 
00264 BOOL CBandwidthSettingsPage::OnCommand(WPARAM wParam, LPARAM lParam)
00265 {
00266         if ( HIWORD( wParam ) == EN_CHANGE && m_bActive )
00267         {
00268                 switch ( LOWORD( wParam ) )
00269                 {
00270                 case IDC_UPIN_LIMIT:
00271                 case IDC_LIN_LIMIT:
00272                 case IDC_PIN_LIMIT:
00273 //              case IDC_DL_LIMIT:
00274                 case IDC_UPOUT_LIMIT:
00275                 case IDC_LOUT_LIMIT:
00276                 case IDC_POUT_LIMIT:
00277                         PostMessage( WM_TIMER, 1 );
00278                         break;
00279                 case IDC_UPIN_TOTAL:
00280                 case IDC_LIN_TOTAL:
00281                 case IDC_PIN_TOTAL:
00282                 case IDC_DL_TOTAL:
00283                 case IDC_UPOUT_TOTAL:
00284                 case IDC_LOUT_TOTAL:
00285                 case IDC_POUT_TOTAL:
00286                 case IDC_UL_TOTAL:
00287                 case IDC_UDP_TOTAL:
00288                         PostMessage( WM_TIMER, 2 );
00289                         break;
00290                 }
00291         }
00292 
00293         return CSettingsPage::OnCommand( wParam, lParam );
00294 }
00295 
00296 BOOL CBandwidthSettingsPage::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
00297 {
00298         NM_UPDOWN* pNotify = reinterpret_cast<NM_UPDOWN*>( lParam );
00299 
00300         if ( pNotify->hdr.code == UDN_DELTAPOS )
00301         {
00302                 CWnd* pWnd              = CWnd::FromHandle( pNotify->hdr.hwndFrom );
00303                 CEdit* pEdit    = reinterpret_cast<CEdit*>( pWnd->GetNextWindow( GW_HWNDPREV ) );
00304                 CString str;
00305 
00306                 pEdit->GetWindowText( str );
00307                 DWORD nSpeed = ToSpeed( str );
00308 
00309                 if ( m_bBytes )
00310                         nSpeed -= pNotify->iDelta * 103;
00311                 else
00312                         nSpeed -= pNotify->iDelta * 128;
00313 
00314                 if ( nSpeed > 0xF0000000 ) nSpeed = 0;
00315 
00316                 str = ToString( nSpeed, TRUE, TRUE );
00317                 pEdit->SetWindowText( str );
00318 
00319                 return TRUE;
00320         }
00321 
00322         return CSettingsPage::OnNotify( wParam, lParam, pResult );
00323 }
00324 
00325 void CBandwidthSettingsPage::OnTimer(UINT nIDEvent)
00326 {
00327         UpdateData( TRUE );
00328         Calculate( nIDEvent == 1 );
00329 }
00330 
00331 HBRUSH CBandwidthSettingsPage::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
00332 {
00333         HBRUSH hbr = CSettingsPage::OnCtlColor(pDC, pWnd, nCtlColor);
00334 
00335         if ( pWnd == &m_wndHeadReceive || pWnd == &m_wndHeadTransmit )
00336         {
00337                 pDC->SelectObject( &theApp.m_gdiFontBold );
00338         }
00339 
00340         return hbr;
00341 }
00342 
00343 void CBandwidthSettingsPage::OnOK()
00344 {
00345         UpdateData();
00346 
00347         Settings.Bandwidth.HubIn                = ToSpeed( m_sUPInLimit );
00348         Settings.Bandwidth.HubOut               = ToSpeed( m_sUPOutLimit );
00349         Settings.Bandwidth.LeafIn               = ToSpeed( m_sLInLimit );
00350         Settings.Bandwidth.LeafOut              = ToSpeed( m_sLOutLimit );
00351         Settings.Bandwidth.PeerIn               = ToSpeed( m_sPInLimit );
00352         Settings.Bandwidth.PeerOut              = ToSpeed( m_sPOutLimit );
00353         Settings.Bandwidth.Downloads    = ToSpeed( m_sDLTotal );
00354         Settings.Bandwidth.Uploads              = ToSpeed( m_sULTotal );
00355         Settings.Bandwidth.UdpOut               = ToSpeed( m_sUDPTotal );
00356 
00357         CSettingsPage::OnOK();
00358 }
00359 

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