ReactOS  0.4.14-dev-77-gd9e7c48
internettime.c
Go to the documentation of this file.
1 /*
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>
7  *
8  */
9 
10 #include "timedate.h"
11 #include <stdlib.h>
12 
14 
15 static VOID
17 {
18  HWND hList;
19  WCHAR szValName[MAX_VALUE_NAME];
20  WCHAR szData[256];
21  DWORD dwIndex = 0;
22  DWORD dwValSize;
23  DWORD dwNameSize;
24  DWORD dwDefault = 1;
25  LONG lRet;
26  HKEY hKey;
27 
30 
32  L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers",
33  0,
35  &hKey);
36  if (lRet != ERROR_SUCCESS)
37  return;
38 
39  while (TRUE)
40  {
41  dwValSize = MAX_VALUE_NAME * sizeof(WCHAR);
42  szValName[0] = L'\0';
43  lRet = RegEnumValueW(hKey,
44  dwIndex,
45  szValName,
46  &dwValSize,
47  NULL,
48  NULL,
49  (LPBYTE)szData,
50  &dwNameSize);
51  if (lRet == ERROR_SUCCESS)
52  {
53  /* Get date from default reg value */
54  if (wcscmp(szValName, L"") == 0) // if (Index == 0)
55  {
56  dwDefault = _wtoi(szData);
57  dwIndex++;
58  }
59  else
60  {
63  0,
64  (LPARAM)szData);
65  dwIndex++;
66  }
67  }
68  else if (lRet != ERROR_MORE_DATA)
69  {
70  break;
71  }
72  }
73 
74  if (dwDefault < 1 || dwDefault > dwIndex)
75  dwDefault = 1;
76 
77  /* Server reg entries count from 1,
78  * Combo boxes count from 0 */
79  dwDefault--;
80 
83  dwDefault,
84  0);
85 
86  RegCloseKey(hKey);
87 }
88 
89 
90 /* Set the selected server in the registry */
91 static VOID
93 {
94  HKEY hKey;
95  HWND hList;
96  UINT uSel;
97  WCHAR szSel[4];
98  LONG lRet;
99  WCHAR buffer[256];
100 
103 
104  uSel = (UINT)SendMessageW(hList, CB_GETCURSEL, 0, 0);
105 
107 
108  /* If there is new data entered then save it in the registry
109  The same key name of "0" is used to store all user entered values
110  */
111  if (uSel == -1)
112  {
114  L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers",
115  0,
117  &hKey);
118  if (lRet != ERROR_SUCCESS)
119  {
120  DisplayWin32Error(lRet);
121  return;
122  }
123  lRet = RegSetValueExW(hKey,
124  L"0",
125  0,
126  REG_SZ,
127  (LPBYTE)buffer,
128  (wcslen(buffer) + 1) * sizeof(WCHAR));
129  if (lRet != ERROR_SUCCESS)
130  DisplayWin32Error(lRet);
131  }
132 
133  /* Server reg entries count from 1,
134  * Combo boxes count from 0 */
135  uSel++;
136 
137  /* Convert to wide char */
138  _itow(uSel, szSel, 10);
139 
141  L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers",
142  0,
144  &hKey);
145  if (lRet != ERROR_SUCCESS)
146  {
147  DisplayWin32Error(lRet);
148  return;
149  }
150 
151  lRet = RegSetValueExW(hKey,
152  L"",
153  0,
154  REG_SZ,
155  (LPBYTE)szSel,
156  (wcslen(szSel) + 1) * sizeof(WCHAR));
157  if (lRet != ERROR_SUCCESS)
158  DisplayWin32Error(lRet);
159 
160  RegCloseKey(hKey);
161 }
162 
163 
164 static VOID
166 {
167  BOOL bChecked;
168  UINT uCheck;
169 
171  bChecked = (uCheck == BST_CHECKED) ? TRUE : FALSE;
172 
178 }
179 
180 
181 static VOID
183 {
184  HKEY hKey;
185  WCHAR szData[8];
186  DWORD dwSize;
187 
189  L"SYSTEM\\CurrentControlSet\\Services\\W32Time\\Parameters",
190  0,
192  &hKey) == ERROR_SUCCESS)
193  {
194  dwSize = 8 * sizeof(WCHAR);
195  if (RegQueryValueExW(hKey,
196  L"Type",
197  NULL,
198  NULL,
199  (LPBYTE)szData,
200  &dwSize) == ERROR_SUCCESS)
201  {
202  if (wcscmp(szData, L"NTP") == 0)
204  else
206  }
207 
208  RegCloseKey(hKey);
209  }
210 }
211 
212 
213 static VOID
215 {
219 }
220 
221 static VOID
223 {
224  HKEY hKey;
225  LONG lRet;
226  LPCWSTR szAuto;
227 
228  if (Sync)
229  szAuto = L"NTP";
230  else
231  szAuto = L"NoSync";
232 
234  L"SYSTEM\\CurrentControlSet\\Services\\W32Time\\Parameters",
235  0,
237  &hKey);
238  if (lRet != ERROR_SUCCESS)
239  {
240  DisplayWin32Error(lRet);
241  return;
242  }
243 
244  lRet = RegSetValueExW(hKey,
245  L"Type",
246  0,
247  REG_SZ,
248  (LPBYTE)szAuto,
249  (wcslen(szAuto) + 1) * sizeof(WCHAR));
250  if (lRet != ERROR_SUCCESS)
251  DisplayWin32Error(lRet);
252 
253  RegCloseKey(hKey);
254 }
255 
256 /* Property page dialog callback */
259  UINT uMsg,
260  WPARAM wParam,
261  LPARAM lParam)
262 {
263  switch (uMsg)
264  {
265  case WM_INITDIALOG:
266  OnInitDialog(hwndDlg);
267  break;
268 
269  case WM_COMMAND:
270  switch(LOWORD(wParam))
271  {
272  case IDC_UPDATEBUTTON:
273  {
274  DWORD dwError;
275 
276  SetNTPServer(hwndDlg);
277 
278  dwError = W32TimeSyncNow(L"localhost", 0, 0);
279  if (dwError != ERROR_SUCCESS)
280  {
281  DisplayWin32Error(dwError);
282  }
283  }
284  break;
285 
286  case IDC_SERVERLIST:
288  {
289  /* Enable the 'Apply' button */
290  PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
291  }
292  break;
293 
294  case IDC_AUTOSYNC:
295  if (HIWORD(wParam) == BN_CLICKED)
296  {
297  EnableDialogText(hwndDlg);
298 
299  /* Enable the 'Apply' button */
300  PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
301  }
302  break;
303  }
304  break;
305 
306  case WM_DESTROY:
307  break;
308 
309  case WM_NOTIFY:
310  {
311  LPNMHDR lpnm = (LPNMHDR)lParam;
312 
313  switch (lpnm->code)
314  {
315  case PSN_APPLY:
316  SetNTPServer(hwndDlg);
317 
319  OnAutoSync(TRUE);
320  else
321  OnAutoSync(FALSE);
322 
323  return TRUE;
324 
325  default:
326  break;
327  }
328  }
329  break;
330  }
331 
332  return FALSE;
333 }
DWORD WINAPI W32TimeSyncNow(LPCWSTR cmdline, UINT blocking, UINT flags)
Definition: w32time.c:338
const DOCKBAR PVOID HWND HWND * hwnd
Definition: tooldock.h:22
#define TRUE
Definition: types.h:120
#define ERROR_SUCCESS
Definition: deptool.c:10
#define KEY_SET_VALUE
Definition: nt_native.h:1017
#define IDC_AUTOSYNC
Definition: resource.h:24
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
static VOID SetNTPServer(HWND hwnd)
Definition: internettime.c:92
#define IDC_NEXTSYNC
Definition: resource.h:26
#define _countof(array)
Definition: fontsub.cpp:30
_Check_return_ _CRTIMP int __cdecl _wtoi(_In_z_ const wchar_t *_Str)
#define WM_GETTEXT
Definition: winuser.h:1600
static const WCHAR szData[]
Definition: msipriv.h:1195
#define CALLBACK
Definition: compat.h:27
GLuint buffer
Definition: glext.h:5915
#define IDC_SUCSYNC
Definition: resource.h:25
UINT_PTR WPARAM
Definition: windef.h:207
_CRTIMP wchar_t *__cdecl _itow(_In_ int _Value, _Pre_notnull_ _Post_z_ wchar_t *_Dest, _In_ int _Radix)
TCHAR * cmdline
Definition: stretchblt.cpp:32
#define PSN_APPLY
Definition: prsht.h:117
int32_t INT_PTR
Definition: typedefs.h:62
#define IDC_SERVERTEXT
Definition: resource.h:27
#define CB_GETCURSEL
Definition: winuser.h:1925
WPARAM wParam
Definition: combotst.c:138
struct tagNMHDR * LPNMHDR
UINT code
Definition: winuser.h:3133
VOID DisplayWin32Error(DWORD dwErrorCode)
Definition: timedate.c:28
static VOID EnableDialogText(HWND hwnd)
Definition: internettime.c:165
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
unsigned char * LPBYTE
Definition: typedefs.h:52
static VOID CreateNTPServerList(HWND hwnd)
Definition: internettime.c:16
unsigned int BOOL
Definition: ntddk_ex.h:94
long LONG
Definition: pedump.c:60
INT_PTR CALLBACK InetTimePageProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: internettime.c:258
#define CB_ADDSTRING
Definition: winuser.h:1918
LONG WINAPI RegEnumValueW(_In_ HKEY hKey, _In_ DWORD index, _Out_ LPWSTR value, _Inout_ PDWORD val_count, _Reserved_ PDWORD reserved, _Out_opt_ PDWORD type, _Out_opt_ LPBYTE data, _Inout_opt_ PDWORD count)
Definition: reg.c:2853
smooth NULL
Definition: ftsmooth.c:416
#define MAX_VALUE_NAME
Definition: control.c:23
LONG_PTR LPARAM
Definition: windef.h:208
#define BM_GETCHECK
Definition: winuser.h:1900
#define WM_DESTROY
Definition: winuser.h:1591
LONG WINAPI RegSetValueExW(_In_ HKEY hKey, _In_ LPCWSTR lpValueName, _In_ DWORD Reserved, _In_ DWORD dwType, _In_ CONST BYTE *lpData, _In_ DWORD cbData)
Definition: reg.c:4895
#define BM_SETCHECK
Definition: winuser.h:1903
#define CBN_EDITCHANGE
Definition: winuser.h:1957
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4116
HWND WINAPI GetDlgItem(_In_opt_ HWND, _In_ int)
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define PropSheet_Changed(d, w)
Definition: prsht.h:326
#define IDC_UPDATEBUTTON
Definition: resource.h:23
#define WINAPI
Definition: msvc.h:8
#define BN_CLICKED
Definition: winuser.h:1907
unsigned long DWORD
Definition: ntddk_ex.h:95
LRESULT WINAPI SendDlgItemMessageW(_In_ HWND, _In_ int, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
GLbitfield flags
Definition: glext.h:7161
static const WCHAR L[]
Definition: oid.c:1250
#define IDC_SERVERLIST
Definition: resource.h:22
#define CB_SETCURSEL
Definition: winuser.h:1943
#define WM_COMMAND
Definition: winuser.h:1722
#define BST_UNCHECKED
Definition: winuser.h:199
#define ERROR_MORE_DATA
Definition: dderror.h:13
BOOL WINAPI EnableWindow(_In_ HWND, _In_ BOOL)
HWND hList
Definition: livecd.c:10
_Check_return_ _CRTIMP int __cdecl wcscmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
HWND WINAPI GetParent(_In_ HWND)
static VOID GetSyncSetting(HWND hwnd)
Definition: internettime.c:182
#define KEY_QUERY_VALUE
Definition: nt_native.h:1016
LRESULT WINAPI SendMessageW(_In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
unsigned int UINT
Definition: ndis.h:50
#define CBN_SELCHANGE
Definition: winuser.h:1961
#define BST_CHECKED
Definition: winuser.h:197
#define HIWORD(l)
Definition: typedefs.h:246
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3366
static VOID OnAutoSync(BOOL Sync)
Definition: internettime.c:222
static VOID OnInitDialog(HWND hwnd)
Definition: internettime.c:214
#define WM_INITDIALOG
Definition: winuser.h:1721
LPARAM lParam
Definition: combotst.c:139
#define LOWORD(l)
Definition: pedump.c:82
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54
#define WM_NOTIFY
Definition: richedit.h:61
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12
#define REG_SZ
Definition: layer.c:22