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

PageSettingsUploads.cpp

Go to the documentation of this file.
00001 //
00002 // PageSettingsUploads.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 "Transfers.h"
00026 #include "UploadQueue.h"
00027 #include "UploadQueues.h"
00028 #include "PageSettingsUploads.h"
00029 #include "DlgQueueProperties.h"
00030 #include "CoolInterface.h"
00031 #include "LiveList.h"
00032 #include "Skin.h"
00033 #include "DlgHelp.h"
00034 
00035 #include "LibraryDictionary.h"
00036 
00037 IMPLEMENT_DYNCREATE(CUploadsSettingsPage, CSettingsPage)
00038 
00039 BEGIN_MESSAGE_MAP(CUploadsSettingsPage, CSettingsPage)
00040         ON_CBN_SELCHANGE(IDC_AGENT_LIST, OnSelChangeAgentList)
00041         ON_CBN_EDITCHANGE(IDC_AGENT_LIST, OnEditChangeAgentList)
00042         ON_BN_CLICKED(IDC_AGENT_ADD, OnAgentAdd)
00043         ON_BN_CLICKED(IDC_AGENT_REMOVE, OnAgentRemove)
00044         ON_NOTIFY(LVN_ITEMCHANGED, IDC_QUEUES, OnItemChangedQueues)
00045         ON_BN_CLICKED(IDC_QUEUE_NEW, OnQueueNew)
00046         ON_BN_CLICKED(IDC_QUEUE_EDIT, OnQueueEdit)
00047         ON_BN_CLICKED(IDC_QUEUE_DELETE, OnQueueDelete)
00048         ON_NOTIFY(NM_DBLCLK, IDC_QUEUES, OnDblClkQueues)
00049         ON_NOTIFY(LVN_DRAGDROP, IDC_QUEUES, OnQueueDrop)
00050         ON_WM_SHOWWINDOW()
00051 END_MESSAGE_MAP()
00052 
00053 
00055 // CUploadsSettingsPage property page
00056 
00057 CUploadsSettingsPage::CUploadsSettingsPage() : CSettingsPage( CUploadsSettingsPage::IDD )
00058 {
00059         m_bSharePartials = FALSE;
00060         m_nMaxPerHost = 0;
00061         m_bHubUnshare = FALSE;
00062         m_bSharePreviews = FALSE;
00063         m_bThrottleMode = FALSE;
00064 }
00065 
00066 CUploadsSettingsPage::~CUploadsSettingsPage()
00067 {
00068 }
00069 
00070 void CUploadsSettingsPage::DoDataExchange(CDataExchange* pDX)
00071 {
00072         CSettingsPage::DoDataExchange(pDX);
00073         DDX_Check(pDX, IDC_SHARE_PARTIALS, m_bSharePartials);
00074         DDX_Check(pDX, IDC_HUB_UNSHARE, m_bHubUnshare);
00075         DDX_Check(pDX, IDC_SHARE_PREVIEW, m_bSharePreviews);
00076         DDX_Text(pDX, IDC_MAX_HOST, m_nMaxPerHost);
00077         DDX_Control(pDX, IDC_MAX_HOST_SPIN, m_wndMaxPerHost);
00078         DDX_Control(pDX, IDC_AGENT_LIST, m_wndAgentList);
00079         DDX_Control(pDX, IDC_AGENT_ADD, m_wndAgentAdd);
00080         DDX_Control(pDX, IDC_AGENT_REMOVE, m_wndAgentRemove);
00081 
00082         DDX_Control(pDX, IDC_UPLOADS_BANDWIDTH_LIMIT, m_wndBandwidthLimit);
00083         DDX_CBString(pDX, IDC_UPLOADS_BANDWIDTH_LIMIT, m_sBandwidthLimit);
00084         DDX_CBIndex(pDX, IDC_THROTTLE_MODE, m_bThrottleMode);
00085 
00086         DDX_Control(pDX, IDC_QUEUES, m_wndQueues);
00087         DDX_Control(pDX, IDC_QUEUE_EDIT, m_wndQueueEdit);
00088         DDX_Control(pDX, IDC_QUEUE_DELETE, m_wndQueueDelete);
00089 
00090 
00091 
00092 }
00093 
00095 // CUploadsSettingsPage message handlers
00096 
00097 BOOL CUploadsSettingsPage::OnInitDialog() 
00098 {
00099         CSettingsPage::OnInitDialog();
00100         
00101         m_wndMaxPerHost.SetRange( 1, 64 );
00102         
00103         CRect rcList;
00104         m_wndQueues.GetClientRect( &rcList );
00105         rcList.right -= GetSystemMetrics( SM_CXVSCROLL );
00106         
00107         m_wndQueues.SetImageList( &CoolInterface.m_pImages, LVSIL_SMALL );
00108         
00109         m_wndQueues.InsertColumn( 0, _T("Name"), LVCFMT_LEFT, rcList.right - 100 - 70 - 70, -1 );
00110         m_wndQueues.InsertColumn( 1, _T("Criteria"), LVCFMT_LEFT, 100, 0 );
00111         m_wndQueues.InsertColumn( 2, _T("Bandwidth"), LVCFMT_CENTER, 70, 1 );
00112         m_wndQueues.InsertColumn( 3, _T("Transfers"), LVCFMT_CENTER, 70, 2 );
00113         m_wndQueues.InsertColumn( 4, _T("Order"), LVCFMT_CENTER, 0, 3 );
00114         Skin.Translate( _T("CUploadQueueList"), m_wndQueues.GetHeaderCtrl() );
00115         
00116         m_wndQueues.SendMessage( LVM_SETEXTENDEDLISTVIEWSTYLE,
00117                 LVS_EX_FULLROWSELECT|LVS_EX_LABELTIP, LVS_EX_FULLROWSELECT|LVS_EX_LABELTIP );
00118         m_wndQueues.EnableToolTips();
00119         
00120         CLiveList::Sort( &m_wndQueues, 4, FALSE );
00121         CLiveList::Sort( &m_wndQueues, 4, FALSE );
00122         
00123         m_nMaxPerHost           = Settings.Uploads.MaxPerHost;
00124         m_bSharePartials        = Settings.Uploads.SharePartials;
00125         m_bSharePreviews        = Settings.Uploads.SharePreviews;
00126         m_bHubUnshare           = Settings.Uploads.HubUnshare;
00127         m_bThrottleMode         = Settings.Uploads.ThrottleMode;
00128         
00129         for ( CString strList = Settings.Uploads.BlockAgents + '|' ; strList.GetLength() ; )
00130         {
00131                 CString strType = strList.SpanExcluding( _T("|") );
00132                 strList = strList.Mid( strType.GetLength() + 1 );
00133                 strType.TrimLeft();
00134                 strType.TrimRight();
00135                 if ( strType.GetLength() ) m_wndAgentList.AddString( strType );
00136         }
00137         
00138         UpdateData( FALSE );
00139         UpdateQueues();
00140         
00141         m_wndAgentAdd.EnableWindow( m_wndAgentList.GetWindowTextLength() > 0 );
00142         m_wndAgentRemove.EnableWindow( m_wndAgentList.GetCurSel() >= 0 );
00143         m_wndQueueEdit.EnableWindow( m_wndQueues.GetSelectedCount() == 1 );
00144         m_wndQueueDelete.EnableWindow( m_wndQueues.GetSelectedCount() > 0 );
00145 
00146         m_bQueuesChanged = FALSE;
00147 
00148         // Update value in limit combo box
00149         if ( Settings.Bandwidth.Uploads )
00150         {
00151                 m_sBandwidthLimit = Settings.SmartVolume( Settings.Bandwidth.Uploads * 8, FALSE, TRUE );
00152         }
00153         else
00154         {
00155                 m_sBandwidthLimit       = Settings.SmartVolume( 0, FALSE, TRUE );
00156                 int nSpace                      = m_sBandwidthLimit.Find( ' ' );
00157                 m_sBandwidthLimit       = _T("MAX") + m_sBandwidthLimit.Mid( nSpace );
00158         }
00159 
00160         UpdateData( FALSE );
00161         
00162         return TRUE;
00163 }
00164 
00165 BOOL CUploadsSettingsPage::OnSetActive() 
00166 {
00167         UpdateQueues();
00168         return CSettingsPage::OnSetActive();
00169 }
00170 
00171 void CUploadsSettingsPage::UpdateQueues()
00172 {
00173         UpdateData( TRUE );
00174         
00175         DWORD nTotal = Settings.Connection.OutSpeed * 1024 / 8;
00176         DWORD nLimit = (DWORD)Settings.ParseVolume( m_sBandwidthLimit, TRUE ) / 8;
00177         
00178         if ( nLimit == 0 || nLimit > nTotal ) nLimit = nTotal;
00179         
00180         CSingleLock pLock( &UploadQueues.m_pSection, TRUE );
00181         CLiveList pQueues( 5 );
00182         int nIndex = 1;
00183         
00184         for ( POSITION pos = UploadQueues.GetIterator() ; pos ; nIndex++ )
00185         {
00186                 BOOL bDonkeyOnlyDisabled = FALSE;
00187 
00188                 CUploadQueue* pQueue = UploadQueues.GetNext( pos );
00189 
00190                 // If this queue is for ed2k only and we have to be connected to upload
00191                 if ( ( ( pQueue->m_nProtocols & ( 1 << PROTOCOL_ED2K ) ) != 0 ) && ( Settings.Connection.RequireForTransfers ) )
00192                 {       // Then the queue is inactive if we haven't got ed2k enabled
00193                         bDonkeyOnlyDisabled = !( Settings.eDonkey.EnableAlways | Settings.eDonkey.EnableToday );
00194                 }
00195 
00196                 // If the queue is inactive and we're in basic GUI mode
00197                 if( ( bDonkeyOnlyDisabled ) && (Settings.General.GUIMode == GUI_BASIC) )
00198                         continue;       // Skip drawing this queue
00199 
00200                 CLiveItem* pItem = pQueues.Add( pQueue );
00201                 
00202                 if( ( pQueue->m_bEnable ) && ( ! bDonkeyOnlyDisabled ) )
00203                 {
00204                         DWORD nBandwidth = nLimit * pQueue->m_nBandwidthPoints / max( 1, UploadQueues.GetTotalBandwidthPoints( TRUE ) );
00205                         pItem->Set( 2, Settings.SmartVolume( nBandwidth * 8, FALSE, TRUE ) + '+' );
00206                         pItem->Format( 3, _T("%i-%i"), pQueue->m_nMinTransfers, pQueue->m_nMaxTransfers );
00207 
00208                         pItem->m_nImage = CoolInterface.ImageForID( ID_VIEW_UPLOADS );
00209                 }
00210                 else
00211                 {
00212                         pItem->Set( 2, _T("- ") );
00213                         pItem->Format( 3, _T("-"));
00214 
00215                         pItem->m_nImage = CoolInterface.ImageForID( ID_SYSTEM_CLEAR );
00216                 }
00217                 
00218                 pItem->Set( 0, pQueue->m_sName );
00219                 pItem->Set( 1, pQueue->GetCriteriaString() );
00220                 
00221                 pItem->Format( 4, _T("%i"), nIndex );
00222                  
00223         }
00224         
00225         pLock.Unlock();
00226         pQueues.Apply( &m_wndQueues, TRUE );
00227 }
00228 
00229 void CUploadsSettingsPage::OnSelChangeAgentList() 
00230 {
00231         m_wndAgentRemove.EnableWindow( m_wndAgentList.GetCurSel() >= 0 );
00232 }
00233 
00234 void CUploadsSettingsPage::OnEditChangeAgentList() 
00235 {
00236         m_wndAgentAdd.EnableWindow( m_wndAgentList.GetWindowTextLength() > 0 );
00237 }
00238 
00239 void CUploadsSettingsPage::OnAgentAdd() 
00240 {
00241         CString strType;
00242         m_wndAgentList.GetWindowText( strType );
00243 
00244         CharLower( strType.GetBuffer() );
00245         strType.ReleaseBuffer();
00246         strType.TrimLeft(); strType.TrimRight();
00247         if ( strType.IsEmpty() ) return;
00248 
00249         if ( m_wndAgentList.FindString( -1, strType ) >= 0 ) return;
00250 
00251         m_wndAgentList.AddString( strType );
00252         m_wndAgentList.SetWindowText( _T("") );
00253 }
00254 
00255 void CUploadsSettingsPage::OnAgentRemove() 
00256 {
00257         int nItem = m_wndAgentList.GetCurSel();
00258         if ( nItem >= 0 ) m_wndAgentList.DeleteString( nItem );
00259         m_wndAgentRemove.EnableWindow( FALSE );
00260 }
00261 
00262 void CUploadsSettingsPage::OnItemChangedQueues(NMHDR* pNMHDR, LRESULT* pResult) 
00263 {
00264         NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
00265         m_wndQueueEdit.EnableWindow( m_wndQueues.GetSelectedCount() == 1 );
00266         m_wndQueueDelete.EnableWindow( m_wndQueues.GetSelectedCount() > 0 );
00267         *pResult = 0;
00268 }
00269 
00270 void CUploadsSettingsPage::OnDblClkQueues(NMHDR* pNMHDR, LRESULT* pResult) 
00271 {
00272         PostMessage( WM_COMMAND, MAKELONG( IDC_QUEUE_EDIT, BN_CLICKED ) );
00273         *pResult = 0;
00274 }
00275 
00276 void CUploadsSettingsPage::OnQueueNew() 
00277 {
00278         CString strQueueName;
00279         LoadString( strQueueName, IDS_UPLOAD_QUEUE_NEW );
00280         CUploadQueue* pQueue = UploadQueues.Create( strQueueName, TRUE );
00281         UpdateQueues();
00282         
00283         CQueuePropertiesDlg dlg( pQueue, TRUE, this );
00284         if ( dlg.DoModal() != IDOK ) UploadQueues.Delete( pQueue );
00285         
00286         UploadQueues.Save();
00287         UpdateQueues();
00288         m_bQueuesChanged = TRUE;
00289 }
00290 
00291 void CUploadsSettingsPage::OnQueueEdit() 
00292 {
00293         int nSelected = m_wndQueues.GetNextItem( -1, LVNI_SELECTED );
00294         if ( nSelected < 0 ) return;
00295         
00296         CUploadQueue* pQueue = (CUploadQueue*)m_wndQueues.GetItemData( nSelected );
00297         
00298         CQueuePropertiesDlg dlg( pQueue, FALSE, this );
00299         dlg.DoModal();
00300         
00301         UploadQueues.Save();
00302         UpdateQueues();
00303         m_bQueuesChanged = TRUE;
00304 }
00305 
00306 void CUploadsSettingsPage::OnQueueDelete() 
00307 {
00308         for ( int nItem = -1 ; ( nItem = m_wndQueues.GetNextItem( nItem, LVNI_SELECTED ) ) >= 0 ; )
00309         {
00310                 CUploadQueue* pQueue = (CUploadQueue*)m_wndQueues.GetItemData( nItem );
00311                 UploadQueues.Delete( pQueue );
00312         }
00313         
00314         UploadQueues.Save();
00315         UpdateQueues();
00316         m_bQueuesChanged = TRUE;
00317 }
00318 
00319 void CUploadsSettingsPage::OnQueueDrop(NMHDR* pNMHDR, LRESULT* pResult) 
00320 {
00321         CSingleLock pLock( &Transfers.m_pSection, TRUE );
00322         NM_LISTVIEW* pNM = (NM_LISTVIEW*)pNMHDR;
00323         
00324         CUploadQueue* pTarget = NULL;
00325         
00326         if ( pNM->iItem >= 0 && pNM->iItem < m_wndQueues.GetItemCount() )
00327         {
00328                 pTarget = (CUploadQueue*)m_wndQueues.GetItemData( pNM->iItem );
00329                 if ( ! UploadQueues.Check( pTarget ) ) pTarget = NULL;
00330         }
00331         
00332         for ( int nItem = -1 ; ( nItem = m_wndQueues.GetNextItem( nItem, LVNI_SELECTED ) ) >= 0 ; )
00333         {
00334                 CUploadQueue* pQueue = (CUploadQueue*)m_wndQueues.GetItemData( nItem );
00335                 
00336                 if ( UploadQueues.Check( pQueue ) && pQueue != pTarget )
00337                 {
00338                         UploadQueues.Reorder( pQueue, pTarget );
00339                 }
00340         }
00341         
00342         UploadQueues.Save();
00343         UpdateQueues(); 
00344         *pResult = 0;
00345 }
00346 
00347 BOOL CUploadsSettingsPage::OnKillActive()
00348 {
00349         UpdateData();
00350         
00351         if ( ( ! IsNotLimited( m_sBandwidthLimit ) ) && Settings.ParseVolume( m_sBandwidthLimit, TRUE ) == 0 )
00352         {
00353                 CString strMessage;
00354                 LoadString( strMessage, IDS_SETTINGS_NEED_BANDWIDTH );
00355                 AfxMessageBox( strMessage, MB_ICONEXCLAMATION );
00356                 GetDlgItem( IDC_UPLOADS_BANDWIDTH_LIMIT )->SetFocus();
00357                 return FALSE;
00358         }
00359         
00360         return CSettingsPage::OnKillActive();
00361 }
00362 
00363 void CUploadsSettingsPage::OnOK()
00364 {
00365         UpdateData();
00366 
00367         DWORD nOldLimit = Settings.Bandwidth.Uploads;
00368         
00369         Settings.Uploads.MaxPerHost                     = m_nMaxPerHost;
00370         Settings.Uploads.SharePartials          = m_bSharePartials;
00371         Settings.Uploads.SharePreviews          = m_bSharePreviews;
00372         Settings.Uploads.HubUnshare                     = m_bHubUnshare;
00373         Settings.Bandwidth.Uploads                      = (DWORD)Settings.ParseVolume( m_sBandwidthLimit, TRUE ) / 8;
00374         Settings.Uploads.ThrottleMode           = m_bThrottleMode;
00375 
00376         /*
00377         // Upload limit cannot exceed upload capacity
00378         if ( Settings.Bandwidth.Uploads )
00379         {
00380                 Settings.Bandwidth.Uploads = min ( Settings.Bandwidth.Uploads, ( ( Settings.Connection.OutSpeed / 8 ) * 1024 ) );
00381         }
00382         */
00383 
00384         // Warn the user about the effects of upload limiting
00385         if ( ( ! Settings.Live.UploadLimitWarning ) && ( Settings.Bandwidth.Uploads > 0 ) && ( Settings.Bandwidth.Uploads != nOldLimit ) )
00386         {
00387                 DWORD nDownload = max( Settings.Bandwidth.Downloads, ( ( Settings.Connection.InSpeed  / 8 ) * 1024 ) );
00388                 DWORD nUpload   = min( Settings.Bandwidth.Uploads,   ( ( Settings.Connection.OutSpeed / 8 ) * 1024 ) );
00389                 
00390                 if ( ( nUpload * 16 ) < ( nDownload ) )
00391                 {
00392                         CHelpDlg::Show( _T("GeneralHelp.UploadWarning") );
00393                         Settings.Live.UploadLimitWarning = TRUE;
00394                 }
00395         }
00396         
00397         // Set blocked user agents/strings
00398         Settings.Uploads.BlockAgents.Empty();
00399         
00400         for ( int nItem = 0 ; nItem < m_wndAgentList.GetCount() ; nItem++ )
00401         {
00402                 CString str;
00403                 m_wndAgentList.GetLBText( nItem, str );
00404                 
00405                 if ( str.GetLength() )
00406                 {
00407                         if ( Settings.Uploads.BlockAgents.IsEmpty() )
00408                                 Settings.Uploads.BlockAgents += '|';
00409                         Settings.Uploads.BlockAgents += str;
00410                         Settings.Uploads.BlockAgents += '|';
00411                 }
00412         }
00413         
00414         // Create/Validate queues
00415         if ( UploadQueues.GetCount() == 0 )
00416         {
00417                 UploadQueues.CreateDefault();
00418                 m_bQueuesChanged = TRUE;
00419         }
00420 
00421         UploadQueues.Validate();
00422 
00423         if ( m_bQueuesChanged )
00424         {
00425                 // Changing queues might change what files are in the hash table
00426                 LibraryDictionary.RebuildHashTable(); 
00427                 // ED2k file list will automatically update on next server connection
00428         }
00429 
00430         UpdateQueues();
00431 }
00432 
00433 
00434 void CUploadsSettingsPage::OnShowWindow(BOOL bShow, UINT nStatus)
00435 {
00436         CSettingsPage::OnShowWindow(bShow, nStatus);
00437         if ( bShow )
00438         {
00439                 // Update the bandwidth limit combo values
00440 
00441                 // Remove any existing strings
00442                 while ( m_wndBandwidthLimit.GetCount() ) m_wndBandwidthLimit.DeleteString( 0 );
00443                 // Add the new ones
00444                 m_wndBandwidthLimit.AddString( Settings.SmartVolume( Settings.Connection.OutSpeed / 4, TRUE, TRUE ) );
00445                 m_wndBandwidthLimit.AddString( Settings.SmartVolume( Settings.Connection.OutSpeed / 2, TRUE, TRUE ) );
00446                 m_wndBandwidthLimit.AddString( Settings.SmartVolume( (Settings.Connection.OutSpeed/2)+(Settings.Connection.OutSpeed/4), TRUE, TRUE ) );
00447                 m_wndBandwidthLimit.AddString( Settings.SmartVolume( Settings.Connection.OutSpeed, TRUE, TRUE ) );
00448                 m_wndBandwidthLimit.AddString( _T("MAX") );
00449 
00450                 UpdateData( FALSE );
00451 
00452                 // Update queue window to show current limit
00453                 UpdateQueues();
00454         }
00455 }
00456 
00457 BOOL CUploadsSettingsPage::IsNotLimited(LPCTSTR pText)
00458 {
00459         if ( ( _tcslen( pText ) == 0 ) ||
00460                  ( _tcsistr( pText, _T("MAX") ) != NULL ) || 
00461                  ( _tcsistr( pText, _T("NONE") ) != NULL ) )
00462                 return TRUE;
00463         else
00464                 return FALSE;
00465 }

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