2 * PROJECT: ReactOS Timedate Control Panel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/cpl/timedate/internettime.c
5 * PURPOSE: Internet Time property page
6 * COPYRIGHT: Copyright 2006 Ged Murphy <gedmurphy@gmail.com>
13 CreateNTPServerList(HWND hwnd
)
16 WCHAR szValName
[MAX_VALUE_NAME
];
25 hList
= GetDlgItem(hwnd
,
28 lRet
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
29 L
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers",
33 if (lRet
!= ERROR_SUCCESS
)
38 dwValSize
= MAX_VALUE_NAME
* sizeof(WCHAR
);
40 lRet
= RegEnumValueW(hKey
,
48 if (lRet
== ERROR_SUCCESS
)
50 /* Get date from default reg value */
51 if (wcscmp(szValName
, L
"") == 0) // if (Index == 0)
53 dwDefault
= _wtoi(szData
);
65 else if (lRet
!= ERROR_MORE_DATA
)
71 if (dwDefault
< 1 || dwDefault
> dwIndex
)
74 /* Server reg entries count from 1,
75 * Combo boxes count from 0 */
87 /* Set the selected server in the registry */
89 SetNTPServer(HWND hwnd
)
97 hList
= GetDlgItem(hwnd
,
100 uSel
= (UINT
)SendMessageW(hList
, CB_GETCURSEL
, 0, 0);
102 /* Server reg entries count from 1,
103 * Combo boxes count from 0 */
106 /* Convert to wide char */
107 _itow(uSel
, szSel
, 10);
109 lRet
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
110 L
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers",
114 if (lRet
!= ERROR_SUCCESS
)
116 DisplayWin32Error(lRet
);
120 lRet
= RegSetValueExW(hKey
,
125 (wcslen(szSel
) + 1) * sizeof(WCHAR
));
126 if (lRet
!= ERROR_SUCCESS
)
127 DisplayWin32Error(lRet
);
133 /* Get the domain name from the registry */
135 GetNTPServerAddress(LPWSTR
*lpAddress
)
142 lRet
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
143 L
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers",
147 if (lRet
!= ERROR_SUCCESS
)
150 /* Get data from default value */
151 dwSize
= 4 * sizeof(WCHAR
);
152 lRet
= RegQueryValueExW(hKey
,
158 if (lRet
!= ERROR_SUCCESS
)
162 lRet
= RegQueryValueExW(hKey
,
168 if (lRet
!= ERROR_SUCCESS
)
171 (*lpAddress
) = (LPWSTR
)HeapAlloc(GetProcessHeap(),
174 if ((*lpAddress
) == NULL
)
176 lRet
= ERROR_NOT_ENOUGH_MEMORY
;
180 lRet
= RegQueryValueExW(hKey
,
186 if (lRet
!= ERROR_SUCCESS
)
194 DisplayWin32Error(lRet
);
197 HeapFree(GetProcessHeap(), 0, *lpAddress
);
202 /* Request the time from the current NTP server */
204 GetTimeFromServer(VOID
)
206 LPWSTR lpAddress
= NULL
;
209 if (GetNTPServerAddress(&lpAddress
))
211 ulTime
= GetServerTime(lpAddress
);
213 HeapFree(GetProcessHeap(),
222 * NTP servers state the number of seconds passed since
223 * 1st Jan, 1900. The time returned from the server
224 * needs adding to that date to get the current Gregorian time
227 UpdateSystemTime(ULONG ulTime
)
233 /* Time at 1st Jan 1900 */
240 stNew
.wMilliseconds
= 0;
242 /* Convert to a file time */
243 if (!SystemTimeToFileTime(&stNew
, &ftNew
))
245 DisplayWin32Error(GetLastError());
249 /* Add on the time passed since 1st Jan 1900 */
250 li
= *(LARGE_INTEGER
*)&ftNew
;
251 li
.QuadPart
+= (LONGLONG
)10000000 * ulTime
;
252 ftNew
= * (FILETIME
*)&li
;
254 /* Convert back to a system time */
255 if (!FileTimeToSystemTime(&ftNew
, &stNew
))
257 DisplayWin32Error(GetLastError());
261 if (!SystemSetLocalTime(&stNew
))
262 DisplayWin32Error(GetLastError());
267 EnableDialogText(HWND hwnd
)
272 uCheck
= (UINT
)SendDlgItemMessageW(hwnd
, IDC_AUTOSYNC
, BM_GETCHECK
, 0, 0);
273 bChecked
= (uCheck
== BST_CHECKED
) ? TRUE
: FALSE
;
275 EnableWindow(GetDlgItem(hwnd
, IDC_SERVERTEXT
), bChecked
);
276 EnableWindow(GetDlgItem(hwnd
, IDC_SERVERLIST
), bChecked
);
277 EnableWindow(GetDlgItem(hwnd
, IDC_UPDATEBUTTON
), bChecked
);
278 EnableWindow(GetDlgItem(hwnd
, IDC_SUCSYNC
), bChecked
);
279 EnableWindow(GetDlgItem(hwnd
, IDC_NEXTSYNC
), bChecked
);
284 GetSyncSetting(HWND hwnd
)
290 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
291 L
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Parameters",
294 &hKey
) == ERROR_SUCCESS
)
296 dwSize
= 8 * sizeof(WCHAR
);
297 if (RegQueryValueExW(hKey
,
302 &dwSize
) == ERROR_SUCCESS
)
304 if (wcscmp(szData
, L
"NTP") == 0)
305 SendDlgItemMessageW(hwnd
, IDC_AUTOSYNC
, BM_SETCHECK
, 0, 0);
314 OnInitDialog(HWND hwnd
)
316 GetSyncSetting(hwnd
);
317 EnableDialogText(hwnd
);
318 CreateNTPServerList(hwnd
);
322 /* Property page dialog callback */
324 InetTimePageProc(HWND hwndDlg
,
332 OnInitDialog(hwndDlg
);
336 switch(LOWORD(wParam
))
338 case IDC_UPDATEBUTTON
:
342 SetNTPServer(hwndDlg
);
344 ulTime
= GetTimeFromServer();
346 UpdateSystemTime(ulTime
);
351 if (HIWORD(wParam
) == CBN_SELCHANGE
)
353 /* Enable the 'Apply' button */
354 PropSheet_Changed(GetParent(hwndDlg
), hwndDlg
);
359 if (HIWORD(wParam
) == BN_CLICKED
)
361 EnableDialogText(hwndDlg
);
363 /* Enable the 'Apply' button */
364 PropSheet_Changed(GetParent(hwndDlg
), hwndDlg
);
375 LPNMHDR lpnm
= (LPNMHDR
)lParam
;
380 SetNTPServer(hwndDlg
);