/*	cvsnt control panel
    Copyright (C) 2004-5 Tony Hoyle and March-Hare Software Ltd

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License version 2.1 as published by the Free Software Foundation.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
// serverPage.cpp : implementation file
//

#include "stdafx.h"
#include "cvsnt.h"
#include "serverPage.h"
#include "NewRootDialog.h"

#define ServiceName _T("CVSNT")
#define ServiceName2 _T("CVSLOCK")

/////////////////////////////////////////////////////////////////////////////
// CserverPage property page

IMPLEMENT_DYNCREATE(CserverPage, CTooltipPropertyPage)

CserverPage::CserverPage() : CTooltipPropertyPage(CserverPage::IDD)
//, m_szSshStatus(_T(""))
{
	m_szVersion = "CVSNT " CVSNT_PRODUCTVERSION_STRING;
	//{{AFX_DATA_INIT(CserverPage)
	m_szStatus = _T("");
	m_szLockStatus =_T("");
	 //}}AFX_DATA_INIT
	m_hService=m_hLockService=m_hSCManager=NULL;
}

CserverPage::~CserverPage()
{
}

void CserverPage::DoDataExchange(CDataExchange* pDX)
{
	CTooltipPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CserverPage)
	DDX_Control(pDX, IDC_START, m_btStart);
	DDX_Control(pDX, IDC_STOP, m_btStop);
	DDX_Text(pDX, IDC_VERSION, m_szVersion);
	DDX_Text(pDX, IDC_STATUS, m_szStatus);
	DDX_Text(pDX, IDC_STATUS2, m_szLockStatus);
	DDX_Control(pDX, IDC_START2, m_btLockStart);
	DDX_Control(pDX, IDC_STOP2, m_btLockStop);
	//}}AFX_DATA_MAP
	DDX_Control(pDX, IDC_LOGO, m_cbLogo);
}


BEGIN_MESSAGE_MAP(CserverPage, CTooltipPropertyPage)
	//{{AFX_MSG_MAP(CserverPage)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_START, OnStart)
	ON_BN_CLICKED(IDC_STOP, OnStop)
	ON_BN_CLICKED(IDC_START2, OnBnClickedStart2)
	ON_BN_CLICKED(IDC_STOP2, OnBnClickedStop2)
	//}}AFX_MSG_MAP
	ON_STN_CLICKED(IDC_LOGO, OnStnClickedLogo)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CserverPage message handlers

BOOL CserverPage::OnInitDialog() 
{
	CTooltipPropertyPage::OnInitDialog();
	CString tmp;

	m_hSCManager=OpenSCManager(NULL,NULL,g_bPrivileged?GENERIC_EXECUTE:GENERIC_READ);
	if(!m_hSCManager)
	{
		CString tmp;
		DWORD e=GetLastError();

		if(e==5)
		{
			tmp.Format(_T("Couldn't open service control manager - Permission Denied"));
			AfxMessageBox(tmp,MB_ICONSTOP);
			GetParent()->PostMessage(WM_CLOSE);
		}
		else
		{
			tmp.Format(_T("Couldn't open service control manager - error %d"),GetLastError());
			AfxMessageBox(tmp,MB_ICONSTOP);
			GetParent()->PostMessage(WM_CLOSE);
		}
	}
	
	if(g_bPrivileged)
	{
		m_hService=OpenService(m_hSCManager,ServiceName,SERVICE_QUERY_STATUS|SERVICE_START|SERVICE_STOP);
		m_hLockService=OpenService(m_hSCManager,ServiceName2,SERVICE_QUERY_STATUS|SERVICE_START|SERVICE_STOP);
	}
	else
	{
		m_hService=OpenService(m_hSCManager,ServiceName,SERVICE_QUERY_STATUS);
		m_hLockService=OpenService(m_hSCManager,ServiceName2,SERVICE_QUERY_STATUS);
	}

	UpdateStatus();
	SetTimer(0,1000,NULL);

	CBitmap *pBitmap = new CBitmap;
	pBitmap->LoadBitmap(IDB_BITMAP1);
	m_cbLogo.SetBitmap(*pBitmap);
	pBitmap->Detach();
	delete pBitmap;

	return TRUE; 
}

void CserverPage::UpdateStatus()
{
	SERVICE_STATUS stat = {0};

	if(!m_hService)
	{
		m_szStatus="Service not installed";
		m_btStart.EnableWindow(FALSE);
		m_btStop.EnableWindow(FALSE);
	}
	else
	{
		QueryServiceStatus(m_hService,&stat);
		switch(stat.dwCurrentState)
		{
			case SERVICE_STOPPED:
				m_szStatus="Stopped";
				m_btStart.EnableWindow(g_bPrivileged?TRUE:FALSE);
				m_btStop.EnableWindow(FALSE);
				break;
			case SERVICE_START_PENDING:
				m_szStatus="Starting";
				m_btStart.EnableWindow(FALSE);
				m_btStop.EnableWindow(FALSE);
				break;
			case SERVICE_STOP_PENDING:
				m_szStatus="Stopping";
				m_btStart.EnableWindow(FALSE);
				m_btStop.EnableWindow(FALSE);
				break;
			case SERVICE_RUNNING:
				m_szStatus="Running";
				m_btStart.EnableWindow(FALSE);
				m_btStop.EnableWindow(g_bPrivileged?TRUE:FALSE);
				break;
			default:
				m_szStatus="Unknown state";
				m_btStart.EnableWindow(FALSE);
				m_btStop.EnableWindow(FALSE);
				break;
		}
	}

	if(!m_hLockService)
	{
		m_szLockStatus="Service not installed";
		m_btLockStart.EnableWindow(FALSE);
		m_btLockStop.EnableWindow(FALSE);
	}
	else
	{
		QueryServiceStatus(m_hLockService,&stat);
		switch(stat.dwCurrentState)
		{
			case SERVICE_STOPPED:
				m_szLockStatus="Stopped";
				m_btLockStart.EnableWindow(g_bPrivileged?TRUE:FALSE);
				m_btLockStop.EnableWindow(FALSE);
				break;
			case SERVICE_START_PENDING:
				m_szLockStatus="Starting";
				m_btLockStart.EnableWindow(FALSE);
				m_btLockStop.EnableWindow(FALSE);
				break;
			case SERVICE_STOP_PENDING:
				m_szLockStatus="Stopping";
				m_btLockStart.EnableWindow(FALSE);
				m_btLockStop.EnableWindow(FALSE);
				break;
			case SERVICE_RUNNING:
				m_szLockStatus="Running";
				m_btLockStart.EnableWindow(FALSE);
				m_btLockStop.EnableWindow(g_bPrivileged?TRUE:FALSE);
				break;
			default:
				m_szLockStatus="Unknown state";
				m_btLockStart.EnableWindow(FALSE);
				m_btLockStop.EnableWindow(FALSE);
				break;
		}
	}

	UpdateData(FALSE);
}

void CserverPage::OnTimer(UINT nIDEvent) 
{
	UpdateStatus();
}

void CserverPage::OnStart() 
{
	m_btStart.EnableWindow(FALSE);
	if(!StartService(m_hService,0,NULL))
	{
		CString tmp;
		tmp.Format(_T("Couldn't start service: %s"),GetErrorString());
		AfxMessageBox(tmp,MB_ICONSTOP);
	}
	UpdateStatus();
}

void CserverPage::OnStop() 
{
	SERVICE_STATUS stat = {0};

	m_btStop.EnableWindow(FALSE);
	if(!ControlService(m_hService,SERVICE_CONTROL_STOP,&stat))
	{
		CString tmp;
		tmp.Format(_T("Couldn't stop service: %s"),GetErrorString());
		AfxMessageBox(tmp,MB_ICONSTOP);
	}
	UpdateStatus();
}

LPCTSTR CserverPage::GetErrorString()
{
	static TCHAR ErrBuf[1024];

	FormatMessage(
    FORMAT_MESSAGE_FROM_SYSTEM |
	FORMAT_MESSAGE_IGNORE_INSERTS,
    NULL,
    GetLastError(),
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
    (LPTSTR) ErrBuf,
    sizeof(ErrBuf),
    NULL );
	return ErrBuf;
};

void CserverPage::OnBnClickedStart2()
{
	m_btLockStart.EnableWindow(FALSE);
	if(!StartService(m_hLockService,0,NULL))
	{
		CString tmp;
		tmp.Format(_T("Couldn't start service: %s"),GetErrorString());
		AfxMessageBox(tmp,MB_ICONSTOP);
	}
	UpdateStatus();
}

void CserverPage::OnBnClickedStop2()
{
	SERVICE_STATUS stat = {0};

	m_btLockStop.EnableWindow(FALSE);
	if(!ControlService(m_hLockService,SERVICE_CONTROL_STOP,&stat))
	{
		CString tmp;
		tmp.Format(_T("Couldn't stop service: %s"),GetErrorString());
		AfxMessageBox(tmp,MB_ICONSTOP);
	}
	UpdateStatus();
}

void CserverPage::OnStnClickedLogo()
{
	ShellExecute(NULL,_T("open"),_T("http://www.march-hare.com/cvspro"),NULL,NULL,SW_SHOWNORMAL);
}


syntax highlighted by Code2HTML, v. 0.9.1