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

PageSettingsPlugins.cpp

Go to the documentation of this file.
00001 //
00002 // PageSettingsPlugins.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 "PageSettingsPlugins.h"
00027 
00028 #ifdef _DEBUG
00029 #define new DEBUG_NEW
00030 #undef THIS_FILE
00031 static char THIS_FILE[] = __FILE__;
00032 #endif
00033 
00034 IMPLEMENT_DYNCREATE(CPluginsSettingsPage, CSettingsPage)
00035 
00036 BEGIN_MESSAGE_MAP(CPluginsSettingsPage, CSettingsPage)
00037         //{{AFX_MSG_MAP(CPluginsSettingsPage)
00038         ON_NOTIFY(LVN_ITEMCHANGING, IDC_PLUGINS, OnItemChangingPlugins)
00039         ON_NOTIFY(LVN_ITEMCHANGED, IDC_PLUGINS, OnItemChangedPlugins)
00040         ON_NOTIFY(NM_CUSTOMDRAW, IDC_PLUGINS, OnCustomDrawPlugins)
00041         ON_BN_CLICKED(IDC_PLUGINS_SETUP, OnPluginsSetup)
00042         ON_BN_CLICKED(IDC_PLUGINS_WEB, OnPluginsWeb)
00043         //}}AFX_MSG_MAP
00044 END_MESSAGE_MAP()
00045 
00046 
00048 // CPluginsSettingsPage property page
00049 
00050 CPluginsSettingsPage::CPluginsSettingsPage() : CSettingsPage( CPluginsSettingsPage::IDD )
00051 {
00052         //{{AFX_DATA_INIT(CPluginsSettingsPage)
00053         //}}AFX_DATA_INIT
00054 }
00055 
00056 CPluginsSettingsPage::~CPluginsSettingsPage()
00057 {
00058 }
00059 
00060 void CPluginsSettingsPage::DoDataExchange(CDataExchange* pDX)
00061 {
00062         CSettingsPage::DoDataExchange(pDX);
00063         //{{AFX_DATA_MAP(CPluginsSettingsPage)
00064         DDX_Control(pDX, IDC_PLUGINS_SETUP, m_wndSetup);
00065         DDX_Control(pDX, IDC_SKIN_DESC, m_wndDesc);
00066         DDX_Control(pDX, IDC_SKIN_NAME, m_wndName);
00067         DDX_Control(pDX, IDC_PLUGINS, m_wndList);
00068         //}}AFX_DATA_MAP
00069 }
00070 
00072 // CPluginsSettingsPage message handlers
00073 
00074 BOOL CPluginsSettingsPage::OnInitDialog()
00075 {
00076         CSettingsPage::OnInitDialog();
00077 
00078         m_gdiImageList.Create( 16, 16, ILC_COLOR32|ILC_MASK, 2, 1 );
00079         m_gdiImageList.Add( theApp.m_bRTL ? CreateMirroredIcon( theApp.LoadIcon( IDI_FILE ) ) : 
00080                 theApp.LoadIcon( IDI_FILE ) );
00081         m_gdiImageList.Add( theApp.m_bRTL ? CreateMirroredIcon( theApp.LoadIcon( IDI_EXECUTABLE ) ) :
00082                 theApp.LoadIcon( IDI_EXECUTABLE ) );
00083 
00084         m_wndList.SetImageList( &m_gdiImageList, LVSIL_SMALL );
00085         m_wndList.InsertColumn( 0, _T("Name"), LVCFMT_LEFT, 382, 0 );
00086         m_wndList.InsertColumn( 1, _T("CLSID"), LVCFMT_LEFT, 0, 1 );
00087 
00088         m_wndList.SetExtendedStyle( LVS_EX_FULLROWSELECT|LVS_EX_CHECKBOXES );
00089 
00090         /*
00091         LVGROUP pGroup;
00092         pGroup.cbSize           = sizeof(pGroup);
00093         pGroup.mask                     = LVGF_ALIGN|LVGF_GROUPID|LVGF_HEADER;
00094         pGroup.uAlign           = LVGA_HEADER_LEFT;
00095         pGroup.pszHeader        = _T("General Plugins");
00096         pGroup.cchHeader        = _tcslen( pGroup.pszHeader );
00097         m_wndList.InsertGroup( 0, &pGroup );
00098         */
00099 
00100         m_bRunning = FALSE;
00101 
00102         EnumerateGenericPlugins();
00103         EnumerateMiscPlugins();
00104 
00105         m_wndSetup.EnableWindow( FALSE );
00106         m_bRunning = TRUE;
00107 
00108         return TRUE;
00109 }
00110 
00111 void CPluginsSettingsPage::OnItemChangingPlugins(NMHDR *pNMHDR, LRESULT *pResult)
00112 {
00113         NMLISTVIEW* pNMListView = reinterpret_cast<NMLISTVIEW*>(pNMHDR);
00114         *pResult = 0;
00115 
00116         if ( ( pNMListView->uOldState & LVIS_STATEIMAGEMASK ) == 0 &&
00117                  ( pNMListView->uNewState & LVIS_STATEIMAGEMASK ) != 0 )
00118         {
00119                 if ( m_bRunning ) *pResult = 1;
00120         }
00121 }
00122 
00123 void CPluginsSettingsPage::OnItemChangedPlugins(NMHDR* pNMHDR, LRESULT* pResult)
00124 {
00125         NMLISTVIEW* pNMListView = reinterpret_cast<NMLISTVIEW*>(pNMHDR);
00126         *pResult = 0;
00127 
00128         if ( m_wndList.GetSelectedCount() == 1 )
00129         {
00130                 int nItem = m_wndList.GetNextItem( -1, LVNI_SELECTED );
00131                 CPlugin* pPlugin = (CPlugin*)m_wndList.GetItemData( nItem );
00132                 m_wndName.SetWindowText( m_wndList.GetItemText( nItem, 0 ).Trim() );
00133                 m_wndSetup.EnableWindow( pPlugin != NULL && pPlugin->m_pPlugin != NULL );
00134         }
00135         else
00136         {
00137                 m_wndName.SetWindowText( _T("...") );
00138                 m_wndSetup.EnableWindow( FALSE );
00139         }
00140 }
00141 
00142 void CPluginsSettingsPage::OnCustomDrawPlugins(NMHDR *pNMHDR, LRESULT *pResult)
00143 {
00144         NMLVCUSTOMDRAW* pDraw = (NMLVCUSTOMDRAW*)pNMHDR;
00145         *pResult = CDRF_DODEFAULT;
00146 
00147         if ( pDraw->nmcd.dwDrawStage == CDDS_PREPAINT )
00148         {
00149                 *pResult = CDRF_NOTIFYITEMDRAW;
00150         }
00151         else if ( pDraw->nmcd.dwDrawStage == CDDS_ITEMPREPAINT )
00152         {
00153                 if ( pDraw->nmcd.lItemlParam != 0 )
00154                 {
00155                         pDraw->clrText = GetSysColor( COLOR_ACTIVECAPTION );
00156                 }
00157                 else
00158                 {
00159                         pDraw->clrText = GetSysColor( COLOR_3DDKSHADOW );
00160                 }
00161         }
00162 }
00163 
00164 void CPluginsSettingsPage::OnPluginsSetup()
00165 {
00166         if ( m_wndList.GetSelectedCount() != 1 ) return;
00167 
00168         int nItem = m_wndList.GetNextItem( -1, LVNI_SELECTED );
00169         CPlugin* pPlugin = (CPlugin*)m_wndList.GetItemData( nItem );
00170 
00171         if ( pPlugin != NULL && pPlugin->m_pPlugin != NULL ) pPlugin->m_pPlugin->Configure();
00172 }
00173 
00174 void CPluginsSettingsPage::OnPluginsWeb()
00175 {
00176         ShellExecute( GetSafeHwnd(), _T("open"),
00177                 _T("http://www.shareaza.com/plugins/?Version=") + theApp.m_sVersion,
00178                 NULL, NULL, SW_SHOWNORMAL );
00179 }
00180 
00181 void CPluginsSettingsPage::OnOK()
00182 {
00183         BOOL bChanged = FALSE;
00184 
00185         for ( int nItem = 0 ; nItem < m_wndList.GetItemCount() ; nItem++ )
00186         {
00187                 CPlugin* pPlugin = (CPlugin*)m_wndList.GetItemData( nItem );
00188                 CString strCLSID = m_wndList.GetItemText( nItem, 1 );
00189 
00190                 TRISTATE bEnabled = m_wndList.GetItemState( nItem, LVIS_STATEIMAGEMASK ) >> 12;
00191 
00192                 if ( bEnabled != TS_UNKNOWN )
00193                 {
00194                         theApp.WriteProfileInt( _T("Plugins"), strCLSID, bEnabled == TS_TRUE );
00195                 }
00196 
00197                 if ( pPlugin != NULL && ( bEnabled == TS_TRUE ) != ( pPlugin->m_pPlugin != NULL ) )
00198                 {
00199                         bChanged = TRUE;
00200 
00201                         if ( bEnabled == TS_TRUE )
00202                                 pPlugin->Start();
00203                         else
00204                                 pPlugin->Stop();
00205                 }
00206         }
00207 
00208         if ( bChanged ) AfxGetMainWnd()->PostMessage( WM_SKINCHANGED );
00209 
00210         CSettingsPage::OnOK();
00211 }
00212 
00214 // CPluginsSettingsPage plugin enumeration
00215 
00216 void CPluginsSettingsPage::InsertPlugin(LPCTSTR pszCLSID, LPCTSTR pszName, int nImage, TRISTATE bEnabled, LPVOID pPlugin)
00217 {
00218     int nItem = 0;
00219         for ( ; nItem < m_wndList.GetItemCount() ; nItem++ )
00220         {
00221                 LPVOID pExisting = (LPVOID)m_wndList.GetItemData( nItem );
00222                 CString strExisting = m_wndList.GetItemText( nItem, 0 );
00223 
00224                 if ( pPlugin != NULL && pExisting == NULL ) break;
00225                 if ( pPlugin == NULL && pExisting != NULL ) continue;
00226                 if ( strExisting.Compare( pszName ) > 0 ) break;
00227         }
00228 
00229         nItem = m_wndList.InsertItem( LVIF_IMAGE|LVIF_TEXT|LVIF_PARAM, nItem,
00230                 pszName, 0, 0, nImage, (LPARAM)pPlugin );
00231 
00232         m_wndList.SetItemText( nItem, 1, pszCLSID );
00233 
00234         if ( bEnabled != TS_UNKNOWN )
00235         {
00236                 m_wndList.SetItemState( nItem, bEnabled << 12, LVIS_STATEIMAGEMASK );
00237         }
00238 }
00239 
00240 void CPluginsSettingsPage::EnumerateGenericPlugins()
00241 {
00242         Plugins.Enumerate();
00243 
00244         for ( POSITION pos = Plugins.GetIterator() ; pos ; )
00245         {
00246                 CPlugin* pPlugin = Plugins.GetNext( pos );
00247                 int nImage = 0;
00248 
00249                 if ( pPlugin->m_hIcon != NULL ) 
00250                         nImage = m_gdiImageList.Add( theApp.m_bRTL ? CreateMirroredIcon ( pPlugin->m_hIcon ) : 
00251                                 pPlugin->m_hIcon );
00252 
00253                 InsertPlugin( pPlugin->GetStringCLSID(), pPlugin->m_sName, nImage,
00254                         pPlugin->m_pPlugin != NULL ? TS_TRUE : TS_FALSE, pPlugin );
00255         }
00256 }
00257 
00258 void CPluginsSettingsPage::EnumerateMiscPlugins()
00259 {
00260         HKEY hPlugins = NULL;
00261 
00262         if ( ERROR_SUCCESS != RegOpenKeyEx( HKEY_LOCAL_MACHINE,
00263                 _T("Software\\Shareaza\\Shareaza\\Plugins"), 0, KEY_READ, &hPlugins ) )
00264                 return;
00265 
00266         for ( DWORD nIndex = 0 ; ; nIndex++ )
00267         {
00268                 HKEY hCategory = NULL;
00269                 TCHAR szName[128];
00270                 DWORD nName = 128;
00271 
00272                 if ( ERROR_SUCCESS != RegEnumKeyEx( hPlugins, nIndex, szName, &nName,
00273                         NULL, NULL, NULL, NULL ) ) break;
00274 
00275                 if ( _tcsicmp( szName, _T("General") ) != 0 )
00276                 {
00277                         if ( ERROR_SUCCESS == RegOpenKeyEx( hPlugins, szName, 0, KEY_READ,
00278                                  &hCategory ) )
00279                         {
00280                                 EnumerateMiscPlugins( szName, hCategory );
00281                                 RegCloseKey( hCategory );
00282                         }
00283                 }
00284         }
00285 
00286         RegCloseKey( hPlugins );
00287 }
00288 
00289 void CPluginsSettingsPage::EnumerateMiscPlugins(LPCTSTR pszType, HKEY hRoot)
00290 {
00291         CStringList pCLSIDs;
00292 
00293         for ( DWORD nIndex = 0 ; ; nIndex++ )
00294         {
00295                 DWORD nName = 128, nValue = 128, nType = REG_SZ;
00296                 TCHAR szName[128], szValue[128];
00297 
00298                 if ( ERROR_SUCCESS != RegEnumValue( hRoot, nIndex, szName, &nName,
00299                         NULL, &nType, (LPBYTE)szValue, &nValue ) ) break;
00300 
00301                 if ( nType == REG_SZ && szValue[0] == '{' )
00302                 {
00303                         if ( pCLSIDs.Find( szValue ) == NULL )
00304                         {
00305                                 pCLSIDs.AddTail( szValue );
00306                                 AddMiscPlugin( pszType, szValue );
00307                         }
00308                 }
00309         }
00310 }
00311 
00312 void CPluginsSettingsPage::AddMiscPlugin(LPCTSTR pszType, LPCTSTR pszCLSID)
00313 {
00314         HKEY hClass = NULL;
00315         CString strClass;
00316         CLSID pCLSID;
00317 
00318         strClass.Format( _T("CLSID\\%s"), pszCLSID );
00319 
00320         if ( ERROR_SUCCESS == RegOpenKeyEx( HKEY_CLASSES_ROOT, strClass, 0, KEY_READ, &hClass ) )
00321         {
00322                 DWORD nValue = 256 * sizeof(TCHAR), nType = REG_SZ;
00323                 TCHAR szValue[256];
00324 
00325                 if ( ERROR_SUCCESS == RegQueryValueEx( hClass, NULL, NULL, &nType,
00326                         (LPBYTE)szValue, &nValue ) )
00327                 {
00328                         if ( GUIDX::Decode( pszCLSID, &pCLSID ) )
00329                         {
00330                                 TRISTATE bEnabled = TS_UNKNOWN;
00331                                 bEnabled = Plugins.LookupEnable( pCLSID, TRUE ) ? TS_TRUE : TS_FALSE;
00332                                 InsertPlugin( pszCLSID, szValue, 1, bEnabled );
00333                         }
00334                 }
00335 
00336                 RegCloseKey( hClass );
00337         }
00338 }

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