home *** CD-ROM | disk | FTP | other *** search
/ Softwarová Záchrana 3 / Softwarova-zachrana-3.bin / pserv.cpl / pserv-2.4.exe / source / pserv2Doc.cpp < prev    next >
C/C++ Source or Header  |  2005-01-05  |  11KB  |  373 lines

  1. // pserv2Doc.cpp : implementation of the CPserv2Doc class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "pserv2.h"
  6. #include "MainFrm.h"
  7. #include "pserv2Doc.h"
  8. #include "pserv2View.h"
  9. #include "CConfiguration.h"
  10. #include "TemplateDialog.h"
  11.  
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CPserv2Doc
  20.  
  21. IMPLEMENT_DYNCREATE(CPserv2Doc, CDocument)
  22.  
  23. BEGIN_MESSAGE_MAP(CPserv2Doc, CDocument)
  24.     //{{AFX_MSG_MAP(CPserv2Doc)
  25.     ON_COMMAND(ID_DISPLAY_EXPORT_ASXML, OnDisplayExportAsXml)
  26.     ON_COMMAND(ID_VIEW_SERVICES, OnViewServices)
  27.     ON_COMMAND(ID_VIEW_DEVICES, OnViewDevices)
  28.     ON_COMMAND(ID_VIEW_SYSTEMEVENTS, OnViewSystemEvents)
  29.     ON_COMMAND(ID_VIEW_SECURITYEVENTS, OnViewSecurityEvents)
  30.     ON_COMMAND(ID_VIEW_PROCESSLIST, OnViewProcessList)
  31.     ON_COMMAND(ID_VIEW_MODULESLIST, OnViewModulesList)
  32.     ON_COMMAND(ID_VIEW_APPLICATIONEVENTS, OnViewApplicationEvents)
  33.     ON_COMMAND(ID_DISPLAY_EXPORT_TOCLIPBOARD, OnDisplayExportToClipboard)
  34.     ON_COMMAND(ID_TEMPLATES_APPLY, OnTemplatesApply)
  35.     //}}AFX_MSG_MAP
  36. END_MESSAGE_MAP()
  37.  
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CPserv2Doc construction/destruction
  40.  
  41. CPserv2Doc::CPserv2Doc()
  42.     :   m_pEntries( &m_ServiceList ),
  43.         m_DocType( DocType_Services )
  44. {
  45.     // TODO: add one-time construction code here
  46.     m_Providers.AddTail(&m_ServiceList);
  47.     m_Providers.AddTail(&m_EventLog);
  48.     m_Providers.AddTail(&m_ProcessList);
  49.     m_Providers.AddTail(&m_ModulesList);
  50.     DoConfigExchange(FALSE);
  51. }
  52.  
  53. CPserv2Doc::~CPserv2Doc()
  54. {
  55.     DoConfigExchange(TRUE);
  56. }
  57.  
  58. void GetCommandLineTokens(CStringArray& tokens)
  59. {
  60.     LPTSTR p = tstrdup(GetCommandLine());
  61.  
  62.     LPCTSTR token = tstrtok( p, _T(" ") );
  63.     while( token != NULL )
  64.     {
  65.         tokens.Add( token );
  66.         token = tstrtok( NULL, _T(" ") );
  67.     }
  68.     free(p);
  69. }
  70.  
  71. void CPserv2Doc::DoConfigExchange(BOOL bSave)
  72. {
  73.     POSITION pos = m_Providers.GetHeadPosition(); 
  74.     while( pos != NULL )
  75.     {
  76.         CListViewEntries* provider = (CListViewEntries*) m_Providers.GetNext(pos);
  77.         ASSERT_VALID(provider);
  78.  
  79.         CRegistry key( HKEY_CURRENT_USER, 
  80.             FormattedString(REGISTRY_BASE _T("\\%s"), (LPCTSTR) provider->m_strName ), 
  81.             bSave );
  82.         provider->DoConfigExchange(key);
  83.     }
  84. }
  85.  
  86. BOOL CPserv2Doc::OnNewDocument()
  87. {
  88.     if (!CDocument::OnNewDocument())
  89.         return FALSE;
  90.  
  91.     CStringArray Tokens;
  92.     GetCommandLineTokens(Tokens);
  93.  
  94.     CString strMachine;
  95.  
  96.     for( int i = 0, imax = Tokens.GetSize(); i < imax; i++ )
  97.     {
  98.         CString token(Tokens.GetAt(i));
  99.         if( token.Left(9).CompareNoCase(_T("/MACHINE=")) == 0 )
  100.         {
  101.             strMachine = LPCTSTR(token) + 9;
  102.         }
  103.         else if( token.CompareNoCase(_T("/DEVICES")) == 0 )
  104.         {
  105.             m_ServiceList.SetEnumType( SERVICE_DRIVER );
  106.         }
  107.         else if( token.CompareNoCase(_T("/SYSEVENTS")) == 0 )
  108.         {
  109.             TheMainFrame->SwitchMenu(IDR_MAINFRAME_EVENTS);
  110.             m_pEntries = &m_EventLog;
  111.             m_DocType = DocType_Events;
  112.             m_EventLog.SetEnumType( _T("System") );
  113.         }
  114.         else if( token.CompareNoCase(_T("/SECURITYEVENTS")) == 0 )
  115.         {
  116.             TheMainFrame->SwitchMenu(IDR_MAINFRAME_EVENTS);
  117.             m_pEntries = &m_EventLog;
  118.             m_DocType = DocType_Events;
  119.             m_EventLog.SetEnumType( _T("Security") );
  120.         }
  121.         else if( token.CompareNoCase(_T("/APPEVENTS")) == 0 )
  122.         {
  123.             TheMainFrame->SwitchMenu(IDR_MAINFRAME_EVENTS);
  124.             m_pEntries = &m_EventLog;
  125.             m_DocType = DocType_Events;
  126.             m_EventLog.SetEnumType( _T("Application") );
  127.         }
  128.         else if( token.CompareNoCase(_T("/MODULES")) == 0 )
  129.         {
  130.             TheMainFrame->SwitchMenu(IDR_MAINFRAME_MODULES);
  131.             m_pEntries = &m_ModulesList;
  132.             m_DocType = DocType_ModulesList;
  133.         }
  134.         else if( token.CompareNoCase(_T("/PROCESSES")) == 0 )
  135.         {
  136.             TheMainFrame->SwitchMenu(IDR_MAINFRAME_PROCESSES);
  137.             m_pEntries = &m_ProcessList;
  138.             m_DocType = DocType_ProcessList;
  139.         }
  140.     }
  141.  
  142.  
  143.     m_pEntries->ConnectTo(strMachine);
  144.     m_pEntries->Refresh();
  145.     return TRUE;
  146. }
  147.  
  148.  
  149.  
  150. /////////////////////////////////////////////////////////////////////////////
  151. // CPserv2Doc serialization
  152.  
  153. void CPserv2Doc::Serialize(CArchive& ar)
  154. {
  155.     if (ar.IsStoring())
  156.     {
  157.         // TODO: add storing code here
  158.     }
  159.     else
  160.     {
  161.         // TODO: add loading code here
  162.     }
  163. }
  164.  
  165. /////////////////////////////////////////////////////////////////////////////
  166. // CPserv2Doc diagnostics
  167.  
  168. #ifdef _DEBUG
  169. void CPserv2Doc::AssertValid() const
  170. {
  171.     CDocument::AssertValid();
  172. }
  173.  
  174. void CPserv2Doc::Dump(CDumpContext& dc) const
  175. {
  176.     CDocument::Dump(dc);
  177. }
  178. #endif //_DEBUG
  179.  
  180. /////////////////////////////////////////////////////////////////////////////
  181. // CPserv2Doc commands
  182.  
  183. extern CPserv2View* theCurrentView;
  184.  
  185. void CPserv2Doc::IGetExportFilename(LPCTSTR lpszDefExt, CString& refFilename, LPCTSTR lpszFilter, BOOL& success, BOOL bOpenFileDialog) 
  186. {
  187.     LPCTSTR lpszFileName = refFilename;
  188.     WIN32_FIND_DATA fd;
  189.     HANDLE hFF = FindFirstFile( refFilename, &fd );
  190.     if( hFF == INVALID_HANDLE_VALUE )
  191.         lpszFileName = NULL;
  192.     else 
  193.         FindClose(hFF);
  194.  
  195.     CFileDialog box( bOpenFileDialog, // BOOL bOpenFileDialog
  196.                      lpszDefExt, // LPCTSTR lpszDefExt = NULL
  197.                      lpszFileName, // LPCTSTR lpszFileName = NULL
  198.                      OFN_OVERWRITEPROMPT, // DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
  199.                      lpszFilter, // LPCTSTR lpszFilter = NULL, 
  200.                      NULL // CWnd* pParentWnd = NULL
  201.                 );
  202.  
  203.     
  204.     if( box.DoModal() == IDOK )
  205.     {
  206.         refFilename = box.GetPathName();
  207.         success = TRUE;
  208.     }
  209. }
  210.  
  211. BOOL CPserv2Doc::GetExportFilename(LPCTSTR lpszDefExt, CString& refFilename, LPCTSTR lpszFilter, BOOL bOpenFileDialog) 
  212. {
  213.     BOOL success = FALSE;
  214.     __try
  215.     {
  216.         IGetExportFilename(lpszDefExt, refFilename, lpszFilter, success,bOpenFileDialog);    
  217.     }
  218.     __except(EXCEPTION_EXECUTE_HANDLER)
  219.     {
  220.     }
  221.     return success;
  222. }
  223.  
  224. void CPserv2Doc::OnDisplayExportAsXml() 
  225. {
  226.     if( GetExportFilename(_T("xml"), theConfiguration.m_strExportXmlFilename, _T("XML files (*.xml)|*.xml|All files (*.*)|(*.*)||"), FALSE ) )
  227.     {
  228.         if( !m_pEntries->ExportAsXml( theConfiguration.m_strExportXmlFilename ) )
  229.         {
  230.             DisplayErrorMessage(_T("Unable to export %s"), (LPCTSTR) theConfiguration.m_strExportXmlFilename );
  231.         }
  232.     }
  233. }
  234.  
  235. void CPserv2Doc::OnViewServices() 
  236. {
  237.     TheMainFrame->SwitchMenu(IDR_MAINFRAME);
  238.     m_pEntries = &m_ServiceList;
  239.     m_DocType = DocType_Services;
  240.     m_ServiceList.SetEnumType( SERVICE_WIN32 );
  241.     m_ServiceList.Refresh();
  242.     UpdateAllViews(0);
  243. }
  244.  
  245. void CPserv2Doc::OnViewDevices() 
  246. {
  247.     TheMainFrame->SwitchMenu(IDR_MAINFRAME);
  248.     m_DocType = DocType_Services;
  249.     m_pEntries = &m_ServiceList;
  250.     m_ServiceList.SetEnumType( SERVICE_DRIVER );
  251.     m_ServiceList.Refresh();
  252.     UpdateAllViews(0);
  253. }
  254.  
  255. void CPserv2Doc::OnViewProcessList()
  256. {
  257.     TheMainFrame->SwitchMenu(IDR_MAINFRAME_PROCESSES);
  258.     m_pEntries = &m_ProcessList;
  259.     m_DocType = DocType_ProcessList;
  260.     m_ProcessList.Refresh();
  261.     UpdateAllViews(0);
  262. }
  263.  
  264. void CPserv2Doc::OnViewModulesList()
  265. {
  266.     TheMainFrame->SwitchMenu(IDR_MAINFRAME_MODULES);
  267.     m_pEntries = &m_ModulesList;
  268.     m_DocType = DocType_ModulesList;
  269.     m_ModulesList.Refresh();
  270.     UpdateAllViews(0);
  271. }
  272.  
  273. void CPserv2Doc::OnViewSecurityEvents() 
  274. {
  275.     TheMainFrame->SwitchMenu(IDR_MAINFRAME_EVENTS);
  276.     m_pEntries = &m_EventLog;
  277.     m_DocType = DocType_Events;
  278.     m_EventLog.SetEnumType( _T("Security") );
  279.     m_EventLog.Refresh();
  280.     UpdateAllViews(0);
  281. }
  282.  
  283. void CPserv2Doc::OnViewSystemEvents() 
  284. {
  285.     TheMainFrame->SwitchMenu(IDR_MAINFRAME_EVENTS);
  286.     m_pEntries = &m_EventLog;
  287.     m_DocType = DocType_Events;
  288.     m_EventLog.SetEnumType( _T("System") );
  289.     m_EventLog.Refresh();
  290.     UpdateAllViews(0);
  291. }
  292.  
  293. void CPserv2Doc::OnViewApplicationEvents() 
  294. {
  295.     TheMainFrame->SwitchMenu(IDR_MAINFRAME_EVENTS);
  296.     m_pEntries = &m_EventLog;
  297.     m_DocType = DocType_Events;
  298.     m_EventLog.SetEnumType( _T("Application") );
  299.     m_EventLog.Refresh();
  300.     UpdateAllViews(0);
  301. }
  302.  
  303. void CPserv2Doc::OnDisplayExportToClipboard() 
  304. {
  305.     CWnd* pWnd = AfxGetMainWnd();
  306.     ASSERT_VALID(pWnd);
  307.  
  308.     CMemFile file;
  309.     m_pEntries->ExportXmlToFile(&file);
  310.     DWORD dwSize = file.GetPosition();
  311.     file.Seek(0,CFile::begin);
  312.     LPCTSTR lpszString = NULL;
  313.     LPCTSTR lpszEndOfString = NULL;
  314.     file.GetBufferPtr(0, 0, (void**) &lpszString, (void**) &lpszEndOfString );
  315.  
  316.     if( pWnd->OpenClipboard() )
  317.     {
  318.         int nstrlen = dwSize;
  319.         HGLOBAL hMemory = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, nstrlen+1 );
  320.         if( hMemory )
  321.         {
  322.             LPTSTR lpMemory = (LPTSTR) GlobalLock( hMemory );
  323.             tstrncpy( lpMemory, lpszString, nstrlen );
  324.             lpMemory[nstrlen] = 0;
  325.             GlobalUnlock( hMemory );
  326.             SetClipboardData( CF_TEXT, (HANDLE) hMemory );
  327.         }
  328.         CloseClipboard();
  329.     }
  330.     
  331. }
  332.  
  333. #include "PXmlReader.h"
  334.  
  335. void CPserv2Doc::OnTemplatesApply() 
  336. {
  337.     if( GetExportFilename(_T("xml"), theConfiguration.m_strExportXmlFilename, _T("XML files (*.xml)|*.xml|All files (*.*)|(*.*)||"), TRUE ) )
  338.     {
  339.         PXmlReader reader;
  340.         if( reader.Read( theConfiguration.m_strExportXmlFilename ) )
  341.         {
  342.             TemplateDialog box( &m_ServiceList, &reader.m_Actions );
  343.             if( box.DoModal() == IDOK )
  344.             {
  345.                 SETSTARTTYPEFN method[] = {
  346.                         &CService::StartAtBoot,
  347.                         &CService::StartAtSystem,
  348.                         &CService::StartAutomatically,
  349.                         &CService::Enable,
  350.                         &CService::Disable,
  351.                     };
  352.  
  353.                 POSITION pos = box.m_ActionsToPerform.GetHeadPosition();
  354.                 while( pos )
  355.                 {
  356.                     ServiceAction* sa = (ServiceAction*)box.m_ActionsToPerform.GetNext( pos );
  357.                     if( !(sa->m_pService->*method[sa->m_dwActionToTake])() )
  358.                     {
  359.                         DisplayErrorMessage(_T("Unable to set startup type to %s '%s'"), 
  360.                             (LPCTSTR) CService::GetStartAsString(sa->m_dwActionToTake), 
  361.                             (LPCTSTR)sa->m_pService->m_strDisplayName );
  362.                     }
  363.                 }
  364.                 UpdateAllViews(0);
  365.             }
  366.         }
  367.         else
  368.         {
  369.             DisplayErrorMessage(_T("Unable to read %s"), (LPCTSTR) theConfiguration.m_strExportXmlFilename );
  370.         }
  371.     }
  372. }
  373.