<commit_before>#include "stdafx.h"
#include "ShutdownDlg.h"
#include "Aboutdlg.h"
#include <thread>

#define WM_TRAY_ICON_MSG (WM_APP + 1)

void CShutdownDlg::DoDataExchange(CDataExchange* pDX)
{
	DDX_Control(pDX, IDC_EDIT_HOURS, m_stEditHours);
	DDX_Control(pDX, IDC_EDIT_MINUTES, m_stEditMinutes);
}

BEGIN_MESSAGE_MAP(CShutdownDlg, CDialog)
	ON_BN_CLICKED(IDC_SETUP_BUTTON, OnSetupButton)
	ON_BN_CLICKED(IDC_MIDNIGHT_BUTTON, OnMidnightButton)
	ON_WM_TIMER()
	ON_WM_DESTROY()
	ON_WM_SYSCOMMAND()
	ON_MESSAGE(WM_TRAY_ICON_MSG, OnSystrayIconMessage)
	ON_MESSAGE(WM_HOTKEY, OnHotKey)
	ON_COMMAND(IDC_SYSTRAYMENU_SHOW, OnSystrayMenuShow)
	ON_COMMAND(IDC_SYSTRAYMENU_EXIT, OnSystrayMenuExit)
	ON_COMMAND(IDC_SYSTRAYMENU_RESETTIMER, OnSystrayMenuResetTimer)
	ON_WM_CTLCOLOR()
	ON_WM_DRAWITEM()
	ON_WM_MEASUREITEM()
END_MESSAGE_MAP()

BOOL CShutdownDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	RegisterHotKey(m_hWnd, 1, MOD_CONTROL | MOD_ALT, 0x53); //ALT+CTRL+S - to shutdown immediately

	m_stSystrayIcon.cbSize = sizeof(NOTIFYICONDATA);
	m_stSystrayIcon.hWnd = m_hWnd;
	m_stSystrayIcon.uID = m_uTrayIconId;
	m_stSystrayIcon.uFlags = NIF_ICON | NIF_MESSAGE;
	m_stSystrayIcon.uCallbackMessage = WM_TRAY_ICON_MSG;
	m_stSystrayIcon.hIcon = m_hIcon;

	Shell_NotifyIcon(NIM_ADD, &m_stSystrayIcon);

	//For acquiring icon rect 
	m_stSystrayIconIdent.cbSize = sizeof(NOTIFYICONIDENTIFIER);
	m_stSystrayIconIdent.hWnd = m_hWnd;
	m_stSystrayIconIdent.uID = m_uTrayIconId;
	m_stSystrayIconIdent.guidItem = GUID_NULL;

	//Tooltip window
	m_hwndTooltip = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL,
		TTS_BALLOON | TTS_NOPREFIX | TTS_ALWAYSTIP,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
		NULL, NULL, NULL, NULL);

	_tcscpy_s(m_strTooltip, m_pTextNoTime);

	//Initializing tooltip
	m_stToolInfo.cbSize = TTTOOLINFO_V1_SIZE;
	m_stToolInfo.uFlags = TTF_TRACK;
	m_stToolInfo.uId = m_uTrayIconId;
	m_stToolInfo.lpszText = m_strTooltip;

	//Prepearing tooltip appearance
	::SendMessage(m_hwndTooltip, TTM_ADDTOOL, 0, (LPARAM)(LPTOOLINFO)&m_stToolInfo);
	::SendMessage(m_hwndTooltip, TTM_SETTIPBKCOLOR, (WPARAM)RGB(0, 0, 0), 0);
	::SendMessage(m_hwndTooltip, TTM_SETTIPTEXTCOLOR, (WPARAM)RGB(255, 255, 255), 0);
	::SendMessage(m_hwndTooltip, TTM_SETTITLE, (WPARAM)TTI_NONE, (LPARAM)L"Time to shutdown:");

	m_stSpinHours.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | UDS_ALIGNRIGHT | UDS_SETBUDDYINT | UDS_ARROWKEYS | UDS_WRAP,
		CRect(0, 0, 0, 0), this, IDC_SPIN_HOURS);
	m_stSpinHours.SetBuddy(&m_stEditHours);
	m_stSpinHours.SetRange(0, 23);
	m_stSpinHours.SetPos(1);

	m_stSpinMinutes.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | UDS_ALIGNRIGHT | UDS_SETBUDDYINT | UDS_ARROWKEYS | UDS_WRAP,
		CRect(0, 0, 0, 0), this, IDC_SPIN_MINUTES);
	m_stSpinMinutes.SetBuddy(&m_stEditMinutes);
	m_stSpinMinutes.SetRange(0, 59);
	m_stSpinMinutes.SetPos(0);

	UDACCEL _stAccel[2];
	_stAccel[0].nSec = 0; _stAccel[0].nInc = 1;
	_stAccel[1].nSec = 1; _stAccel[1].nInc = 8;
	m_stSpinMinutes.SetAccel(2, _stAccel);	//acceleration of minutes_spin

	CMenu* _pSysMenu = GetSystemMenu(FALSE);
	_pSysMenu->DeleteMenu(0, MF_BYPOSITION);
	_pSysMenu->DeleteMenu(1, MF_BYPOSITION);
	_pSysMenu->DeleteMenu(2, MF_BYPOSITION);
	_pSysMenu->AppendMenu(MF_SEPARATOR);
	_pSysMenu->AppendMenu(MF_ENABLED, IDC_MENU_ABOUT, L"About...");

	m_stMenuSystray.CreatePopupMenu();
	m_stMenuSystray.AppendMenu(MF_OWNERDRAW, IDC_SYSTRAYMENU_SHOW, L"Main window");
	m_stMenuSystray.AppendMenu(MF_OWNERDRAW | MF_DISABLED, IDC_SYSTRAYMENU_RESETTIMER, L"Reset timer");
	m_stMenuSystray.AppendMenu(MF_OWNERDRAW, IDC_SYSTRAYMENU_EXIT, L"Exit");

	m_hbrBlack = CreateSolidBrush(RGB(0, 0, 0));
	m_hbrWhite = CreateSolidBrush(RGB(255, 255, 255));

	SetIcon(m_hIcon, TRUE);
	SetIcon(m_hIcon, FALSE);

	return TRUE;
}

void CShutdownDlg::ShutdownPC()
{
	TOKEN_PRIVILEGES tkp;
	tkp.PrivilegeCount = 1;
	tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
	LookupPrivilegeValue(nullptr, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);

	HANDLE hToken;
	OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
	AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, nullptr, nullptr);
	InitiateSystemShutdownEx(nullptr, nullptr, NULL, TRUE, FALSE, SHTDN_REASON_FLAG_PLANNED);

	DestroyWindow();
}

void CShutdownDlg::OnSetupButton()
{
	m_unTimeTotal = (m_stSpinHours.GetPos() * 60) + m_stSpinMinutes.GetPos();
	m_stMenuSystray.EnableMenuItem(IDC_SYSTRAYMENU_RESETTIMER, MF_ENABLED);
	ShowWindow(SW_HIDE);
	SetTimer(m_uTimerShutdown, 60000, nullptr);

	OnTimer(m_uTimerShutdown);
}

void CShutdownDlg::OnMidnightButton()
{
	time_t t = time(0);
	tm now;
	localtime_s(&now, &t);

	//minutes left to midnight
	m_unTimeTotal = (24 - now.tm_hour) * 60 - now.tm_min;

	m_stMenuSystray.EnableMenuItem(IDC_SYSTRAYMENU_RESETTIMER, MF_ENABLED);
	ShowWindow(SW_HIDE);
	SetTimer(m_uTimerShutdown, 60000, nullptr);

	OnTimer(m_uTimerShutdown);
}

void CShutdownDlg::OnTimer(UINT nIDEvent)
{
	if (nIDEvent == m_uTimerShutdown)
	{
		UpdateTooltipText();

		switch (m_unTimeTotal) {//time in minutes to shutdown
		case 0:
			ShutdownPC();
			break;
		case 1:
		{
			m_unTimeTotal--;

			std::thread beep(Beep, 15000, 350); //calling Beep() in async state->
			beep.detach(); //->and detaching immidiately 

			INT_PTR dlg_rtrnvalue = m_stDlgLastmin.DoModal();

			if (dlg_rtrnvalue == IDCANCEL)<commit_msg>cmd -m added<commit_after>#include "stdafx.h"
#include "ShutdownDlg.h"
#include "Aboutdlg.h"
#include <thread>

#define WM_TRAY_ICON_MSG (WM_APP + 1)

void CShutdownDlg::DoDataExchange(CDataExchange* pDX)
{
	DDX_Control(pDX, IDC_EDIT_HOURS, m_stEditHours);
	DDX_Control(pDX, IDC_EDIT_MINUTES, m_stEditMinutes);
}

BEGIN_MESSAGE_MAP(CShutdownDlg, CDialog)
	ON_BN_CLICKED(IDC_SETUP_BUTTON, OnSetupButton)
	ON_BN_CLICKED(IDC_MIDNIGHT_BUTTON, OnMidnightButton)
	ON_WM_TIMER()
	ON_WM_DESTROY()
	ON_WM_SYSCOMMAND()
	ON_MESSAGE(WM_TRAY_ICON_MSG, OnSystrayIconMessage)
	ON_MESSAGE(WM_HOTKEY, OnHotKey)
	ON_COMMAND(IDC_SYSTRAYMENU_SHOW, OnSystrayMenuShow)
	ON_COMMAND(IDC_SYSTRAYMENU_EXIT, OnSystrayMenuExit)
	ON_COMMAND(IDC_SYSTRAYMENU_RESETTIMER, OnSystrayMenuResetTimer)
	ON_WM_CTLCOLOR()
	ON_WM_DRAWITEM()
	ON_WM_MEASUREITEM()
END_MESSAGE_MAP()

BOOL CShutdownDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	RegisterHotKey(m_hWnd, 1, MOD_CONTROL | MOD_ALT, 0x53); //ALT+CTRL+S - to shutdown immediately

	m_stSystrayIcon.cbSize = sizeof(NOTIFYICONDATA);
	m_stSystrayIcon.hWnd = m_hWnd;
	m_stSystrayIcon.uID = m_uTrayIconId;
	m_stSystrayIcon.uFlags = NIF_ICON | NIF_MESSAGE;
	m_stSystrayIcon.uCallbackMessage = WM_TRAY_ICON_MSG;
	m_stSystrayIcon.hIcon = m_hIcon;

	Shell_NotifyIcon(NIM_ADD, &m_stSystrayIcon);

	//For acquiring icon rect 
	m_stSystrayIconIdent.cbSize = sizeof(NOTIFYICONIDENTIFIER);
	m_stSystrayIconIdent.hWnd = m_hWnd;
	m_stSystrayIconIdent.uID = m_uTrayIconId;
	m_stSystrayIconIdent.guidItem = GUID_NULL;

	//Tooltip window
	m_hwndTooltip = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL,
		TTS_BALLOON | TTS_NOPREFIX | TTS_ALWAYSTIP,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
		NULL, NULL, NULL, NULL);

	_tcscpy_s(m_strTooltip, m_pTextNoTime);

	//Initializing tooltip
	m_stToolInfo.cbSize = TTTOOLINFO_V1_SIZE;
	m_stToolInfo.uFlags = TTF_TRACK;
	m_stToolInfo.uId = m_uTrayIconId;
	m_stToolInfo.lpszText = m_strTooltip;

	//Prepearing tooltip appearance
	::SendMessage(m_hwndTooltip, TTM_ADDTOOL, 0, (LPARAM)(LPTOOLINFO)&m_stToolInfo);
	::SendMessage(m_hwndTooltip, TTM_SETTIPBKCOLOR, (WPARAM)RGB(0, 0, 0), 0);
	::SendMessage(m_hwndTooltip, TTM_SETTIPTEXTCOLOR, (WPARAM)RGB(255, 255, 255), 0);
	::SendMessage(m_hwndTooltip, TTM_SETTITLE, (WPARAM)TTI_NONE, (LPARAM)L"Time to shutdown:");

	m_stSpinHours.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | UDS_ALIGNRIGHT | UDS_SETBUDDYINT | UDS_ARROWKEYS | UDS_WRAP,
		CRect(0, 0, 0, 0), this, IDC_SPIN_HOURS);
	m_stSpinHours.SetBuddy(&m_stEditHours);
	m_stSpinHours.SetRange(0, 23);
	m_stSpinHours.SetPos(1);

	m_stSpinMinutes.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | UDS_ALIGNRIGHT | UDS_SETBUDDYINT | UDS_ARROWKEYS | UDS_WRAP,
		CRect(0, 0, 0, 0), this, IDC_SPIN_MINUTES);
	m_stSpinMinutes.SetBuddy(&m_stEditMinutes);
	m_stSpinMinutes.SetRange(0, 59);
	m_stSpinMinutes.SetPos(0);

	UDACCEL _stAccel[2];
	_stAccel[0].nSec = 0; _stAccel[0].nInc = 1;
	_stAccel[1].nSec = 1; _stAccel[1].nInc = 8;
	m_stSpinMinutes.SetAccel(2, _stAccel);	//acceleration of minutes_spin

	CMenu* _pSysMenu = GetSystemMenu(FALSE);
	_pSysMenu->DeleteMenu(0, MF_BYPOSITION);
	_pSysMenu->DeleteMenu(1, MF_BYPOSITION);
	_pSysMenu->DeleteMenu(2, MF_BYPOSITION);
	_pSysMenu->AppendMenu(MF_SEPARATOR);
	_pSysMenu->AppendMenu(MF_ENABLED, IDC_MENU_ABOUT, L"About...");

	m_stMenuSystray.CreatePopupMenu();
	m_stMenuSystray.AppendMenu(MF_OWNERDRAW, IDC_SYSTRAYMENU_SHOW, L"Main window");
	m_stMenuSystray.AppendMenu(MF_OWNERDRAW | MF_DISABLED, IDC_SYSTRAYMENU_RESETTIMER, L"Reset timer");
	m_stMenuSystray.AppendMenu(MF_OWNERDRAW, IDC_SYSTRAYMENU_EXIT, L"Exit");

	m_hbrBlack = CreateSolidBrush(RGB(0, 0, 0));
	m_hbrWhite = CreateSolidBrush(RGB(255, 255, 255));

	SetIcon(m_hIcon, TRUE);
	SetIcon(m_hIcon, FALSE);

	// Parsing cmnLine params
	// If "-m" then going to midnight mode
	// and minimizing main window.
	if (wcsstr(AfxGetApp()->m_lpCmdLine, L"-m"))
	{
		OnMidnightButton();
		PostMessageW(WM_SYSCOMMAND, SC_MINIMIZE, 0);
	}

	return TRUE;
}

void CShutdownDlg::ShutdownPC()
{
	TOKEN_PRIVILEGES tkp;
	tkp.PrivilegeCount = 1;
	tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
	LookupPrivilegeValue(nullptr, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);

	HANDLE hToken;
	OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
	AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, nullptr, nullptr);
	InitiateSystemShutdownEx(nullptr, nullptr, NULL, TRUE, FALSE, SHTDN_REASON_FLAG_PLANNED);

	DestroyWindow();
}

void CShutdownDlg::OnSetupButton()
{
	m_unTimeTotal = (m_stSpinHours.GetPos() * 60) + m_stSpinMinutes.GetPos();
	m_stMenuSystray.EnableMenuItem(IDC_SYSTRAYMENU_RESETTIMER, MF_ENABLED);
	ShowWindow(SW_HIDE);
	SetTimer(m_uTimerShutdown, 60000, nullptr);

	OnTimer(m_uTimerShutdown);
}

void CShutdownDlg::OnMidnightButton()
{
	time_t t = time(0);
	tm now;
	localtime_s(&now, &t);

	//minutes left to midnight
	m_unTimeTotal = (24 - now.tm_hour) * 60 - now.tm_min;

	m_stMenuSystray.EnableMenuItem(IDC_SYSTRAYMENU_RESETTIMER, MF_ENABLED);

	SetTimer(m_uTimerShutdown, 60000, nullptr);
	OnTimer(m_uTimerShutdown);
	ShowWindow(SW_HIDE);
}

void CShutdownDlg::OnTimer(UINT nIDEvent)
{
	if (nIDEvent == m_uTimerShutdown)
	{
		UpdateTooltipText();

		switch (m_unTimeTotal) {//time in minutes to shutdown
		case 0:
			ShutdownPC();
			break;
		case 1:
		{
			m_unTimeTotal--;

			std::thread beep(Beep, 15000, 350); //calling Beep() in async state->
			beep.detach(); //->and detaching immidiately 

			INT_PTR dlg_rtrnvalue = m_stDlgLastmin.DoModal();

			if (dlg_rtrnvalue == IDCANCEL)
{'fraction_non_alphanumeric': 0.11583470469921375, 'fraction_numerical': 0.017096361309197294, 'mean_word_length': 7.049300956585725, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3793605', 'n_tokens_mistral': 5120, 'n_tokens_neox': 4855, 'n_words': 690}