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

Plugins.cpp

Go to the documentation of this file.
00001 //
00002 // Plugins.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 "Plugins.h"
00026 #include "Application.h"
00027 #include "CtrlCoolBar.h"
00028 #include "WndPlugin.h"
00029 
00030 #ifdef _DEBUG
00031 #undef THIS_FILE
00032 static char THIS_FILE[]=__FILE__;
00033 #define new DEBUG_NEW
00034 #endif
00035 
00036 CPlugins Plugins;
00037 
00038 
00040 // CPlugins construction
00041 
00042 CPlugins::CPlugins()
00043 {
00044         m_nCommandID = ID_PLUGIN_FIRST;
00045 }
00046 
00047 CPlugins::~CPlugins()
00048 {
00049         Clear();
00050 }
00051 
00053 // CPlugins enumerate
00054 
00055 void CPlugins::Enumerate()
00056 {
00057         HKEY hKey;
00058 
00059         if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
00060                 _T("Software\\Shareaza\\Shareaza\\Plugins\\General"),
00061                 NULL, KEY_READ, &hKey ) != ERROR_SUCCESS ) return;
00062 
00063         for ( DWORD nKey = 0 ; ; nKey++ )
00064         {
00065                 TCHAR szName[128], szCLSID[64];
00066                 DWORD dwType, dwName = 128, dwCLSID = 64 * sizeof(TCHAR);
00067 
00068                 if ( RegEnumValue( hKey, nKey, szName, &dwName, NULL, &dwType, (LPBYTE)szCLSID, &dwCLSID )
00069                          != ERROR_SUCCESS ) break;
00070 
00071                 if ( dwType != REG_SZ ) continue;
00072                 szCLSID[ 38 ] = 0;
00073 
00074                 CLSID pCLSID;
00075                 if ( ! GUIDX::Decode( szCLSID, &pCLSID ) ) continue;
00076 
00077                 for ( POSITION pos = GetIterator() ; pos ; )
00078                 {
00079                         if ( GetNext( pos )->m_pCLSID == pCLSID )
00080                         {
00081                                 pCLSID = GUID_NULL;
00082                                 break;
00083                         }
00084                 }
00085 
00086                 if ( pCLSID == GUID_NULL ) continue;
00087 
00088                 CPlugin* pPlugin = new CPlugin( pCLSID, szName );
00089                 m_pList.AddTail( pPlugin );
00090 
00091                 pPlugin->StartIfEnabled();
00092         }
00093 
00094         RegCloseKey( hKey );
00095 }
00096 
00098 // CPlugins clear
00099 
00100 void CPlugins::Clear()
00101 {
00102         for ( POSITION pos = GetIterator() ; pos ; )
00103         {
00104                 delete GetNext( pos );
00105         }
00106 
00107         m_pList.RemoveAll();
00108 }
00109 
00111 // CPlugins CLSID helpers
00112 
00113 BOOL CPlugins::LookupCLSID(LPCTSTR pszGroup, LPCTSTR pszKey, CLSID& pCLSID, BOOL bEnableDefault)
00114 {
00115         DWORD dwType, dwCLSID;
00116         TCHAR szCLSID[64];
00117         CString strKey;
00118         HKEY hKey;
00119 
00120         strKey.Format( _T("Software\\Shareaza\\Shareaza\\Plugins\\%s"), pszGroup );
00121 
00122         if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, strKey,
00123                 NULL, KEY_READ, &hKey ) == ERROR_SUCCESS )
00124         {
00125                 dwType = REG_SZ;
00126                 dwCLSID = 64 * sizeof(TCHAR);
00127                 if ( ERROR_SUCCESS != RegQueryValueEx( hKey, pszKey, NULL, &dwType,
00128                         (LPBYTE)szCLSID, &dwCLSID ) ) dwType = 0;
00129                 RegCloseKey( hKey );
00130 
00131                 if ( dwType == REG_SZ )
00132                 {
00133                         szCLSID[ 38 ] = 0;
00134 
00135                         return  GUIDX::Decode( szCLSID, &pCLSID ) &&
00136                                         LookupEnable( pCLSID, bEnableDefault );
00137                 }
00138         }
00139 
00140         return FALSE;
00141 }
00142 
00143 BOOL CPlugins::LookupEnable(REFCLSID pCLSID, BOOL bDefault)
00144 {
00145         CString strCLSID = GUIDX::Encode( &pCLSID );
00146         return theApp.GetProfileInt( _T("Plugins"), strCLSID, bDefault );
00147 }
00148 
00150 // CPlugins skin changed event
00151 
00152 void CPlugins::OnSkinChanged()
00153 {
00154         for ( POSITION pos = GetIterator() ; pos ; )
00155         {
00156                 CPlugin* pPlugin = GetNext( pos );
00157 
00158                 if ( pPlugin->m_pCommand ) pPlugin->m_pCommand->InsertCommands();
00159                 if ( pPlugin->m_pPlugin ) pPlugin->m_pPlugin->OnSkinChanged();
00160         }
00161 }
00162 
00164 // CPlugins command ID registration
00165 
00166 void CPlugins::RegisterCommands()
00167 {
00168         m_nCommandID = ID_PLUGIN_FIRST;
00169 
00170         for ( POSITION pos = GetIterator() ; pos ; )
00171         {
00172                 CPlugin* pPlugin = GetNext( pos );
00173                 if ( pPlugin->m_pCommand ) pPlugin->m_pCommand->RegisterCommands();
00174         }
00175 }
00176 
00177 UINT CPlugins::GetCommandID()
00178 {
00179         return m_nCommandID++;
00180 }
00181 
00183 // CPlugins command handling
00184 
00185 BOOL CPlugins::OnUpdate(CChildWnd* pActiveWnd, CCmdUI* pCmdUI)
00186 {
00187         UINT nCommandID         = pCmdUI->m_nID;
00188         STRISTATE bVisible      = TSTRUE;
00189         STRISTATE bEnabled      = TSTRUE;
00190         STRISTATE bChecked      = TSUNKNOWN;
00191 
00192         CCoolBarItem* pCoolUI = CCoolBarItem::FromCmdUI( pCmdUI );
00193 
00194         if ( pActiveWnd != NULL && pActiveWnd->IsKindOf( RUNTIME_CLASS(CPluginWnd) ) )
00195         {
00196                 CPluginWnd* pPluginWnd = (CPluginWnd*)pActiveWnd;
00197 
00198                 if ( pPluginWnd->m_pOwner )
00199                 {
00200                         if ( pPluginWnd->m_pOwner->OnUpdate( nCommandID, &bVisible, &bEnabled, &bChecked ) == S_OK )
00201                         {
00202                                 if ( bVisible != TSUNKNOWN && pCoolUI != NULL )
00203                                         pCoolUI->Show( bVisible == TSTRUE );
00204                                 if ( bEnabled != TSUNKNOWN )
00205                                         pCmdUI->Enable( bEnabled == TSTRUE );
00206                                 if ( bChecked != TSUNKNOWN )
00207                                         pCmdUI->SetCheck( bChecked == TSTRUE );
00208 
00209                                 return TRUE;
00210                         }
00211                 }
00212         }
00213 
00214         for ( POSITION pos = GetIterator() ; pos ; )
00215         {
00216                 CPlugin* pPlugin = GetNext( pos );
00217 
00218                 if ( pPlugin->m_pCommand )
00219                 {
00220                         if ( pPlugin->m_pCommand->OnUpdate( nCommandID, &bVisible, &bEnabled, &bChecked ) == S_OK )
00221                         {
00222                                 if ( bVisible != TSUNKNOWN && pCoolUI != NULL )
00223                                         pCoolUI->Show( bVisible == TSTRUE );
00224                                 if ( bEnabled != TSUNKNOWN )
00225                                         pCmdUI->Enable( bEnabled == TSTRUE );
00226                                 if ( bChecked != TSUNKNOWN )
00227                                         pCmdUI->SetCheck( bChecked == TSTRUE );
00228 
00229                                 return TRUE;
00230                         }
00231                 }
00232         }
00233 
00234         return FALSE;
00235 }
00236 
00237 BOOL CPlugins::OnCommand(CChildWnd* pActiveWnd, UINT nCommandID)
00238 {
00239         if ( pActiveWnd != NULL && pActiveWnd->IsKindOf( RUNTIME_CLASS(CPluginWnd) ) )
00240         {
00241                 CPluginWnd* pPluginWnd = (CPluginWnd*)pActiveWnd;
00242 
00243                 if ( pPluginWnd->m_pOwner )
00244                 {
00245                         if ( pPluginWnd->m_pOwner->OnCommand( nCommandID ) == S_OK ) return TRUE;
00246                 }
00247         }
00248 
00249         for ( POSITION pos = GetIterator() ; pos ; )
00250         {
00251                 CPlugin* pPlugin = GetNext( pos );
00252 
00253                 if ( pPlugin->m_pCommand )
00254                 {
00255                         if ( pPlugin->m_pCommand->OnCommand( nCommandID ) == S_OK ) return TRUE;
00256                 }
00257         }
00258 
00259         return FALSE;
00260 }
00261 
00263 // CPlugins file execution events
00264 
00265 BOOL CPlugins::OnExecuteFile(LPCTSTR pszFile)
00266 {
00267         COleVariant vFile( pszFile );
00268         vFile.ChangeType( VT_BSTR );
00269 
00270         for ( POSITION pos = GetIterator() ; pos ; )
00271         {
00272                 CPlugin* pPlugin = GetNext( pos );
00273 
00274                 if ( pPlugin->m_pExecute )
00275                 {
00276                         if ( pPlugin->m_pExecute->OnExecute( vFile.bstrVal ) == S_OK )
00277                                 return TRUE;
00278                 }
00279         }
00280 
00281         return FALSE;
00282 }
00283 
00284 BOOL CPlugins::OnEnqueueFile(LPCTSTR pszFile)
00285 {
00286         COleVariant vFile( pszFile );
00287         vFile.ChangeType( VT_BSTR );
00288 
00289         for ( POSITION pos = GetIterator() ; pos ; )
00290         {
00291                 CPlugin* pPlugin = GetNext( pos );
00292 
00293                 if ( pPlugin->m_pExecute )
00294                 {
00295                         if ( pPlugin->m_pExecute->OnEnqueue( vFile.bstrVal ) == S_OK )
00296                                 return TRUE;
00297                 }
00298         }
00299 
00300         return FALSE;
00301 }
00302 
00303 
00305 // CPlugin construction
00306 
00307 CPlugin::CPlugin(REFCLSID pCLSID, LPCTSTR pszName)
00308 {
00309         m_pCLSID        = pCLSID;
00310         m_sName         = pszName;
00311         m_hIcon         = LookupIcon();
00312 
00313         m_pPlugin       = NULL;
00314         m_pCommand      = NULL;
00315         m_pExecute      = NULL;
00316 }
00317 
00318 CPlugin::~CPlugin()
00319 {
00320         Stop();
00321         if ( m_hIcon != NULL ) DestroyIcon( m_hIcon );
00322 }
00323 
00325 // CPlugin start / stop operations
00326 
00327 BOOL CPlugin::Start()
00328 {
00329         if ( m_pPlugin != NULL ) return FALSE;
00330 
00331         HRESULT hResult = CoCreateInstance( m_pCLSID, NULL, CLSCTX_INPROC_SERVER,
00332                 IID_IGeneralPlugin, (void**)&m_pPlugin );
00333 
00334         if ( FAILED( hResult ) || m_pPlugin == NULL )
00335         {
00336                 m_pPlugin = NULL;
00337                 return FALSE;
00338         }
00339 
00340         m_pPlugin->SetApplication(
00341                 (IApplication*)Application.GetInterface( IID_IApplication, FALSE ) );
00342 
00343         m_nCapabilities = 0;
00344         m_pPlugin->QueryCapabilities( &m_nCapabilities );
00345 
00346         m_pPlugin->QueryInterface( IID_ICommandPlugin, (void**)&m_pCommand );
00347 
00348         m_pPlugin->QueryInterface( IID_IExecutePlugin, (void**)&m_pExecute );
00349 
00350         return TRUE;
00351 }
00352 
00353 void CPlugin::Stop()
00354 {
00355         if ( m_pExecute != NULL )
00356         {
00357                 m_pExecute->Release();
00358                 m_pExecute = NULL;
00359         }
00360 
00361         if ( m_pCommand != NULL )
00362         {
00363                 m_pCommand->Release();
00364                 m_pCommand = NULL;
00365         }
00366 
00367         if ( m_pPlugin != NULL )
00368         {
00369                 m_pPlugin->Release();
00370                 m_pPlugin = NULL;
00371         }
00372 }
00373 
00374 BOOL CPlugin::StartIfEnabled()
00375 {
00376         if ( Plugins.LookupEnable( m_pCLSID, TRUE ) )
00377                 return Start();
00378         else
00379                 return FALSE;
00380 }
00381 
00383 // CPlugin CLSID helper
00384 
00385 CString CPlugin::GetStringCLSID() const
00386 {
00387         return GUIDX::Encode( &m_pCLSID );
00388 }
00389 
00391 // CPlugin icon helper
00392 
00393 HICON CPlugin::LookupIcon()
00394 {
00395         CString strName;
00396         HKEY hKey;
00397 
00398         strName.Format( _T("CLSID\\%s\\InprocServer32"), (LPCTSTR)GetStringCLSID() );
00399 
00400         if ( RegOpenKeyEx( HKEY_CLASSES_ROOT, strName, 0, KEY_QUERY_VALUE, &hKey ) )
00401                 return NULL;
00402 
00403         DWORD dwType = REG_SZ, dwSize = 256 * sizeof(TCHAR);
00404         LONG lResult = RegQueryValueEx( hKey, _T(""), NULL, &dwType, (LPBYTE)strName.GetBuffer( 256 ), &dwSize );
00405         strName.ReleaseBuffer( dwSize / sizeof(TCHAR) );
00406         RegCloseKey( hKey );
00407 
00408         if ( lResult != ERROR_SUCCESS ) return NULL;
00409 
00410         HICON hIcon = NULL;
00411         ExtractIconEx( strName, 0, NULL, &hIcon, 1 );
00412         return hIcon;
00413 }

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