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

Remote.cpp

Go to the documentation of this file.
00001 //
00002 // Remote.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 "Remote.h"
00026 
00027 #include "Network.h"
00028 #include "MatchObjects.h"
00029 #include "QuerySearch.h"
00030 #include "QueryHit.h"
00031 #include "VendorCache.h"
00032 #include "SchemaCache.h"
00033 #include "Schema.h"
00034 #include "Transfers.h"
00035 #include "Downloads.h"
00036 #include "Download.h"
00037 #include "DownloadGroups.h"
00038 #include "DownloadGroup.h"
00039 #include "DownloadSource.h"
00040 #include "DownloadTransfer.h"
00041 #include "Uploads.h"
00042 #include "UploadQueues.h"
00043 #include "UploadQueue.h"
00044 #include "UploadFile.h"
00045 #include "UploadTransfer.h"
00046 #include "UploadTransferBT.h"
00047 #include "Neighbours.h"
00048 #include "G1Neighbour.h"
00049 #include "G2Neighbour.h"
00050 #include "EDNeighbour.h"
00051 #include "EDPacket.h"
00052 #include "GProfile.h"
00053 #include "ShareazaURL.h"
00054 
00055 #include "WndMain.h"
00056 #include "WndSearch.h"
00057 #include "CtrlDownloads.h"
00058 #include "CtrlUploads.h"
00059 
00060 #ifdef _DEBUG
00061 #define new DEBUG_NEW
00062 #undef THIS_FILE
00063 static char THIS_FILE[] = __FILE__;
00064 #endif
00065 
00066 CList<int> CRemote::m_pCookies;
00067 
00069 // CRemote construction
00070 
00071 CRemote::CRemote(CConnection* pConnection)
00072 {
00073         CTransfer::AttachTo( pConnection );
00074         m_mInput.pLimit = m_mOutput.pLimit = NULL;
00075         OnRead();
00076 }
00077 
00078 CRemote::~CRemote()
00079 {
00080 }
00081 
00083 // CRemote run event
00084 
00085 BOOL CRemote::OnRun()
00086 {
00087         DWORD tNow = GetTickCount();
00088         
00089         if ( ( tNow - m_mOutput.tLast > 180000 ) || ( ! Network.IsConnected() ) )
00090         {
00091                 Close();
00092                 delete this;
00093                 return FALSE;
00094         }
00095         
00096         return TRUE;
00097 }
00098 
00100 // CRemote dropped event
00101 
00102 void CRemote::OnDropped(BOOL bError)
00103 {
00104         Close();
00105         delete this;
00106 }
00107 
00109 // CRemote read event
00110 
00111 BOOL CRemote::OnRead()
00112 {
00113         if ( ! CTransfer::OnRead() ) return FALSE;
00114         
00115         if ( m_sHandshake.IsEmpty() )
00116         {
00117                 if ( m_pInput->m_nLength > 4096 || ! Settings.Remote.Enable )
00118                 {
00119                         Close();
00120                         return FALSE;
00121                 }
00122                 
00123                 m_pInput->ReadLine( m_sHandshake );
00124         }
00125         
00126         if ( ! m_sHandshake.IsEmpty() )
00127         {
00128                 return ReadHeaders();
00129         }
00130         
00131         return TRUE;
00132 }
00133 
00135 // CRemote headers complete event
00136 
00137 BOOL CRemote::OnHeadersComplete()
00138 {
00139         if ( m_sHandshake.Find( _T("GET /") ) != 0 )
00140         {
00141                 Close();
00142                 delete this;
00143                 return FALSE;
00144         }
00145         
00146         m_sHandshake = m_sHandshake.Mid( 4 ).SpanExcluding( _T(" \t") );
00147         
00148         CString strPath = m_sHandshake.SpanExcluding( _T("?&") );
00149         CharLower( strPath.GetBuffer() );
00150         strPath.ReleaseBuffer();
00151         
00152         m_sRedirect.Empty();
00153         m_sHeader.Empty();
00154         m_sResponse.Empty();
00155         m_pResponse.Clear();
00156         
00157         PageSwitch( strPath );
00158         
00159         if ( ! m_sRedirect.IsEmpty() )
00160         {
00161                 m_pOutput->Print( "HTTP/1.1 302 Found\r\n" );
00162                 m_pOutput->Print( "Content-Length: 0\r\n" );
00163                 if ( ! m_sHeader.IsEmpty() ) m_pOutput->Print( m_sHeader );
00164                 m_pOutput->Print( "Location: " );
00165                 m_pOutput->Print( m_sRedirect );
00166                 m_pOutput->Print( "\r\n\r\n" );
00167         }
00168         else if ( ! m_sResponse.IsEmpty() )
00169         {
00170                 CString strLength;
00171                 Prepare();
00172                 Output( _T("commonFooter") );
00173                 int nBytes = WideCharToMultiByte( CP_UTF8, 0, m_sResponse, m_sResponse.GetLength(), NULL, 0, NULL, NULL );
00174                 strLength.Format( _T("Content-Length: %i\r\n"), nBytes );
00175                 m_pOutput->Print( "HTTP/1.1 200 OK\r\n" );
00176                 m_pOutput->Print( "Content-Type: text/html; charset=UTF-8\r\n" );
00177                 m_pOutput->Print( strLength );
00178                 if ( ! m_sHeader.IsEmpty() ) m_pOutput->Print( m_sHeader );
00179                 m_pOutput->Print( "\r\n" );
00180                 m_pOutput->Print( m_sResponse, CP_UTF8 );
00181                 m_sResponse.Empty();
00182         }
00183         else if ( m_pResponse.m_nLength > 0 )
00184         {
00185                 CString strLength;
00186                 strLength.Format( _T("Content-Length: %i\r\n"), m_pResponse.m_nLength );
00187                 m_pOutput->Print( "HTTP/1.1 200 OK\r\n" );
00188                 m_pOutput->Print( strLength );
00189                 if ( ! m_sHeader.IsEmpty() ) m_pOutput->Print( m_sHeader );
00190                 m_pOutput->Print( "\r\n" );
00191                 m_pOutput->AddBuffer( &m_pResponse );
00192         }
00193         else
00194         {
00195                 m_pOutput->Print( "HTTP/1.1 404 Not Found\r\n" );
00196                 m_pOutput->Print( "Content-Length: 0\r\n" );
00197                 m_pOutput->Print( "Content-Type: text/html\r\n" );
00198                 m_pOutput->Print( "\r\n" );
00199         }
00200         
00201         m_sHandshake.Empty();
00202         ClearHeaders();
00203         OnWrite();
00204         
00205         return TRUE;
00206 }
00207 
00209 // CRemote get a query string key
00210 
00211 CString CRemote::GetKey(LPCTSTR pszName)
00212 {
00213         int nStart = 0;
00214         CString strPair = m_sHandshake.Tokenize( _T("&?"), nStart );
00215         
00216         while ( ! strPair.IsEmpty() )
00217         {
00218                 CString strName = strPair.SpanExcluding( _T("=") );
00219                 
00220                 if ( strName.CompareNoCase( pszName ) == 0 )
00221                 {
00222                         strName = strPair.Mid( strName.GetLength() + 1 );
00223                         return CConnection::URLDecode( strName );
00224                 }
00225                 
00226                 strPair = m_sHandshake.Tokenize( _T("&?"), nStart );
00227         }
00228         
00229         strPair.Empty();
00230         return strPair;
00231 }
00232 
00234 // CRemote check access cookie helper
00235 
00236 BOOL CRemote::CheckCookie()
00237 {
00238         for ( INT_PTR nHeader = 0 ; nHeader < m_pHeaderName.GetSize() ; nHeader ++ )
00239         {
00240                 if ( m_pHeaderName.GetAt( nHeader ).CompareNoCase( _T("Cookie") ) == 0 )
00241                 {
00242                         CString strValue( m_pHeaderValue.GetAt( nHeader ) );
00243                         CharLower( strValue.GetBuffer() );
00244                         strValue.ReleaseBuffer();
00245                         
00246                         int nPos = strValue.Find( _T("shareazaremote=") );
00247                         
00248                         if ( nPos >= 0 )
00249                         {
00250                                 int nCookie = 0;
00251                                 _stscanf( strValue.Mid( nPos + 15 ), _T("%i"), &nCookie );
00252                                 if ( m_pCookies.Find( nCookie ) != NULL ) return FALSE;
00253                         }
00254                 }
00255         }
00256         
00257         m_sRedirect = _T("/remote/");
00258         return TRUE;
00259 }
00260 
00262 // CRemote prepare to output a HTML segment
00263 
00264 void CRemote::Prepare(LPCTSTR pszPrefix)
00265 {
00266         if ( pszPrefix == NULL )
00267         {
00268                 m_pKeys.RemoveAll();
00269                 if ( m_sResponse.IsEmpty() ) Output( _T("commonHeader") );
00270         }
00271         else
00272         {
00273                 for ( POSITION pos = m_pKeys.GetStartPosition() ; pos != NULL ; )
00274                 {
00275                         CString strKey, strValue;
00276                         m_pKeys.GetNextAssoc( pos, strKey, strValue );
00277                         if ( strKey.Find( pszPrefix ) == 0 ) m_pKeys.RemoveKey( strKey );
00278                 }
00279         }
00280 }
00281 
00283 // CRemote add a substitution key for the next HTML segment
00284 
00285 void CRemote::Add(LPCTSTR pszKey, LPCTSTR pszValue)
00286 {
00287         CString strKey( pszKey );
00288         CharLower( strKey.GetBuffer() );
00289         strKey.ReleaseBuffer();
00290         m_pKeys.SetAt( strKey, pszValue );
00291 }
00292 
00294 // CRemote output a HTML segment
00295 
00296 void CRemote::Output(LPCTSTR pszName)
00297 {
00298         CString strBody, strValue;
00299         CFile hFile;
00300         
00301         if ( _tcsstr( pszName, _T("..") ) || _tcschr( pszName, '/' ) ) return;
00302         strValue = Settings.General.Path + _T("\\Remote\\") + pszName + _T(".htm");
00303         if ( ! hFile.Open( strValue, CFile::modeRead ) ) return;
00304         
00305         int nBytes              = (int)hFile.GetLength();
00306         CHAR* pBytes    = new CHAR[ nBytes ];
00307         hFile.Read( pBytes, nBytes );
00308         hFile.Close();
00309         
00310         int nWide = MultiByteToWideChar( CP_UTF8, 0, pBytes, nBytes, NULL, 0 );
00311         MultiByteToWideChar( CP_UTF8, 0, pBytes, nBytes, strBody.GetBuffer( nWide ), nWide );
00312         strBody.ReleaseBuffer( nWide );
00313         delete [] pBytes;
00314         
00315         CList<BOOL> pDisplayStack;
00316         
00317         for ( BOOL bDisplay = TRUE ; ; )
00318         {
00319                 int nStart = strBody.Find( _T("<%") );
00320                 
00321                 if ( nStart < 0 )
00322                 {
00323                         if ( bDisplay ) m_sResponse += strBody;
00324                         break;
00325                 }
00326                 else if ( nStart >= 0 )
00327                 {
00328                         if ( bDisplay && nStart > 0 ) m_sResponse += strBody.Left( nStart );
00329                         strBody = strBody.Mid( nStart + 2 );
00330                 }
00331                 
00332                 int nEnd = strBody.Find( _T("%>") );
00333                 if ( nEnd < 0 ) break;
00334                 
00335                 CString strKey = strBody.Left( nEnd );
00336                 strBody = strBody.Mid( nEnd + 2 );
00337                 
00338                 strKey.TrimLeft();
00339                 strKey.TrimRight();
00340                 CharLower( strKey.GetBuffer() );
00341                 strKey.ReleaseBuffer();
00342                 
00343                 if ( strKey.IsEmpty() )
00344                 {
00345                 }
00346                 else if ( strKey.GetAt( 0 ) == '=' && bDisplay )
00347                 {
00348                         strKey = strKey.Mid( 1 );
00349                         strKey.Trim();
00350                         if ( m_pKeys.Lookup( strKey, strValue ) ) m_sResponse += strValue;
00351                 }
00352                 else if ( strKey.GetAt( 0 ) == '?' )
00353                 {
00354                         strKey = strKey.Mid( 1 );
00355                         strKey.Trim();
00356                         
00357                         if ( strKey.IsEmpty() )
00358                         {
00359                                 if ( ! pDisplayStack.IsEmpty() ) bDisplay = pDisplayStack.RemoveTail();
00360                         }
00361                         else
00362                         {
00363                                 if ( strKey.GetAt( 0 ) == '!' )
00364                                 {
00365                                         strKey = strKey.Mid( 1 );
00366                                         strKey.Trim();
00367                                         if ( ! m_pKeys.Lookup( strKey, strValue ) ) strValue.Empty();
00368                                         pDisplayStack.AddTail( bDisplay );
00369                                         bDisplay = bDisplay && strValue.IsEmpty();
00370                                 }
00371                                 else
00372                                 {
00373                                         if ( ! m_pKeys.Lookup( strKey, strValue ) ) strValue.Empty();
00374                                         pDisplayStack.AddTail( bDisplay );
00375                                         bDisplay = bDisplay && ! strValue.IsEmpty();
00376                                 }
00377                         }
00378                 }
00379         }
00380 }
00381 
00383 // CRemote page switch
00384 
00385 void CRemote::PageSwitch(CString& strPath)
00386 {
00387         if ( strPath == _T("/") || strPath == _T("/remote") )
00388         {
00389                 m_sRedirect = _T("/remote/");
00390         }
00391         else if ( strPath == _T("/remote/") )
00392         {
00393                 PageLogin();
00394         }
00395         else if ( strPath == _T("/remote/home") )
00396         {
00397                 PageHome();
00398         }
00399         else if ( strPath == _T("/remote/search") )
00400         {
00401                 PageSearch();
00402         }
00403         else if ( strPath == _T("/remote/newsearch") )
00404         {
00405                 PageNewSearch();
00406         }
00407         else if ( strPath == _T("/remote/downloads") )
00408         {
00409                 PageDownloads();
00410         }
00411         else if ( strPath == _T("/remote/newdownload") )
00412         {
00413                 PageNewDownload();
00414         }
00415         else if ( strPath == _T("/remote/uploads") )
00416         {
00417                 PageUploads();
00418         }
00419         else if ( strPath == _T("/remote/network") )
00420         {
00421                 PageNetwork();
00422         }
00423         else if ( strPath == _T("/remote/header_1.png") || strPath == _T("/remote/header_2.png") )
00424         {
00425                 PageBanner( strPath );
00426         }
00427         else if ( strPath.Find( _T("/remote/images/") ) == 0 )
00428         {
00429                 PageImage( strPath );
00430         }
00431 }
00432 
00434 // CRemote page : login
00435 
00436 void CRemote::PageLogin()
00437 {
00438         CString strPassword = GetKey( _T("password") );
00439         
00440         if ( ! strPassword.IsEmpty() )
00441         {
00442                 CSHA pSHA1;
00443                 pSHA1.Add( (LPCTSTR)strPassword, strPassword.GetLength() * sizeof(TCHAR) );
00444                 pSHA1.Finish();
00445                 strPassword = pSHA1.GetHashString( FALSE );
00446         }
00447         
00448         if ( GetKey( _T("username") ) == Settings.Remote.Username &&
00449                                           strPassword == Settings.Remote.Password &&
00450                  Settings.Remote.Username.GetLength() > 0 &&
00451                  Settings.Remote.Password.GetLength() > 0 )
00452         {
00453                 int nCookie = rand();
00454                 m_pCookies.AddTail( nCookie );
00455                 m_sHeader.Format( _T("Set-Cookie: ShareazaRemote=%i; path=/remote\r\n"), nCookie );
00456                 m_sRedirect.Format( _T("/remote/home?%i"), rand() );
00457         }
00458         else
00459         {
00460                 Prepare();
00461                 if ( GetKey( _T("submit") ).GetLength() > 0 ) Add( _T("failure"), _T("true") );
00462                 Output( _T("login") );
00463         }
00464 }
00465 
00467 // CRemote page : home
00468 
00469 void CRemote::PageHome()
00470 {
00471         if ( CheckCookie() ) return;
00472         
00473         Prepare();
00474         Output( _T("home") );
00475 }
00476 
00478 // CRemote page : search
00479 
00480 void CRemote::PageSearch()
00481 {
00482         if ( CheckCookie() ) return;
00483         
00484         CSingleLock pLock( &theApp.m_pSection );
00485         if ( ! pLock.Lock( 1000 ) ) return;
00486         CMainWnd* pMainWnd = (CMainWnd*)theApp.m_pMainWnd;
00487         if ( pMainWnd == NULL || ! pMainWnd->IsKindOf( RUNTIME_CLASS(CMainWnd) ) ) return;
00488         
00489         int nSearchID = 0, nCloseID = 0;
00490         CSearchWnd* pSearchWnd = NULL;
00491         CString str;
00492         
00493         _stscanf( GetKey( _T("id") ), _T("%i"), &nSearchID );
00494         _stscanf( GetKey( _T("close") ), _T("%i"), &nCloseID );
00495         
00496         Prepare();
00497         Output( _T("searchHeader") );
00498         
00499         for ( CSearchWnd* pFindWnd = NULL ; pFindWnd = (CSearchWnd*)pMainWnd->m_pWindows.Find( RUNTIME_CLASS(CSearchWnd), pFindWnd ) ; )
00500         {
00501                 Prepare();
00502                 
00503                 if ( nCloseID == (int)pFindWnd )
00504                 {
00505                         pFindWnd->PostMessage( WM_CLOSE );
00506                         continue;
00507                 }
00508                 else if ( nSearchID == (int)pFindWnd )
00509                 {
00510                         pSearchWnd = pFindWnd;
00511                         Add( _T("search_selected"), _T("true") );
00512                 }
00513                 
00514                 str.Format( _T("%i"), (int)pFindWnd );
00515                 Add( _T("search_id"), str );
00516                 str = pFindWnd->m_sCaption;
00517                 if ( str.Find( _T("Search : ") ) == 0 ) str = str.Mid( 9 ).SpanExcluding( _T("[") );
00518                 Add( _T("search_caption"), str );
00519                 Output( _T("searchTab") );
00520         }
00521         
00522         if ( pSearchWnd == NULL )
00523         {
00524                 str.Empty();
00525 
00526                 for ( POSITION pos = SchemaCache.GetIterator() ; pos != NULL ; )
00527                 {
00528                         CSchema* pSchema = SchemaCache.GetNext( pos );
00529                         if ( ! pSchema->m_bPrivate && pSchema->m_nType == CSchema::stFile )
00530                         {
00531                                 str += _T("<option value=\"") + pSchema->m_sURI;
00532                                 str += _T("\">") + pSchema->m_sTitle;
00533                                 str += _T("</option>\r\n");
00534                         }
00535                 }
00536                 
00537                 Prepare();
00538                 Add( _T("schema_option_list"), str );
00539                 Output( _T("searchNew") );
00540                 Output( _T("searchFooter") );
00541                 return;
00542         }
00543         
00544         if ( ! GetKey( _T("stop") ).IsEmpty() )
00545         {
00546                 pSearchWnd->PostMessage( WM_COMMAND, ID_SEARCH_STOP );
00547                 Sleep( 500 );
00548         }
00549         
00550         str = GetKey( _T("sort") );
00551         if ( ! str.IsEmpty() )
00552         {
00553                 int nColumn = 0;
00554                 _stscanf( str, _T("%i"), &nColumn );
00555                 
00556                 if ( pSearchWnd->m_pMatches->m_nSortColumn == nColumn )
00557                 {
00558                         if ( pSearchWnd->m_pMatches->m_bSortDir == 1 )
00559                         {
00560                                 pSearchWnd->m_pMatches->SetSortColumn( nColumn, TRUE );
00561                         }
00562                         else
00563                         {
00564                                 pSearchWnd->m_pMatches->SetSortColumn( nColumn, FALSE );
00565                         }
00566                 }
00567                 else
00568                 {
00569                         pSearchWnd->m_pMatches->SetSortColumn( nColumn, TRUE );
00570                 }
00571                 
00572                 pSearchWnd->PostMessage( WM_TIMER, 7 );
00573         }
00574         
00575         str = GetKey( _T("expcol") );
00576         if ( ! str.IsEmpty() )
00577         {
00578                 CMatchFile** pLoop = pSearchWnd->m_pMatches->m_pFiles;
00579                 for ( DWORD nCount = 0 ; nCount < pSearchWnd->m_pMatches->m_nFiles ; nCount++, pLoop++ )
00580                 {
00581                         if ( (*pLoop)->GetURN() == str )
00582                         {
00583                                 (*pLoop)->Expand( GetKey( _T("collapse") ).IsEmpty() );
00584                                 pSearchWnd->PostMessage( WM_TIMER, 7 );
00585                                 break;
00586                         }
00587                 }
00588         }
00589         
00590         str = GetKey( _T("download") );
00591         if ( ! str.IsEmpty() )
00592         {
00593                 CMatchFile** pLoop = pSearchWnd->m_pMatches->m_pFiles;
00594                 for ( DWORD nCount = 0 ; nCount < pSearchWnd->m_pMatches->m_nFiles ; nCount++, pLoop++ )
00595                 {
00596                         if ( (*pLoop)->GetURN() == str )
00597                         {
00598                                 Downloads.Add( *pLoop );
00599                                 pSearchWnd->PostMessage( WM_TIMER, 7 );
00600                                 m_sResponse.Empty();
00601                                 m_sRedirect = _T("downloads?group_reveal=all");
00602                                 return;
00603                         }
00604                 }
00605         }
00606         
00607         if ( ! GetKey( _T("setfilter") ).IsEmpty() )
00608         {
00609                 pSearchWnd->m_pMatches->m_sFilter = GetKey( _T("filter") );
00610                 pSearchWnd->m_pMatches->Filter();
00611                 pSearchWnd->PostMessage( WM_TIMER, 7 );
00612         }
00613         
00614         Prepare();
00615         str.Format( _T("%i"), nSearchID );
00616         Add( _T("search_id"), str );
00617         str.Format( _T("%i"), rand() );
00618         Add( _T("random"), str );
00619         if ( ! pSearchWnd->m_bPaused ) Add( _T("searching"), _T("true") );
00620         Add( _T("search_filter"), pSearchWnd->m_pMatches->m_sFilter );
00621         Output( _T("searchTop") );
00622         
00623         PageSearchHeaderColumn( MATCH_COL_NAME, _T("Filename"), _T("left") );
00624         PageSearchHeaderColumn( MATCH_COL_SIZE, _T("Size"), _T("center") );
00625         PageSearchHeaderColumn( MATCH_COL_RATING, _T("Rating"), _T("center") );
00626         PageSearchHeaderColumn( MATCH_COL_STATUS, _T("Status"), _T("center") );
00627         PageSearchHeaderColumn( MATCH_COL_COUNT, _T("Host/Count"), _T("center") );
00628         PageSearchHeaderColumn( MATCH_COL_SPEED, _T("Speed"), _T("center") );
00629         PageSearchHeaderColumn( MATCH_COL_CLIENT, _T("Client"), _T("center") );
00630         
00631         Output( _T("searchMiddle") );
00632         
00633         pLock.Unlock();
00634         CSingleLock pLock2( &pSearchWnd->m_pMatches->m_pSection, TRUE );
00635         CMatchFile** pLoop = pSearchWnd->m_pMatches->m_pFiles;
00636         
00637         for ( DWORD nCount = 0 ; nCount < pSearchWnd->m_pMatches->m_nFiles ; nCount++, pLoop++ )
00638         {
00639                 CMatchFile* pFile = *pLoop;
00640                 if ( pFile->GetFilteredCount() == 0 ) continue;
00641                 
00642                 Add( _T("row_urn"), pFile->GetURN() );
00643                 Add( _T("row_filename"), pFile->m_pBest->m_sName );
00644                 if ( pFile->GetFilteredCount() > 1 )
00645                 {
00646                         if ( pFile->m_bExpanded )
00647                                 Add( _T("row_expanded"), _T("true") );
00648                         else
00649                                 Add( _T("row_collapsed"), _T("true") );
00650                 }
00651                 else
00652                 {
00653                         Add( _T("row_single"), _T("true") );
00654                 }
00655                 Output( _T("searchRowStart") );
00656                 
00657                 PageSearchRowColumn( MATCH_COL_SIZE, pFile, Settings.SmartVolume( pFile->m_nSize, FALSE ) );
00658                 
00659                 str.Empty();
00660                 for ( int nStar = pFile->m_nRating / max( 1, pFile->m_nRated ) ; nStar > 1 ; nStar -- ) str += _T("*");
00661                 PageSearchRowColumn( MATCH_COL_RATING, pFile, str );
00662                 
00663                 str.Empty();
00664                 if ( pFile->m_bBusy == TS_TRUE ) str += 'B'; else str += '-';
00665                 if ( pFile->m_bPush == TS_TRUE ) str += 'F'; else str += '-';
00666                 if ( pFile->m_bStable == TS_FALSE ) str += 'U'; else str += '-';
00667                 PageSearchRowColumn( MATCH_COL_STATUS, pFile, str );
00668                 
00669                 if ( pFile->GetFilteredCount() > 1 )
00670                 {
00671                         str.Format( _T("(%i sources)"), pFile->GetFilteredCount() );
00672                         PageSearchRowColumn( MATCH_COL_COUNT, pFile, str );
00673                 }
00674                 else
00675                 {
00676                         PageSearchRowColumn( MATCH_COL_COUNT, pFile,
00677                                 CString( inet_ntoa( pFile->m_pBest->m_pAddress ) ) );
00678                 }
00679                 
00680                 PageSearchRowColumn( MATCH_COL_SPEED, pFile, pFile->m_sSpeed );
00681                 PageSearchRowColumn( MATCH_COL_CLIENT, pFile, pFile->GetFilteredCount() == 1 ? pFile->m_pBest->m_pVendor->m_sName : _T("") );
00682                 
00683                 Output( _T("searchRowEnd") );
00684                 Prepare( _T("column_") );
00685                 Prepare( _T("row_") );
00686                 
00687                 if ( pFile->m_bExpanded )
00688                 {
00689                         for ( CQueryHit* pHit = pFile->m_pHits ; pHit != NULL ; pHit = pHit->m_pNext )
00690                         {
00691                                 if ( ! pHit->m_bFiltered ) continue;
00692                                 
00693                                 Add( _T("row_urn"), pFile->GetURN() );
00694                                 Add( _T("row_filename"), pHit->m_sName );
00695                                 Add( _T("row_source"), _T("true") );
00696                                 Output( _T("searchRowStart") );
00697                                 
00698                                 PageSearchRowColumn( MATCH_COL_SIZE, pFile, Settings.SmartVolume( pHit->m_nSize, FALSE ) );
00699                                 str.Empty();
00700                                 for ( int nStar = pHit->m_nRating ; nStar > 1 ; nStar -- ) str += _T("*");
00701                                 PageSearchRowColumn( MATCH_COL_RATING, pFile, str );
00702                                 str.Empty();
00703                                 if ( pHit->m_bBusy == TS_TRUE ) str += 'B'; else str += '-';
00704                                 if ( pHit->m_bPush == TS_TRUE ) str += 'F'; else str += '-';
00705                                 if ( pHit->m_bStable == TS_FALSE ) str += 'U'; else str += '-';
00706                                 PageSearchRowColumn( MATCH_COL_STATUS, pFile, str );
00707                                 PageSearchRowColumn( MATCH_COL_COUNT, pFile,
00708                                         CString( inet_ntoa( pHit->m_pAddress ) ) );
00709                                 PageSearchRowColumn( MATCH_COL_SPEED, pFile, pHit->m_sSpeed );
00710                                 PageSearchRowColumn( MATCH_COL_CLIENT, pFile, pHit->m_pVendor->m_sName );
00711                                 
00712                                 Output( _T("searchRowEnd") );
00713                                 Prepare( _T("column_") );
00714                                 Prepare( _T("row_") );
00715                         }
00716                 }
00717         }
00718         
00719         Output( _T("searchBottom") );
00720         Prepare();
00721         Output( _T("searchFooter") );
00722 }
00723 
00724 void CRemote::PageSearchHeaderColumn(int nColumnID, LPCTSTR pszCaption, LPCTSTR pszAlign)
00725 {
00726         CString str;
00727         str.Format( _T("%i"), nColumnID );
00728         Add( _T("column_id"), str );
00729         Add( _T("column_align"), pszAlign );
00730         Add( _T("column_caption"), pszCaption );
00731         Output( _T("searchColumn") );
00732         Prepare( _T("column_") );
00733 }
00734 
00735 void CRemote::PageSearchRowColumn(int nColumnID, CMatchFile* pFile, LPCTSTR pszValue, LPCTSTR pszAlign)
00736 {
00737         CString str;
00738         str.Format( _T("%i"), nColumnID );
00739         Add( _T("column_id"), str );
00740         Add( _T("column_align"), pszAlign );
00741         Add( _T("row_urn"), pFile->GetURN() );
00742         Add( _T("row_value"), pszValue );
00743         Output( _T("searchRowColumn") );
00744         Prepare( _T("column_") );
00745         Prepare( _T("row_") );
00746 }
00747 
00749 // CRemote page : newsearch
00750 
00751 void CRemote::PageNewSearch()
00752 {
00753         CString strURI;
00754         if ( CheckCookie() ) return;
00755         
00756         CSingleLock pLock( &theApp.m_pSection );
00757         if ( ! pLock.Lock( 1000 ) ) return;
00758         CMainWnd* pMainWnd = (CMainWnd*)theApp.m_pMainWnd;
00759         if ( pMainWnd == NULL || ! pMainWnd->IsKindOf( RUNTIME_CLASS(CMainWnd) ) ) return;
00760         
00761         CString strSearch       = GetKey( _T("search") );
00762         CString strSchema       = GetKey( _T("schema") );
00763         
00764         if ( strSearch.IsEmpty() || ( ! strSchema.IsEmpty() && SchemaCache.Get( strSchema ) == NULL ) )
00765         {
00766                 m_sRedirect = _T("home");
00767                 return;
00768         }
00769         
00770         CQuerySearch* pSearch   = new CQuerySearch();
00771         pSearch->m_sSearch              = strSearch;
00772         pSearch->m_pSchema              = SchemaCache.Get( strSchema );
00773 
00774         if ( pSearch->m_pSchema != NULL ) strURI = pSearch->m_pSchema->m_sURI;
00775         
00776         Settings.Search.LastSchemaURI = strURI;
00777         
00778         pMainWnd->PostMessage( WM_OPENSEARCH, (WPARAM)pSearch );
00779         pLock.Unlock();
00780         Sleep( 500 );
00781         
00782         m_sRedirect = _T("search");
00783 }
00784 
00786 // CRemote page : downloads
00787 
00788 void CRemote::PageDownloads()
00789 {
00790         if ( CheckCookie() ) return;
00791         
00792         CSingleLock pLock( &DownloadGroups.m_pSection, TRUE );
00793         CString str;
00794         
00795         Prepare();
00796         str.Format( _T("%i"), rand() );
00797         Add( _T("random"), str );
00798         Output( _T("downloadsHeader") );
00799         
00800         BOOL bExclusive = ! GetKey( _T("group_exclusive") ).IsEmpty();
00801         BOOL bReveal = ! GetKey( _T("group_reveal") ).IsEmpty();
00802         
00803         for ( POSITION posGroup = DownloadGroups.GetIterator() ; posGroup != NULL ; )
00804         {
00805                 CDownloadGroup* pGroup = DownloadGroups.GetNext( posGroup );
00806                 
00807                 str.Format( _T("%i"), pGroup );
00808                 Add( _T("group_id"), str );
00809                 
00810                 if ( bExclusive )
00811                 {
00812                         pGroup->m_bRemoteSelected = ( GetKey( _T("group_exclusive") ) == str );
00813                 }
00814                 else
00815                 {
00816                         if ( bReveal ) pGroup->m_bRemoteSelected = TRUE;
00817                         else if ( GetKey( _T("group_select") ) == str ) pGroup->m_bRemoteSelected = TRUE;
00818                         else if ( GetKey( _T("group_deselect") ) == str ) pGroup->m_bRemoteSelected = FALSE;
00819                 }
00820                 
00821                 Add( _T("group_caption"), pGroup->m_sName );
00822                 if ( pGroup->m_bRemoteSelected ) Add( _T("group_selected"), _T("true") );
00823                 Output( _T("downloadsTab") );
00824                 Prepare( _T("group_") );
00825         }
00826         
00827         if ( ! GetKey( _T("filter_set") ).IsEmpty() )
00828         {
00829                 Settings.Downloads.FilterMask &= ~( DLF_ACTIVE | DLF_QUEUED | DLF_SOURCES | DLF_PAUSED );
00830                 if ( GetKey( _T("filter_active") ) == _T("1") ) Settings.Downloads.FilterMask |= DLF_ACTIVE;
00831                 if ( GetKey( _T("filter_queued") ) == _T("1") ) Settings.Downloads.FilterMask |= DLF_QUEUED;
00832                 if ( GetKey( _T("filter_sources") ) == _T("1") ) Settings.Downloads.FilterMask |= DLF_SOURCES;
00833                 if ( GetKey( _T("filter_paused") ) == _T("1") ) Settings.Downloads.FilterMask |= DLF_PAUSED;
00834                 Settings.Downloads.ShowSources = ( GetKey( _T("filter_show_all") ) == _T("1") );
00835         }
00836         
00837         Add( _T("filter_active"), ( Settings.Downloads.FilterMask & DLF_ACTIVE ) ? _T("checked") : _T("") );
00838         Add( _T("filter_queued"), ( Settings.Downloads.FilterMask & DLF_QUEUED ) ? _T("checked") : _T("") );
00839         Add( _T("filter_sources"), ( Settings.Downloads.FilterMask & DLF_SOURCES ) ? _T("checked") : _T("") );
00840         Add( _T("filter_paused"), ( Settings.Downloads.FilterMask & DLF_PAUSED ) ? _T("checked") : _T("") );
00841         Add( _T("filter_show_all"), Settings.Downloads.ShowSources ? _T("checked") : _T("") );
00842         Output( _T("downloadsTop") );
00843         
00844         for ( POSITION posDownload = Downloads.GetIterator() ; posDownload != NULL ; )
00845         {
00846                 CDownload* pDownload = Downloads.GetNext( posDownload );
00847                 str.Format( _T("%i"), pDownload );
00848                 
00849                 if ( GetKey( _T("modify_id") ) == str )
00850                 {
00851                         CString str( GetKey( _T("modify_action") ) );
00852                         CharLower( str.GetBuffer() );
00853                         str.ReleaseBuffer();
00854                         
00855                         if ( str == _T("expand") && CDownloadsCtrl::IsExpandable( pDownload ) )
00856                         {
00857                                 pDownload->m_bExpanded = TRUE;
00858                         }
00859                         else if ( str == _T("collapse") && CDownloadsCtrl::IsExpandable( pDownload ) )
00860                         {
00861                                 pDownload->m_bExpanded = FALSE;
00862                         }
00863                         else if ( str == _T("resume") )
00864                         {
00865                                 pDownload->Resume();
00866                         }
00867                         else if ( str == _T("pause") )
00868                         {
00869                                 if ( ! pDownload->IsPaused() && ! pDownload->IsMoving() ) pDownload->Pause();
00870                         }
00871                         else if ( str == _T("cancel") )
00872                         {
00873                                 pDownload->Remove();
00874                                 continue;
00875                         }
00876                         else if ( str == _T("clear") )
00877                         {
00878                                 if ( pDownload->IsCompleted() && ! pDownload->IsPreviewVisible() )
00879                                 {
00880                                         pDownload->Remove();
00881                                         continue;
00882                                 }
00883                         }
00884                         // roo_koo_too improvement
00885                         else if ( str == _T("more_sources"))
00886                         { 
00887                                 pDownload->FindMoreSources();
00888                         }
00889                         str.Format( _T("%i"), pDownload );
00890                 }
00891                 
00892                 if ( CDownloadsCtrl::IsFiltered( pDownload ) ) continue;
00893                 
00894                 CDownloadGroup* pGroup = NULL;
00895                 
00896                 for ( POSITION posGroup = DownloadGroups.GetIterator() ; posGroup != NULL ; )
00897                 {
00898                         pGroup = DownloadGroups.GetNext( posGroup );
00899                         if ( pGroup->m_bRemoteSelected && pGroup->Contains( pDownload ) ) break;
00900                         pGroup = NULL;
00901                 }
00902                 
00903                 if ( pGroup == NULL ) continue;
00904                 
00905                 CString strStatus1, strStatus2;
00906                 Add( _T("download_id"), str );
00907                 Add( _T("download_filename"), pDownload->GetDisplayName() );
00908                 LoadString( strStatus1, IDS_STATUS_UNKNOWN );
00909                 Add( _T("download_size"), pDownload->m_nSize == SIZE_UNKNOWN ? strStatus1 : Settings.SmartVolume( pDownload->m_nSize, FALSE ) );
00910                 float nProgress = ( pDownload->IsCompleted() || pDownload->IsMoving() ) ? 1.0f : pDownload->GetProgress();
00911                 str.Format( _T("%i"), (int)( 100.0f * nProgress ) );
00912                 Add( _T("download_percent"), str );
00913                 str.Format( _T("%i"), (int)( 100.0f * ( 1.0f - nProgress ) ) );
00914                 Add( _T("download_percent_inverse"), str );
00915                 Add( _T("download_speed"), Settings.SmartVolume( pDownload->GetMeasuredSpeed() * 8, FALSE, TRUE ) );
00916                 if ( CDownloadsCtrl::IsExpandable( pDownload ) )
00917                 {
00918                         if ( pDownload->m_bExpanded ) Add( _T("download_is_expanded"), _T("true") );
00919                         else Add( _T("download_is_collapsed"), _T("true") );
00920                 }
00921                 if ( pDownload->IsCompleted() )
00922                 {
00923                         LoadString( strStatus1, IDS_STATUS_SEEDING );
00924                         LoadString( strStatus2, IDS_STATUS_COMPLETED );
00925                         str = pDownload->IsSeeding() ? strStatus1 : strStatus2;
00926                         Add( _T("download_is_complete"), _T("true") );
00927                 }
00928                 else if ( pDownload->IsMoving() )
00929                 {
00930                         LoadString( str, IDS_STATUS_MOVING );
00931                 }
00932                 else if ( pDownload->IsPaused() )
00933                 {
00934                         Add( _T("download_is_paused"), _T("true") );
00935                         if ( pDownload->m_bDiskFull )
00936                         {
00937                                 LoadString( strStatus1, IDS_STATUS_CANTMOVE );
00938                                 LoadString( strStatus2, IDS_STATUS_FILEERROR );
00939                                 str = ( pDownload->IsCompleted() ) ? strStatus1 : strStatus2;
00940                         }
00941                         else
00942                                 LoadString( str, IDS_STATUS_PAUSED );
00943                 }
00944                 else if ( pDownload->GetProgress() == 1.0f && pDownload->IsStarted() )
00945                         LoadString( str, IDS_STATUS_VERIFYING );
00946                 else if ( pDownload->IsDownloading() )
00947                 {
00948                         DWORD tNow = pDownload->GetTimeRemaining();
00949                         if ( tNow == 0xFFFFFFFF )
00950                                 LoadString( str, IDS_STATUS_ACTIVE );
00951                         else
00952                         {
00953                                 if ( tNow > 86400 )
00954                                         str.Format( _T("%i:%.2i:%.2i:%.2i"), tNow / 86400, ( tNow / 3600 ) % 24, ( tNow / 60 ) % 60, tNow % 60 );
00955                                 else
00956                                         str.Format( _T("%i:%.2i:%.2i"), tNow / 3600, ( tNow / 60 ) % 60, tNow % 60 );
00957                         }
00958                 }
00959                 else if ( pDownload->GetSourceCount() > 0 )
00960                         LoadString( str, IDS_STATUS_PENDING );
00961                 else if ( pDownload->m_bBTH )
00962                 {
00963                         if ( pDownload->IsTasking() )
00964                                 LoadString( str, IDS_STATUS_CREATING );
00965                         else if ( pDownload->m_bTorrentTrackerError )
00966                                 LoadString( str, IDS_STATUS_TRACKERDOWN );
00967                         else
00968                                 LoadString( str, IDS_STATUS_TORRENT );
00969                 }
00970                 else
00971                         LoadString( str, IDS_STATUS_SEARCHING );
00972                 Add( _T("download_status"), str );
00973                 if ( pDownload->IsCompleted() )
00974                 {
00975                         if ( pDownload->m_bVerify == TS_TRUE )
00976                                 LoadString( str, IDS_STATUS_VERIFIED );
00977                         else if ( pDownload->m_bVerify == TS_FALSE )
00978                                 LoadString( str, IDS_STATUS_UNVERIFIED );
00979                 }
00980                 // roo_koo_too source count fix
00981                 else if ( pDownload->GetSourceCount() == 1 )
00982                 {
00983                         CString strSC;
00984                         LoadSourcesString( strSC, 1 );
00985                         str.Format( _T("(1 %s)"), strSC );
00986                 }
00987                 else if ( pDownload->GetSourceCount() > 1 )
00988                 {
00989                         int nSources = pDownload->GetSourceCount();
00990                         CString strSC;
00991                         LoadSourcesString( strSC, nSources );
00992                         str.Format( _T("(%i %s)"), nSources, strSC );
00993                 }
00994                 else
00995                         LoadString( str, IDS_STATUS_NOSOURCES );
00996                 Add( _T("download_sources"), str );
00997                 Output( _T("downloadsDownload") );
00998                 
00999                 if ( pDownload->m_bExpanded && CDownloadsCtrl::IsExpandable( pDownload ) )
01000                 {
01001                         for ( CDownloadSource* pSource = pDownload->GetFirstSource(), *pNext = NULL ; pSource != NULL ; pSource = pNext )
01002                         {
01003                                 pNext = pSource->m_pNext;
01004                                 str.Format( _T("%i"), pSource );
01005                                 
01006                                 if ( GetKey( _T("modify_id") ) == str )
01007                                 {
01008                                         str = GetKey( _T("modify_action") );
01009                                         CharLower( str.GetBuffer() );
01010                                         str.ReleaseBuffer();
01011                                         
01012                                         if ( str == _T("access") )
01013                                         {
01014                                                 pDownload->Resume();
01015                                                 if ( pSource->m_bPushOnly )
01016                                                         pSource->PushRequest();
01017                                                 else if ( pSource->m_pTransfer == NULL ) // Only create a new Transfer if there isn't already one 
01018                                                 {       
01019                                                         if ( CDownloadTransfer* pTransfer = pSource->CreateTransfer() )
01020                                                                 pTransfer->Initiate();
01021                                                 }
01022                                         }
01023                                         else if ( str == _T("forget") )
01024                                         {
01025                                                 pSource->Remove( TRUE, TRUE );
01026                                                 continue;
01027                                         }
01028                                         
01029                                         str.Format( _T("%i"), pSource );
01030                                 }
01031                                 
01032                                 if ( Settings.Downloads.ShowSources || ( pSource->m_pTransfer != NULL && pSource->m_pTransfer->m_nState > dtsConnecting ) )
01033                                 {
01034                                         Add( _T("source_id"), str );
01035                                         Add( _T("source_agent"), pSource->m_sServer );
01036                                         Add( _T("source_nick"), pSource->m_sNick );
01037                                         
01038                                         if ( pSource->m_pTransfer != NULL )
01039                                         {
01040                                                 Add( _T("source_status"), pSource->m_pTransfer->GetStateText( FALSE ) );
01041                                                 Add( _T("source_volume"), Settings.SmartVolume( pSource->m_pTransfer->m_nDownloaded, FALSE ) );
01042                                                 if ( DWORD nSpeed = pSource->m_pTransfer->GetMeasuredSpeed() * 8 )
01043                                                         Add( _T("source_speed"), Settings.SmartVolume( nSpeed, FALSE, TRUE ) );
01044                                                 Add( _T("source_address"), pSource->m_pTransfer->m_sAddress );
01045                                                 Add( _T("source_caption"), pSource->m_pTransfer->m_sAddress + _T(" - ") + pSource->m_sNick );
01046                                         }
01047                                         else
01048                                         {
01049                                                 Add( _T("source_address"), CString( inet_ntoa( pSource->m_pAddress ) ) );
01050                                                 Add( _T("source_caption"), CString( inet_ntoa( pSource->m_pAddress ) ) + _T(" - ") + pSource->m_sNick );
01051                                                 
01052                                                 if ( pSource->m_tAttempt > 0 )
01053                                                 {
01054                                                         DWORD tNow = GetTickCount();
01055                                                         
01056                                                         if ( pSource->m_tAttempt >= tNow )
01057                                                         {
01058                                                                 tNow = ( pSource->m_tAttempt - tNow ) / 1000;
01059                                                                 str.Format( _T("%.2u:%.2u"), tNow / 60, tNow % 60 );
01060                                                                 Add( _T("source_status"), str );
01061                                                         }
01062                                                 }
01063                                         }
01064                                         
01065                                         Output( _T("downloadsSource") );
01066                                         Prepare( _T("source_") );
01067                                 }
01068                         }
01069                 }
01070                 
01071                 Prepare( _T("download_") );
01072         }
01073         
01074         Output( _T("downloadsBottom") );
01075         Output( _T("downloadsFooter") );
01076 }
01077 
01079 // CRemote page : newDownload
01080 
01081 void CRemote::PageNewDownload()
01082 {
01083         if ( CheckCookie() ) return;
01084         
01085         CShareazaURL pURI;
01086         if ( pURI.Parse( GetKey( _T("uri") ) ) ) Downloads.Add( &pURI );
01087         
01088         m_sRedirect = _T("downloads?group_reveal=all");
01089 }
01090 
01092 // CRemote page : uploads
01093 
01094 void CRemote::PageUploads()
01095 {
01096         if ( CheckCookie() ) return;
01097         
01098         CString str;
01099         
01100         Prepare();
01101         str.Format( _T("%i"), rand() );
01102         Add( _T("random"), str );
01103         Output( _T("uploadsHeader") );
01104         
01105         for ( POSITION posQueue = CUploadsCtrl::GetQueueIterator() ; posQueue != NULL ; )
01106         {
01107                 CUploadQueue* pQueue = CUploadsCtrl::GetNextQueue( posQueue );
01108                 
01109                 str.Format( _T("%i"), pQueue );
01110                 
01111                 if ( GetKey( _T("queue_expand") ) == str ) pQueue->m_bExpanded = TRUE;
01112                 else if ( GetKey( _T("queue_collapse") ) == str ) pQueue->m_bExpanded = FALSE;
01113                 
01114                 POSITION posFile = CUploadsCtrl::GetFileIterator( pQueue );
01115                 if ( posFile == NULL ) continue;
01116                 
01117                 Prepare();
01118                 Add( _T("queue_id"), str );
01119                 Add( _T("queue_caption"), pQueue->m_sName );
01120                 if ( pQueue->m_bExpanded ) Add( _T("queue_expanded"), _T("true") );
01121                 
01122                 if ( pQueue != UploadQueues.m_pTorrentQueue && pQueue != UploadQueues.m_pHistoryQueue )
01123                 {
01124                         str.Format( _T("%i"), pQueue->GetTransferCount() );
01125                         Add( _T("queue_transfers"), str );
01126                         str.Format( _T("%i"), pQueue->GetQueuedCount() );
01127                         Add( _T("queue_queued"), str );
01128                         Add( _T("queue_bandwidth"), Settings.SmartVolume( pQueue->GetMeasuredSpeed() * 8, FALSE, TRUE ) );
01129                 }
01130                 
01131                 Output( _T("uploadsQueueStart") );
01132                 
01133                 if ( pQueue->m_bExpanded )
01134                 {
01135                         while ( posFile != NULL )
01136                         {
01137                                 int nPosition;
01138                                 CUploadFile* pFile = CUploadsCtrl::GetNextFile( pQueue, posFile, &nPosition );
01139                                 if ( pFile == NULL ) continue;
01140                                 CUploadTransfer* pTransfer = pFile->GetActive();
01141                                 
01142                                 str.Format( _T("%i"), pFile );
01143                                 
01144                                 if ( GetKey( _T("drop") ) == str )
01145                                 {
01146                                         pFile->Remove();
01147                                         continue;
01148                                 }
01149                                 
01150                                 Add( _T("file_id"), str );
01151                                 Add( _T("file_filename"), pFile->m_sName );
01152                                 Add( _T("file_size"), Settings.SmartVolume( pFile->m_nSize, FALSE ) );
01153                                 
01154                                 if ( pTransfer != NULL )
01155                                 {
01156                                         Add( _T("file_address"), pTransfer->m_sAddress );
01157                                         Add( _T("file_nick"), pTransfer->m_sNick );
01158                                         Add( _T("file_user"), pTransfer->m_sAddress + _T(" - ") + pTransfer->m_sNick );
01159                                         Add( _T("file_agent"), pTransfer->m_sUserAgent );
01160                                 }
01161                                 
01162                                 if ( pTransfer == NULL || pTransfer->m_nState == upsNull )
01163                                 {
01164                                         LoadString( str, IDS_STATUS_COMPLETED );
01165                                 }
01166                                 else if ( pTransfer->m_nProtocol == PROTOCOL_BT )
01167                                 {
01168                                         CUploadTransferBT* pBT = (CUploadTransferBT*)pTransfer;
01169                                         
01170                                         if ( ! pBT->m_bInterested )
01171                                                 LoadString( str, IDS_STATUS_UNINTERESTED );
01172                                         else if ( pBT->m_bChoked )
01173                                                 LoadString( str, IDS_STATUS_CHOKED );
01174                                         else if ( DWORD nSpeed = pTransfer->GetMeasuredSpeed() * 8 )
01175                                                 str = Settings.SmartVolume( nSpeed, FALSE, TRUE );
01176                                 }
01177                                 else if ( nPosition > 0 )
01178                                 {
01179                                         LoadString( str, IDS_STATUS_Q );
01180                                         str.Format( _T("%s %i"), str, nPosition );
01181                                 }
01182                                 else if ( DWORD nSpeed = pTransfer->GetMeasuredSpeed() * 8 )
01183                                         str = Settings.SmartVolume( nSpeed, FALSE, TRUE );
01184                                 else
01185                                         LoadString( str, IDS_STATUS_NEXT );
01186                                 Add( _T("file_speed"), str );
01187                                 Add( _T("file_status"), str );
01188                                 
01189                                 Output( _T("uploadsFile") );
01190                                 Prepare( _T("file_") );
01191                         }
01192                 }
01193                 
01194                 Output( _T("uploadsQueueEnd") );
01195                 Prepare( _T("queue_") );
01196         }
01197         
01198         Prepare();
01199         Output( _T("uploadsFooter") );
01200 }
01201 
01203 // CRemote page : network
01204 
01205 void CRemote::PageNetwork()
01206 {
01207         if ( CheckCookie() ) return;
01208         
01209         CSingleLock pLock( &Network.m_pSection );
01210         if ( ! pLock.Lock( 1000 ) ) return;
01211         
01212         DWORD nNeighbourID = 0;
01213         _stscanf( GetKey( _T("drop") ), _T("%i"), &nNeighbourID );
01214         
01215         if ( nNeighbourID != 0 )
01216         {
01217                 if ( CNeighbour* pNeighbour = Neighbours.Get( nNeighbourID ) )
01218                 {
01219                         pNeighbour->Close();
01220                 }
01221         }
01222         
01223         CString str;
01224         
01225         Prepare();
01226         str.Format( _T("%i"), rand() );
01227         Add( _T("random"), str );
01228         Output( _T("networkHeader") );
01229         
01230         PageNetworkNetwork( PROTOCOL_G2, &Settings.Gnutella2.EnableToday, _T("Gnutella2") );
01231         PageNetworkNetwork( PROTOCOL_G1, &Settings.Gnutella1.EnableToday, _T("Gnutella1") );
01232         PageNetworkNetwork( PROTOCOL_ED2K, &Settings.eDonkey.EnableToday, _T("eDonkey2000") );
01233         
01234         Output( _T("networkFooter") );
01235 }
01236 
01237 void CRemote::PageNetworkNetwork(int nID, BOOL* pbConnect, LPCTSTR pszName)
01238 {
01239         CString str;
01240         
01241         str.Format( _T("%i"), nID );
01242         
01243         if ( GetKey( _T("connect") ) == str )
01244         {
01245                 *pbConnect = TRUE;
01246                 Network.Connect( TRUE );
01247         }
01248         else if ( GetKey( _T("disconnect") ) == str )
01249         {
01250                 *pbConnect = FALSE;
01251                 
01252                 for ( POSITION pos = Neighbours.GetIterator() ; pos != NULL ; )
01253                 {
01254                         CNeighbour* pNeighbour = Neighbours.GetNext( pos );
01255                         if ( pNeighbour->m_nProtocol == PROTOCOL_NULL ||
01256                                  pNeighbour->m_nProtocol == nID ) pNeighbour->Close();
01257                 }
01258         }
01259         
01260         Add( _T("network_id"), str );
01261         Add( _T("network_caption"), pszName );
01262         if ( *pbConnect ) Add( _T("network_connected"), _T("true") );
01263         Output( _T("networkNetStart") );
01264         
01265         for ( POSITION pos = Neighbours.GetIterator() ; pos != NULL ; )
01266         {
01267                 CNeighbour* pNeighbour = Neighbours.GetNext( pos );
01268                 if ( pNeighbour->m_nProtocol != nID ) continue;
01269                 pNeighbour->Measure();
01270                 
01271                 str.Format( _T("%i"), pNeighbour->m_nUnique );
01272                 Add( _T("row_id"), str );
01273                 Add( _T("row_address"), pNeighbour->m_sAddress );
01274                 Add( _T("row_agent"), pNeighbour->m_sUserAgent );
01275                 str.Format( _T("%i -/- %i"), pNeighbour->m_nInputCount, pNeighbour->m_nOutputCount );
01276                 Add( _T("row_packets"), str );
01277                 str.Format( _T("%s -/- %s"), (LPCTSTR)Settings.SmartVolume( pNeighbour->m_mInput.nMeasure * 8, FALSE, TRUE ), (LPCTSTR)Settings.SmartVolume( pNeighbour->m_mOutput.nMeasure * 8, FALSE, TRUE ) );
01278                 Add( _T("row_bandwidth"), str );
01279                 str.Format( _T("%s -/- %s"), (LPCTSTR)Settings.SmartVolume( pNeighbour->m_mInput.nTotal, FALSE ), (LPCTSTR)Settings.SmartVolume( pNeighbour->m_mOutput.nTotal, FALSE ) );
01280                 Add( _T("row_total"), str );
01281                 
01282                 switch ( pNeighbour->m_nState )
01283                 {
01284                 case nrsConnecting:
01285                         LoadString( str, IDS_NEIGHBOUR_CONNECTING );
01286                         break;
01287                 case nrsHandshake1:
01288                 case nrsHandshake2:
01289                 case nrsHandshake3:
01290                         LoadString( str, IDS_NEIGHBOUR_HANDSHAKING );
01291                         break;
01292                 case nrsRejected:
01293                         LoadString( str, IDS_NEIGHBOUR_REJECTED );
01294                         break;
01295                 case nrsClosing:
01296                         LoadString( str, IDS_NEIGHBOUR_CLOSING );
01297                         break;
01298                 case nrsConnected:
01299                         {
01300                                 DWORD tNow = ( GetTickCount() - pNeighbour->m_tConnected ) / 1000;
01301                                 if ( tNow > 86400 )
01302                                         str.Format( _T("%i:%.2i:%.2i:%.2i"), tNow / 86400, ( tNow / 3600 ) % 24, ( tNow / 60 ) % 60, tNow % 60 );
01303                                 else
01304                                         str.Format( _T("%i:%.2i:%.2i"), tNow / 3600, ( tNow / 60 ) % 60, tNow % 60 );
01305                         }
01306                         break;
01307                 default:
01308                         LoadString( str, IDS_NEIGHBOUR_UNKNOWN );
01309                         break;
01310                 }
01311                 Add( _T("row_time"), str );
01312                 
01313                 if ( pNeighbour->m_nProtocol == PROTOCOL_G1 )
01314                 {
01315                         CG1Neighbour* pG1 = reinterpret_cast<CG1Neighbour*>(pNeighbour);
01316                         
01317                         switch ( pNeighbour->m_nNodeType )
01318                         {
01319                         case ntNode:
01320                                 LoadString( str, IDS_NEIGHBOUR_G1PEER );
01321                                 break;
01322                         case ntHub:
01323                                 LoadString( str, IDS_NEIGHBOUR_G1ULTRA );
01324                                 break;
01325                         case ntLeaf:
01326                                 LoadString( str, IDS_NEIGHBOUR_G1LEAF );
01327                                 break;
01328                         }
01329                         
01330                         Add( _T("row_mode"), str );
01331                         str.Empty();
01332                 }
01333                 else if ( pNeighbour->m_nProtocol == PROTOCOL_G2 )
01334                 {
01335                         CG2Neighbour* pG2 = reinterpret_cast<CG2Neighbour*>(pNeighbour);
01336                         
01337                         switch ( pNeighbour->m_nNodeType )
01338                         {
01339                         case ntNode:
01340                                 LoadString( str, IDS_NEIGHBOUR_G2PEER );
01341                                 break;
01342                         case ntHub:
01343                                 LoadString( str, IDS_NEIGHBOUR_G2HUB );
01344                                 break;
01345                         case ntLeaf:
01346                                 LoadString( str, IDS_NEIGHBOUR_G2LEAF );
01347                                 break;
01348                         }
01349                         
01350                         Add( _T("row_mode"), str );
01351                         
01352                         if ( pG2->m_nLeafCount > 0 )
01353                         {
01354                                 if ( pG2->m_nLeafLimit > 0 )
01355                                 {
01356                                         str.Format( _T("%i/%i"), pG2->m_nLeafCount, pG2->m_nLeafLimit );
01357                                 }
01358                                 else
01359                                 {
01360                                         str.Format( _T("%i"), pG2->m_nLeafCount );
01361                                 }
01362                                 
01363                                 Add( _T("row_leaves"), str );
01364                         }
01365                         
01366                         str.Empty();
01367                         if ( pG2->m_pProfile != NULL ) str = pG2->m_pProfile->GetNick();
01368                 }
01369                 else if ( pNeighbour->m_nProtocol == PROTOCOL_ED2K )
01370                 {
01371                         CEDNeighbour* pED2K = reinterpret_cast<CEDNeighbour*>(pNeighbour);
01372                         
01373                         if ( pED2K->m_nClientID > 0 )
01374                         {
01375                                 if ( pED2K->m_nUserLimit > 0 )
01376                                 {
01377                                         str.Format( _T("%i/%i"), pED2K->m_nUserCount, pED2K->m_nUserLimit );
01378                                 }
01379                                 else
01380                                 {
01381                                         str.Format( _T("%i"), pED2K->m_nUserCount );
01382                                 }
01383                                 
01384                                 Add( _T("row_leaves"), str );
01385                                 CString strText1, strText2;
01386                                 LoadString( strText1, IDS_NEIGHBOUR_ED2K_LOWID );
01387                                 LoadString( strText2, IDS_NEIGHBOUR_ED2K_HIGHID );
01388                                 Add( _T("row_mode"), CEDPacket::IsLowID( pED2K->m_nClientID ) ? strText1 : strText2 );
01389                         }
01390                         else
01391                         {
01392                                 Add( _T("row_mode"), _T("eDonkey2000") );
01393                         }
01394                         
01395                         str = pED2K->m_sServerName;
01396                 }
01397                 
01398                 Add( _T("row_nick"), str );
01399                 str = pNeighbour->m_sAddress + _T(" - ") + str;
01400                 Add( _T("row_caption"), str );
01401                 
01402                 Output( _T("networkRow") );
01403                 Prepare( _T("row_") );
01404         }
01405         
01406         Output( _T("networkNetEnd") );
01407         Prepare( _T("network_") );
01408 }
01409 
01411 // CRemote page : banner
01412 
01413 void CRemote::PageBanner(CString& strPath)
01414 {
01415         HMODULE hModule = GetModuleHandle( NULL );
01416         UINT nID = 0;
01417         
01418         if ( strPath == _T("/remote/header_1.png") )
01419                 nID = IDR_HOME_HEADER_1;
01420         else if ( strPath == _T("/remote/header_2.png") )
01421                 nID = IDR_HOME_HEADER_2;
01422         
01423         if ( HRSRC hRes = FindResource( hModule, MAKEINTRESOURCE( nID ), _T("PNG") ) )
01424         {
01425                 DWORD nSize                     = SizeofResource( hModule, hRes );
01426                 HGLOBAL hMemory         = LoadResource( hModule, hRes );
01427                 LPVOID pSource          = LockResource( hMemory );
01428                 
01429                 m_pResponse.EnsureBuffer( nSize );
01430                 CopyMemory( m_pResponse.m_pBuffer, pSource, nSize );
01431                 m_pResponse.m_nLength = nSize;
01432                 m_sHeader = _T("Cache-Control: max-age=259200\r\nContent-Type: image/png\r\n");
01433         }
01434 }
01435 
01437 // CRemote page : image server
01438 
01439 void CRemote::PageImage(CString& strPath)
01440 {
01441         if ( CheckCookie() ) return;
01442         
01443         strPath = strPath.Mid( 15 );
01444         if ( strPath.Find( '%' ) >= 0 ) return;
01445         if ( strPath.Find( '/' ) >= 0 ) return;
01446         if ( strPath.Find( '\\' ) >= 0 ) return;
01447         
01448         CFile hFile;
01449         strPath = Settings.General.Path + _T("\\Remote\\images\\") + strPath;
01450         
01451         if ( hFile.Open( strPath, CFile::modeRead ) )
01452         {
01453                 m_pResponse.EnsureBuffer( (DWORD)hFile.GetLength() );
01454                 hFile.Read( m_pResponse.m_pBuffer, (UINT)hFile.GetLength() );
01455                 m_pResponse.m_nLength += (DWORD)hFile.GetLength();
01456                 hFile.Close();
01457         }
01458 }

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