[TIMEDATE] Avoid the use of DisplayWin32Error in time sync utility functions. CORE...
[reactos.git] / dll / cpl / timedate / internettime.c
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
12 static VOID
13 CreateNTPServerList(HWND hwnd)
14 {
15 HWND hList;
16 WCHAR szValName[MAX_VALUE_NAME];
17 WCHAR szData[256];
18 DWORD dwIndex = 0;
19 DWORD dwValSize;
20 DWORD dwNameSize;
21 DWORD dwDefault = 1;
22 LONG lRet;
23 HKEY hKey;
24
25 hList = GetDlgItem(hwnd,
26 IDC_SERVERLIST);
27
28 lRet = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
29 L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers",
30 0,
31 KEY_QUERY_VALUE,
32 &hKey);
33 if (lRet != ERROR_SUCCESS)
34 return;
35
36 while (TRUE)
37 {
38 dwValSize = MAX_VALUE_NAME * sizeof(WCHAR);
39 szValName[0] = L'\0';
40 lRet = RegEnumValueW(hKey,
41 dwIndex,
42 szValName,
43 &dwValSize,
44 NULL,
45 NULL,
46 (LPBYTE)szData,
47 &dwNameSize);
48 if (lRet == ERROR_SUCCESS)
49 {
50 /* Get date from default reg value */
51 if (wcscmp(szValName, L"") == 0) // if (Index == 0)
52 {
53 dwDefault = _wtoi(szData);
54 dwIndex++;
55 }
56 else
57 {
58 SendMessageW(hList,
59 CB_ADDSTRING,
60 0,
61 (LPARAM)szData);
62 dwIndex++;
63 }
64 }
65 else if (lRet != ERROR_MORE_DATA)
66 {
67 break;
68 }
69 }
70
71 if (dwDefault < 1 || dwDefault > dwIndex)
72 dwDefault = 1;
73
74 /* Server reg entries count from 1,
75 * Combo boxes count from 0 */
76 dwDefault--;
77
78 SendMessageW(hList,
79 CB_SETCURSEL,
80 dwDefault,
81 0);
82
83 RegCloseKey(hKey);
84 }
85
86
87 /* Set the selected server in the registry */
88 static VOID
89 SetNTPServer(HWND hwnd)
90 {
91 HKEY hKey;
92 HWND hList;
93 UINT uSel;
94 WCHAR szSel[4];
95 LONG lRet;
96
97 hList = GetDlgItem(hwnd,
98 IDC_SERVERLIST);
99
100 uSel = (UINT)SendMessageW(hList, CB_GETCURSEL, 0, 0);
101
102 /* Server reg entries count from 1,
103 * Combo boxes count from 0 */
104 uSel++;
105
106 /* Convert to wide char */
107 _itow(uSel, szSel, 10);
108
109 lRet = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
110 L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers",
111 0,
112 KEY_SET_VALUE,
113 &hKey);
114 if (lRet != ERROR_SUCCESS)
115 {
116 DisplayWin32Error(lRet);
117 return;
118 }
119
120 lRet = RegSetValueExW(hKey,
121 L"",
122 0,
123 REG_SZ,
124 (LPBYTE)szSel,
125 (wcslen(szSel) + 1) * sizeof(WCHAR));
126 if (lRet != ERROR_SUCCESS)
127 DisplayWin32Error(lRet);
128
129 RegCloseKey(hKey);
130 }
131
132
133 static VOID
134 EnableDialogText(HWND hwnd)
135 {
136 BOOL bChecked;
137 UINT uCheck;
138
139 uCheck = (UINT)SendDlgItemMessageW(hwnd, IDC_AUTOSYNC, BM_GETCHECK, 0, 0);
140 bChecked = (uCheck == BST_CHECKED) ? TRUE : FALSE;
141
142 EnableWindow(GetDlgItem(hwnd, IDC_SERVERTEXT), bChecked);
143 EnableWindow(GetDlgItem(hwnd, IDC_SERVERLIST), bChecked);
144 EnableWindow(GetDlgItem(hwnd, IDC_UPDATEBUTTON), bChecked);
145 EnableWindow(GetDlgItem(hwnd, IDC_SUCSYNC), bChecked);
146 EnableWindow(GetDlgItem(hwnd, IDC_NEXTSYNC), bChecked);
147 }
148
149
150 static VOID
151 GetSyncSetting(HWND hwnd)
152 {
153 HKEY hKey;
154 WCHAR szData[8];
155 DWORD dwSize;
156
157 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
158 L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Parameters",
159 0,
160 KEY_QUERY_VALUE,
161 &hKey) == ERROR_SUCCESS)
162 {
163 dwSize = 8 * sizeof(WCHAR);
164 if (RegQueryValueExW(hKey,
165 L"Type",
166 NULL,
167 NULL,
168 (LPBYTE)szData,
169 &dwSize) == ERROR_SUCCESS)
170 {
171 if (wcscmp(szData, L"NTP") == 0)
172 SendDlgItemMessageW(hwnd, IDC_AUTOSYNC, BM_SETCHECK, 0, 0);
173 }
174
175 RegCloseKey(hKey);
176 }
177 }
178
179
180 static VOID
181 OnInitDialog(HWND hwnd)
182 {
183 GetSyncSetting(hwnd);
184 EnableDialogText(hwnd);
185 CreateNTPServerList(hwnd);
186 }
187
188
189 /* Property page dialog callback */
190 INT_PTR CALLBACK
191 InetTimePageProc(HWND hwndDlg,
192 UINT uMsg,
193 WPARAM wParam,
194 LPARAM lParam)
195 {
196 switch (uMsg)
197 {
198 case WM_INITDIALOG:
199 OnInitDialog(hwndDlg);
200 break;
201
202 case WM_COMMAND:
203 switch(LOWORD(wParam))
204 {
205 case IDC_UPDATEBUTTON:
206 {
207 DWORD dwError;
208
209 SetNTPServer(hwndDlg);
210
211 dwError = SyncTimeNow();
212 if (dwError != ERROR_SUCCESS)
213 {
214 DisplayWin32Error(dwError);
215 }
216 }
217 break;
218
219 case IDC_SERVERLIST:
220 if (HIWORD(wParam) == CBN_SELCHANGE)
221 {
222 /* Enable the 'Apply' button */
223 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
224 }
225 break;
226
227 case IDC_AUTOSYNC:
228 if (HIWORD(wParam) == BN_CLICKED)
229 {
230 EnableDialogText(hwndDlg);
231
232 /* Enable the 'Apply' button */
233 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
234 }
235 break;
236 }
237 break;
238
239 case WM_DESTROY:
240 break;
241
242 case WM_NOTIFY:
243 {
244 LPNMHDR lpnm = (LPNMHDR)lParam;
245
246 switch (lpnm->code)
247 {
248 case PSN_APPLY:
249 SetNTPServer(hwndDlg);
250 return TRUE;
251
252 default:
253 break;
254 }
255 }
256 break;
257 }
258
259 return FALSE;
260 }