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 "QuerySearch.h"
00026 #include "WndSearchMonitor.h"
00027 #include "WndSearch.h"
00028 #include "LiveList.h"
00029 #include "XML.h"
00030 #include "SHA.h"
00031 #include "ED2K.h"
00032 #include "TigerTree.h"
00033
00034 #ifdef _DEBUG
00035 #define new DEBUG_NEW
00036 #undef THIS_FILE
00037 static char THIS_FILE[] = __FILE__;
00038 #endif
00039
00040 IMPLEMENT_SERIAL(CSearchMonitorWnd, CPanelWnd, 0)
00041
00042 BEGIN_MESSAGE_MAP(CSearchMonitorWnd, CPanelWnd)
00043
00044 ON_WM_CREATE()
00045 ON_WM_SIZE()
00046 ON_WM_DESTROY()
00047 ON_WM_CONTEXTMENU()
00048 ON_UPDATE_COMMAND_UI(ID_SEARCHMONITOR_PAUSE, OnUpdateSearchMonitorPause)
00049 ON_COMMAND(ID_SEARCHMONITOR_PAUSE, OnSearchMonitorPause)
00050 ON_COMMAND(ID_SEARCHMONITOR_CLEAR, OnSearchMonitorClear)
00051 ON_UPDATE_COMMAND_UI(ID_HITMONITOR_SEARCH, OnUpdateSearchMonitorSearch)
00052 ON_COMMAND(ID_HITMONITOR_SEARCH, OnSearchMonitorSearch)
00053 ON_NOTIFY(LVN_COLUMNCLICK, IDC_SEARCHES, OnDblClkList)
00054 ON_WM_TIMER()
00055
00056 END_MESSAGE_MAP()
00057
00058
00060
00061
00062 CSearchMonitorWnd::CSearchMonitorWnd()
00063 {
00064 Create( IDR_SEARCHMONITORFRAME );
00065 }
00066
00067 CSearchMonitorWnd::~CSearchMonitorWnd()
00068 {
00069 }
00070
00072
00073
00074 int CSearchMonitorWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
00075 {
00076 if ( CPanelWnd::OnCreate( lpCreateStruct ) == -1 ) return -1;
00077
00078 m_wndList.Create( WS_VISIBLE|LVS_ICON|LVS_AUTOARRANGE|LVS_REPORT|LVS_SHOWSELALWAYS,
00079 rectDefault, this, IDC_SEARCHES );
00080
00081 m_pSizer.Attach( &m_wndList );
00082
00083 m_wndList.SendMessage( LVM_SETEXTENDEDLISTVIEWSTYLE,
00084 LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP|LVS_EX_LABELTIP,
00085 LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP|LVS_EX_LABELTIP );
00086
00087 m_gdiImageList.Create( 16, 16, ILC_MASK|ILC_COLOR16, 1, 1 );
00088 m_gdiImageList.Add( theApp.m_bRTL ? CreateMirroredIcon( theApp.LoadIcon( IDR_SEARCHMONITORFRAME ) ) :
00089 theApp.LoadIcon( IDR_SEARCHMONITORFRAME ) );
00090 m_wndList.SetImageList( &m_gdiImageList, LVSIL_SMALL );
00091
00092 m_wndList.InsertColumn( 0, _T("Search"), LVCFMT_LEFT, 200, -1 );
00093 m_wndList.InsertColumn( 1, _T("URN"), LVCFMT_LEFT, 120, 0 );
00094 m_wndList.InsertColumn( 2, _T("Schema"), LVCFMT_LEFT, 120, 1 );
00095
00096 m_wndList.SetFont( &theApp.m_gdiFont );
00097
00098 LoadState( _T("CSearchMonitorWnd"), TRUE );
00099
00100 m_bPaused = FALSE;
00101 SetTimer( 2, 250, NULL );
00102
00103 return 0;
00104 }
00105
00106 void CSearchMonitorWnd::OnDestroy()
00107 {
00108 KillTimer( 2 );
00109
00110 CSingleLock pLock( &m_pSection, TRUE );
00111 m_bPaused = TRUE;
00112
00113 for ( POSITION pos = m_pQueue.GetHeadPosition() ; pos ; )
00114 {
00115 delete (CLiveItem*)m_pQueue.GetNext( pos );
00116 }
00117 m_pQueue.RemoveAll();
00118
00119 pLock.Unlock();
00120
00121 Settings.SaveList( _T("CSearchMonitorWnd"), &m_wndList );
00122 SaveState( _T("CSearchMonitorWnd") );
00123
00124 CPanelWnd::OnDestroy();
00125 }
00126
00127 void CSearchMonitorWnd::OnSkinChange()
00128 {
00129 CPanelWnd::OnSkinChange();
00130 Settings.LoadList( _T("CSearchMonitorWnd"), &m_wndList );
00131 }
00132
00133 void CSearchMonitorWnd::OnSize(UINT nType, int cx, int cy)
00134 {
00135 CPanelWnd::OnSize( nType, cx, cy );
00136 m_pSizer.Resize( cx );
00137 m_wndList.SetWindowPos( NULL, 0, 0, cx, cy, SWP_NOZORDER );
00138 }
00139
00140 void CSearchMonitorWnd::OnContextMenu(CWnd* pWnd, CPoint point)
00141 {
00142 TrackPopupMenu( _T("CSearchMonitorWnd"), point, ID_HITMONITOR_SEARCH );
00143 }
00144
00145 void CSearchMonitorWnd::OnUpdateSearchMonitorSearch(CCmdUI* pCmdUI)
00146 {
00147 pCmdUI->Enable( m_wndList.GetSelectedCount() == 1 );
00148 }
00149
00150 void CSearchMonitorWnd::OnSearchMonitorSearch()
00151 {
00152 int nItem = m_wndList.GetNextItem( -1, LVNI_SELECTED );
00153
00154 if ( nItem >= 0 )
00155 {
00156 CQuerySearch* pSearch = new CQuerySearch();
00157 pSearch->m_sSearch = m_wndList.GetItemText( nItem, 0 );
00158 if ( pSearch->OpenWindow() == NULL ) delete pSearch;
00159 }
00160 }
00161
00162 void CSearchMonitorWnd::OnUpdateSearchMonitorPause(CCmdUI* pCmdUI)
00163 {
00164 pCmdUI->SetCheck( m_bPaused );
00165 }
00166
00167 void CSearchMonitorWnd::OnSearchMonitorPause()
00168 {
00169 m_bPaused = ! m_bPaused;
00170 }
00171
00172 void CSearchMonitorWnd::OnSearchMonitorClear()
00173 {
00174 m_wndList.DeleteAllItems();
00175 }
00176
00177 void CSearchMonitorWnd::OnDblClkList(NMHDR* pNotifyStruct, LRESULT *pResult)
00178 {
00179 OnSearchMonitorSearch();
00180 *pResult = 0;
00181 }
00182
00184
00185
00186 void CSearchMonitorWnd::OnQuerySearch(CQuerySearch* pSearch)
00187 {
00188 if ( m_bPaused || m_hWnd == NULL ) return;
00189
00190 CSingleLock pLock( &m_pSection, TRUE );
00191
00192 if ( m_bPaused ) return;
00193
00194 CLiveItem* pItem = new CLiveItem( 3, NULL );
00195
00196 CString strSearch = pSearch->m_sSearch;
00197 CString strSchema = _T("None");
00198 CString strURN = _T("None");
00199
00200 if ( pSearch->m_bSHA1 && pSearch->m_bTiger )
00201 {
00202 strURN = _T("bitprint:")
00203 + CSHA::HashToString( &pSearch->m_pSHA1 )
00204 + '.'
00205 + CTigerNode::HashToString( &pSearch->m_pTiger );
00206 }
00207 else if ( pSearch->m_bTiger )
00208 {
00209 strURN = _T("tree:tiger/:") + CTigerNode::HashToString( &pSearch->m_pTiger );
00210 }
00211 else if ( pSearch->m_bSHA1 )
00212 {
00213 strURN = _T("sha1:") + CSHA::HashToString( &pSearch->m_pSHA1 );
00214 }
00215 else if ( pSearch->m_bED2K )
00216 {
00217 strURN = _T("ed2k:") + CED2K::HashToString( &pSearch->m_pED2K );
00218 }
00219 else if ( pSearch->m_bBTH )
00220 {
00221 strURN = _T("btih:") + CSHA::HashToString( &pSearch->m_pBTH );
00222 }
00223
00224 if ( pSearch->m_pXML )
00225 {
00226 strSearch += ' ';
00227 strSearch += pSearch->m_pXML->GetRecursiveWords();
00228
00229 strSchema = pSearch->m_pXML->GetAttributeValue( CXMLAttribute::schemaName, _T("") );
00230
00231 int nSlash = strSchema.ReverseFind( '/' );
00232 if ( nSlash > 0 ) strSchema = strSchema.Mid( nSlash + 1 );
00233 }
00234
00235 pItem->Set( 0, strSearch );
00236 pItem->Set( 1, strURN );
00237 pItem->Set( 2, strSchema );
00238
00239 m_pQueue.AddTail( pItem );
00240 }
00241
00242 void CSearchMonitorWnd::OnTimer(UINT nIDEvent)
00243 {
00244 if ( nIDEvent != 2 ) return;
00245
00246 BOOL bScroll = m_wndList.GetTopIndex() + m_wndList.GetCountPerPage() >= m_wndList.GetItemCount();
00247
00248 CSingleLock pLock( &m_pSection );
00249
00250 while ( TRUE )
00251 {
00252 pLock.Lock();
00253
00254 if ( m_pQueue.GetCount() == 0 ) break;
00255 CLiveItem* pItem = (CLiveItem*)m_pQueue.RemoveHead();
00256
00257 pLock.Unlock();
00258
00259 if ( (DWORD)m_wndList.GetItemCount() >= Settings.Search.MonitorQueue && Settings.Search.MonitorQueue > 0 )
00260 {
00261 m_wndList.DeleteItem( 0 );
00262 }
00263
00264 int nItem = pItem->Add( &m_wndList, -1, 3 );
00265
00266 delete pItem;
00267 }
00268
00269 if ( bScroll ) m_wndList.EnsureVisible( m_wndList.GetItemCount() - 1, FALSE );
00270 }