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

DDEServer.cpp

Go to the documentation of this file.
00001 //
00002 // DDEServer.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 "DDEServer.h"
00026 
00027 #include "ShareazaURL.h"
00028 #include "BTInfo.h"
00029 
00030 #ifdef _DEBUG
00031 #undef THIS_FILE
00032 static char THIS_FILE[]=__FILE__;
00033 #define new DEBUG_NEW
00034 #endif
00035 
00036 CDDEServer DDEServer( _T("Shareaza") );
00037 
00038 
00040 // CDDEServer construction
00041 
00042 CDDEServer* CDDEServer::m_pServer = NULL;
00043 
00044 
00045 CDDEServer::CDDEServer(LPCTSTR pszService)
00046 {
00047         m_pServer               = this;
00048         m_hInstance             = NULL;
00049         m_hszService    = NULL;
00050         m_sService              = pszService;
00051 }
00052 
00053 CDDEServer::~CDDEServer()
00054 {
00055         Close();
00056         m_pServer = NULL;
00057 }
00058 
00060 // CDDEServer create
00061 
00062 BOOL CDDEServer::Create()
00063 {
00064         USES_CONVERSION;
00065         DWORD dwFilterFlags = 0;
00066         UINT uiResult;
00067 
00068         uiResult = DdeInitialize( &m_hInstance, DDECallback, dwFilterFlags, 0 );
00069         if ( uiResult != DMLERR_NO_ERROR ) return FALSE;
00070 
00071         m_hszService = DdeCreateStringHandle( m_hInstance, (LPCTSTR)m_sService, CP_WINUNICODE );
00072 
00073 
00074     DdeNameService( m_hInstance, m_hszService, NULL, DNS_REGISTER );
00075 
00076         return TRUE;
00077 }
00078 
00080 // CDDEServer close
00081 
00082 void CDDEServer::Close()
00083 {
00084         if ( m_hInstance == NULL ) return;
00085 
00086         DdeNameService( m_hInstance, m_hszService, NULL, DNS_UNREGISTER );
00087 
00088         DdeFreeStringHandle( m_hInstance, m_hszService );
00089 
00090         DdeUninitialize( m_hInstance );
00091         m_hInstance = NULL;
00092 }
00093 
00095 // CDDEServer static callback
00096 
00097 HDDEDATA CALLBACK CDDEServer::DDECallback(UINT wType, UINT wFmt, HCONV hConv, HSZ hsz1, HSZ hsz2, HDDEDATA hData, DWORD dwData1, DWORD dwData2)
00098 {
00099         HDDEDATA hResult = NULL;
00100 
00101         if ( ! m_pServer ) return hResult;
00102 
00103         switch ( wType )
00104         {
00105         case XTYP_CONNECT:
00106 
00107                 hResult = m_pServer->CheckAccept( m_pServer->StringFromHsz( hsz1 ) ) ?
00108                         (HDDEDATA)TRUE : (HDDEDATA)FALSE;
00109                 break;
00110 
00111         case XTYP_CONNECT_CONFIRM:
00112 
00113                 // m_pServer->AddConversation( hConv, hsz1 );
00114                 break;
00115 
00116         case XTYP_DISCONNECT:
00117 
00118                 // m_pServer->RemoveConversation( hConv );
00119                 break;
00120 
00121         case XTYP_EXECUTE:
00122 
00123                 m_pServer->Execute( m_pServer->StringFromHsz( hsz1 ), hData, &hResult );
00124                 break;
00125 
00126         }
00127 
00128         return hResult;
00129 }
00130 
00132 // CDDEServer HSZ to string helper
00133 
00134 CString CDDEServer::StringFromHsz(HSZ hsz)
00135 {
00136         CString str;
00137 
00138         DWORD nLen = DdeQueryString( m_hInstance, hsz, NULL, 0, CP_WINUNICODE );
00139         if ( nLen == 0 ) return str;
00140 
00141         LPTSTR pBuf = new TCHAR[ nLen + 1 ];
00142         DdeQueryString( m_hInstance, hsz, pBuf, nLen + 1, CP_WINUNICODE );
00143         pBuf[nLen] = 0;
00144 
00145         str = pBuf;
00146         delete [] pBuf;
00147 
00148         return str;
00149 }
00150 
00152 // CDDEServer argument helper
00153 
00154 CString CDDEServer::ReadArgument(LPCTSTR& pszMessage)
00155 {
00156         BOOL bEscape = FALSE;
00157         CString strPath;
00158 
00159         for ( pszMessage += 7 ; *pszMessage ; pszMessage++ )
00160         {
00161                 if ( bEscape )
00162                 {
00163                         strPath += *pszMessage;
00164                         bEscape = FALSE;
00165                 }
00166                 else if ( *pszMessage == '\"' )
00167                 {
00168                         if ( pszMessage[1] == '\"' )
00169                         {
00170                                 strPath += '\"';
00171                                 pszMessage++;
00172                         }
00173                         else
00174                         {
00175                                 break;
00176                         }
00177                 }
00178                 else if ( *pszMessage == '\\' )
00179                 {
00180                         bEscape = TRUE;
00181                 }
00182                 else
00183                 {
00184                         strPath += *pszMessage;
00185                 }
00186         }
00187 
00188         return strPath;
00189 }
00190 
00192 // CDDEServer check accept
00193 
00194 BOOL CDDEServer::CheckAccept(LPCTSTR pszTopic)
00195 {
00196         return  _tcsicmp( pszTopic, _T("URL") ) == 0 ||
00197                         _tcsicmp( pszTopic, _T("TORRENT") ) == 0 ||
00198                         _tcsicmp( pszTopic, _T("COLLECTION") ) == 0;
00199 }
00200 
00202 // CDDEServer execute HDDEDATA mode
00203 
00204 BOOL CDDEServer::Execute(LPCTSTR pszTopic, HDDEDATA hData, HDDEDATA* phResult)
00205 {
00206         DWORD nLength   = 0;
00207         LPVOID pData    = DdeAccessData( hData, &nLength );
00208 
00209         BOOL bResult = Execute( pszTopic, pData, nLength );
00210 
00211         DdeUnaccessData( hData );
00212 
00213         *phResult = (HDDEDATA)( bResult ? DDE_FACK : DDE_FNOTPROCESSED );
00214 
00215         return bResult;
00216 }
00217 
00219 // CDDEServer execute LPCVOID mode
00220 
00221 BOOL CDDEServer::Execute(LPCTSTR pszTopic, LPCVOID pData, DWORD nLength)
00222 {
00223         CString str;
00224 
00225         if ( theApp.m_bNT )
00226         {
00227                 // Copy data info a buffer
00228                 LPWSTR pszData = new WCHAR[ nLength + 1 ];
00229                 CopyMemory( pszData, pData, nLength );
00230                 // Ensure it has a null terminator
00231                 pszData[ nLength ] = 0;
00232                 // Assign it to the Cstring and remove buffer
00233                 str = pszData;
00234                 delete [] pszData;
00235         }
00236         else
00237         {
00238                 // Windows 9x will return the data as an ASCII string. (even though UNICODE was specified)
00239                 int nWide = MultiByteToWideChar( CP_ACP, 0, (LPCSTR)pData, (int)nLength, NULL, 0 );
00240                 MultiByteToWideChar( CP_ACP, 0, (LPCSTR)pData, (int)nLength, str.GetBuffer( nWide ), nWide );
00241                 str.ReleaseBuffer( nWide );
00242         }
00243 
00244         return Execute( pszTopic, str );
00245 }
00246 
00248 // CDDEServer execute string mode
00249 
00250 BOOL CDDEServer::Execute(LPCTSTR pszTopic, LPCTSTR pszMessage)
00251 {
00252         CWnd* pWnd = AfxGetMainWnd();
00253 
00254         if ( _tcscmp( pszTopic, _T("URL") ) == 0 )
00255         {
00256                 theApp.Message( MSG_SYSTEM, IDS_URL_RECEIVED, pszMessage );
00257 
00258                 CShareazaURL* pURL = new CShareazaURL();
00259 
00260                 if ( pURL->Parse( pszMessage ) )
00261                 {
00262                         if ( ! pWnd->PostMessage( WM_URL, (WPARAM)pURL ) ) delete pURL;
00263                         return TRUE;
00264                 }
00265 
00266                 delete pURL;
00267                 theApp.Message( MSG_ERROR, IDS_URL_PARSE_ERROR );
00268         }
00269         else if ( _tcscmp( pszTopic, _T("TORRENT") ) == 0 )
00270         {
00271                 theApp.Message( MSG_SYSTEM, IDS_BT_PREFETCH_FILE, pszMessage );
00272 
00273                 CBTInfo* pTorrent = new CBTInfo();
00274 
00275                 if ( pTorrent->LoadTorrentFile( pszMessage ) )
00276                 {
00277                         if ( pTorrent->HasEncodingError() ) theApp.Message( MSG_SYSTEM, _T("Possible encoding error detected while parsing torrent") );
00278                         CShareazaURL* pURL = new CShareazaURL( pTorrent );
00279                         if ( ! pWnd->PostMessage( WM_URL, (WPARAM)pURL ) ) delete pURL;
00280                         return TRUE;
00281                 }
00282 
00283                 delete pTorrent;
00284                 theApp.Message( MSG_ERROR, IDS_BT_PREFETCH_ERROR, pszMessage );
00285         }
00286         else if ( _tcscmp( pszTopic, _T("COLLECTION") ) == 0 )
00287         {
00288                 LPTSTR pszPath = new TCHAR[ _tcslen(pszMessage) + 1 ];
00289                 CopyMemory( pszPath, pszMessage, sizeof(TCHAR) * ( _tcslen(pszMessage) + 1 ) );
00290 
00291                 if ( pWnd->PostMessage( WM_COLLECTION, (WPARAM)pszPath ) )
00292                 {
00293                         return TRUE;
00294                 }
00295                 else
00296                 {
00297                         delete [] pszPath;
00298                 }
00299         }
00300 
00301         return FALSE;
00302 }
00303 

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