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

  1. // pserv2View.cpp : implementation of the CPserv2View class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "pserv2.h"
  6. #include "ServiceControlDialog.h"
  7. #include "pserv2Doc.h"
  8. #include "pserv2View.h"
  9. #include "PageStartup.h"
  10. #include "CConfiguration.h"
  11. #include "AdvancedPage.h"
  12. #include "RemoteConnectDialog.h"
  13. #include "ChooseColumnsDialog.h"
  14. #include "CNTMachineAccount.h"
  15.  
  16. #ifdef _DEBUG
  17. #define new DEBUG_NEW
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CPserv2View
  25.  
  26. IMPLEMENT_DYNCREATE(CPserv2View, CListView)
  27.  
  28. BEGIN_MESSAGE_MAP(CPserv2View, CListView)
  29.     //{{AFX_MSG_MAP(CPserv2View)
  30.     ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
  31.     ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, OnColumnclick)
  32.     ON_COMMAND(ID_OBJECT_STOP, OnObjectStop)
  33.     ON_COMMAND(ID_OBJECT_START, OnObjectStart)
  34.     ON_COMMAND(ID_OBJECT_RESTART, OnObjectRestart)
  35.     ON_COMMAND(ID_ANSICHT_REFRESH, OnViewRefresh)
  36.     ON_COMMAND(ID_OBJECT_PAUSE, OnObjectPause)
  37.     ON_COMMAND(ID_OBJECT_CONTINUE, OnObjectContinue)
  38.     ON_WM_MOUSEWHEEL()
  39.     ON_UPDATE_COMMAND_UI(ID_OBJECT_STOP, OnUpdateObjectStop)
  40.     ON_UPDATE_COMMAND_UI(ID_OBJECT_START, OnUpdateObjectStart)
  41.     ON_UPDATE_COMMAND_UI(ID_OBJECT_RESTART, OnUpdateObjectRestart)
  42.     ON_UPDATE_COMMAND_UI(ID_OBJECT_PAUSE, OnUpdateObjectPause)
  43.     ON_UPDATE_COMMAND_UI(ID_OBJECT_CONTINUE, OnUpdateObjectContinue)
  44.     ON_WM_CONTEXTMENU()
  45.     ON_NOTIFY_REFLECT(NM_DBLCLK, OnDoubleClickItem)
  46.     ON_COMMAND(ID_OBJECT_START_OR_CONTINUE, OnObjectStartOrContinue)
  47.     ON_UPDATE_COMMAND_UI(ID_OBJECT_START_OR_CONTINUE, OnUpdateObjectStartOrContinue)
  48.     ON_NOTIFY_REFLECT(LVN_GETINFOTIP, OnGetInfoTip)
  49.     ON_WM_MOUSEMOVE()
  50.     ON_COMMAND(ID_DISPLAY_CONNECTTOLOCALMACHINE, OnConnectToLocalMachine)
  51.     ON_COMMAND(ID_DISPLAY_CONNECTTOREMOTEMACHINE, OnConnectToRemoteMachine)
  52.     ON_COMMAND(ID_OPTIONS_FONT, OnOptionsFont)
  53.     ON_COMMAND(ID_OBJECT_PROPERTIES, OnObjectProperties)
  54.     ON_COMMAND(ID_VIEW_FIND, OnViewFind)
  55.     ON_COMMAND(ID_VIEW_FINDNEXT, OnViewFindNext)
  56.     ON_COMMAND(ID_OBJECT_ENABLE, OnObjectEnable)
  57.     ON_UPDATE_COMMAND_UI(ID_OBJECT_ENABLE, OnUpdateObjectEnable)
  58.     ON_COMMAND(ID_OBJECT_DISABLE, OnObjectDisable)
  59.     ON_UPDATE_COMMAND_UI(ID_OBJECT_DISABLE, OnUpdateObjectDisable)
  60.     ON_COMMAND(ID_OBJECT_STARTAUTOMATICALLY, OnObjectStartAutomatically)
  61.     ON_UPDATE_COMMAND_UI(ID_OBJECT_STARTAUTOMATICALLY, OnUpdateObjectStartAutomatically)
  62.     ON_UPDATE_COMMAND_UI(ID_OBJECT_PROPERTIES, OnUpdateObjectProperties)
  63.     ON_COMMAND(ID_OBJECT_INSTALL, OnObjectInstall)
  64.     ON_COMMAND(ID_OBJECT_DELETE, OnObjectDelete)
  65.     ON_UPDATE_COMMAND_UI(ID_OBJECT_DELETE, OnUpdateObjectDelete)
  66.     ON_UPDATE_COMMAND_UI(ID_OBJECT_KILLPROCESS, OnUpdateObjectKillprocess)
  67.     ON_COMMAND(ID_OBJECT_KILLPROCESS, OnObjectKillprocess)
  68.     ON_NOTIFY(HDN_ITEMCHANGED, 0, OnHeaderItemChanged)
  69.     ON_UPDATE_COMMAND_UI(ID_FILE_PRINT_PREVIEW, OnUpdateFilePrintPreview)
  70.     ON_UPDATE_COMMAND_UI(ID_FILE_PRINT, OnUpdateFilePrint)
  71.     ON_UPDATE_COMMAND_UI(ID_OBJECT_INSTALL, OnUpdateObjectInstall)
  72.     ON_UPDATE_COMMAND_UI(ID_TEMPLATES_APPLY, OnUpdateTemplatesApply)
  73.     ON_COMMAND(ID_OPTIONS_AUTORESIZEALLCOLUMNS, OnOptionsAutoresizeallcolumns)
  74.     ON_COMMAND(ID_VIEW_CHOOSECOLUMNS, OnViewChoosecolumns)
  75.     //}}AFX_MSG_MAP
  76.     // Standard printing commands
  77.     ON_COMMAND(ID_FILE_PRINT, CListView::OnFilePrint)
  78.     ON_COMMAND(ID_FILE_PRINT_DIRECT, CListView::OnFilePrint)
  79.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CListView::OnFilePrintPreview)
  80. END_MESSAGE_MAP()
  81.  
  82. /////////////////////////////////////////////////////////////////////////////
  83. // CPserv2View construction/destruction
  84. CPserv2View* theCurrentView = NULL;
  85.  
  86. CPserv2View::CPserv2View()
  87.     :   m_bAnythingHasBeenDisplayed( FALSE ),
  88.         m_bInsideAutoResize( FALSE ),
  89.         m_pEntries( NULL )
  90. {
  91.     theCurrentView = this;
  92.  
  93. }
  94.  
  95. CPserv2View::~CPserv2View()
  96. {
  97. }
  98.  
  99. BOOL CPserv2View::PreCreateWindow(CREATESTRUCT& cs)
  100. {
  101.     cs.style |= LVS_REPORT;
  102.  
  103.     return CListView::PreCreateWindow(cs);
  104. }
  105.  
  106.  
  107. /////////////////////////////////////////////////////////////////////////////
  108. // CPserv2View drawing
  109.  
  110. void CPserv2View::OnDraw(CDC* pDC)
  111. {
  112.     CPserv2Doc* pDoc = GetDocument();
  113.     ASSERT_VALID(pDoc);
  114.     // TODO: add draw code for native data here
  115. }
  116.  
  117.  
  118. void CPserv2View::OnInitialUpdate()
  119. {
  120.     CListView::OnInitialUpdate();
  121.  
  122.     CDC* dc = GetDC();
  123.     m_Font.CreatePointFont( theConfiguration.m_dwFontSize, theConfiguration.m_strFontFace, dc );
  124.     ReleaseDC(dc);
  125.     SetFont(&m_Font);
  126.  
  127.     // this code only works for a report-mode list view
  128.     ASSERT(GetStyle() & LVS_REPORT);
  129.  
  130.    // Gain a reference to the list control itself
  131.     CListCtrl& theCtrl = GetListCtrl();
  132.     theCtrl.SetExtendedStyle(theCtrl.GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP );
  133.     DisplayAllItems();
  134.  
  135. }
  136.  
  137. /////////////////////////////////////////////////////////////////////////////
  138. // CPserv2View printing
  139.  
  140. void CPserv2View::DisplayAllItems(CListViewEntry* pSelect)
  141. {
  142.     DWORD dwIndex;
  143.  
  144.     SetRedraw(FALSE);
  145.     CListCtrl& theCtrl = GetListCtrl();
  146.     
  147.     CListViewEntries* pEntries = GetDocument()->m_pEntries;
  148.  
  149.     theCtrl.DeleteAllItems();
  150.  
  151.     // update columns now 
  152.     if( m_pEntries != pEntries )
  153.     {
  154.         m_pEntries = pEntries;
  155.         while( theCtrl.DeleteColumn(0) )
  156.         {
  157.         }
  158.     
  159.         m_dwNumberOfColumns = pEntries->GetNumberOfColumns();
  160.         for( dwIndex = 0; dwIndex < m_dwNumberOfColumns; dwIndex++ )
  161.         {
  162.             CListViewColumn* c = (CListViewColumn*) pEntries->GetColumn(dwIndex);
  163.             theCtrl.InsertColumn(dwIndex, c->m_strCaption, LVCFMT_LEFT, c->m_dwWidth);
  164.         }
  165.     }
  166.  
  167.     m_pServices = &(pEntries->m_Entries);
  168.     int nElements = m_pServices->GetSize();
  169.     for( int nIndex = 0; nIndex < nElements; nIndex++ )
  170.     {
  171.         CListViewEntry* entry = (CListViewEntry*) m_pServices->GetAt(nIndex);
  172.  
  173.         for( dwIndex = 0; dwIndex < m_dwNumberOfColumns; dwIndex++ )
  174.         {
  175.             if(!dwIndex)
  176.                 theCtrl.InsertItem( nIndex, m_pEntries->GetDisplayString(entry, dwIndex) );
  177.             else
  178.                 theCtrl.SetItem( nIndex, dwIndex, LVIF_TEXT, m_pEntries->GetDisplayString(entry, dwIndex), 0, 0, 0, 0);
  179.         }
  180.  
  181.         if( entry == pSelect )
  182.         {
  183.             theCtrl.SetItemState(nIndex, LVIS_FOCUSED|LVIS_SELECTED, 0x000F );
  184.         }
  185.     }
  186.     
  187.  
  188.     m_bInsideAutoResize = TRUE;
  189.  
  190.     for( dwIndex = 0; dwIndex < m_dwNumberOfColumns; dwIndex++ )
  191.     {
  192.         CListViewColumn* c = (CListViewColumn*) pEntries->GetColumn(dwIndex);
  193.         if( c->m_dwWidth == LVSCW_AUTOSIZE )
  194.         {
  195.             theCtrl.SetColumnWidth( dwIndex, LVSCW_AUTOSIZE );
  196.         }
  197.     }
  198.     SetRedraw(TRUE);
  199.     m_bInsideAutoResize = FALSE;
  200.     m_bAnythingHasBeenDisplayed = TRUE;
  201.  
  202.     AfxGetMainWnd()->SetWindowText( GetDocument()->m_pEntries->m_strTitle );
  203.     
  204. }
  205.  
  206. BOOL CPserv2View::OnPreparePrinting(CPrintInfo* pInfo)
  207. {
  208.     // default preparation
  209.     return DoPreparePrinting(pInfo);
  210. }
  211.  
  212. void CPserv2View::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult) 
  213. {
  214.     LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;
  215.  
  216.     switch(lplvcd->nmcd.dwDrawStage)
  217.     {
  218.     case CDDS_PREPAINT:
  219.         // Request prepaint notifications for each item.
  220.         *pResult = CDRF_NOTIFYITEMDRAW;
  221.         break;
  222.  
  223.     case CDDS_ITEMPREPAINT: // Requested notification
  224.         {
  225.             if( lplvcd->nmcd.dwItemSpec < (DWORD) m_pServices->GetSize() )
  226.             {
  227.                 CListViewEntry* entry = (CListViewEntry*) m_pServices->GetAt( lplvcd->nmcd.dwItemSpec );
  228.                 if( entry )
  229.                 {
  230.                     lplvcd->clrText = entry->GetTextColor();
  231.                 }
  232.                 *pResult = CDRF_DODEFAULT;
  233.             }
  234.         }
  235.         break;
  236.     }
  237. }
  238.  
  239.  
  240. /////////////////////////////////////////////////////////////////////////////
  241. // CPserv2View diagnostics
  242.  
  243. #ifdef _DEBUG
  244. void CPserv2View::AssertValid() const
  245. {
  246.     CListView::AssertValid();
  247. }
  248.  
  249. void CPserv2View::Dump(CDumpContext& dc) const
  250. {
  251.     CListView::Dump(dc);
  252. }
  253.  
  254. CPserv2Doc* CPserv2View::GetDocument() // non-debug version is inline
  255. {
  256.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPserv2Doc)));
  257.     return (CPserv2Doc*)m_pDocument;
  258. }
  259. #endif //_DEBUG
  260.  
  261.  
  262. void CPserv2View::OnColumnclick(NMHDR* pNMHDR, LRESULT* pResult) 
  263. {
  264.     NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  265.  
  266.     if( (pNMListView->iSubItem >= 0) && (pNMListView->iSubItem < int(m_dwNumberOfColumns)) )
  267.     {
  268.         if( m_pEntries->m_dwSortMethod == (DWORD) pNMListView->iSubItem )
  269.         {
  270.             m_pEntries->m_bSortAscending = m_pEntries->m_bSortAscending ? FALSE : TRUE;
  271.         }
  272.         else
  273.         {
  274.             m_pEntries->m_bSortAscending = TRUE;
  275.             m_pEntries->m_dwSortMethod = (DWORD) pNMListView->iSubItem;
  276.         }
  277.         GetDocument()->m_pEntries->Sort();
  278.         DisplayAllItems();
  279.     }
  280.     
  281.     *pResult = 0;
  282. }
  283.  
  284. int CPserv2View::GetSelectedIndices(CUIntArray& listOfIndices)
  285. {
  286.     listOfIndices.RemoveAll();
  287.     ASSERT( listOfIndices.GetSize() == 0 );
  288.  
  289.     CListCtrl& theCtrl = GetListCtrl();
  290.     POSITION pos = theCtrl.GetFirstSelectedItemPosition();
  291.     if( pos )
  292.     {
  293.         while( pos )
  294.         {
  295.             listOfIndices.Add(theCtrl.GetNextSelectedItem(pos));   
  296.         }
  297.     }
  298.     return listOfIndices.GetSize();
  299. }
  300.  
  301. int CPserv2View::GetFirstSelectedIndex()
  302. {
  303.     CListCtrl& theCtrl = GetListCtrl();
  304.     POSITION pos = theCtrl.GetFirstSelectedItemPosition();
  305.     if( pos )
  306.     {
  307.         return theCtrl.GetNextSelectedItem(pos);
  308.     }
  309.     return -1;
  310. }
  311.  
  312. CListViewEntry* CPserv2View::GetCurrentEntry()
  313. {
  314.     CListCtrl& theCtrl = GetListCtrl();
  315.     POSITION pos = theCtrl.GetFirstSelectedItemPosition();
  316.     if( pos )
  317.     {
  318.         m_nCurrentIndex = theCtrl.GetNextSelectedItem(pos);
  319.         return (CListViewEntry*) m_pServices->GetAt(m_nCurrentIndex);
  320.     }
  321.     return NULL;
  322. }
  323.  
  324. void CPserv2View::UpdateCurrentService()
  325. {
  326.     CListCtrl& theCtrl = GetListCtrl();
  327.  
  328.     CListViewEntry* entry = (CListViewEntry*) m_pServices->GetAt(m_nCurrentIndex);
  329.     for( DWORD dwIndex = 0; dwIndex < m_dwNumberOfColumns; dwIndex++ )
  330.     {
  331.         theCtrl.SetItem( m_nCurrentIndex, dwIndex, LVIF_TEXT, m_pEntries->GetDisplayString(entry, dwIndex), 0, 0, 0, 0);
  332.     }
  333. }
  334.  
  335.  
  336.  
  337. void CPserv2View::OnViewRefresh() 
  338. {
  339.     GetDocument()->m_pEntries->Refresh();
  340.     DisplayAllItems();
  341. }
  342.  
  343. BOOL CPserv2View::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt) 
  344. {
  345.     if( nFlags & MK_CONTROL )
  346.     {
  347.         if( zDelta < 0 )
  348.         {
  349.             if( theConfiguration.m_dwFontSize > 40 )
  350.                 theConfiguration.m_dwFontSize -= 10;
  351.         }
  352.         else
  353.         {
  354.             if( theConfiguration.m_dwFontSize < 320 )
  355.                 theConfiguration.m_dwFontSize += 10;
  356.         }
  357.         CDC* dc = GetDC();
  358.         m_Font.DeleteObject();
  359.         m_Font.CreatePointFont(theConfiguration.m_dwFontSize, theConfiguration.m_strFontFace, dc );
  360.         ReleaseDC(dc);
  361.         SetFont(&m_Font);
  362.     }
  363.  
  364.     
  365.     return CListView::OnMouseWheel(nFlags, zDelta, pt);
  366. }
  367.  
  368.  
  369. void CPserv2View::OnContextMenu(CWnd* pWnd, CPoint point) 
  370. {
  371.     // make sure window is active
  372.     GetParentFrame()->ActivateFrame();
  373.  
  374.     CPoint local = point;
  375.     ScreenToClient(&local);
  376.     CMenu menu;
  377.     if(menu.LoadMenu(m_pEntries->GetContextMenuID()))
  378.     {
  379.         CMenu* pPopup = menu.GetSubMenu(0);
  380.         ASSERT(pPopup != NULL);
  381.  
  382.         pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, AfxGetMainWnd());
  383.     } 
  384. }
  385.  
  386. void CPserv2View::OnDoubleClickItem(NMHDR* pNMHDR, LRESULT* pResult) 
  387. {
  388.     if( GetDocument()->m_DocType == DocType_Services )
  389.         OnObjectProperties();
  390.  
  391.     *pResult = 0;
  392. }
  393.  
  394.  
  395. void CPserv2View::OnGetInfoTip(NMHDR* pNMHDR, LRESULT* pResult) 
  396. {
  397.     LPNMLVGETINFOTIP pDispInfo = (LPNMLVGETINFOTIP)pNMHDR;
  398.     
  399.     CListCtrl& theCtrl = GetListCtrl();
  400.     int nIndex = theCtrl.HitTest(m_LastPoint);
  401.     if( nIndex >= 0 )
  402.     {
  403.         CListViewEntry* instance = (CListViewEntry*) m_pServices->GetAt(nIndex);
  404.  
  405.         CString strInfoTip(instance->GetInfoTip());
  406.         if( !IsEmptyString(strInfoTip) )
  407.         {
  408.             tstrncpy( pDispInfo->pszText, strInfoTip, pDispInfo->cchTextMax );
  409.             *pResult = 1;
  410.         }
  411.     }
  412. }
  413.  
  414. void CPserv2View::OnMouseMove(UINT nFlags, CPoint point) 
  415. {
  416.     m_LastPoint = point;    
  417.     CListView::OnMouseMove(nFlags, point);
  418. }
  419.  
  420. static int nElementsPerPage = 19;
  421.  
  422. inline LPCTSTR SAFESTR(LPCTSTR p, LPCTSTR d)
  423. {
  424.     return p ? p : d;
  425. }
  426.  
  427. void CPserv2View::OnPrint(CDC* pDC, CPrintInfo* pInfo) 
  428. {
  429.     m_nPrintServiceIndex = (pInfo->m_nCurPage-1) * nElementsPerPage;
  430.  
  431.     CFont courier;
  432.     courier.CreatePointFont(80, _T("Courier New"), pDC );
  433.  
  434.     CFont bodyBold;
  435.     bodyBold.CreatePointFont(80, _T("Arial Bold"), pDC );
  436.  
  437.     CFont bodyNormal;
  438.     bodyNormal.CreatePointFont(80, _T("Arial"), pDC );
  439.  
  440.     CFont * pOldFont = pDC->SelectObject(&bodyNormal);
  441.  
  442.  
  443.     int nPos = 0;
  444.  
  445.     CBrush active;
  446.     active.CreateSolidBrush(RGB(0xD0,0xD0,0xD0));
  447.     pDC->SetBkMode(TRANSPARENT);
  448.  
  449.  
  450.     int nElements = m_pServices->GetSize();
  451.     for( int nIndex = 0; nIndex < nElementsPerPage; nIndex++ )
  452.     {
  453.         if( nIndex+ m_nPrintServiceIndex >= nElements )
  454.             break;
  455.  
  456.         int nPointsNeededPerPage = nPos;
  457.  
  458.         CService* service = (CService*) m_pServices->GetAt(nIndex+m_nPrintServiceIndex);
  459.         pDC->SelectObject(&bodyBold);
  460.         CSize TextSize =  pDC->GetOutputTextExtent(service->m_strDisplayName) ;
  461.  
  462.         if( service->IsRunning() == SERVICE_STATE_REACHED )
  463.         {
  464.             CRect rect(0, nPos, pInfo->m_rectDraw.right, nPos + (TextSize.cy*3) );
  465.             pDC->FillRect(&rect, &active);
  466.         }
  467.  
  468.         pDC->TextOut(0, nPos, service->m_strDisplayName );
  469.         pDC->SelectObject(&bodyNormal);
  470.         pDC->TextOut(TextSize.cx, nPos, FormattedString( _T(" - %s"), (LPCTSTR) service->m_strServiceName) );
  471.         nPos += TextSize.cy;
  472.         pDC->TextOut(0, nPos, FormattedString( _T("Status: %s - Start: %s - Type: %s - PID: %d"), 
  473.         SAFESTR(service->GetPrintStatusAsString(),_T("???")),
  474.         SAFESTR(service->GetStartAsString(),_T("???")),
  475.         SAFESTR(service->GetTypeAsString(),_T("???")),
  476.         SAFESTR(service->GetPidAsString(),_T("???")) ));
  477.         nPos += TextSize.cy;
  478.  
  479.         pDC->SelectObject(&courier);
  480.         pDC->TextOut(0, nPos, SAFESTR(service->m_strBinaryPathName,_T("???")));
  481.  
  482.         nPos += TextSize.cy;
  483.         nPos += TextSize.cy;
  484.  
  485.     }
  486.     m_nPrintServiceIndex += 30;
  487.     pDC->SelectObject(pOldFont);
  488. }
  489.  
  490. void CPserv2View::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
  491. {
  492.     int nElements = m_pServices->GetSize();
  493.  
  494.     int nPageHeight = pDC->GetDeviceCaps(PHYSICALHEIGHT);
  495.  
  496.     nElementsPerPage = nPageHeight/ 360 ;
  497.  
  498.     int nPages = (nElements % nElementsPerPage) ? 1 : 0;
  499.     nPages += nElements / nElementsPerPage;
  500.  
  501.     pInfo->SetMaxPage(nPages);
  502.     m_nPrintServiceIndex = 0;
  503. }
  504.  
  505. void CPserv2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  506. {
  507.     // TODO: add cleanup after printing
  508. }
  509.  
  510. void CPserv2View::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
  511. {
  512.     DisplayAllItems();
  513. }
  514.  
  515. void CPserv2View::OnOptionsFont() 
  516. {
  517.     LOGFONT lf;
  518.     m_Font.GetLogFont(&lf);
  519.  
  520.     CFontDialog dialog(&lf);
  521.     if( dialog.DoModal() == IDOK )
  522.     {
  523.         theConfiguration.m_dwFontSize = dialog.GetSize();
  524.         theConfiguration.m_strFontFace = dialog.GetFaceName();
  525.  
  526.         CDC* dc = GetDC();
  527.         m_Font.DeleteObject();
  528.         m_Font.CreatePointFont(theConfiguration.m_dwFontSize, theConfiguration.m_strFontFace, dc );
  529.         ReleaseDC(dc);
  530.         SetFont(&m_Font);
  531.     }
  532. }
  533.  
  534. void CPserv2View::OnObjectProperties() 
  535. {
  536.     CService* instance = GetCurrentService();
  537.     if( instance )
  538.     {
  539.         CPropertySheet sheet(FormattedString(_T("Settings for %s"), (LPCTSTR) instance->m_strServiceName ), this);
  540.         instance->m_dwChangeFlags = 0;
  541.  
  542.         CPageStartup pageStartup;
  543.         pageStartup.SetService(instance);
  544.         sheet.AddPage(&pageStartup);
  545.  
  546.         CAdvancedPage advanced;
  547.         advanced.SetService(instance);
  548.         sheet.AddPage(&advanced);
  549.         
  550.         sheet.m_psh.dwFlags |= PSH_NOAPPLYNOW | PSH_PROPTITLE;
  551.         sheet.m_psh.dwFlags &= ~(PSH_HASHELP|PSH_WIZARDCONTEXTHELP);
  552.  
  553.         if( sheet.DoModal() == IDOK )
  554.         {
  555.             if( !instance->ApplyChanges() )
  556.             {
  557.                 DisplayErrorMessage(_T("Unable to settings for %s"),(LPCTSTR) instance->m_strServiceName );
  558.             }
  559.             UpdateCurrentService();
  560.         }
  561.     }
  562. }
  563.  
  564. void CPserv2View::OnViewFind() 
  565. {
  566.     CFindDialog fd;
  567.     if( fd.DoModal() == IDOK )
  568.     {
  569.         m_SearchContext.Refresh(GetFirstSelectedIndex());
  570.         OnViewFindNext();
  571.     }
  572. }
  573.  
  574. void CPserv2View::OnViewFindNext() 
  575. {
  576.     if( !m_SearchContext.IsValid() )
  577.     {
  578.         OnViewFind();
  579.     }
  580.     else
  581.     {
  582.         CUIntArray results;
  583.         int nResultCount = m_SearchContext.FindNextIndices(m_pEntries,results);
  584.         if( nResultCount == 0 )
  585.         {
  586.             AfxMessageBox( FormattedString(_T("Cannot find '%s'"), m_SearchContext.GetSearchString()) );
  587.         }
  588.         else
  589.         {
  590.             // first remove selection from existing items
  591.             CUIntArray indices;
  592.             int nMax = GetSelectedIndices(indices), nIndex;
  593.             CListCtrl& theCtrl = GetListCtrl();
  594.             for( nIndex = 0; nIndex < nMax; nIndex++ )
  595.             {
  596.                 theCtrl.SetItemState(indices.GetAt(nIndex), 0, 0x000F );
  597.             }
  598.             for( nIndex = 0; nIndex < nResultCount; nIndex++ )
  599.             {
  600.                 theCtrl.SetItemState(results.GetAt(nIndex), LVIS_SELECTED, 0x000F );
  601.             }
  602.         }
  603.     }
  604. }
  605.  
  606.  
  607. void CPserv2View::OnSetObjectsStartType(SETSTARTTYPEFN lpfn, LPCTSTR lpszAction) 
  608. {
  609.     CUIntArray indices;
  610.     int nMaxIndex = GetSelectedIndices(indices);
  611.  
  612.     for( int i=0; i < nMaxIndex; i++)
  613.     {
  614.         m_nCurrentIndex = indices.GetAt(i);
  615.         CService* instance = (CService*) m_pServices->GetAt(m_nCurrentIndex);
  616.         if( instance )
  617.         {
  618.             if( !(instance->*lpfn)() )
  619.             {
  620.                 DisplayErrorMessage(_T("Unable to %s '%s'"), lpszAction, (LPCTSTR)instance->m_strDisplayName );
  621.             }
  622.             UpdateCurrentService();
  623.         }
  624.     }
  625. }
  626.  
  627. void CPserv2View::OnObjectEnable() 
  628. {
  629.     OnSetObjectsStartType(CService::Enable,_T("enable"));
  630. }
  631.  
  632. void CPserv2View::OnUpdateObjectEnable(CCmdUI* pCmdUI) 
  633. {
  634.     if( GetDocument()->m_DocType == DocType_Services )
  635.     {
  636.         pCmdUI->Enable( GetCurrentEntry() != NULL );
  637.     }
  638.     else pCmdUI->Enable( FALSE );
  639. }
  640.  
  641. void CPserv2View::OnObjectDisable() 
  642. {
  643.     OnSetObjectsStartType(CService::Disable,_T("disable"));
  644. }
  645.  
  646. void CPserv2View::OnUpdateObjectDisable(CCmdUI* pCmdUI) 
  647. {
  648.     if( GetDocument()->m_DocType == DocType_Services )
  649.     {
  650.         pCmdUI->Enable( GetCurrentEntry() != NULL );
  651.     }
  652.     else pCmdUI->Enable( FALSE );
  653. }
  654.  
  655. void CPserv2View::OnObjectStartAutomatically() 
  656. {
  657.     OnSetObjectsStartType(CService::StartAutomatically,_T("change startup type to automatic for"));
  658. }
  659.  
  660. void CPserv2View::OnUpdateObjectStartAutomatically(CCmdUI* pCmdUI) 
  661. {
  662.     if( GetDocument()->m_DocType == DocType_Services )
  663.     {
  664.         CService* instance = GetCurrentService();
  665.         pCmdUI->Enable( instance != NULL );
  666.     }
  667.     else pCmdUI->Enable( FALSE );
  668. }
  669.  
  670. void CPserv2View::OnUpdateObjectProperties(CCmdUI* pCmdUI) 
  671. {
  672.     if( GetDocument()->m_DocType == DocType_Services )
  673.     {
  674.         pCmdUI->Enable( GetCurrentEntry() != NULL );
  675.     }
  676.     else pCmdUI->Enable( FALSE );
  677. }
  678.  
  679. void CPserv2View::OnObjectInstall() 
  680. {
  681.     CPropertySheet sheet(_T("Install object"), this);
  682.     CServiceList* pServiceList = (CServiceList*) &(GetDocument()->m_pEntries);
  683.     CService* instance = new CService(pServiceList);
  684.     if( !instance )
  685.     {
  686.         DisplayErrorMessage(_T("Fatal Error, out of memory allocating another %d bytes"), sizeof(CService) );
  687.     }
  688.     else
  689.     {
  690.         CPageStartup pageStartup;
  691.         pageStartup.SetService(instance);
  692.         sheet.AddPage(&pageStartup);
  693.  
  694.         CAdvancedPage advanced;
  695.         advanced.SetService(instance);
  696.         sheet.AddPage(&advanced);
  697.     
  698.         sheet.m_psh.dwFlags |= PSH_NOAPPLYNOW | PSH_PROPTITLE;
  699.         sheet.m_psh.dwFlags &= ~(PSH_HASHELP|PSH_WIZARDCONTEXTHELP);
  700.  
  701.         if( sheet.DoModal() == IDOK )
  702.         {
  703.             BeginWaitCursor();
  704.             if( !instance->Install() )
  705.             {
  706.                 DisplayErrorMessage(_T("Unable to install %s."),(LPCTSTR) instance->m_strServiceName );
  707.             }
  708.             else
  709.             {
  710.                 pServiceList->m_Entries.Add(instance);
  711.                 Sleep(500);
  712.                 OnViewRefresh();
  713.                 EndWaitCursor();
  714.             }
  715.         }
  716.     }
  717. }
  718.  
  719. void CPserv2View::OnObjectDelete() 
  720. {
  721.     CUIntArray indices;
  722.     int nMaxIndex = GetSelectedIndices(indices);
  723.     BOOL bAnyChanges = FALSE;
  724.     for( int i=0; i < nMaxIndex; i++)
  725.     {
  726.         m_nCurrentIndex = indices.GetAt(i);
  727.         CService* instance = (CService*) m_pServices->GetAt(m_nCurrentIndex);
  728.         if( instance )
  729.         {
  730.             if( AfxMessageBox( FormattedString(_T("Are you sure you want to delete '%s'"), (LPCTSTR) instance->m_strDisplayName ), MB_OKCANCEL|MB_ICONQUESTION ) == IDOK )
  731.             {
  732.                 BeginWaitCursor();
  733.                 BOOL bSuccess = instance->Delete();
  734.                 EndWaitCursor();
  735.                 if( !bSuccess )
  736.                 {
  737.                     DisplayErrorMessage(_T("Unable to delete '%s'"), (LPCTSTR)instance->m_strDisplayName );
  738.                 }
  739.                 else
  740.                 {
  741.                     bAnyChanges = TRUE;
  742.                 }
  743.             }
  744.         }
  745.     }
  746.     if( bAnyChanges )
  747.     {
  748.         Sleep(500);
  749.         OnViewRefresh();
  750.     }
  751. }
  752.  
  753. void CPserv2View::OnUpdateObjectDelete(CCmdUI* pCmdUI) 
  754. {
  755.     if( GetDocument()->m_DocType == DocType_Services )
  756.     {
  757.         CService* instance = GetCurrentService();
  758.         pCmdUI->Enable( instance != NULL );
  759.     }
  760.     else pCmdUI->Enable( FALSE );
  761. }
  762.  
  763. void CPserv2View::OnUpdateObjectKillprocess(CCmdUI* pCmdUI) 
  764. {
  765.     switch( GetDocument()->m_DocType )
  766.     {
  767.     case DocType_Services:
  768.         {
  769.             CService* instance = GetCurrentService();
  770.             pCmdUI->Enable( instance && (instance->GetProcessID() != 0) );
  771.         }
  772.         break;
  773.         
  774.     case DocType_ProcessList:
  775.         {
  776.             pCmdUI->Enable( GetCurrentEntry() != NULL );
  777.         }
  778.         break;
  779.  
  780.     default:
  781.         pCmdUI->Enable( FALSE );
  782.     }
  783.     
  784. }
  785.  
  786. void CPserv2View::OnObjectKillprocess() 
  787. {
  788.     CUIntArray indices;
  789.     int nMaxIndex = GetSelectedIndices(indices);
  790.     BOOL bAnyChanges = FALSE;
  791.     for( int i=0; i < nMaxIndex; i++)
  792.     {
  793.         m_nCurrentIndex = indices.GetAt(i);
  794.  
  795.         CListViewEntry* instance = (CListViewEntry*) m_pServices->GetAt(m_nCurrentIndex);
  796.         if( instance )
  797.         {
  798.             CString strMessage;
  799.             DWORD dwProcessID = 0;
  800.  
  801.             switch( GetDocument()->m_DocType )
  802.             {
  803.             case DocType_Services:
  804.                 {
  805.                     CService* service = (CService*) instance;
  806.                     dwProcessID = service->GetProcessID();
  807.                     strMessage.Format( _T("Are you sure you want to kill the process for '%s' (ID %ld)"), 
  808.                         (LPCTSTR) service->m_strDisplayName, dwProcessID );
  809.                 }
  810.                 break;
  811.  
  812.             case DocType_ProcessList:
  813.                 {
  814.                     CProcessInfo* process = (CProcessInfo*) instance;
  815.                     dwProcessID = process->m_dwProcessID;
  816.                     strMessage.Format( _T("Are you sure you want to kill the process for '%s' (ID %ld)"), 
  817.                         (LPCTSTR) process->m_strFileName, dwProcessID );
  818.                 }
  819.                 break;
  820.             }
  821.             ASSERT(dwProcessID);
  822.  
  823.             if( AfxMessageBox( strMessage, MB_OKCANCEL|MB_ICONQUESTION ) == IDOK )
  824.             {
  825.                 BeginWaitCursor();
  826.                 BOOL bSuccess = KillProcess(dwProcessID);
  827.                 EndWaitCursor();
  828.                 if( !bSuccess )
  829.                 {
  830.                     DisplayErrorMessage(_T("Unable to kill process") );
  831.                 }
  832.                 else bAnyChanges = TRUE;
  833.             }
  834.         }
  835.     }
  836.     if( bAnyChanges )
  837.     {
  838.         Sleep(500);
  839.         OnViewRefresh();
  840.     }
  841. }
  842.  
  843. BOOL CPserv2View::KillProcess(DWORD dwProcessID)
  844. {
  845.     if( !dwProcessID )
  846.         return TRUE;
  847.  
  848.     theMachineAccount.Impersonate();
  849.     BOOL bSuccess = FALSE;
  850.     HANDLE hProcess = OpenProcess( PROCESS_TERMINATE, FALSE, dwProcessID );
  851.     DWORD dwLastError = GetLastError();
  852.     if( hProcess != NULL )
  853.     {
  854.         bSuccess = TerminateProcess( hProcess, 0 );
  855.         dwLastError = GetLastError();
  856.         CloseHandle(hProcess);
  857.     }
  858.     theMachineAccount.RevertToSelf();
  859.     SetLastError(dwLastError);
  860.     return bSuccess;
  861. }
  862.  
  863. void CPserv2View::OnHeaderItemChanged(NMHDR* pNMHDR, LRESULT* pResult) 
  864. {
  865.     if( !m_bInsideAutoResize )
  866.     {
  867.         CListCtrl& theCtrl = GetListCtrl();
  868.  
  869.         for( DWORD dwIndex = 0; dwIndex < m_dwNumberOfColumns; dwIndex++ )
  870.         {
  871.             CListViewColumn* c = (CListViewColumn*) m_pEntries->GetColumn(dwIndex);
  872.             c->m_dwWidth = theCtrl.GetColumnWidth(dwIndex);
  873.         }
  874.     }
  875.     *pResult = 0;
  876. }
  877.  
  878. void CPserv2View::OnObjectStop() 
  879. {
  880.     OnObjectControl( &CService::BeginStop, &CService::IsStopped, _T("stop") );
  881. }
  882.  
  883.  
  884. void CPserv2View::OnObjectControl(BEGINSTATFN begin, QUERYSTATEFN query, LPCTSTR lpszAction) 
  885. {
  886.     if( GetDocument()->m_DocType == DocType_Services )
  887.     {
  888.         CUIntArray indices;
  889.         int nMaxIndex = GetSelectedIndices(indices);
  890.  
  891.         for( int i=0; i < nMaxIndex; i++)
  892.         {
  893.             m_nCurrentIndex = indices.GetAt(i);
  894.             CService* instance = (CService*) m_pServices->GetAt(m_nCurrentIndex);
  895.             if( instance )
  896.             {
  897.                 DWORD dwWaitHint = 10 * 1000;
  898.                 SERVICE_STATE_TYPE sst = (instance->*begin)(&dwWaitHint);
  899.                 if( sst == SERVICE_STATE_FAILED )
  900.                 {
  901.                     DisplayErrorMessage(_T("Unable to %s '%s'"), lpszAction, (LPCTSTR)instance->m_strDisplayName );
  902.                 }
  903.                 else 
  904.                 {
  905.                     CServiceControlDialog(instance, dwWaitHint, query, lpszAction, this).DoModal();
  906.                 }
  907.                 instance->FinishStateType();
  908.                 UpdateCurrentService();
  909.             }
  910.         }
  911.     }
  912. }
  913.  
  914. void CPserv2View::OnObjectStart() 
  915. {
  916.     OnObjectControl( &CService::BeginStart, &CService::IsStarted, _T("start") );
  917. }
  918.  
  919. void CPserv2View::OnObjectRestart() 
  920. {
  921.     OnObjectControl( &CService::BeginRestart, &CService::IsRestarted, _T("restart") );
  922. }
  923.  
  924.  
  925. void CPserv2View::OnObjectPause() 
  926. {
  927.     OnObjectControl( &CService::BeginPause, &CService::IsPaused, _T("pause") );
  928. }
  929.  
  930. void CPserv2View::OnObjectContinue() 
  931. {
  932.     OnObjectControl( &CService::BeginContinue, &CService::IsContinued, _T("continue") );
  933. }
  934.  
  935. void CPserv2View::OnUpdateObjectStop(CCmdUI* pCmdUI) 
  936. {
  937.     if( GetDocument()->m_DocType == DocType_Services )
  938.     {
  939.         CService* instance = GetCurrentService();
  940.         pCmdUI->Enable( instance && instance->IsStarted() == SERVICE_STATE_REACHED );
  941.     }
  942.     else pCmdUI->Enable( FALSE );
  943. }
  944.  
  945. void CPserv2View::OnUpdateObjectStart(CCmdUI* pCmdUI) 
  946. {
  947.     if( GetDocument()->m_DocType == DocType_Services )
  948.     {
  949.         CService* instance = GetCurrentService();
  950.         pCmdUI->Enable( instance && instance->IsStopped() == SERVICE_STATE_REACHED );
  951.     }
  952.     else pCmdUI->Enable( FALSE );
  953. }
  954.  
  955. void CPserv2View::OnUpdateObjectRestart(CCmdUI* pCmdUI) 
  956. {
  957.     if( GetDocument()->m_DocType == DocType_Services )
  958.     {
  959.         CService* instance = GetCurrentService();
  960.         pCmdUI->Enable( instance && instance->IsStarted() == SERVICE_STATE_REACHED );
  961.     }
  962.     else pCmdUI->Enable( FALSE );
  963. }
  964.  
  965. void CPserv2View::OnUpdateObjectPause(CCmdUI* pCmdUI) 
  966. {
  967.     if( GetDocument()->m_DocType == DocType_Services )
  968.     {
  969.         CService* instance = GetCurrentService();
  970.         pCmdUI->Enable( instance && instance->IsStarted() == SERVICE_STATE_REACHED );
  971.     }
  972.     else pCmdUI->Enable( FALSE );
  973. }
  974.  
  975. void CPserv2View::OnUpdateObjectContinue(CCmdUI* pCmdUI) 
  976. {
  977.     if( GetDocument()->m_DocType == DocType_Services )
  978.     {
  979.         CService* instance = GetCurrentService();
  980.         pCmdUI->Enable( instance && instance->IsPaused() == SERVICE_STATE_REACHED );
  981.     }
  982.     else pCmdUI->Enable( FALSE );
  983. }
  984.  
  985.  
  986. void CPserv2View::OnObjectStartOrContinue() 
  987. {
  988.     CService* instance = GetCurrentService();
  989.     if( instance->IsPaused() == SERVICE_STATE_REACHED )
  990.     {
  991.         OnObjectControl( &CService::BeginContinue, &CService::IsContinued, _T("continue") );
  992.     }
  993.     else
  994.     {
  995.         OnObjectControl( &CService::BeginStart, &CService::IsStarted, _T("start") );
  996.     }
  997. }
  998.  
  999. void CPserv2View::OnUpdateObjectStartOrContinue(CCmdUI* pCmdUI) 
  1000. {
  1001.     if( GetDocument()->m_DocType == DocType_Services )
  1002.     {
  1003.         CService* instance = GetCurrentService();
  1004.         pCmdUI->Enable( instance && (instance->IsPaused() == SERVICE_STATE_REACHED ||
  1005.                                      instance->IsStopped() == SERVICE_STATE_REACHED) );
  1006.     }
  1007.     else pCmdUI->Enable( FALSE );
  1008. }
  1009.  
  1010.  
  1011. void CPserv2View::OnConnectToLocalMachine() 
  1012. {
  1013.     CListViewEntries* sl = GetDocument()->m_pEntries;
  1014.     sl->ConnectTo(NULL);
  1015.     sl->Refresh();
  1016.     DisplayAllItems();
  1017. }
  1018.  
  1019. void CPserv2View::OnConnectToRemoteMachine() 
  1020. {
  1021.     CRemoteConnectDialog box;
  1022.     if( box.DoModal() == IDOK )
  1023.     {
  1024.         BeginWaitCursor();
  1025.         CListViewEntries* sl = GetDocument()->m_pEntries;
  1026.         CString strOldMachine( sl->m_strServiceMachine );
  1027.         CString strNewMachine(theConfiguration.m_ComputerNames.GetAt(0));
  1028.         sl->ConnectTo(strNewMachine);
  1029.         if( !sl->Refresh() )
  1030.         {
  1031.             sl->ConnectTo(strOldMachine);
  1032.             if( !sl->Refresh() ) 
  1033.             {
  1034.                 sl->ConnectTo(strOldMachine);
  1035.                 sl->Refresh();
  1036.             }
  1037.         }
  1038.         DisplayAllItems();
  1039.         EndWaitCursor();
  1040.     }
  1041. }
  1042.  
  1043.  
  1044. void CPserv2View::OnUpdateFilePrintPreview(CCmdUI* pCmdUI) 
  1045. {
  1046.     pCmdUI->Enable( GetDocument()->m_DocType == DocType_Services );
  1047. }
  1048.  
  1049. void CPserv2View::OnUpdateFilePrint(CCmdUI* pCmdUI) 
  1050. {
  1051.     pCmdUI->Enable( GetDocument()->m_DocType == DocType_Services );
  1052. }
  1053.  
  1054. void CPserv2View::OnUpdateObjectInstall(CCmdUI* pCmdUI) 
  1055. {
  1056.     pCmdUI->Enable( GetDocument()->m_DocType == DocType_Services );
  1057. }
  1058.  
  1059. void CPserv2View::OnUpdateTemplatesApply(CCmdUI* pCmdUI) 
  1060. {
  1061.     pCmdUI->Enable( GetDocument()->m_DocType == DocType_Services );
  1062. }
  1063.  
  1064. void CPserv2View::OnOptionsAutoresizeallcolumns() 
  1065. {
  1066.     SetRedraw(FALSE);
  1067.     CListCtrl& theCtrl = GetListCtrl();
  1068.     m_bInsideAutoResize = TRUE;
  1069.     for( DWORD i = 0; i < m_dwNumberOfColumns; i++ )
  1070.     {
  1071.         CListViewColumn* c = (CListViewColumn*) m_pEntries->GetColumn(i);
  1072.         c->m_dwWidth = LVSCW_AUTOSIZE;
  1073.         theCtrl.SetColumnWidth( i, LVSCW_AUTOSIZE );
  1074.     }
  1075.     SetRedraw(TRUE);
  1076.     m_bInsideAutoResize = FALSE;
  1077. }
  1078.  
  1079. void CPserv2View::OnViewChoosecolumns() 
  1080. {
  1081.     CChooseColumnsDialog box(m_pEntries, this);
  1082.     if( box.DoModal() )
  1083.     {
  1084.         m_pEntries = NULL;
  1085.         OnViewRefresh();
  1086.     }
  1087. }
  1088.