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 >
Wrap
C/C++ Source or Header
|
2005-01-05
|
32KB
|
1,088 lines
// pserv2View.cpp : implementation of the CPserv2View class
//
#include "stdafx.h"
#include "pserv2.h"
#include "ServiceControlDialog.h"
#include "pserv2Doc.h"
#include "pserv2View.h"
#include "PageStartup.h"
#include "CConfiguration.h"
#include "AdvancedPage.h"
#include "RemoteConnectDialog.h"
#include "ChooseColumnsDialog.h"
#include "CNTMachineAccount.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPserv2View
IMPLEMENT_DYNCREATE(CPserv2View, CListView)
BEGIN_MESSAGE_MAP(CPserv2View, CListView)
//{{AFX_MSG_MAP(CPserv2View)
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, OnColumnclick)
ON_COMMAND(ID_OBJECT_STOP, OnObjectStop)
ON_COMMAND(ID_OBJECT_START, OnObjectStart)
ON_COMMAND(ID_OBJECT_RESTART, OnObjectRestart)
ON_COMMAND(ID_ANSICHT_REFRESH, OnViewRefresh)
ON_COMMAND(ID_OBJECT_PAUSE, OnObjectPause)
ON_COMMAND(ID_OBJECT_CONTINUE, OnObjectContinue)
ON_WM_MOUSEWHEEL()
ON_UPDATE_COMMAND_UI(ID_OBJECT_STOP, OnUpdateObjectStop)
ON_UPDATE_COMMAND_UI(ID_OBJECT_START, OnUpdateObjectStart)
ON_UPDATE_COMMAND_UI(ID_OBJECT_RESTART, OnUpdateObjectRestart)
ON_UPDATE_COMMAND_UI(ID_OBJECT_PAUSE, OnUpdateObjectPause)
ON_UPDATE_COMMAND_UI(ID_OBJECT_CONTINUE, OnUpdateObjectContinue)
ON_WM_CONTEXTMENU()
ON_NOTIFY_REFLECT(NM_DBLCLK, OnDoubleClickItem)
ON_COMMAND(ID_OBJECT_START_OR_CONTINUE, OnObjectStartOrContinue)
ON_UPDATE_COMMAND_UI(ID_OBJECT_START_OR_CONTINUE, OnUpdateObjectStartOrContinue)
ON_NOTIFY_REFLECT(LVN_GETINFOTIP, OnGetInfoTip)
ON_WM_MOUSEMOVE()
ON_COMMAND(ID_DISPLAY_CONNECTTOLOCALMACHINE, OnConnectToLocalMachine)
ON_COMMAND(ID_DISPLAY_CONNECTTOREMOTEMACHINE, OnConnectToRemoteMachine)
ON_COMMAND(ID_OPTIONS_FONT, OnOptionsFont)
ON_COMMAND(ID_OBJECT_PROPERTIES, OnObjectProperties)
ON_COMMAND(ID_VIEW_FIND, OnViewFind)
ON_COMMAND(ID_VIEW_FINDNEXT, OnViewFindNext)
ON_COMMAND(ID_OBJECT_ENABLE, OnObjectEnable)
ON_UPDATE_COMMAND_UI(ID_OBJECT_ENABLE, OnUpdateObjectEnable)
ON_COMMAND(ID_OBJECT_DISABLE, OnObjectDisable)
ON_UPDATE_COMMAND_UI(ID_OBJECT_DISABLE, OnUpdateObjectDisable)
ON_COMMAND(ID_OBJECT_STARTAUTOMATICALLY, OnObjectStartAutomatically)
ON_UPDATE_COMMAND_UI(ID_OBJECT_STARTAUTOMATICALLY, OnUpdateObjectStartAutomatically)
ON_UPDATE_COMMAND_UI(ID_OBJECT_PROPERTIES, OnUpdateObjectProperties)
ON_COMMAND(ID_OBJECT_INSTALL, OnObjectInstall)
ON_COMMAND(ID_OBJECT_DELETE, OnObjectDelete)
ON_UPDATE_COMMAND_UI(ID_OBJECT_DELETE, OnUpdateObjectDelete)
ON_UPDATE_COMMAND_UI(ID_OBJECT_KILLPROCESS, OnUpdateObjectKillprocess)
ON_COMMAND(ID_OBJECT_KILLPROCESS, OnObjectKillprocess)
ON_NOTIFY(HDN_ITEMCHANGED, 0, OnHeaderItemChanged)
ON_UPDATE_COMMAND_UI(ID_FILE_PRINT_PREVIEW, OnUpdateFilePrintPreview)
ON_UPDATE_COMMAND_UI(ID_FILE_PRINT, OnUpdateFilePrint)
ON_UPDATE_COMMAND_UI(ID_OBJECT_INSTALL, OnUpdateObjectInstall)
ON_UPDATE_COMMAND_UI(ID_TEMPLATES_APPLY, OnUpdateTemplatesApply)
ON_COMMAND(ID_OPTIONS_AUTORESIZEALLCOLUMNS, OnOptionsAutoresizeallcolumns)
ON_COMMAND(ID_VIEW_CHOOSECOLUMNS, OnViewChoosecolumns)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CListView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CListView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CListView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPserv2View construction/destruction
CPserv2View* theCurrentView = NULL;
CPserv2View::CPserv2View()
: m_bAnythingHasBeenDisplayed( FALSE ),
m_bInsideAutoResize( FALSE ),
m_pEntries( NULL )
{
theCurrentView = this;
}
CPserv2View::~CPserv2View()
{
}
BOOL CPserv2View::PreCreateWindow(CREATESTRUCT& cs)
{
cs.style |= LVS_REPORT;
return CListView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CPserv2View drawing
void CPserv2View::OnDraw(CDC* pDC)
{
CPserv2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
void CPserv2View::OnInitialUpdate()
{
CListView::OnInitialUpdate();
CDC* dc = GetDC();
m_Font.CreatePointFont( theConfiguration.m_dwFontSize, theConfiguration.m_strFontFace, dc );
ReleaseDC(dc);
SetFont(&m_Font);
// this code only works for a report-mode list view
ASSERT(GetStyle() & LVS_REPORT);
// Gain a reference to the list control itself
CListCtrl& theCtrl = GetListCtrl();
theCtrl.SetExtendedStyle(theCtrl.GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP );
DisplayAllItems();
}
/////////////////////////////////////////////////////////////////////////////
// CPserv2View printing
void CPserv2View::DisplayAllItems(CListViewEntry* pSelect)
{
DWORD dwIndex;
SetRedraw(FALSE);
CListCtrl& theCtrl = GetListCtrl();
CListViewEntries* pEntries = GetDocument()->m_pEntries;
theCtrl.DeleteAllItems();
// update columns now
if( m_pEntries != pEntries )
{
m_pEntries = pEntries;
while( theCtrl.DeleteColumn(0) )
{
}
m_dwNumberOfColumns = pEntries->GetNumberOfColumns();
for( dwIndex = 0; dwIndex < m_dwNumberOfColumns; dwIndex++ )
{
CListViewColumn* c = (CListViewColumn*) pEntries->GetColumn(dwIndex);
theCtrl.InsertColumn(dwIndex, c->m_strCaption, LVCFMT_LEFT, c->m_dwWidth);
}
}
m_pServices = &(pEntries->m_Entries);
int nElements = m_pServices->GetSize();
for( int nIndex = 0; nIndex < nElements; nIndex++ )
{
CListViewEntry* entry = (CListViewEntry*) m_pServices->GetAt(nIndex);
for( dwIndex = 0; dwIndex < m_dwNumberOfColumns; dwIndex++ )
{
if(!dwIndex)
theCtrl.InsertItem( nIndex, m_pEntries->GetDisplayString(entry, dwIndex) );
else
theCtrl.SetItem( nIndex, dwIndex, LVIF_TEXT, m_pEntries->GetDisplayString(entry, dwIndex), 0, 0, 0, 0);
}
if( entry == pSelect )
{
theCtrl.SetItemState(nIndex, LVIS_FOCUSED|LVIS_SELECTED, 0x000F );
}
}
m_bInsideAutoResize = TRUE;
for( dwIndex = 0; dwIndex < m_dwNumberOfColumns; dwIndex++ )
{
CListViewColumn* c = (CListViewColumn*) pEntries->GetColumn(dwIndex);
if( c->m_dwWidth == LVSCW_AUTOSIZE )
{
theCtrl.SetColumnWidth( dwIndex, LVSCW_AUTOSIZE );
}
}
SetRedraw(TRUE);
m_bInsideAutoResize = FALSE;
m_bAnythingHasBeenDisplayed = TRUE;
AfxGetMainWnd()->SetWindowText( GetDocument()->m_pEntries->m_strTitle );
}
BOOL CPserv2View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CPserv2View::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;
switch(lplvcd->nmcd.dwDrawStage)
{
case CDDS_PREPAINT:
// Request prepaint notifications for each item.
*pResult = CDRF_NOTIFYITEMDRAW;
break;
case CDDS_ITEMPREPAINT: // Requested notification
{
if( lplvcd->nmcd.dwItemSpec < (DWORD) m_pServices->GetSize() )
{
CListViewEntry* entry = (CListViewEntry*) m_pServices->GetAt( lplvcd->nmcd.dwItemSpec );
if( entry )
{
lplvcd->clrText = entry->GetTextColor();
}
*pResult = CDRF_DODEFAULT;
}
}
break;
}
}
/////////////////////////////////////////////////////////////////////////////
// CPserv2View diagnostics
#ifdef _DEBUG
void CPserv2View::AssertValid() const
{
CListView::AssertValid();
}
void CPserv2View::Dump(CDumpContext& dc) const
{
CListView::Dump(dc);
}
CPserv2Doc* CPserv2View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPserv2Doc)));
return (CPserv2Doc*)m_pDocument;
}
#endif //_DEBUG
void CPserv2View::OnColumnclick(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
if( (pNMListView->iSubItem >= 0) && (pNMListView->iSubItem < int(m_dwNumberOfColumns)) )
{
if( m_pEntries->m_dwSortMethod == (DWORD) pNMListView->iSubItem )
{
m_pEntries->m_bSortAscending = m_pEntries->m_bSortAscending ? FALSE : TRUE;
}
else
{
m_pEntries->m_bSortAscending = TRUE;
m_pEntries->m_dwSortMethod = (DWORD) pNMListView->iSubItem;
}
GetDocument()->m_pEntries->Sort();
DisplayAllItems();
}
*pResult = 0;
}
int CPserv2View::GetSelectedIndices(CUIntArray& listOfIndices)
{
listOfIndices.RemoveAll();
ASSERT( listOfIndices.GetSize() == 0 );
CListCtrl& theCtrl = GetListCtrl();
POSITION pos = theCtrl.GetFirstSelectedItemPosition();
if( pos )
{
while( pos )
{
listOfIndices.Add(theCtrl.GetNextSelectedItem(pos));
}
}
return listOfIndices.GetSize();
}
int CPserv2View::GetFirstSelectedIndex()
{
CListCtrl& theCtrl = GetListCtrl();
POSITION pos = theCtrl.GetFirstSelectedItemPosition();
if( pos )
{
return theCtrl.GetNextSelectedItem(pos);
}
return -1;
}
CListViewEntry* CPserv2View::GetCurrentEntry()
{
CListCtrl& theCtrl = GetListCtrl();
POSITION pos = theCtrl.GetFirstSelectedItemPosition();
if( pos )
{
m_nCurrentIndex = theCtrl.GetNextSelectedItem(pos);
return (CListViewEntry*) m_pServices->GetAt(m_nCurrentIndex);
}
return NULL;
}
void CPserv2View::UpdateCurrentService()
{
CListCtrl& theCtrl = GetListCtrl();
CListViewEntry* entry = (CListViewEntry*) m_pServices->GetAt(m_nCurrentIndex);
for( DWORD dwIndex = 0; dwIndex < m_dwNumberOfColumns; dwIndex++ )
{
theCtrl.SetItem( m_nCurrentIndex, dwIndex, LVIF_TEXT, m_pEntries->GetDisplayString(entry, dwIndex), 0, 0, 0, 0);
}
}
void CPserv2View::OnViewRefresh()
{
GetDocument()->m_pEntries->Refresh();
DisplayAllItems();
}
BOOL CPserv2View::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
if( nFlags & MK_CONTROL )
{
if( zDelta < 0 )
{
if( theConfiguration.m_dwFontSize > 40 )
theConfiguration.m_dwFontSize -= 10;
}
else
{
if( theConfiguration.m_dwFontSize < 320 )
theConfiguration.m_dwFontSize += 10;
}
CDC* dc = GetDC();
m_Font.DeleteObject();
m_Font.CreatePointFont(theConfiguration.m_dwFontSize, theConfiguration.m_strFontFace, dc );
ReleaseDC(dc);
SetFont(&m_Font);
}
return CListView::OnMouseWheel(nFlags, zDelta, pt);
}
void CPserv2View::OnContextMenu(CWnd* pWnd, CPoint point)
{
// make sure window is active
GetParentFrame()->ActivateFrame();
CPoint local = point;
ScreenToClient(&local);
CMenu menu;
if(menu.LoadMenu(m_pEntries->GetContextMenuID()))
{
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, AfxGetMainWnd());
}
}
void CPserv2View::OnDoubleClickItem(NMHDR* pNMHDR, LRESULT* pResult)
{
if( GetDocument()->m_DocType == DocType_Services )
OnObjectProperties();
*pResult = 0;
}
void CPserv2View::OnGetInfoTip(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMLVGETINFOTIP pDispInfo = (LPNMLVGETINFOTIP)pNMHDR;
CListCtrl& theCtrl = GetListCtrl();
int nIndex = theCtrl.HitTest(m_LastPoint);
if( nIndex >= 0 )
{
CListViewEntry* instance = (CListViewEntry*) m_pServices->GetAt(nIndex);
CString strInfoTip(instance->GetInfoTip());
if( !IsEmptyString(strInfoTip) )
{
tstrncpy( pDispInfo->pszText, strInfoTip, pDispInfo->cchTextMax );
*pResult = 1;
}
}
}
void CPserv2View::OnMouseMove(UINT nFlags, CPoint point)
{
m_LastPoint = point;
CListView::OnMouseMove(nFlags, point);
}
static int nElementsPerPage = 19;
inline LPCTSTR SAFESTR(LPCTSTR p, LPCTSTR d)
{
return p ? p : d;
}
void CPserv2View::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
m_nPrintServiceIndex = (pInfo->m_nCurPage-1) * nElementsPerPage;
CFont courier;
courier.CreatePointFont(80, _T("Courier New"), pDC );
CFont bodyBold;
bodyBold.CreatePointFont(80, _T("Arial Bold"), pDC );
CFont bodyNormal;
bodyNormal.CreatePointFont(80, _T("Arial"), pDC );
CFont * pOldFont = pDC->SelectObject(&bodyNormal);
int nPos = 0;
CBrush active;
active.CreateSolidBrush(RGB(0xD0,0xD0,0xD0));
pDC->SetBkMode(TRANSPARENT);
int nElements = m_pServices->GetSize();
for( int nIndex = 0; nIndex < nElementsPerPage; nIndex++ )
{
if( nIndex+ m_nPrintServiceIndex >= nElements )
break;
int nPointsNeededPerPage = nPos;
CService* service = (CService*) m_pServices->GetAt(nIndex+m_nPrintServiceIndex);
pDC->SelectObject(&bodyBold);
CSize TextSize = pDC->GetOutputTextExtent(service->m_strDisplayName) ;
if( service->IsRunning() == SERVICE_STATE_REACHED )
{
CRect rect(0, nPos, pInfo->m_rectDraw.right, nPos + (TextSize.cy*3) );
pDC->FillRect(&rect, &active);
}
pDC->TextOut(0, nPos, service->m_strDisplayName );
pDC->SelectObject(&bodyNormal);
pDC->TextOut(TextSize.cx, nPos, FormattedString( _T(" - %s"), (LPCTSTR) service->m_strServiceName) );
nPos += TextSize.cy;
pDC->TextOut(0, nPos, FormattedString( _T("Status: %s - Start: %s - Type: %s - PID: %d"),
SAFESTR(service->GetPrintStatusAsString(),_T("???")),
SAFESTR(service->GetStartAsString(),_T("???")),
SAFESTR(service->GetTypeAsString(),_T("???")),
SAFESTR(service->GetPidAsString(),_T("???")) ));
nPos += TextSize.cy;
pDC->SelectObject(&courier);
pDC->TextOut(0, nPos, SAFESTR(service->m_strBinaryPathName,_T("???")));
nPos += TextSize.cy;
nPos += TextSize.cy;
}
m_nPrintServiceIndex += 30;
pDC->SelectObject(pOldFont);
}
void CPserv2View::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
int nElements = m_pServices->GetSize();
int nPageHeight = pDC->GetDeviceCaps(PHYSICALHEIGHT);
nElementsPerPage = nPageHeight/ 360 ;
int nPages = (nElements % nElementsPerPage) ? 1 : 0;
nPages += nElements / nElementsPerPage;
pInfo->SetMaxPage(nPages);
m_nPrintServiceIndex = 0;
}
void CPserv2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CPserv2View::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
DisplayAllItems();
}
void CPserv2View::OnOptionsFont()
{
LOGFONT lf;
m_Font.GetLogFont(&lf);
CFontDialog dialog(&lf);
if( dialog.DoModal() == IDOK )
{
theConfiguration.m_dwFontSize = dialog.GetSize();
theConfiguration.m_strFontFace = dialog.GetFaceName();
CDC* dc = GetDC();
m_Font.DeleteObject();
m_Font.CreatePointFont(theConfiguration.m_dwFontSize, theConfiguration.m_strFontFace, dc );
ReleaseDC(dc);
SetFont(&m_Font);
}
}
void CPserv2View::OnObjectProperties()
{
CService* instance = GetCurrentService();
if( instance )
{
CPropertySheet sheet(FormattedString(_T("Settings for %s"), (LPCTSTR) instance->m_strServiceName ), this);
instance->m_dwChangeFlags = 0;
CPageStartup pageStartup;
pageStartup.SetService(instance);
sheet.AddPage(&pageStartup);
CAdvancedPage advanced;
advanced.SetService(instance);
sheet.AddPage(&advanced);
sheet.m_psh.dwFlags |= PSH_NOAPPLYNOW | PSH_PROPTITLE;
sheet.m_psh.dwFlags &= ~(PSH_HASHELP|PSH_WIZARDCONTEXTHELP);
if( sheet.DoModal() == IDOK )
{
if( !instance->ApplyChanges() )
{
DisplayErrorMessage(_T("Unable to settings for %s"),(LPCTSTR) instance->m_strServiceName );
}
UpdateCurrentService();
}
}
}
void CPserv2View::OnViewFind()
{
CFindDialog fd;
if( fd.DoModal() == IDOK )
{
m_SearchContext.Refresh(GetFirstSelectedIndex());
OnViewFindNext();
}
}
void CPserv2View::OnViewFindNext()
{
if( !m_SearchContext.IsValid() )
{
OnViewFind();
}
else
{
CUIntArray results;
int nResultCount = m_SearchContext.FindNextIndices(m_pEntries,results);
if( nResultCount == 0 )
{
AfxMessageBox( FormattedString(_T("Cannot find '%s'"), m_SearchContext.GetSearchString()) );
}
else
{
// first remove selection from existing items
CUIntArray indices;
int nMax = GetSelectedIndices(indices), nIndex;
CListCtrl& theCtrl = GetListCtrl();
for( nIndex = 0; nIndex < nMax; nIndex++ )
{
theCtrl.SetItemState(indices.GetAt(nIndex), 0, 0x000F );
}
for( nIndex = 0; nIndex < nResultCount; nIndex++ )
{
theCtrl.SetItemState(results.GetAt(nIndex), LVIS_SELECTED, 0x000F );
}
}
}
}
void CPserv2View::OnSetObjectsStartType(SETSTARTTYPEFN lpfn, LPCTSTR lpszAction)
{
CUIntArray indices;
int nMaxIndex = GetSelectedIndices(indices);
for( int i=0; i < nMaxIndex; i++)
{
m_nCurrentIndex = indices.GetAt(i);
CService* instance = (CService*) m_pServices->GetAt(m_nCurrentIndex);
if( instance )
{
if( !(instance->*lpfn)() )
{
DisplayErrorMessage(_T("Unable to %s '%s'"), lpszAction, (LPCTSTR)instance->m_strDisplayName );
}
UpdateCurrentService();
}
}
}
void CPserv2View::OnObjectEnable()
{
OnSetObjectsStartType(CService::Enable,_T("enable"));
}
void CPserv2View::OnUpdateObjectEnable(CCmdUI* pCmdUI)
{
if( GetDocument()->m_DocType == DocType_Services )
{
pCmdUI->Enable( GetCurrentEntry() != NULL );
}
else pCmdUI->Enable( FALSE );
}
void CPserv2View::OnObjectDisable()
{
OnSetObjectsStartType(CService::Disable,_T("disable"));
}
void CPserv2View::OnUpdateObjectDisable(CCmdUI* pCmdUI)
{
if( GetDocument()->m_DocType == DocType_Services )
{
pCmdUI->Enable( GetCurrentEntry() != NULL );
}
else pCmdUI->Enable( FALSE );
}
void CPserv2View::OnObjectStartAutomatically()
{
OnSetObjectsStartType(CService::StartAutomatically,_T("change startup type to automatic for"));
}
void CPserv2View::OnUpdateObjectStartAutomatically(CCmdUI* pCmdUI)
{
if( GetDocument()->m_DocType == DocType_Services )
{
CService* instance = GetCurrentService();
pCmdUI->Enable( instance != NULL );
}
else pCmdUI->Enable( FALSE );
}
void CPserv2View::OnUpdateObjectProperties(CCmdUI* pCmdUI)
{
if( GetDocument()->m_DocType == DocType_Services )
{
pCmdUI->Enable( GetCurrentEntry() != NULL );
}
else pCmdUI->Enable( FALSE );
}
void CPserv2View::OnObjectInstall()
{
CPropertySheet sheet(_T("Install object"), this);
CServiceList* pServiceList = (CServiceList*) &(GetDocument()->m_pEntries);
CService* instance = new CService(pServiceList);
if( !instance )
{
DisplayErrorMessage(_T("Fatal Error, out of memory allocating another %d bytes"), sizeof(CService) );
}
else
{
CPageStartup pageStartup;
pageStartup.SetService(instance);
sheet.AddPage(&pageStartup);
CAdvancedPage advanced;
advanced.SetService(instance);
sheet.AddPage(&advanced);
sheet.m_psh.dwFlags |= PSH_NOAPPLYNOW | PSH_PROPTITLE;
sheet.m_psh.dwFlags &= ~(PSH_HASHELP|PSH_WIZARDCONTEXTHELP);
if( sheet.DoModal() == IDOK )
{
BeginWaitCursor();
if( !instance->Install() )
{
DisplayErrorMessage(_T("Unable to install %s."),(LPCTSTR) instance->m_strServiceName );
}
else
{
pServiceList->m_Entries.Add(instance);
Sleep(500);
OnViewRefresh();
EndWaitCursor();
}
}
}
}
void CPserv2View::OnObjectDelete()
{
CUIntArray indices;
int nMaxIndex = GetSelectedIndices(indices);
BOOL bAnyChanges = FALSE;
for( int i=0; i < nMaxIndex; i++)
{
m_nCurrentIndex = indices.GetAt(i);
CService* instance = (CService*) m_pServices->GetAt(m_nCurrentIndex);
if( instance )
{
if( AfxMessageBox( FormattedString(_T("Are you sure you want to delete '%s'"), (LPCTSTR) instance->m_strDisplayName ), MB_OKCANCEL|MB_ICONQUESTION ) == IDOK )
{
BeginWaitCursor();
BOOL bSuccess = instance->Delete();
EndWaitCursor();
if( !bSuccess )
{
DisplayErrorMessage(_T("Unable to delete '%s'"), (LPCTSTR)instance->m_strDisplayName );
}
else
{
bAnyChanges = TRUE;
}
}
}
}
if( bAnyChanges )
{
Sleep(500);
OnViewRefresh();
}
}
void CPserv2View::OnUpdateObjectDelete(CCmdUI* pCmdUI)
{
if( GetDocument()->m_DocType == DocType_Services )
{
CService* instance = GetCurrentService();
pCmdUI->Enable( instance != NULL );
}
else pCmdUI->Enable( FALSE );
}
void CPserv2View::OnUpdateObjectKillprocess(CCmdUI* pCmdUI)
{
switch( GetDocument()->m_DocType )
{
case DocType_Services:
{
CService* instance = GetCurrentService();
pCmdUI->Enable( instance && (instance->GetProcessID() != 0) );
}
break;
case DocType_ProcessList:
{
pCmdUI->Enable( GetCurrentEntry() != NULL );
}
break;
default:
pCmdUI->Enable( FALSE );
}
}
void CPserv2View::OnObjectKillprocess()
{
CUIntArray indices;
int nMaxIndex = GetSelectedIndices(indices);
BOOL bAnyChanges = FALSE;
for( int i=0; i < nMaxIndex; i++)
{
m_nCurrentIndex = indices.GetAt(i);
CListViewEntry* instance = (CListViewEntry*) m_pServices->GetAt(m_nCurrentIndex);
if( instance )
{
CString strMessage;
DWORD dwProcessID = 0;
switch( GetDocument()->m_DocType )
{
case DocType_Services:
{
CService* service = (CService*) instance;
dwProcessID = service->GetProcessID();
strMessage.Format( _T("Are you sure you want to kill the process for '%s' (ID %ld)"),
(LPCTSTR) service->m_strDisplayName, dwProcessID );
}
break;
case DocType_ProcessList:
{
CProcessInfo* process = (CProcessInfo*) instance;
dwProcessID = process->m_dwProcessID;
strMessage.Format( _T("Are you sure you want to kill the process for '%s' (ID %ld)"),
(LPCTSTR) process->m_strFileName, dwProcessID );
}
break;
}
ASSERT(dwProcessID);
if( AfxMessageBox( strMessage, MB_OKCANCEL|MB_ICONQUESTION ) == IDOK )
{
BeginWaitCursor();
BOOL bSuccess = KillProcess(dwProcessID);
EndWaitCursor();
if( !bSuccess )
{
DisplayErrorMessage(_T("Unable to kill process") );
}
else bAnyChanges = TRUE;
}
}
}
if( bAnyChanges )
{
Sleep(500);
OnViewRefresh();
}
}
BOOL CPserv2View::KillProcess(DWORD dwProcessID)
{
if( !dwProcessID )
return TRUE;
theMachineAccount.Impersonate();
BOOL bSuccess = FALSE;
HANDLE hProcess = OpenProcess( PROCESS_TERMINATE, FALSE, dwProcessID );
DWORD dwLastError = GetLastError();
if( hProcess != NULL )
{
bSuccess = TerminateProcess( hProcess, 0 );
dwLastError = GetLastError();
CloseHandle(hProcess);
}
theMachineAccount.RevertToSelf();
SetLastError(dwLastError);
return bSuccess;
}
void CPserv2View::OnHeaderItemChanged(NMHDR* pNMHDR, LRESULT* pResult)
{
if( !m_bInsideAutoResize )
{
CListCtrl& theCtrl = GetListCtrl();
for( DWORD dwIndex = 0; dwIndex < m_dwNumberOfColumns; dwIndex++ )
{
CListViewColumn* c = (CListViewColumn*) m_pEntries->GetColumn(dwIndex);
c->m_dwWidth = theCtrl.GetColumnWidth(dwIndex);
}
}
*pResult = 0;
}
void CPserv2View::OnObjectStop()
{
OnObjectControl( &CService::BeginStop, &CService::IsStopped, _T("stop") );
}
void CPserv2View::OnObjectControl(BEGINSTATFN begin, QUERYSTATEFN query, LPCTSTR lpszAction)
{
if( GetDocument()->m_DocType == DocType_Services )
{
CUIntArray indices;
int nMaxIndex = GetSelectedIndices(indices);
for( int i=0; i < nMaxIndex; i++)
{
m_nCurrentIndex = indices.GetAt(i);
CService* instance = (CService*) m_pServices->GetAt(m_nCurrentIndex);
if( instance )
{
DWORD dwWaitHint = 10 * 1000;
SERVICE_STATE_TYPE sst = (instance->*begin)(&dwWaitHint);
if( sst == SERVICE_STATE_FAILED )
{
DisplayErrorMessage(_T("Unable to %s '%s'"), lpszAction, (LPCTSTR)instance->m_strDisplayName );
}
else
{
CServiceControlDialog(instance, dwWaitHint, query, lpszAction, this).DoModal();
}
instance->FinishStateType();
UpdateCurrentService();
}
}
}
}
void CPserv2View::OnObjectStart()
{
OnObjectControl( &CService::BeginStart, &CService::IsStarted, _T("start") );
}
void CPserv2View::OnObjectRestart()
{
OnObjectControl( &CService::BeginRestart, &CService::IsRestarted, _T("restart") );
}
void CPserv2View::OnObjectPause()
{
OnObjectControl( &CService::BeginPause, &CService::IsPaused, _T("pause") );
}
void CPserv2View::OnObjectContinue()
{
OnObjectControl( &CService::BeginContinue, &CService::IsContinued, _T("continue") );
}
void CPserv2View::OnUpdateObjectStop(CCmdUI* pCmdUI)
{
if( GetDocument()->m_DocType == DocType_Services )
{
CService* instance = GetCurrentService();
pCmdUI->Enable( instance && instance->IsStarted() == SERVICE_STATE_REACHED );
}
else pCmdUI->Enable( FALSE );
}
void CPserv2View::OnUpdateObjectStart(CCmdUI* pCmdUI)
{
if( GetDocument()->m_DocType == DocType_Services )
{
CService* instance = GetCurrentService();
pCmdUI->Enable( instance && instance->IsStopped() == SERVICE_STATE_REACHED );
}
else pCmdUI->Enable( FALSE );
}
void CPserv2View::OnUpdateObjectRestart(CCmdUI* pCmdUI)
{
if( GetDocument()->m_DocType == DocType_Services )
{
CService* instance = GetCurrentService();
pCmdUI->Enable( instance && instance->IsStarted() == SERVICE_STATE_REACHED );
}
else pCmdUI->Enable( FALSE );
}
void CPserv2View::OnUpdateObjectPause(CCmdUI* pCmdUI)
{
if( GetDocument()->m_DocType == DocType_Services )
{
CService* instance = GetCurrentService();
pCmdUI->Enable( instance && instance->IsStarted() == SERVICE_STATE_REACHED );
}
else pCmdUI->Enable( FALSE );
}
void CPserv2View::OnUpdateObjectContinue(CCmdUI* pCmdUI)
{
if( GetDocument()->m_DocType == DocType_Services )
{
CService* instance = GetCurrentService();
pCmdUI->Enable( instance && instance->IsPaused() == SERVICE_STATE_REACHED );
}
else pCmdUI->Enable( FALSE );
}
void CPserv2View::OnObjectStartOrContinue()
{
CService* instance = GetCurrentService();
if( instance->IsPaused() == SERVICE_STATE_REACHED )
{
OnObjectControl( &CService::BeginContinue, &CService::IsContinued, _T("continue") );
}
else
{
OnObjectControl( &CService::BeginStart, &CService::IsStarted, _T("start") );
}
}
void CPserv2View::OnUpdateObjectStartOrContinue(CCmdUI* pCmdUI)
{
if( GetDocument()->m_DocType == DocType_Services )
{
CService* instance = GetCurrentService();
pCmdUI->Enable( instance && (instance->IsPaused() == SERVICE_STATE_REACHED ||
instance->IsStopped() == SERVICE_STATE_REACHED) );
}
else pCmdUI->Enable( FALSE );
}
void CPserv2View::OnConnectToLocalMachine()
{
CListViewEntries* sl = GetDocument()->m_pEntries;
sl->ConnectTo(NULL);
sl->Refresh();
DisplayAllItems();
}
void CPserv2View::OnConnectToRemoteMachine()
{
CRemoteConnectDialog box;
if( box.DoModal() == IDOK )
{
BeginWaitCursor();
CListViewEntries* sl = GetDocument()->m_pEntries;
CString strOldMachine( sl->m_strServiceMachine );
CString strNewMachine(theConfiguration.m_ComputerNames.GetAt(0));
sl->ConnectTo(strNewMachine);
if( !sl->Refresh() )
{
sl->ConnectTo(strOldMachine);
if( !sl->Refresh() )
{
sl->ConnectTo(strOldMachine);
sl->Refresh();
}
}
DisplayAllItems();
EndWaitCursor();
}
}
void CPserv2View::OnUpdateFilePrintPreview(CCmdUI* pCmdUI)
{
pCmdUI->Enable( GetDocument()->m_DocType == DocType_Services );
}
void CPserv2View::OnUpdateFilePrint(CCmdUI* pCmdUI)
{
pCmdUI->Enable( GetDocument()->m_DocType == DocType_Services );
}
void CPserv2View::OnUpdateObjectInstall(CCmdUI* pCmdUI)
{
pCmdUI->Enable( GetDocument()->m_DocType == DocType_Services );
}
void CPserv2View::OnUpdateTemplatesApply(CCmdUI* pCmdUI)
{
pCmdUI->Enable( GetDocument()->m_DocType == DocType_Services );
}
void CPserv2View::OnOptionsAutoresizeallcolumns()
{
SetRedraw(FALSE);
CListCtrl& theCtrl = GetListCtrl();
m_bInsideAutoResize = TRUE;
for( DWORD i = 0; i < m_dwNumberOfColumns; i++ )
{
CListViewColumn* c = (CListViewColumn*) m_pEntries->GetColumn(i);
c->m_dwWidth = LVSCW_AUTOSIZE;
theCtrl.SetColumnWidth( i, LVSCW_AUTOSIZE );
}
SetRedraw(TRUE);
m_bInsideAutoResize = FALSE;
}
void CPserv2View::OnViewChoosecolumns()
{
CChooseColumnsDialog box(m_pEntries, this);
if( box.DoModal() )
{
m_pEntries = NULL;
OnViewRefresh();
}
}