2006-06-23 08:13:50 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS Timedate Control Panel
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
2011-11-29 14:55:58 +00:00
|
|
|
* FILE: dll/cpl/timedate/internettime.c
|
2006-06-23 08:13:50 +00:00
|
|
|
* PURPOSE: Internet Time property page
|
|
|
|
* COPYRIGHT: Copyright 2006 Ged Murphy <gedmurphy@gmail.com>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-01-24 23:00:42 +00:00
|
|
|
#include "timedate.h"
|
2006-06-23 08:13:50 +00:00
|
|
|
|
|
|
|
static VOID
|
|
|
|
CreateNTPServerList(HWND hwnd)
|
|
|
|
{
|
|
|
|
HWND hList;
|
2007-07-31 21:17:57 +00:00
|
|
|
WCHAR szValName[MAX_VALUE_NAME];
|
|
|
|
WCHAR szData[256];
|
2007-06-07 16:18:38 +00:00
|
|
|
DWORD dwIndex = 0;
|
|
|
|
DWORD dwValSize;
|
2006-06-23 08:13:50 +00:00
|
|
|
DWORD dwNameSize;
|
2007-06-07 16:18:38 +00:00
|
|
|
DWORD dwDefault = 1;
|
|
|
|
LONG lRet;
|
2006-06-23 08:13:50 +00:00
|
|
|
HKEY hKey;
|
|
|
|
|
|
|
|
hList = GetDlgItem(hwnd,
|
|
|
|
IDC_SERVERLIST);
|
|
|
|
|
2007-07-31 21:17:57 +00:00
|
|
|
lRet = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
|
|
|
|
L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers",
|
2006-06-23 08:13:50 +00:00
|
|
|
0,
|
|
|
|
KEY_QUERY_VALUE,
|
|
|
|
&hKey);
|
2007-06-07 16:18:38 +00:00
|
|
|
if (lRet != ERROR_SUCCESS)
|
2006-06-23 08:13:50 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
while (TRUE)
|
|
|
|
{
|
2007-07-31 21:17:57 +00:00
|
|
|
dwValSize = MAX_VALUE_NAME * sizeof(WCHAR);
|
|
|
|
szValName[0] = L'\0';
|
|
|
|
lRet = RegEnumValueW(hKey,
|
|
|
|
dwIndex,
|
|
|
|
szValName,
|
|
|
|
&dwValSize,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
(LPBYTE)szData,
|
|
|
|
&dwNameSize);
|
2007-06-07 16:18:38 +00:00
|
|
|
if (lRet == ERROR_SUCCESS)
|
|
|
|
{
|
2011-11-29 14:55:58 +00:00
|
|
|
/* Get date from default reg value */
|
2007-07-31 21:17:57 +00:00
|
|
|
if (wcscmp(szValName, L"") == 0) // if (Index == 0)
|
2006-06-23 08:13:50 +00:00
|
|
|
{
|
2007-07-31 21:17:57 +00:00
|
|
|
dwDefault = _wtoi(szData);
|
2007-06-07 16:18:38 +00:00
|
|
|
dwIndex++;
|
2006-06-23 08:13:50 +00:00
|
|
|
}
|
2007-06-07 16:18:38 +00:00
|
|
|
else
|
|
|
|
{
|
2007-07-31 21:17:57 +00:00
|
|
|
SendMessageW(hList,
|
|
|
|
CB_ADDSTRING,
|
|
|
|
0,
|
|
|
|
(LPARAM)szData);
|
2007-06-07 16:18:38 +00:00
|
|
|
dwIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (lRet != ERROR_MORE_DATA)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2006-06-23 08:13:50 +00:00
|
|
|
}
|
|
|
|
|
2007-06-07 16:18:38 +00:00
|
|
|
if (dwDefault < 1 || dwDefault > dwIndex)
|
|
|
|
dwDefault = 1;
|
2006-06-23 08:13:50 +00:00
|
|
|
|
2011-11-29 14:55:58 +00:00
|
|
|
/* Server reg entries count from 1,
|
|
|
|
* Combo boxes count from 0 */
|
2007-06-07 16:18:38 +00:00
|
|
|
dwDefault--;
|
2006-06-23 08:13:50 +00:00
|
|
|
|
2007-07-31 21:17:57 +00:00
|
|
|
SendMessageW(hList,
|
|
|
|
CB_SETCURSEL,
|
|
|
|
dwDefault,
|
|
|
|
0);
|
2006-06-23 08:13:50 +00:00
|
|
|
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Set the selected server in the registry */
|
|
|
|
static VOID
|
|
|
|
SetNTPServer(HWND hwnd)
|
|
|
|
{
|
|
|
|
HKEY hKey;
|
|
|
|
HWND hList;
|
2007-06-07 16:18:38 +00:00
|
|
|
UINT uSel;
|
2007-07-31 21:17:57 +00:00
|
|
|
WCHAR szSel[4];
|
2007-06-07 16:18:38 +00:00
|
|
|
LONG lRet;
|
2006-06-23 08:13:50 +00:00
|
|
|
|
|
|
|
hList = GetDlgItem(hwnd,
|
|
|
|
IDC_SERVERLIST);
|
|
|
|
|
2007-07-31 21:17:57 +00:00
|
|
|
uSel = (UINT)SendMessageW(hList, CB_GETCURSEL, 0, 0);
|
2006-06-23 08:13:50 +00:00
|
|
|
|
2011-11-29 14:55:58 +00:00
|
|
|
/* Server reg entries count from 1,
|
|
|
|
* Combo boxes count from 0 */
|
2007-06-07 16:18:38 +00:00
|
|
|
uSel++;
|
2006-06-23 08:13:50 +00:00
|
|
|
|
2011-11-29 14:55:58 +00:00
|
|
|
/* Convert to wide char */
|
2007-06-07 16:18:38 +00:00
|
|
|
_itow(uSel, szSel, 10);
|
2006-06-23 08:13:50 +00:00
|
|
|
|
2007-07-31 21:17:57 +00:00
|
|
|
lRet = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
|
|
|
|
L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers",
|
|
|
|
0,
|
|
|
|
KEY_SET_VALUE,
|
|
|
|
&hKey);
|
2007-06-07 16:18:38 +00:00
|
|
|
if (lRet != ERROR_SUCCESS)
|
2006-06-23 08:13:50 +00:00
|
|
|
{
|
2007-06-07 16:18:38 +00:00
|
|
|
DisplayWin32Error(lRet);
|
2006-06-23 08:13:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-06-07 16:18:38 +00:00
|
|
|
lRet = RegSetValueExW(hKey,
|
2007-07-31 21:17:57 +00:00
|
|
|
L"",
|
2007-06-07 16:18:38 +00:00
|
|
|
0,
|
|
|
|
REG_SZ,
|
|
|
|
(LPBYTE)szSel,
|
2007-07-31 21:17:57 +00:00
|
|
|
(wcslen(szSel) + 1) * sizeof(WCHAR));
|
2007-06-07 16:18:38 +00:00
|
|
|
if (lRet != ERROR_SUCCESS)
|
|
|
|
DisplayWin32Error(lRet);
|
2006-06-23 08:13:50 +00:00
|
|
|
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-29 14:55:58 +00:00
|
|
|
/* Get the domain name from the registry */
|
2006-06-23 08:13:50 +00:00
|
|
|
static BOOL
|
2007-07-31 21:17:57 +00:00
|
|
|
GetNTPServerAddress(LPWSTR *lpAddress)
|
2006-06-23 08:13:50 +00:00
|
|
|
{
|
|
|
|
HKEY hKey;
|
2007-07-31 21:17:57 +00:00
|
|
|
WCHAR szSel[4];
|
2006-06-23 08:13:50 +00:00
|
|
|
DWORD dwSize;
|
2007-06-07 16:18:38 +00:00
|
|
|
LONG lRet;
|
2006-06-23 08:13:50 +00:00
|
|
|
|
2007-07-31 21:17:57 +00:00
|
|
|
lRet = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
|
|
|
|
L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers",
|
|
|
|
0,
|
|
|
|
KEY_QUERY_VALUE,
|
|
|
|
&hKey);
|
2007-06-07 16:18:38 +00:00
|
|
|
if (lRet != ERROR_SUCCESS)
|
2006-06-23 08:13:50 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* Get data from default value */
|
2007-07-31 21:17:57 +00:00
|
|
|
dwSize = 4 * sizeof(WCHAR);
|
|
|
|
lRet = RegQueryValueExW(hKey,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
(LPBYTE)szSel,
|
|
|
|
&dwSize);
|
2007-06-07 16:18:38 +00:00
|
|
|
if (lRet != ERROR_SUCCESS)
|
2006-06-23 08:13:50 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
dwSize = 0;
|
2007-07-31 21:17:57 +00:00
|
|
|
lRet = RegQueryValueExW(hKey,
|
|
|
|
szSel,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&dwSize);
|
2007-06-07 16:18:38 +00:00
|
|
|
if (lRet != ERROR_SUCCESS)
|
|
|
|
goto fail;
|
2006-06-23 08:13:50 +00:00
|
|
|
|
2007-07-31 21:17:57 +00:00
|
|
|
(*lpAddress) = (LPWSTR)HeapAlloc(GetProcessHeap(),
|
2007-06-07 16:18:38 +00:00
|
|
|
0,
|
|
|
|
dwSize);
|
|
|
|
if ((*lpAddress) == NULL)
|
|
|
|
{
|
|
|
|
lRet = ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
goto fail;
|
2006-06-23 08:13:50 +00:00
|
|
|
}
|
2007-06-07 16:18:38 +00:00
|
|
|
|
2007-07-31 21:17:57 +00:00
|
|
|
lRet = RegQueryValueExW(hKey,
|
|
|
|
szSel,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
(LPBYTE)*lpAddress,
|
|
|
|
&dwSize);
|
2007-06-07 16:18:38 +00:00
|
|
|
if (lRet != ERROR_SUCCESS)
|
2006-06-23 08:13:50 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
fail:
|
2007-06-07 16:18:38 +00:00
|
|
|
DisplayWin32Error(lRet);
|
|
|
|
if (hKey)
|
|
|
|
RegCloseKey(hKey);
|
2006-07-09 16:19:39 +00:00
|
|
|
HeapFree(GetProcessHeap(), 0, *lpAddress);
|
2006-06-23 08:13:50 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-29 14:55:58 +00:00
|
|
|
/* Request the time from the current NTP server */
|
2006-06-23 08:13:50 +00:00
|
|
|
static ULONG
|
|
|
|
GetTimeFromServer(VOID)
|
|
|
|
{
|
2007-07-31 21:17:57 +00:00
|
|
|
LPWSTR lpAddress = NULL;
|
2006-06-23 08:13:50 +00:00
|
|
|
ULONG ulTime = 0;
|
|
|
|
|
2007-04-30 19:05:04 +00:00
|
|
|
if (GetNTPServerAddress(&lpAddress))
|
2006-06-23 08:13:50 +00:00
|
|
|
{
|
2007-04-30 19:05:04 +00:00
|
|
|
ulTime = GetServerTime(lpAddress);
|
2006-06-23 08:13:50 +00:00
|
|
|
|
2007-04-30 19:05:04 +00:00
|
|
|
HeapFree(GetProcessHeap(),
|
|
|
|
0,
|
|
|
|
lpAddress);
|
|
|
|
}
|
2006-06-23 08:13:50 +00:00
|
|
|
|
|
|
|
return ulTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NTP servers state the number of seconds passed since
|
|
|
|
* 1st Jan, 1900. The time returned from the server
|
|
|
|
* needs adding to that date to get the current Gregorian time
|
|
|
|
*/
|
|
|
|
static VOID
|
|
|
|
UpdateSystemTime(ULONG ulTime)
|
|
|
|
{
|
|
|
|
FILETIME ftNew;
|
|
|
|
LARGE_INTEGER li;
|
|
|
|
SYSTEMTIME stNew;
|
|
|
|
|
2011-11-29 14:55:58 +00:00
|
|
|
/* Time at 1st Jan 1900 */
|
2006-06-23 08:13:50 +00:00
|
|
|
stNew.wYear = 1900;
|
|
|
|
stNew.wMonth = 1;
|
|
|
|
stNew.wDay = 1;
|
|
|
|
stNew.wHour = 0;
|
|
|
|
stNew.wMinute = 0;
|
|
|
|
stNew.wSecond = 0;
|
|
|
|
stNew.wMilliseconds = 0;
|
|
|
|
|
2011-11-29 14:55:58 +00:00
|
|
|
/* Convert to a file time */
|
2007-06-07 16:18:38 +00:00
|
|
|
if (!SystemTimeToFileTime(&stNew, &ftNew))
|
2006-06-23 08:13:50 +00:00
|
|
|
{
|
2006-10-03 17:02:36 +00:00
|
|
|
DisplayWin32Error(GetLastError());
|
2006-06-23 08:13:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-29 14:55:58 +00:00
|
|
|
/* Add on the time passed since 1st Jan 1900 */
|
2006-06-23 08:13:50 +00:00
|
|
|
li = *(LARGE_INTEGER *)&ftNew;
|
|
|
|
li.QuadPart += (LONGLONG)10000000 * ulTime;
|
|
|
|
ftNew = * (FILETIME *)&li;
|
|
|
|
|
2011-11-29 14:55:58 +00:00
|
|
|
/* Convert back to a system time */
|
2007-06-07 16:18:38 +00:00
|
|
|
if (!FileTimeToSystemTime(&ftNew, &stNew))
|
2006-06-23 08:13:50 +00:00
|
|
|
{
|
2006-10-03 17:02:36 +00:00
|
|
|
DisplayWin32Error(GetLastError());
|
2006-06-23 08:13:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-16 15:40:03 +00:00
|
|
|
/* Use SystemSetTime with SystemTime = TRUE to set System Time */
|
|
|
|
if (!SystemSetTime(&stNew, TRUE))
|
2006-10-03 17:02:36 +00:00
|
|
|
DisplayWin32Error(GetLastError());
|
2006-06-23 08:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static VOID
|
|
|
|
EnableDialogText(HWND hwnd)
|
|
|
|
{
|
|
|
|
BOOL bChecked;
|
2007-06-07 16:18:38 +00:00
|
|
|
UINT uCheck;
|
2006-06-23 08:13:50 +00:00
|
|
|
|
2007-07-31 21:17:57 +00:00
|
|
|
uCheck = (UINT)SendDlgItemMessageW(hwnd, IDC_AUTOSYNC, BM_GETCHECK, 0, 0);
|
2007-06-07 16:18:38 +00:00
|
|
|
bChecked = (uCheck == BST_CHECKED) ? TRUE : FALSE;
|
2006-06-23 08:13:50 +00:00
|
|
|
|
|
|
|
EnableWindow(GetDlgItem(hwnd, IDC_SERVERTEXT), bChecked);
|
|
|
|
EnableWindow(GetDlgItem(hwnd, IDC_SERVERLIST), bChecked);
|
|
|
|
EnableWindow(GetDlgItem(hwnd, IDC_UPDATEBUTTON), bChecked);
|
|
|
|
EnableWindow(GetDlgItem(hwnd, IDC_SUCSYNC), bChecked);
|
|
|
|
EnableWindow(GetDlgItem(hwnd, IDC_NEXTSYNC), bChecked);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-09 16:19:39 +00:00
|
|
|
static VOID
|
|
|
|
GetSyncSetting(HWND hwnd)
|
|
|
|
{
|
|
|
|
HKEY hKey;
|
2007-07-31 21:17:57 +00:00
|
|
|
WCHAR szData[8];
|
2007-06-07 16:18:38 +00:00
|
|
|
DWORD dwSize;
|
2006-07-09 16:19:39 +00:00
|
|
|
|
2007-07-31 21:17:57 +00:00
|
|
|
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
|
|
|
|
L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Parameters",
|
|
|
|
0,
|
|
|
|
KEY_QUERY_VALUE,
|
|
|
|
&hKey) == ERROR_SUCCESS)
|
2007-06-07 16:18:38 +00:00
|
|
|
{
|
2007-07-31 21:17:57 +00:00
|
|
|
dwSize = 8 * sizeof(WCHAR);
|
|
|
|
if (RegQueryValueExW(hKey,
|
|
|
|
L"Type",
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
(LPBYTE)szData,
|
|
|
|
&dwSize) == ERROR_SUCCESS)
|
2006-10-04 21:47:08 +00:00
|
|
|
{
|
2007-07-31 21:17:57 +00:00
|
|
|
if (wcscmp(szData, L"NTP") == 0)
|
|
|
|
SendDlgItemMessageW(hwnd, IDC_AUTOSYNC, BM_SETCHECK, 0, 0);
|
2006-10-04 21:47:08 +00:00
|
|
|
}
|
2007-06-07 16:18:38 +00:00
|
|
|
|
2006-10-04 21:47:08 +00:00
|
|
|
RegCloseKey(hKey);
|
2006-07-09 16:19:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-23 08:13:50 +00:00
|
|
|
static VOID
|
2007-04-30 19:05:04 +00:00
|
|
|
OnInitDialog(HWND hwnd)
|
2006-06-23 08:13:50 +00:00
|
|
|
{
|
2006-07-09 16:19:39 +00:00
|
|
|
GetSyncSetting(hwnd);
|
2006-06-23 08:13:50 +00:00
|
|
|
EnableDialogText(hwnd);
|
|
|
|
CreateNTPServerList(hwnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Property page dialog callback */
|
|
|
|
INT_PTR CALLBACK
|
|
|
|
InetTimePageProc(HWND hwndDlg,
|
|
|
|
UINT uMsg,
|
|
|
|
WPARAM wParam,
|
|
|
|
LPARAM lParam)
|
|
|
|
{
|
|
|
|
switch (uMsg)
|
|
|
|
{
|
|
|
|
case WM_INITDIALOG:
|
2007-04-30 19:05:04 +00:00
|
|
|
OnInitDialog(hwndDlg);
|
2007-06-07 16:18:38 +00:00
|
|
|
break;
|
2006-06-23 08:13:50 +00:00
|
|
|
|
|
|
|
case WM_COMMAND:
|
|
|
|
switch(LOWORD(wParam))
|
|
|
|
{
|
|
|
|
case IDC_UPDATEBUTTON:
|
|
|
|
{
|
|
|
|
ULONG ulTime;
|
|
|
|
|
|
|
|
SetNTPServer(hwndDlg);
|
|
|
|
|
|
|
|
ulTime = GetTimeFromServer();
|
|
|
|
if (ulTime != 0)
|
|
|
|
UpdateSystemTime(ulTime);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IDC_SERVERLIST:
|
|
|
|
if (HIWORD(wParam) == CBN_SELCHANGE)
|
2007-06-07 16:18:38 +00:00
|
|
|
{
|
2006-06-23 08:13:50 +00:00
|
|
|
/* Enable the 'Apply' button */
|
|
|
|
PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
|
2007-06-07 16:18:38 +00:00
|
|
|
}
|
|
|
|
break;
|
2006-06-23 08:13:50 +00:00
|
|
|
|
|
|
|
case IDC_AUTOSYNC:
|
|
|
|
if (HIWORD(wParam) == BN_CLICKED)
|
|
|
|
{
|
|
|
|
EnableDialogText(hwndDlg);
|
|
|
|
|
|
|
|
/* Enable the 'Apply' button */
|
|
|
|
PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
|
|
|
|
}
|
2007-06-07 16:18:38 +00:00
|
|
|
break;
|
2006-06-23 08:13:50 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WM_DESTROY:
|
2007-06-07 16:18:38 +00:00
|
|
|
break;
|
2006-06-23 08:13:50 +00:00
|
|
|
|
|
|
|
case WM_NOTIFY:
|
|
|
|
{
|
|
|
|
LPNMHDR lpnm = (LPNMHDR)lParam;
|
|
|
|
|
|
|
|
switch (lpnm->code)
|
|
|
|
{
|
|
|
|
case PSN_APPLY:
|
|
|
|
SetNTPServer(hwndDlg);
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
default:
|
2007-06-07 16:18:38 +00:00
|
|
|
break;
|
2006-06-23 08:13:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|