00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "Settings.h"
00025 #include "Network.h"
00026 #include "DiscoveryServices.h"
00027 #include "LiveList.h"
00028 #include "WndDiscovery.h"
00029 #include "DlgDiscoveryService.h"
00030
00031 #ifdef _DEBUG
00032 #define new DEBUG_NEW
00033 #undef THIS_FILE
00034 static char THIS_FILE[] = __FILE__;
00035 #endif
00036
00037 IMPLEMENT_SERIAL(CDiscoveryWnd, CPanelWnd, 0)
00038
00039 BEGIN_MESSAGE_MAP(CDiscoveryWnd, CPanelWnd)
00040
00041 ON_WM_CREATE()
00042 ON_WM_DESTROY()
00043 ON_WM_SIZE()
00044 ON_WM_TIMER()
00045 ON_NOTIFY(NM_DBLCLK, IDC_SERVICES, OnDblClkList)
00046 ON_NOTIFY(LVN_COLUMNCLICK, IDC_SERVICES, OnSortList)
00047 ON_UPDATE_COMMAND_UI(ID_DISCOVERY_QUERY, OnUpdateDiscoveryQuery)
00048 ON_COMMAND(ID_DISCOVERY_QUERY, OnDiscoveryQuery)
00049 ON_UPDATE_COMMAND_UI(ID_DISCOVERY_REMOVE, OnUpdateDiscoveryRemove)
00050 ON_COMMAND(ID_DISCOVERY_REMOVE, OnDiscoveryRemove)
00051 ON_COMMAND(ID_DISCOVERY_ADD, OnDiscoveryAdd)
00052 ON_COMMAND(ID_DISCOVERY_EDIT, OnDiscoveryEdit)
00053 ON_UPDATE_COMMAND_UI(ID_DISCOVERY_EDIT, OnUpdateDiscoveryEdit)
00054 ON_WM_CONTEXTMENU()
00055 ON_UPDATE_COMMAND_UI(ID_DISCOVERY_GNUTELLA, OnUpdateDiscoveryGnutella)
00056 ON_COMMAND(ID_DISCOVERY_GNUTELLA, OnDiscoveryGnutella)
00057 ON_UPDATE_COMMAND_UI(ID_DISCOVERY_WEBCACHE, OnUpdateDiscoveryWebcache)
00058 ON_COMMAND(ID_DISCOVERY_WEBCACHE, OnDiscoveryWebcache)
00059 ON_UPDATE_COMMAND_UI(ID_DISCOVERY_SERVERMET, OnUpdateDiscoveryServerMet)
00060 ON_COMMAND(ID_DISCOVERY_SERVERMET, OnDiscoveryServerMet)
00061 ON_UPDATE_COMMAND_UI(ID_DISCOVERY_BLOCKED, OnUpdateDiscoveryBlocked)
00062 ON_COMMAND(ID_DISCOVERY_BLOCKED, OnDiscoveryBlocked)
00063 ON_UPDATE_COMMAND_UI(ID_DISCOVERY_ADVERTISE, OnUpdateDiscoveryAdvertise)
00064 ON_COMMAND(ID_DISCOVERY_ADVERTISE, OnDiscoveryAdvertise)
00065 ON_UPDATE_COMMAND_UI(ID_DISCOVERY_BROWSE, OnUpdateDiscoveryBrowse)
00066 ON_COMMAND(ID_DISCOVERY_BROWSE, OnDiscoveryBrowse)
00067
00068
00069 END_MESSAGE_MAP()
00070
00071
00073
00074
00075 CDiscoveryWnd::CDiscoveryWnd()
00076 {
00077 Create( IDR_DISCOVERYFRAME );
00078 }
00079
00080 CDiscoveryWnd::~CDiscoveryWnd()
00081 {
00082 }
00083
00085
00086
00087 int CDiscoveryWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
00088 {
00089 if ( CPanelWnd::OnCreate( lpCreateStruct ) == -1 ) return -1;
00090
00091 m_wndList.Create( WS_VISIBLE|LVS_ICON|LVS_AUTOARRANGE|LVS_REPORT|LVS_SHOWSELALWAYS,
00092 rectDefault, this, IDC_SERVICES );
00093 m_pSizer.Attach( &m_wndList );
00094
00095 m_wndList.SendMessage( LVM_SETEXTENDEDLISTVIEWSTYLE,
00096 LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP|LVS_EX_LABELTIP,
00097 LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP|LVS_EX_LABELTIP );
00098
00099 m_gdiImageList.Create( 16, 16, ILC_MASK|ILC_COLOR32, 4, 1 );
00100 m_gdiImageList.Add( theApp.m_bRTL ? CreateMirroredIcon( theApp.LoadIcon( IDR_HOSTCACHEFRAME ) ) :
00101 theApp.LoadIcon( IDR_HOSTCACHEFRAME ) );
00102 m_gdiImageList.Add( theApp.m_bRTL ? CreateMirroredIcon( theApp.LoadIcon( IDI_DISCOVERY_GRAY ) ) :
00103 theApp.LoadIcon( IDI_DISCOVERY_GRAY ) );
00104 m_gdiImageList.Add( theApp.m_bRTL ? CreateMirroredIcon( theApp.LoadIcon( IDR_DISCOVERYFRAME ) ) :
00105 theApp.LoadIcon( IDR_DISCOVERYFRAME ) );
00106 m_gdiImageList.Add( theApp.m_bRTL ? CreateMirroredIcon( theApp.LoadIcon( IDI_WEB_URL ) ) :
00107 theApp.LoadIcon( IDI_WEB_URL ) );
00108 m_gdiImageList.Add( theApp.m_bRTL ? CreateMirroredIcon( theApp.LoadIcon( IDI_DISCOVERY_BLUE ) ) :
00109 theApp.LoadIcon( IDI_DISCOVERY_BLUE ) );
00110 m_gdiImageList.Add( theApp.m_bRTL ? CreateMirroredIcon( theApp.LoadIcon( IDI_FIREWALLED ) ) :
00111 theApp.LoadIcon( IDI_FIREWALLED ) );
00112 m_wndList.SetImageList( &m_gdiImageList, LVSIL_SMALL );
00113
00114 m_wndList.InsertColumn( 0, _T("Address"), LVCFMT_LEFT, 260, -1 );
00115 m_wndList.InsertColumn( 1, _T("Type"), LVCFMT_CENTER, 80, 0 );
00116 m_wndList.InsertColumn( 2, _T("Last Access"), LVCFMT_CENTER, 130, 1 );
00117 m_wndList.InsertColumn( 3, _T("Hosts"), LVCFMT_CENTER, 50, 2 );
00118 m_wndList.InsertColumn( 4, _T("Accesses"), LVCFMT_CENTER, 70, 3 );
00119 m_wndList.InsertColumn( 5, _T("Updates"), LVCFMT_CENTER, 55, 4 );
00120 m_wndList.InsertColumn( 6, _T("Failures"), LVCFMT_CENTER, 55, 5 );
00121
00122 m_wndList.SetFont( &theApp.m_gdiFont );
00123
00124 LoadState( _T("CDiscoveryWnd"), TRUE );
00125
00126 m_bShowGnutella = TRUE;
00127 m_bShowWebCache = TRUE;
00128 m_bShowServerMet = TRUE;
00129 m_bShowBlocked = TRUE;
00130
00131 CWaitCursor pCursor;
00132 Update();
00133
00134 return 0;
00135 }
00136
00137 void CDiscoveryWnd::OnDestroy()
00138 {
00139 CSingleLock pLock( &Network.m_pSection );
00140 if ( pLock.Lock( 250 ) ) DiscoveryServices.Save();
00141
00142 Settings.SaveList( _T("CDiscoveryWnd"), &m_wndList );
00143 SaveState( _T("CDiscoveryWnd") );
00144
00145 CPanelWnd::OnDestroy();
00146 }
00147
00149
00150
00151 void CDiscoveryWnd::Update()
00152 {
00153 CSingleLock pLock( &Network.m_pSection, TRUE );
00154 CLiveList pLiveList( 7 );
00155
00156 for ( POSITION pos = DiscoveryServices.GetIterator() ; pos ; )
00157 {
00158 CDiscoveryService* pService = DiscoveryServices.GetNext( pos );
00159
00160 CLiveItem* pItem = NULL;
00161
00162 if ( pService->m_nType == CDiscoveryService::dsGnutella )
00163 {
00164 if ( ! m_bShowGnutella ) continue;
00165 pItem = pLiveList.Add( pService );
00166 pItem->Set( 1, _T("Bootstrap") );
00167 pItem->m_nImage = 0;
00168 }
00169 else if ( pService->m_nType == CDiscoveryService::dsWebCache )
00170 {
00171 if ( ! m_bShowWebCache ) continue;
00172 pItem = pLiveList.Add( pService );
00173 pItem->Set( 1, _T("GWebCache") );
00174 if ( pService->m_bGnutella2 && pService->m_bGnutella1 )
00175 {
00176 pItem->m_nImage = 2;
00177 }
00178 else
00179 {
00180 if ( pService->m_bGnutella2 )
00181 pItem->m_nImage = 4;
00182 else if ( pService->m_bGnutella1 )
00183 pItem->m_nImage = 1;
00184 else
00185 pItem->m_nImage = 3;
00186 }
00187 }
00188 else if ( pService->m_nType == CDiscoveryService::dsServerMet )
00189 {
00190 if ( ! m_bShowServerMet ) continue;
00191 pItem = pLiveList.Add( pService );
00192 pItem->Set( 1, _T("Server.met") );
00193 pItem->m_nImage = 3;
00194 }
00195 else if ( pService->m_nType == CDiscoveryService::dsBlocked )
00196 {
00197 if ( ! m_bShowBlocked ) continue;
00198 pItem = pLiveList.Add( pService );
00199 pItem->Set( 1, _T("Blocked") );
00200 pItem->m_nImage = 5;
00201 }
00202 else
00203 {
00204 continue;
00205 }
00206
00207 pItem->Set( 0, pService->m_sAddress );
00208
00209 if ( pService->m_tAccessed )
00210 {
00211 CTime pTime( (time_t)pService->m_tAccessed );
00212 pItem->Set( 2, pTime.Format( _T("%Y-%m-%d %H:%M:%S") ) );
00213 }
00214 else
00215 {
00216 if ( pService->m_nType != CDiscoveryService::dsBlocked )
00217 pItem->Set( 2, _T("0 - Never") );
00218 }
00219
00220 if ( pService->m_nType == CDiscoveryService::dsWebCache ||
00221 pService->m_nType == CDiscoveryService::dsServerMet )
00222 {
00223 pItem->Format( 4, _T("%u"), pService->m_nAccesses );
00224 pItem->Format( 6, _T("%u"), pService->m_nFailures );
00225
00226 if ( pService->m_tAccessed )
00227 {
00228 pItem->Format( 3, _T("%u"), pService->m_nHosts );
00229 pItem->Format( 5, _T("%u"), pService->m_nUpdates );
00230 }
00231 }
00232 }
00233
00234 pLiveList.Apply( &m_wndList, TRUE );
00235 }
00236
00237 CDiscoveryService* CDiscoveryWnd::GetItem(int nItem)
00238 {
00239 if ( nItem >= 0 && m_wndList.GetItemState( nItem, LVIS_SELECTED ) )
00240 {
00241 CDiscoveryService* pService = (CDiscoveryService*)m_wndList.GetItemData( nItem );
00242 if ( DiscoveryServices.Check( pService ) ) return pService;
00243 }
00244
00245 return NULL;
00246 }
00247
00248 void CDiscoveryWnd::OnSkinChange()
00249 {
00250 CPanelWnd::OnSkinChange();
00251 Settings.LoadList( _T("CDiscoveryWnd"), &m_wndList, 3 );
00252 }
00253
00254 void CDiscoveryWnd::OnSize(UINT nType, int cx, int cy)
00255 {
00256 CPanelWnd::OnSize(nType, cx, cy);
00257 m_pSizer.Resize( cx );
00258 m_wndList.SetWindowPos( NULL, 0, 0, cx, cy, SWP_NOZORDER );
00259 }
00260
00261 void CDiscoveryWnd::OnTimer(UINT nIDEvent)
00262 {
00263 if ( ( nIDEvent == 1 ) && ( IsPartiallyVisible() ) ) Update();
00264 }
00265
00266 void CDiscoveryWnd::OnDblClkList(NMHDR* pNMHDR, LRESULT* pResult)
00267 {
00268 OnDiscoveryEdit();
00269 *pResult = 0;
00270 }
00271
00272 void CDiscoveryWnd::OnSortList(NMHDR* pNotifyStruct, LRESULT *pResult)
00273 {
00274 NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNotifyStruct;
00275 CLiveList::Sort( &m_wndList, pNMListView->iSubItem );
00276 *pResult = 0;
00277 }
00278
00279 void CDiscoveryWnd::OnContextMenu(CWnd* pWnd, CPoint point)
00280 {
00281 TrackPopupMenu( _T("CDiscoveryWnd"), point, ID_DISCOVERY_EDIT );
00282 }
00283
00284 void CDiscoveryWnd::OnUpdateDiscoveryQuery(CCmdUI* pCmdUI)
00285 {
00286 if ( m_wndList.GetSelectedCount() == 1 )
00287 {
00288 CDiscoveryService* pService = GetItem( m_wndList.GetNextItem( -1, LVIS_SELECTED ) );
00289 if ( pService && pService->m_nType != CDiscoveryService::dsBlocked )
00290 {
00291 pCmdUI->Enable( TRUE );
00292 return;
00293 }
00294 }
00295
00296 pCmdUI->Enable( FALSE );
00297 }
00298
00299 void CDiscoveryWnd::OnDiscoveryQuery()
00300 {
00301 CSingleLock pLock( &Network.m_pSection, TRUE );
00302
00303 for ( int nItem = -1 ; ( nItem = m_wndList.GetNextItem( nItem, LVIS_SELECTED ) ) >= 0 ; )
00304 {
00305 CDiscoveryService* pService = GetItem( nItem );
00306
00307 if ( pService != NULL )
00308 {
00309 pService->Execute( ( GetAsyncKeyState( VK_SHIFT ) & 0x8000 ) ?
00310 CDiscoveryServices::wcmCaches : CDiscoveryServices::wcmHosts );
00311 break;
00312 }
00313 }
00314 }
00315
00316 void CDiscoveryWnd::OnUpdateDiscoveryAdvertise(CCmdUI* pCmdUI)
00317 {
00318 CSingleLock pLock( &Network.m_pSection );
00319
00320 if ( pLock.Lock( 100 ) )
00321 {
00322 CDiscoveryService* pService = GetItem( m_wndList.GetNextItem( -1, LVIS_SELECTED ) );
00323 pCmdUI->Enable( pService && pService->m_nType == CDiscoveryService::dsWebCache );
00324 }
00325 else
00326 {
00327 pCmdUI->Enable( FALSE );
00328 }
00329 }
00330
00331 void CDiscoveryWnd::OnDiscoveryAdvertise()
00332 {
00333 CSingleLock pLock( &Network.m_pSection, TRUE );
00334
00335 CDiscoveryService* pService = GetItem( m_wndList.GetNextItem( -1, LVIS_SELECTED ) );
00336
00337 if ( pService )
00338 {
00339 pService->Execute( CDiscoveryServices::wcmSubmit );
00340 }
00341 }
00342
00343 void CDiscoveryWnd::OnUpdateDiscoveryBrowse(CCmdUI* pCmdUI)
00344 {
00345 OnUpdateDiscoveryAdvertise( pCmdUI );
00346 }
00347
00348 void CDiscoveryWnd::OnDiscoveryBrowse()
00349 {
00350 CSingleLock pLock( &Network.m_pSection, TRUE );
00351
00352 CDiscoveryService* pService = GetItem( m_wndList.GetNextItem( -1, LVIS_SELECTED ) );
00353 CString strURL;
00354
00355 if ( pService && pService->m_nType == CDiscoveryService::dsWebCache )
00356 strURL = pService->m_sAddress;
00357
00358 pLock.Unlock();
00359
00360 if ( strURL.GetLength() )
00361 {
00362 ShellExecute( GetSafeHwnd(), _T("open"), strURL, NULL, NULL, SW_SHOWNORMAL );
00363 }
00364 }
00365
00366 void CDiscoveryWnd::OnUpdateDiscoveryRemove(CCmdUI* pCmdUI)
00367 {
00368 pCmdUI->Enable( m_wndList.GetSelectedCount() > 0 );
00369 }
00370
00371 void CDiscoveryWnd::OnDiscoveryRemove()
00372 {
00373 CSingleLock pLock( &Network.m_pSection, TRUE );
00374
00375 for ( int nItem = -1 ; ( nItem = m_wndList.GetNextItem( nItem, LVIS_SELECTED ) ) >= 0 ; )
00376 {
00377 CDiscoveryService* pService = GetItem( nItem );
00378 if ( pService ) pService->Remove( FALSE );
00379 }
00380
00381 DiscoveryServices.CheckMinimumServices();
00382
00383 Update();
00384 }
00385
00386 void CDiscoveryWnd::OnUpdateDiscoveryEdit(CCmdUI* pCmdUI)
00387 {
00388 pCmdUI->Enable( m_wndList.GetSelectedCount() == 1 );
00389 }
00390
00391 void CDiscoveryWnd::OnDiscoveryEdit()
00392 {
00393 CSingleLock pLock( &Network.m_pSection, TRUE );
00394
00395 CDiscoveryService* pService = GetItem( m_wndList.GetNextItem( -1, LVIS_SELECTED ) );
00396 if ( ! pService ) return;
00397
00398 pLock.Unlock();
00399
00400 CDiscoveryServiceDlg dlg( NULL, pService );
00401
00402 if ( dlg.DoModal() == IDOK ) Update();
00403 }
00404
00405 void CDiscoveryWnd::OnUpdateDiscoveryGnutella(CCmdUI* pCmdUI)
00406 {
00407 pCmdUI->SetCheck( m_bShowGnutella );
00408 }
00409
00410 void CDiscoveryWnd::OnDiscoveryGnutella()
00411 {
00412 m_bShowGnutella = ! m_bShowGnutella;
00413 Update();
00414 }
00415
00416 void CDiscoveryWnd::OnUpdateDiscoveryWebcache(CCmdUI* pCmdUI)
00417 {
00418 pCmdUI->SetCheck( m_bShowWebCache );
00419 }
00420
00421 void CDiscoveryWnd::OnDiscoveryWebcache()
00422 {
00423 m_bShowWebCache = ! m_bShowWebCache;
00424 Update();
00425 }
00426
00427 void CDiscoveryWnd::OnUpdateDiscoveryServerMet(CCmdUI* pCmdUI)
00428 {
00429 pCmdUI->SetCheck( m_bShowServerMet );
00430 }
00431
00432 void CDiscoveryWnd::OnDiscoveryServerMet()
00433 {
00434 m_bShowServerMet = ! m_bShowServerMet;
00435 Update();
00436 }
00437
00438 void CDiscoveryWnd::OnUpdateDiscoveryBlocked(CCmdUI* pCmdUI)
00439 {
00440 pCmdUI->SetCheck( m_bShowBlocked );
00441 }
00442
00443 void CDiscoveryWnd::OnDiscoveryBlocked()
00444 {
00445 m_bShowBlocked = ! m_bShowBlocked;
00446 Update();
00447 }
00448
00449 void CDiscoveryWnd::OnDiscoveryAdd()
00450 {
00451 CDiscoveryServiceDlg dlg;
00452
00453 if ( dlg.DoModal() == IDOK ) Update();
00454 }