Split startup options for service and applet. Toggle service state between "manual" and "auto" rather than "disabled" and "auto".

This commit is contained in:
Ron Pedde 2007-03-15 12:30:12 +00:00
parent aa2a9c2c43
commit cf8b867165
7 changed files with 224 additions and 288 deletions

View File

@ -29,22 +29,16 @@ LRESULT CAdvancedPage::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*l
DoDataExchange(false);
m_port_spin.SetRange32(1, 65535);
switch (GetApplication()->IsAutoStartEnabled())
{
case 0:
m_autostart_check.SetCheck(BST_UNCHECKED);
break;
case 1:
if(GetApplication()->IsServiceAutoStartEnabled())
m_autostart_check.SetCheck(BST_CHECKED);
break;
case 2:
// Be sneaky here. Make it capable of showing indeterminate but
// don't make it automatically change on click. We'll revert
// to a normal checkbox when the click happens.
m_autostart_check.SetButtonStyle(BS_3STATE);
m_autostart_check.SetCheck(BST_INDETERMINATE);
break;
}
else
m_autostart_check.SetCheck(BST_UNCHECKED);
if(GetApplication()->IsAppletAutoStartEnabled())
m_autostart_icon_check.SetCheck(BST_CHECKED);
else
m_autostart_icon_check.SetCheck(BST_UNCHECKED);
UpdateControls();
GetDlgItem(IDC_STARTSERVICE).SendMessage(BCM_SETSHIELD,0,TRUE);
@ -54,13 +48,11 @@ LRESULT CAdvancedPage::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*l
return 0;
}
void CAdvancedPage::OnDestroy()
{
void CAdvancedPage::OnDestroy() {
GetApplication()->ServiceStatusUnsubscribe(this);
}
void CAdvancedPage::UpdateControls()
{
void CAdvancedPage::UpdateControls() {
Service::Status status = GetApplication()->GetServiceStatus();
UpdateControls(status);
}
@ -123,18 +115,8 @@ int CAdvancedPage::OnApply()
IniFile ini(GetApplication()->GetConfigPath());
ini.SetInteger(_T("general"), _T("port"), m_server_port);
switch (m_autostart_check.GetCheck())
{
case BST_CHECKED:
GetApplication()->EnableAutoStart(m_hWnd, true);
break;
case BST_UNCHECKED:
GetApplication()->EnableAutoStart(m_hWnd, false);
break;
case BST_INDETERMINATE:
// Ignore
break;
}
GetApplication()->EnableServiceAutoStart(m_hWnd,m_autostart_check.GetCheck() == BST_CHECKED);
GetApplication()->EnableAppletAutoStart(m_hWnd,m_autostart_icon_check.GetCheck() == BST_CHECKED);
// Incorrectly documented in WTL
return true;

View File

@ -45,6 +45,7 @@ private:
unsigned int m_server_port;
CUpDownCtrl m_port_spin;
CButton m_autostart_check;
CButton m_autostart_icon_check;
void UpdateControls(Service::Status status);
void UpdateControls();
@ -55,7 +56,6 @@ private:
BEGIN_MSG_MAP(CAdvancedPage)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
COMMAND_HANDLER_EX(IDC_SERVERPORT, EN_CHANGE, OnChange)
COMMAND_HANDLER_EX(IDC_AUTOSTART, BN_CLICKED, OnClickAutostart)
COMMAND_ID_HANDLER(IDC_STARTSERVICE, OnStartService)
COMMAND_ID_HANDLER(IDC_STOPSERVICE, OnStopService)
COMMAND_ID_HANDLER(IDC_WEBADMIN, OnWebAdmin)
@ -67,6 +67,7 @@ private:
DDX_UINT(IDC_SERVERPORT, m_server_port);
DDX_CONTROL_HANDLE(IDC_PORTSPIN, m_port_spin);
DDX_CONTROL_HANDLE(IDC_AUTOSTART, m_autostart_check);
DDX_CONTROL_HANDLE(IDC_AUTOSTART_ICON, m_autostart_icon_check);
END_DDX_MAP()
// MessageHandlers;
@ -77,22 +78,10 @@ private:
void OnDestroy();
void OnTimer(UINT id, TIMERPROC proc);
int OnApply();
void OnChange(UINT uCode, int nCtrlID, HWND hwndCtrl)
{
void OnChange(UINT uCode, int nCtrlID, HWND hwndCtrl) {
// Lots of things could have changed.
SetModified();
}
void OnClickAutostart(UINT uCode, int nCtrlID, HWND hwndCtrl)
{
// When clicked revert to being a normal checkbox in case
// we were intermediate.
if (m_autostart_check.GetButtonStyle() != BS_AUTOCHECKBOX)
{
m_autostart_check.SetButtonStyle(BS_AUTOCHECKBOX);
m_autostart_check.SetCheck(TRUE);
}
OnChange(uCode, nCtrlID, hwndCtrl);
}
};
#endif // ADVANCEDPAGE_H

View File

@ -299,63 +299,45 @@ CString Application::MakeRunKeyValue()
return required_path;
}
void Application::EnableAutoStart(HWND hwnd, bool enable)
{
// First let's control the service.
int required_startup = enable ? SERVICE_AUTO_START : SERVICE_DISABLED;
void Application::EnableServiceAutoStart(HWND hwnd, bool enable) {
int required_startup = enable ? SERVICE_AUTO_START : SERVICE_DEMAND_START;
if (m_service.GetStartup() != required_startup)
{
if (!m_service.ConfigureStartup(required_startup))
{
if (m_service.GetStartup() != required_startup) {
if (!m_service.ConfigureStartup(required_startup)) {
MessageBox(hwnd, IDS_FAILED_CONFIGURE_SERVICE, MB_OK);
}
}
}
// Now let's set up the Run key.
void Application::EnableAppletAutoStart(HWND hwnd, bool enable) {
HKEY hkey;
LONG result = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, RUN_KEY, 0, KEY_SET_VALUE | STANDARD_RIGHTS_WRITE, &hkey);
if (result == ERROR_SUCCESS)
{
if (enable)
{
LONG result = ::RegOpenKeyEx(HKEY_CURRENT_USER, RUN_KEY, 0, KEY_SET_VALUE | STANDARD_RIGHTS_WRITE, &hkey);
if (result == ERROR_SUCCESS) {
if (enable) {
// We need to quote it because the path may contain spaces.
CString str = MakeRunKeyValue();
result = RegSetValueEx(hkey, RUN_VALUE, 0UL, REG_SZ, reinterpret_cast<LPCBYTE>(static_cast<LPCTSTR>(str)), (str.GetLength() + 1) * sizeof(TCHAR));
}
else
{
} else {
result = RegDeleteValue(hkey, RUN_VALUE);
if (result == ERROR_FILE_NOT_FOUND)
result = 0;
}
if (result != ERROR_SUCCESS)
{
if (result != ERROR_SUCCESS) {
ATLTRACE("Error:%u\n", result);
MessageBox(hwnd, IDS_FAILED_CONFIGURE_STARTUP, MB_OK);
}
}
}
int Application::IsAutoStartEnabled() const
{
// Look at the service
int service_result = 2;
switch (m_service.GetStartup())
{
case SERVICE_AUTO_START:
service_result = true;
break;
case SERVICE_DISABLED:
service_result = false;
break;
bool Application::IsServiceAutoStartEnabled() const {
return (m_service.GetStartup() == SERVICE_AUTO_START);
}
// Look at the Run key
int run_result = 2;
bool Application::IsAppletAutoStartEnabled() const {
bool run_result = false;
HKEY hkey;
if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, RUN_KEY, 0, KEY_QUERY_VALUE | STANDARD_RIGHTS_READ, &hkey) == ERROR_SUCCESS)
if (::RegOpenKeyEx(HKEY_CURRENT_USER, RUN_KEY, 0, KEY_QUERY_VALUE | STANDARD_RIGHTS_READ, &hkey) == ERROR_SUCCESS)
{
DWORD dwType, cbData;
TCHAR buffer[_MAX_PATH + 1];
@ -370,7 +352,12 @@ int Application::IsAutoStartEnabled() const
else
{
// It's there - but it isn't us that it will start.
run_result = 2;
// But from the user's perspective, it *is* us, so to make the perception
// match the setting, we'll make this true, rather than some indeterminate
// thing which nobody understands. -- Ron
run_result = true;
}
}
else
@ -380,15 +367,9 @@ int Application::IsAutoStartEnabled() const
}
}
else
{
run_result = false;
}
// If the answers agree then return them. Otherwise we're indeterminate.
if (run_result == service_result)
return run_result;
else
return 2;
}
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)

View File

@ -137,10 +137,12 @@ public:
void EnableServerEvents(bool);
/// Enable/disable automatic startup of service and FireflyShell.
void EnableAutoStart(HWND, bool);
void EnableAppletAutoStart(HWND, bool);
void EnableServiceAutoStart(HWND, bool);
/// Reports 0 for disabled, 1 for enabled, 2 for indeterminate
int IsAutoStartEnabled() const;
/// Reports 0 for disabled, 1 for enabled
bool IsAppletAutoStartEnabled() const;
bool IsServiceAutoStartEnabled() const;
int MessageBox(HWND hwnd, UINT id, UINT flags)
{

View File

@ -68,21 +68,16 @@ STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "About"
FONT 8, "MS Sans Serif", 0, 0, 0x0
BEGIN
CTEXT "For help, tips and troubleshooting advice...",
IDC_FIREFLYLINK,12,92,276,9,NOT WS_GROUP
CTEXT "For help, tips and troubleshooting advice...",IDC_FIREFLYLINK,12,92,276,9,NOT WS_GROUP
GROUPBOX "Version information",IDC_STATIC,6,121,288,100
CONTROL "",IDC_VERSIONLIST,"SysListView32",LVS_REPORT |
LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP,12,132,276,68
CONTROL "",IDC_VERSIONLIST,"SysListView32",LVS_REPORT | LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP,12,132,276,68
PUSHBUTTON "&Copy to Clipboard",IDC_COPY,192,203,96,14
CONTROL 208,IDC_LOGO,"Static",SS_BITMAP | SS_CENTERIMAGE |
SS_SUNKEN | WS_BORDER,6,7,288,73
CTEXT "For information on Firefly compatible music players...",
IDC_ROKULINK,12,106,276,9,NOT WS_GROUP
CONTROL 208,IDC_LOGO,"Static",SS_BITMAP | SS_CENTERIMAGE | SS_SUNKEN | WS_BORDER,6,7,288,73
CTEXT "For information on Firefly compatible music players...",IDC_ROKULINK,12,106,276,9,NOT WS_GROUP
END
IDD_PAGE_BASIC DIALOGEX 0, 0, 300, 230
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION |
WS_SYSMENU
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Library"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
@ -92,51 +87,43 @@ BEGIN
EDITTEXT IDC_PATH,86,57,202,14,ES_AUTOHSCROLL
PUSHBUTTON "&Browse...",IDC_BROWSE,228,76,60,14
GROUPBOX "Security",IDC_STATIC,7,113,287,103
CONTROL "&Protect Firefly media library with a password",
IDC_PROTECT,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,12,135,
276,10
CONTROL "&Protect Firefly media library with a password",IDC_PROTECT,
"Button",BS_AUTOCHECKBOX | WS_TABSTOP,12,135,276,10
LTEXT "Pass&word",IDC_STATIC,12,156,74,8
EDITTEXT IDC_PASSWORD,86,154,114,14,ES_PASSWORD | ES_AUTOHSCROLL
LTEXT "You can limit access to your music library by assigning a password. If a password is assigned, only clients that provide the correct password will be able to play music from your library.",
IDC_STATIC,12,180,276,26,SS_NOPREFIX
LTEXT "You can limit access to your music library by assigning a password. If a password is assigned, only clients that provide the correct password will be able to play music from your library.",IDC_STATIC,12,180,276,26,SS_NOPREFIX
END
IDD_PAGE_LOG DIALOGEX 0, 0, 300, 220
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION |
WS_SYSMENU
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Log"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
EDITTEXT IDC_LOG,6,6,288,189,ES_MULTILINE | ES_AUTOHSCROLL |
ES_READONLY | WS_VSCROLL | WS_HSCROLL
EDITTEXT IDC_LOG,6,6,288,189,ES_MULTILINE | ES_AUTOHSCROLL | ES_READONLY | WS_VSCROLL | WS_HSCROLL
PUSHBUTTON "&Refresh",IDC_REFRESH,244,200,50,14
END
IDD_PAGE_ADVANCED DIALOGEX 0, 0, 300, 220
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION |
WS_SYSMENU
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Server"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
GROUPBOX "Server Status",IDC_STATIC,6,7,288,82
LTEXT "Current state of the server goes here",IDC_SERVERSTATE,
12,28,199,25
LTEXT "Current state of the server goes here",IDC_SERVERSTATE,12,28,199,25
PUSHBUTTON "&Stop Server",IDC_STOPSERVICE,218,28,70,14
PUSHBUTTON "&Start Server",IDC_STARTSERVICE,218,28,70,14
CONTROL "&Start Firefly when Windows starts",IDC_AUTOSTART,
"Button",BS_AUTOCHECKBOX | WS_TABSTOP,12,63,276,10
"Button",BS_AUTOCHECKBOX | WS_TABSTOP,12,55,276,10
CONTROL "&Enable Firefly icon in system tray",IDC_AUTOSTART_ICON,
"Button",BS_AUTOCHECKBOX | WS_TABSTOP,12,68,276,10
GROUPBOX "Advanced",IDC_STATIC,6,93,288,61
LTEXT "Server port number",IDC_STATIC,12,108,99,11
EDITTEXT IDC_SERVERPORT,105,105,42,14,ES_AUTOHSCROLL | ES_NUMBER
CONTROL "",IDC_PORTSPIN,"msctls_updown32",UDS_SETBUDDYINT |
UDS_ALIGNRIGHT | UDS_AUTOBUDDY | UDS_ARROWKEYS |
UDS_NOTHOUSANDS | UDS_HOTTRACK,146,105,11,14
LTEXT "You may need to change this if another program is already using this port.",
IDC_STATIC,12,129,276,23
CONTROL "",IDC_PORTSPIN,"msctls_updown32",UDS_SETBUDDYINT | UDS_ALIGNRIGHT | UDS_AUTOBUDDY | UDS_ARROWKEYS | UDS_NOTHOUSANDS | UDS_HOTTRACK,146,105,11,14
LTEXT "You may need to change this if another program is already using this port.",IDC_STATIC,12,129,276,23
GROUPBOX "Web administration",IDC_STATIC,6,159,288,54
LTEXT "Firefly Media Server also provides a web administration interface.",
IDC_STATIC,12,177,198,20
LTEXT "Firefly Media Server also provides a web administration interface.",IDC_STATIC,12,177,198,20
PUSHBUTTON "&Open",IDC_WEBADMIN,218,177,70,14
PUSHBUTTON "&Start Server",IDC_STARTSERVICE,218,28,70,14
END

View File

@ -65,17 +65,13 @@ bool Service::Open(const TCHAR *name)
DWORD dwDesiredAccess = USER_ACCESS;
m_sc_manager = ::OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, dwDesiredAccess);
if (!m_sc_manager)
{
m_can_control = false;
}
else
{
m_can_control = true;
}
if (m_sc_manager)
{
if (!m_sc_manager)
m_can_control = false;
else
m_can_control = true;
if (m_sc_manager) {
m_sc_service = ::OpenService(m_sc_manager, name, dwDesiredAccess);
if (m_sc_service) {
m_name = name;
@ -87,15 +83,12 @@ bool Service::Open(const TCHAR *name)
return false;
}
void Service::Close()
{
if (m_sc_service)
{
void Service::Close() {
if (m_sc_service) {
::CloseServiceHandle(m_sc_service);
m_sc_service = NULL;
}
if (m_sc_manager)
{
if (m_sc_manager) {
::CloseServiceHandle(m_sc_manager);
m_sc_manager = NULL;
}
@ -231,13 +224,13 @@ DWORD Service::GetStartup() const
return result;
}
bool Service::ConfigureStartup(DWORD startup)
{
if (::ChangeServiceConfig(m_sc_service, SERVICE_NO_CHANGE, startup, SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL))
return true;
else
bool Service::ConfigureStartup(DWORD startup) {
if(startup != GetStartup()) { // don't boost privs if we don't need to
if (!::ChangeServiceConfig(m_sc_service, SERVICE_NO_CHANGE, startup, SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL))
return false;
}
return true;
}
void ServiceStatusMonitor::Poll(Service *service)
{

View File

@ -46,6 +46,8 @@
#define IDC_AUTOSTART 1012
#define IDC_WEBADMIN 1013
#define IDC_WEBSITE 1014
#define IDC_AUTOSTART2 1014
#define IDC_AUTOSTART_ICON 1014
#define IDC_VERSIONLIST 1015
#define IDC_COPY 1016
#define IDC_REFRESH 1018