From 3ec93b6744328421a82b9fd39d711eeea0d9749a Mon Sep 17 00:00:00 2001 From: George Hazan Date: Thu, 13 Aug 2015 22:13:41 +0000 Subject: - 64-bit compatibility; - code cleaning git-svn-id: http://svn.miranda-ng.org/main/trunk@14950 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/AutoShutdown/src/shutdownsvc.cpp | 329 ++++++++++++++++--------------- 1 file changed, 167 insertions(+), 162 deletions(-) (limited to 'plugins/AutoShutdown/src/shutdownsvc.cpp') diff --git a/plugins/AutoShutdown/src/shutdownsvc.cpp b/plugins/AutoShutdown/src/shutdownsvc.cpp index 778fcd79e2..68f398d0f5 100644 --- a/plugins/AutoShutdown/src/shutdownsvc.cpp +++ b/plugins/AutoShutdown/src/shutdownsvc.cpp @@ -30,7 +30,8 @@ static HANDLE hEventOkToShutdown,hEventShutdown; /************************* Utils **************************************/ -TCHAR *desc[] = { +TCHAR *desc[] = +{ LPGENT("Miranda NG is going to be automatically closed in %u second(s)."), LPGENT("All Miranda NG protocols are going to be set to offline in %u second(s)."), LPGENT("You will be logged off automatically in %u second(s)."), @@ -42,87 +43,88 @@ TCHAR *desc[] = { LPGENT("The computer will automatically be shut down in %u second(s).") }; -static BOOL WinNT_SetPrivilege(TCHAR *pszPrivName,BOOL bEnable) +static BOOL WinNT_SetPrivilege(TCHAR *pszPrivName, BOOL bEnable) { - BOOL bReturn=FALSE; + BOOL bReturn = FALSE; HANDLE hToken; TOKEN_PRIVILEGES tkp; /* get a token for this process */ - if (OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken)) { - tkp.PrivilegeCount=1; /* one privilege is to set */ + if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) { + tkp.PrivilegeCount = 1; /* one privilege is to set */ /* get the LUID for the shutdown privilege */ - if (LookupPrivilegeValue(NULL,pszPrivName,&tkp.Privileges[0].Luid)) { - tkp.Privileges[0].Attributes=bEnable?SE_PRIVILEGE_ENABLED:0; + if (LookupPrivilegeValue(NULL, pszPrivName, &tkp.Privileges[0].Luid)) { + tkp.Privileges[0].Attributes = bEnable ? SE_PRIVILEGE_ENABLED : 0; /* get the shutdown privilege for this process */ - bReturn=AdjustTokenPrivileges(hToken,FALSE,&tkp,0,(PTOKEN_PRIVILEGES)NULL,0); + bReturn = AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0); } CloseHandle(hToken); } return bReturn; } -static void BroadcastEndSession(DWORD dwRecipients,LPARAM lParam) +static void BroadcastEndSession(DWORD dwRecipients, LPARAM lParam) { - BroadcastSystemMessage(BSF_FORCEIFHUNG,&dwRecipients,WM_ENDSESSION,TRUE,lParam); + BroadcastSystemMessage(BSF_FORCEIFHUNG, &dwRecipients, WM_ENDSESSION, TRUE, lParam); } /************************* Workers ************************************/ static BOOL IsShutdownTypeEnabled(BYTE shutdownType) { - BOOL bReturn=FALSE; - switch(shutdownType) { - case SDSDT_HIBERNATE: - case SDSDT_STANDBY: - bReturn = shutdownType == SDSDT_HIBERNATE ? IsPwrHibernateAllowed() : IsPwrSuspendAllowed() != 0; - /* test privilege */ - if (bReturn) { - bReturn=WinNT_SetPrivilege(SE_SHUTDOWN_NAME,TRUE); - if (bReturn) WinNT_SetPrivilege(SE_SHUTDOWN_NAME,FALSE); - } - break; - case SDSDT_LOGOFF: - { HKEY hKey; - DWORD dwSetting,dwSize; - /* NoLogOff is BINARY on Win9x/ME and DWORD on Win2000+ */ - bReturn=TRUE; - if (RegOpenKeyEx(HKEY_CURRENT_USER,_T("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"),0,KEY_QUERY_VALUE,&hKey)==ERROR_SUCCESS) { - dwSize=sizeof(dwSetting); - if (RegQueryValueEx(hKey, _T("NoLogOff"), 0, NULL, (LPBYTE)&dwSetting, &dwSize) == ERROR_SUCCESS) - if (dwSetting) bReturn=FALSE; - RegCloseKey(hKey); - } - } - break; - case SDSDT_LOCKWORKSTATION: - { - HKEY hKey; - DWORD dwSize,dwSetting; - /* DisableLockWorkstation is DWORD on Win2000+ */ - bReturn=TRUE; - if (RegOpenKeyEx(HKEY_CURRENT_USER,_T("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"),0,KEY_QUERY_VALUE,&hKey)==ERROR_SUCCESS) { - dwSize=sizeof(dwSetting); - if (!RegQueryValueEx(hKey, _T("DisableLockWorkstation"), 0, NULL, (LPBYTE)&dwSetting, &dwSize)) - if (dwSetting) - bReturn=FALSE; - RegCloseKey(hKey); - } + BOOL bReturn = FALSE; + switch (shutdownType) { + case SDSDT_HIBERNATE: + case SDSDT_STANDBY: + bReturn = shutdownType == SDSDT_HIBERNATE ? IsPwrHibernateAllowed() : IsPwrSuspendAllowed() != 0; + /* test privilege */ + if (bReturn) { + bReturn = WinNT_SetPrivilege(SE_SHUTDOWN_NAME, TRUE); + if (bReturn) WinNT_SetPrivilege(SE_SHUTDOWN_NAME, FALSE); + } + break; + case SDSDT_LOGOFF: + { + HKEY hKey; + DWORD dwSetting, dwSize; + /* NoLogOff is BINARY on Win9x/ME and DWORD on Win2000+ */ + bReturn = TRUE; + if (RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"), 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) { + dwSize = sizeof(dwSetting); + if (RegQueryValueEx(hKey, _T("NoLogOff"), 0, NULL, (LPBYTE)&dwSetting, &dwSize) == ERROR_SUCCESS) + if (dwSetting) bReturn = FALSE; + RegCloseKey(hKey); } - break; - case SDSDT_CLOSERASCONNECTIONS: - /* check if RAS installed/available */ + } + break; + case SDSDT_LOCKWORKSTATION: + { + HKEY hKey; + DWORD dwSize, dwSetting; + /* DisableLockWorkstation is DWORD on Win2000+ */ bReturn = TRUE; - break; - case SDSDT_SETMIRANDAOFFLINE: - case SDSDT_CLOSEMIRANDA: - bReturn=TRUE; /* always possible */ - break; - case SDSDT_REBOOT: - case SDSDT_SHUTDOWN: - /* test privileges */ - bReturn=WinNT_SetPrivilege(SE_SHUTDOWN_NAME,TRUE); - if (bReturn) - WinNT_SetPrivilege(SE_SHUTDOWN_NAME,FALSE); - break; + if (RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"), 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) { + dwSize = sizeof(dwSetting); + if (!RegQueryValueEx(hKey, _T("DisableLockWorkstation"), 0, NULL, (LPBYTE)&dwSetting, &dwSize)) + if (dwSetting) + bReturn = FALSE; + RegCloseKey(hKey); + } + } + break; + case SDSDT_CLOSERASCONNECTIONS: + /* check if RAS installed/available */ + bReturn = TRUE; + break; + case SDSDT_SETMIRANDAOFFLINE: + case SDSDT_CLOSEMIRANDA: + bReturn = TRUE; /* always possible */ + break; + case SDSDT_REBOOT: + case SDSDT_SHUTDOWN: + /* test privileges */ + bReturn = WinNT_SetPrivilege(SE_SHUTDOWN_NAME, TRUE); + if (bReturn) + WinNT_SetPrivilege(SE_SHUTDOWN_NAME, FALSE); + break; } return bReturn; } @@ -130,94 +132,94 @@ static BOOL IsShutdownTypeEnabled(BYTE shutdownType) static DWORD ShutdownNow(BYTE shutdownType) { DWORD dwErrCode = ERROR_SUCCESS; - switch(shutdownType) { + switch (shutdownType) { case SDSDT_CLOSEMIRANDA: if (!Miranda_Terminated()) { /* waiting for short until ready (but not too long...) */ - DWORD dwLastTickCount=GetTickCount(); - while(!CallService(MS_SYSTEM_OKTOEXIT,0,0)) { + DWORD dwLastTickCount = GetTickCount(); + while (!CallService(MS_SYSTEM_OKTOEXIT, 0, 0)) { /* infinite loop protection (max 5 sec) */ - if (GetTickCount()-dwLastTickCount>=5000) { /* wraparound works */ + if (GetTickCount() - dwLastTickCount >= 5000) { /* wraparound works */ OutputDebugStringA("Timeout (5 sec)\n"); /* tell others, all ascii */ break; } - SleepEx(1000,TRUE); + SleepEx(1000, TRUE); if (Miranda_Terminated()) break; /* someone else did it */ OutputDebugStringA("Not ready to exit. Waiting...\n"); /* tell others, all ascii */ } /* shutdown service must be called from main thread anyway */ if (!DestroyWindow(pcli->hwndContactList)) - dwErrCode=GetLastError(); + dwErrCode = GetLastError(); } break; case SDSDT_SETMIRANDAOFFLINE: /* set global status mode to offline (is remembered by Miranda on exit) */ - CallService(MS_CLIST_SETSTATUSMODE,(WPARAM)ID_STATUS_OFFLINE,0); + CallService(MS_CLIST_SETSTATUSMODE, (WPARAM)ID_STATUS_OFFLINE, 0); break; case SDSDT_STANDBY: case SDSDT_HIBERNATE: - WinNT_SetPrivilege(SE_SHUTDOWN_NAME,TRUE); + WinNT_SetPrivilege(SE_SHUTDOWN_NAME, TRUE); if (!SetSystemPowerState(shutdownType == SDSDT_STANDBY, TRUE)) dwErrCode = GetLastError(); - WinNT_SetPrivilege(SE_SHUTDOWN_NAME,FALSE); + WinNT_SetPrivilege(SE_SHUTDOWN_NAME, FALSE); break; case SDSDT_LOCKWORKSTATION: - if ( !IsWorkstationLocked()) + if (!IsWorkstationLocked()) dwErrCode = GetLastError(); break; case SDSDT_CLOSERASCONNECTIONS: ShutdownNow(SDSDT_SETMIRANDAOFFLINE); /* set Miranda offline */ /* hang up all ras connections */ - { + { DWORD dwRetries; RASCONNSTATUS rcs; - DWORD dw,dwLastTickCount; + DWORD dw, dwLastTickCount; - DWORD dwConnSize=sizeof(RASCONN); - DWORD dwConnItems=0; - RASCONN *paConn=(RASCONN*)mir_alloc(dwConnSize); - dwErrCode=ERROR_NOT_ENOUGH_MEMORY; + DWORD dwConnSize = sizeof(RASCONN); + DWORD dwConnItems = 0; + RASCONN *paConn = (RASCONN*)mir_alloc(dwConnSize); + dwErrCode = ERROR_NOT_ENOUGH_MEMORY; if (paConn != NULL) { - for(dwRetries=5; dwRetries != 0; dwRetries--) { /* prevent infinite loop (rare) */ + for (dwRetries = 5; dwRetries != 0; dwRetries--) { /* prevent infinite loop (rare) */ memset(paConn, 0, dwConnSize); paConn[0].dwSize = sizeof(RASCONN); dwErrCode = RasEnumConnections(paConn, &dwConnSize, &dwConnItems); if (dwErrCode != ERROR_BUFFER_TOO_SMALL) break; - RASCONN *paConnBuf=(RASCONN*)mir_realloc(paConn,dwConnSize); + RASCONN *paConnBuf = (RASCONN*)mir_realloc(paConn, dwConnSize); if (paConnBuf == NULL) { mir_free(paConn); paConn = NULL; dwErrCode = ERROR_NOT_ENOUGH_MEMORY; break; } - paConn=paConnBuf; + paConn = paConnBuf; } - if (dwErrCode==ERROR_SUCCESS || dwErrCode==ERROR_BUFFER_TOO_SMALL) { - for(dw=0;dw3000) + if (dwRetries - dwLastTickCount > 3000) break; /* wraparound works */ } } @@ -233,21 +235,21 @@ static DWORD ShutdownNow(BYTE shutdownType) case SDSDT_REBOOT: case SDSDT_SHUTDOWN: if (GetSystemMetrics(SM_SHUTTINGDOWN)) { /* Win2000+, 0 on error */ - dwErrCode=ERROR_SHUTDOWN_IN_PROGRESS; + dwErrCode = ERROR_SHUTDOWN_IN_PROGRESS; break; } /* WinNT4/2000/XP */ { - WinNT_SetPrivilege(SE_SHUTDOWN_NAME,TRUE); + WinNT_SetPrivilege(SE_SHUTDOWN_NAME, TRUE); /* does not send out WM_ENDSESSION messages, so we do it manually to * give the applications the chance to save their data */ - WinNT_SetPrivilege(SE_TCB_NAME,TRUE); /* for BSM_ALLDESKTOPS */ - BroadcastEndSession(BSM_APPLICATIONS|BSM_ALLDESKTOPS,ENDSESSION_CLOSEAPP); /* app should close itself */ - WinNT_SetPrivilege(SE_TCB_NAME,FALSE); + WinNT_SetPrivilege(SE_TCB_NAME, TRUE); /* for BSM_ALLDESKTOPS */ + BroadcastEndSession(BSM_APPLICATIONS | BSM_ALLDESKTOPS, ENDSESSION_CLOSEAPP); /* app should close itself */ + WinNT_SetPrivilege(SE_TCB_NAME, FALSE); - if (!InitiateSystemShutdownEx(NULL,TranslateT("AutoShutdown"),0,TRUE,shutdownType==SDSDT_REBOOT,SHTDN_REASON_MAJOR_OTHER|SHTDN_REASON_MINOR_OTHER|SHTDN_REASON_FLAG_PLANNED)) - dwErrCode=GetLastError(); + if (!InitiateSystemShutdownEx(NULL, TranslateT("AutoShutdown"), 0, TRUE, shutdownType == SDSDT_REBOOT, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER | SHTDN_REASON_FLAG_PLANNED)) + dwErrCode = GetLastError(); /* cleanly close Miranda */ if (!dwErrCode) ShutdownNow(SDSDT_CLOSEMIRANDA); @@ -257,20 +259,20 @@ static DWORD ShutdownNow(BYTE shutdownType) case SDSDT_LOGOFF: { UINT flags; - switch(shutdownType) { - case SDSDT_LOGOFF: flags=EWX_LOGOFF; break; - case SDSDT_REBOOT: flags=EWX_REBOOT; break; - default: flags=EWX_SHUTDOWN|EWX_POWEROFF; + switch (shutdownType) { + case SDSDT_LOGOFF: flags = EWX_LOGOFF; break; + case SDSDT_REBOOT: flags = EWX_REBOOT; break; + default: flags = EWX_SHUTDOWN | EWX_POWEROFF; } if (shutdownType == SDSDT_LOGOFF && !IsWorkstationLocked()) flags |= EWX_FORCEIFHUNG; /* only considered for WM_ENDSESSION messages */ else - flags|= EWX_FORCE; /* must be used when workstation locked */ + flags |= EWX_FORCE; /* must be used when workstation locked */ if (flags & EWX_FORCE) { /* EWX_FORCE does not send out WM_ENDSESSION messages, so we do it * manually to give the applications the chance to save their data */ - BroadcastEndSession(BSM_APPLICATIONS,(shutdownType==SDSDT_LOGOFF)?ENDSESSION_LOGOFF:0); + BroadcastEndSession(BSM_APPLICATIONS, (shutdownType == SDSDT_LOGOFF) ? ENDSESSION_LOGOFF : 0); /* Windows Me/98/95 (msdn): Because of the design of the shell, * calling ExitWindowsEx with EWX_FORCE fails to completely log off @@ -279,7 +281,7 @@ static DWORD ShutdownNow(BYTE shutdownType) * To log off the user forcibly, terminate the Explorer process before calling * ExitWindowsEx with EWX_LOGOFF and EWX_FORCE. */ } - if (!ExitWindowsEx(flags,SHTDN_REASON_MAJOR_OTHER|SHTDN_REASON_MINOR_OTHER|SHTDN_REASON_FLAG_PLANNED)) + if (!ExitWindowsEx(flags, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER | SHTDN_REASON_FLAG_PLANNED)) dwErrCode = GetLastError(); /* cleanly close Miranda */ if (!dwErrCode) @@ -294,69 +296,72 @@ static DWORD ShutdownNow(BYTE shutdownType) #define M_START_SHUTDOWN (WM_APP+111) #define M_UPDATE_COUNTDOWN (WM_APP+112) -static INT_PTR CALLBACK ShutdownDlgProc(HWND hwndDlg,UINT msg,WPARAM wParam,LPARAM lParam) +static INT_PTR CALLBACK ShutdownDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) { - BYTE shutdownType=(BYTE)GetWindowLongPtr(hwndDlg, DWLP_USER); - WORD countdown=(WORD)GetWindowLongPtr(GetDlgItem(hwndDlg, IDC_TEXT_HEADER), GWLP_USERDATA); + BYTE shutdownType = (BYTE)GetWindowLongPtr(hwndDlg, DWLP_USER); + WORD countdown = (WORD)GetWindowLongPtr(GetDlgItem(hwndDlg, IDC_TEXT_HEADER), GWLP_USERDATA); - switch(msg) { + switch (msg) { case WM_INITDIALOG: - hwndShutdownDlg=hwndDlg; + hwndShutdownDlg = hwndDlg; SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)lParam); TranslateDialogDefault(hwndDlg); - if (lParam==SDSDT_SHUTDOWN || lParam==SDSDT_REBOOT || lParam==SDSDT_LOGOFF) - ShowWindow(GetDlgItem(hwndDlg,IDC_TEXT_UNSAVEDWARNING),SW_SHOW); - SendDlgItemMessage(hwndDlg,IDC_ICON_HEADER,STM_SETIMAGE,IMAGE_ICON,(LPARAM)IcoLib_GetIcon("AutoShutdown_Header")); + if (lParam == SDSDT_SHUTDOWN || lParam == SDSDT_REBOOT || lParam == SDSDT_LOGOFF) + ShowWindow(GetDlgItem(hwndDlg, IDC_TEXT_UNSAVEDWARNING), SW_SHOW); + SendDlgItemMessage(hwndDlg, IDC_ICON_HEADER, STM_SETIMAGE, IMAGE_ICON, (LPARAM)IcoLib_GetIcon("AutoShutdown_Header")); { HFONT hBoldFont; LOGFONT lf; - if (GetObject((HFONT)SendDlgItemMessage(hwndDlg,IDC_TEXT_HEADER,WM_GETFONT,0,0),sizeof(lf),&lf)) { - lf.lfWeight=FW_BOLD; - hBoldFont=CreateFontIndirect(&lf); + if (GetObject((HFONT)SendDlgItemMessage(hwndDlg, IDC_TEXT_HEADER, WM_GETFONT, 0, 0), sizeof(lf), &lf)) { + lf.lfWeight = FW_BOLD; + hBoldFont = CreateFontIndirect(&lf); } - else hBoldFont=NULL; - SendDlgItemMessage(hwndDlg,IDC_TEXT_HEADER,WM_SETFONT,(WPARAM)hBoldFont,FALSE); + else hBoldFont = NULL; + SendDlgItemMessage(hwndDlg, IDC_TEXT_HEADER, WM_SETFONT, (WPARAM)hBoldFont, FALSE); SetWindowLongPtr(GetDlgItem(hwndDlg, IDC_TEXT_HEADER), GWLP_USERDATA, (LONG_PTR)hBoldFont); } { - WORD countdown=db_get_w(NULL,"AutoShutdown","ConfirmDlgCountdown",SETTING_CONFIRMDLGCOUNTDOWN_DEFAULT); - if (countdown<3) countdown=SETTING_CONFIRMDLGCOUNTDOWN_DEFAULT; + countdown = db_get_w(NULL, "AutoShutdown", "ConfirmDlgCountdown", SETTING_CONFIRMDLGCOUNTDOWN_DEFAULT); + if (countdown < 3) + countdown = SETTING_CONFIRMDLGCOUNTDOWN_DEFAULT; SetWindowLongPtr(GetDlgItem(hwndDlg, IDC_TEXT_HEADER), GWLP_USERDATA, countdown); - SendMessage(hwndDlg,M_UPDATE_COUNTDOWN,0,countdown); + SendMessage(hwndDlg, M_UPDATE_COUNTDOWN, 0, countdown); } SkinPlaySound("AutoShutdown_Countdown"); - if (!SetTimer(hwndDlg,1,1000,NULL)) PostMessage(hwndDlg,M_START_SHUTDOWN,0,0); - Utils_RestoreWindowPositionNoSize(hwndDlg,NULL,"AutoShutdown","ConfirmDlg_"); + if (!SetTimer(hwndDlg, 1, 1000, NULL)) + PostMessage(hwndDlg, M_START_SHUTDOWN, 0, 0); + + Utils_RestoreWindowPositionNoSize(hwndDlg, NULL, "AutoShutdown", "ConfirmDlg_"); /* disallow foreground window changes (WinMe/2000+) */ SetForegroundWindow(hwndDlg); LockSetForegroundWindow(LSFW_LOCK); - SendMessage(hwndDlg,WM_NEXTDLGCTL,(WPARAM)GetDlgItem(hwndDlg,IDCANCEL),TRUE); + SendMessage(hwndDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hwndDlg, IDCANCEL), TRUE); return FALSE; /* focus set on cancel */ case WM_DESTROY: { - hwndShutdownDlg=NULL; - ShowWindow(hwndDlg,SW_HIDE); + hwndShutdownDlg = NULL; + ShowWindow(hwndDlg, SW_HIDE); /* reallow foreground window changes (WinMe/2000+) */ LockSetForegroundWindow(LSFW_UNLOCK); - Utils_SaveWindowPosition(hwndDlg,NULL,"AutoShutdown","ConfirmDlg_"); - HFONT hFont=(HFONT)SendDlgItemMessage(hwndDlg,IDC_TEXT_HEADER,WM_GETFONT,0,0); - SendDlgItemMessage(hwndDlg,IDC_TEXT_HEADER,WM_SETFONT,0,FALSE); /* no return value */ + Utils_SaveWindowPosition(hwndDlg, NULL, "AutoShutdown", "ConfirmDlg_"); + HFONT hFont = (HFONT)SendDlgItemMessage(hwndDlg, IDC_TEXT_HEADER, WM_GETFONT, 0, 0); + SendDlgItemMessage(hwndDlg, IDC_TEXT_HEADER, WM_SETFONT, 0, FALSE); /* no return value */ if (hFont != NULL) DeleteObject(hFont); } return TRUE; case M_START_SHUTDOWN: - if (IsWindowEnabled(GetDlgItem(hwndDlg,IDC_BUTTON_SHUTDOWNNOW))) { - EnableWindow(GetDlgItem(hwndDlg,IDC_BUTTON_SHUTDOWNNOW),FALSE); - ShowWindow(hwndDlg,SW_HIDE); /* get rid of the dialog immediately */ - DWORD dwErrCode=ShutdownNow(shutdownType); + if (IsWindowEnabled(GetDlgItem(hwndDlg, IDC_BUTTON_SHUTDOWNNOW))) { + EnableWindow(GetDlgItem(hwndDlg, IDC_BUTTON_SHUTDOWNNOW), FALSE); + ShowWindow(hwndDlg, SW_HIDE); /* get rid of the dialog immediately */ + DWORD dwErrCode = ShutdownNow(shutdownType); if (dwErrCode != ERROR_SUCCESS) { - char *pszErr=GetWinErrorDescription(dwErrCode); - ShowInfoMessage(NIIF_ERROR,Translate("Automatic Shutdown Error"),Translate("The shutdown process failed!\nReason: %s"),(pszErr != NULL)?pszErr:Translate("Unknown")); + char *pszErr = GetWinErrorDescription(dwErrCode); + ShowInfoMessage(NIIF_ERROR, Translate("Automatic Shutdown Error"), Translate("The shutdown process failed!\nReason: %s"), (pszErr != NULL) ? pszErr : Translate("Unknown")); if (pszErr != NULL) LocalFree(pszErr); } DestroyWindow(hwndDlg); @@ -367,37 +372,37 @@ static INT_PTR CALLBACK ShutdownDlgProc(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR if (countdown) { --countdown; SetWindowLongPtr(GetDlgItem(hwndDlg, IDC_TEXT_HEADER), GWLP_USERDATA, countdown); - if (countdown==27 || countdown==24 || countdown==21 || countdown==19 || - countdown==17 || countdown==15 || countdown==13 || countdown==11 || - countdown<=10) + if (countdown == 27 || countdown == 24 || countdown == 21 || countdown == 19 || + countdown == 17 || countdown == 15 || countdown == 13 || countdown == 11 || + countdown <= 10) SkinPlaySound("AutoShutdown_Countdown"); } - else KillTimer(hwndDlg,wParam); /* countdown finished */ - PostMessage(hwndDlg,M_UPDATE_COUNTDOWN,0,countdown); + else KillTimer(hwndDlg, wParam); /* countdown finished */ + PostMessage(hwndDlg, M_UPDATE_COUNTDOWN, 0, countdown); return TRUE; case M_UPDATE_COUNTDOWN: /* lParam=(WORD)countdown */ { TCHAR szText[256]; - mir_sntprintf(szText,_countof(szText),TranslateTS(desc[shutdownType-1]),lParam); - SetDlgItemText(hwndDlg,IDC_TEXT_HEADER,szText); + mir_sntprintf(szText, _countof(szText), TranslateTS(desc[shutdownType - 1]), lParam); + SetDlgItemText(hwndDlg, IDC_TEXT_HEADER, szText); /* countdown finished */ if (!lParam) - PostMessage(hwndDlg,M_START_SHUTDOWN,0,0); + PostMessage(hwndDlg, M_START_SHUTDOWN, 0, 0); } return TRUE; case WM_COMMAND: - switch(LOWORD(wParam)) { + switch (LOWORD(wParam)) { case IDC_BUTTON_SHUTDOWNNOW: - KillTimer(hwndDlg,1); + KillTimer(hwndDlg, 1); SetWindowLongPtr(GetDlgItem(hwndDlg, IDC_TEXT_HEADER), GWLP_USERDATA, 0); - SendMessage(hwndDlg,M_UPDATE_COUNTDOWN,0,(LONG)0); - PostMessage(hwndDlg,M_START_SHUTDOWN,0,0); + SendMessage(hwndDlg, M_UPDATE_COUNTDOWN, 0, (LONG)0); + PostMessage(hwndDlg, M_START_SHUTDOWN, 0, 0); return TRUE; case IDCANCEL: /* WM_CLOSE */ if (countdown) { - KillTimer(hwndDlg,1); + KillTimer(hwndDlg, 1); DestroyWindow(hwndDlg); } return TRUE; @@ -409,22 +414,22 @@ static INT_PTR CALLBACK ShutdownDlgProc(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR /************************* Services ***********************************/ -INT_PTR ServiceShutdown(WPARAM wParam,LPARAM lParam) +INT_PTR ServiceShutdown(WPARAM wParam, LPARAM lParam) { /* passing 0 as wParam is only to be used internally, undocumented */ - if (!wParam) wParam=db_get_b(NULL,"AutoShutdown","ShutdownType",SETTING_SHUTDOWNTYPE_DEFAULT); + if (!wParam) wParam = db_get_b(NULL, "AutoShutdown", "ShutdownType", SETTING_SHUTDOWNTYPE_DEFAULT); if (!IsShutdownTypeEnabled((BYTE)wParam)) return 1; /* does shutdownType range check */ if ((BOOL)lParam && hwndShutdownDlg != NULL) return 2; /* ask others if allowed */ - if (NotifyEventHooks(hEventOkToShutdown,wParam,lParam)) { + if (NotifyEventHooks(hEventOkToShutdown, wParam, lParam)) { OutputDebugStringA("automatic shutdown denied by event hook\n"); /* all ascii */ return 3; } /* tell others */ - NotifyEventHooks(hEventShutdown,wParam,lParam); + NotifyEventHooks(hEventShutdown, wParam, lParam); /* show dialog */ - if (lParam && db_get_b(NULL,"AutoShutdown","ShowConfirmDlg",SETTING_SHOWCONFIRMDLG_DEFAULT)) + if (lParam && db_get_b(NULL, "AutoShutdown", "ShowConfirmDlg", SETTING_SHOWCONFIRMDLG_DEFAULT)) if (CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_SHUTDOWNNOW), NULL, ShutdownDlgProc, (BYTE)wParam) != NULL) return 0; /* show error */ @@ -432,7 +437,7 @@ INT_PTR ServiceShutdown(WPARAM wParam,LPARAM lParam) DWORD dwErrCode = ShutdownNow((BYTE)wParam); if (dwErrCode != ERROR_SUCCESS) { char *pszErr = GetWinErrorDescription(dwErrCode); - ShowInfoMessage(NIIF_ERROR,Translate("Automatic Shutdown Error"),Translate("Inititiating the shutdown process failed!\nReason: %s"),(pszErr != NULL)?pszErr:Translate("Unknown")); + ShowInfoMessage(NIIF_ERROR, Translate("Automatic Shutdown Error"), Translate("Inititiating the shutdown process failed!\nReason: %s"), (pszErr != NULL) ? pszErr : Translate("Unknown")); if (pszErr != NULL) LocalFree(pszErr); return 4; @@ -441,7 +446,7 @@ INT_PTR ServiceShutdown(WPARAM wParam,LPARAM lParam) return 0; } -INT_PTR ServiceIsTypeEnabled(WPARAM wParam,LPARAM) +INT_PTR ServiceIsTypeEnabled(WPARAM wParam, LPARAM) { return IsShutdownTypeEnabled((BYTE)wParam); /* does shutdownType range check */ } @@ -466,19 +471,19 @@ const TCHAR *apszLong[] = { LPGENT("Sets all protocols to offline and closes all RAS connections.") }; -INT_PTR ServiceGetTypeDescription(WPARAM wParam,LPARAM lParam) +INT_PTR ServiceGetTypeDescription(WPARAM wParam, LPARAM lParam) { /* shutdownType range check */ - if (!wParam || (BYTE)wParam>SDSDT_MAX) return 0; + if (!wParam || (BYTE)wParam > SDSDT_MAX) return 0; /* select description */ - TCHAR *pszDesc=(TCHAR*)((lParam&GSTDF_LONGDESC)?apszLong:apszShort)[wParam-1]; - if (!(lParam&GSTDF_UNTRANSLATED)) pszDesc=TranslateTS(pszDesc); + TCHAR *pszDesc = (TCHAR*)((lParam&GSTDF_LONGDESC) ? apszLong : apszShort)[wParam - 1]; + if (!(lParam&GSTDF_UNTRANSLATED)) pszDesc = TranslateTS(pszDesc); /* convert as needed */ if (!(lParam&GSTDF_UNICODE)) { static char szConvBuf[128]; - char *buf=u2a(pszDesc); - if (buf==NULL) return 0; - mir_strncpy(szConvBuf,buf,sizeof(szConvBuf)); + char *buf = u2a(pszDesc); + if (buf == NULL) return 0; + mir_strncpy(szConvBuf, buf, sizeof(szConvBuf)); mir_free(buf); return (INT_PTR)szConvBuf; } @@ -490,13 +495,13 @@ INT_PTR ServiceGetTypeDescription(WPARAM wParam,LPARAM lParam) void InitShutdownSvc(void) { /* Shutdown Dialog */ - hwndShutdownDlg=NULL; - SkinAddNewSoundExT("AutoShutdown_Countdown",LPGENT("Alerts"),LPGENT("Automatic Shutdown Countdown")); + hwndShutdownDlg = NULL; + SkinAddNewSoundExT("AutoShutdown_Countdown", LPGENT("Alerts"), LPGENT("Automatic Shutdown Countdown")); /* Events */ hEventOkToShutdown = CreateHookableEvent(ME_AUTOSHUTDOWN_OKTOSHUTDOWN); hEventShutdown = CreateHookableEvent(ME_AUTOSHUTDOWN_SHUTDOWN); - + /* Services */ CreateServiceFunction(MS_AUTOSHUTDOWN_SHUTDOWN, ServiceShutdown); CreateServiceFunction(MS_AUTOSHUTDOWN_ISTYPEENABLED, ServiceIsTypeEnabled); -- cgit v1.2.3