Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygeninternettime.c
Go to the documentation of this file.
00001 /* 00002 * PROJECT: ReactOS Timedate Control Panel 00003 * LICENSE: GPL - See COPYING in the top level directory 00004 * FILE: dll/cpl/timedate/internettime.c 00005 * PURPOSE: Internet Time property page 00006 * COPYRIGHT: Copyright 2006 Ged Murphy <gedmurphy@gmail.com> 00007 * 00008 */ 00009 00010 #include <timedate.h> 00011 00012 static VOID 00013 CreateNTPServerList(HWND hwnd) 00014 { 00015 HWND hList; 00016 WCHAR szValName[MAX_VALUE_NAME]; 00017 WCHAR szData[256]; 00018 DWORD dwIndex = 0; 00019 DWORD dwValSize; 00020 DWORD dwNameSize; 00021 DWORD dwDefault = 1; 00022 LONG lRet; 00023 HKEY hKey; 00024 00025 hList = GetDlgItem(hwnd, 00026 IDC_SERVERLIST); 00027 00028 lRet = RegOpenKeyExW(HKEY_LOCAL_MACHINE, 00029 L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers", 00030 0, 00031 KEY_QUERY_VALUE, 00032 &hKey); 00033 if (lRet != ERROR_SUCCESS) 00034 return; 00035 00036 while (TRUE) 00037 { 00038 dwValSize = MAX_VALUE_NAME * sizeof(WCHAR); 00039 szValName[0] = L'\0'; 00040 lRet = RegEnumValueW(hKey, 00041 dwIndex, 00042 szValName, 00043 &dwValSize, 00044 NULL, 00045 NULL, 00046 (LPBYTE)szData, 00047 &dwNameSize); 00048 if (lRet == ERROR_SUCCESS) 00049 { 00050 /* Get date from default reg value */ 00051 if (wcscmp(szValName, L"") == 0) // if (Index == 0) 00052 { 00053 dwDefault = _wtoi(szData); 00054 dwIndex++; 00055 } 00056 else 00057 { 00058 SendMessageW(hList, 00059 CB_ADDSTRING, 00060 0, 00061 (LPARAM)szData); 00062 dwIndex++; 00063 } 00064 } 00065 else if (lRet != ERROR_MORE_DATA) 00066 { 00067 break; 00068 } 00069 } 00070 00071 if (dwDefault < 1 || dwDefault > dwIndex) 00072 dwDefault = 1; 00073 00074 /* Server reg entries count from 1, 00075 * Combo boxes count from 0 */ 00076 dwDefault--; 00077 00078 SendMessageW(hList, 00079 CB_SETCURSEL, 00080 dwDefault, 00081 0); 00082 00083 RegCloseKey(hKey); 00084 } 00085 00086 00087 /* Set the selected server in the registry */ 00088 static VOID 00089 SetNTPServer(HWND hwnd) 00090 { 00091 HKEY hKey; 00092 HWND hList; 00093 UINT uSel; 00094 WCHAR szSel[4]; 00095 LONG lRet; 00096 00097 hList = GetDlgItem(hwnd, 00098 IDC_SERVERLIST); 00099 00100 uSel = (UINT)SendMessageW(hList, CB_GETCURSEL, 0, 0); 00101 00102 /* Server reg entries count from 1, 00103 * Combo boxes count from 0 */ 00104 uSel++; 00105 00106 /* Convert to wide char */ 00107 _itow(uSel, szSel, 10); 00108 00109 lRet = RegOpenKeyExW(HKEY_LOCAL_MACHINE, 00110 L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers", 00111 0, 00112 KEY_SET_VALUE, 00113 &hKey); 00114 if (lRet != ERROR_SUCCESS) 00115 { 00116 DisplayWin32Error(lRet); 00117 return; 00118 } 00119 00120 lRet = RegSetValueExW(hKey, 00121 L"", 00122 0, 00123 REG_SZ, 00124 (LPBYTE)szSel, 00125 (wcslen(szSel) + 1) * sizeof(WCHAR)); 00126 if (lRet != ERROR_SUCCESS) 00127 DisplayWin32Error(lRet); 00128 00129 RegCloseKey(hKey); 00130 } 00131 00132 00133 /* Get the domain name from the registry */ 00134 static BOOL 00135 GetNTPServerAddress(LPWSTR *lpAddress) 00136 { 00137 HKEY hKey; 00138 WCHAR szSel[4]; 00139 DWORD dwSize; 00140 LONG lRet; 00141 00142 lRet = RegOpenKeyExW(HKEY_LOCAL_MACHINE, 00143 L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers", 00144 0, 00145 KEY_QUERY_VALUE, 00146 &hKey); 00147 if (lRet != ERROR_SUCCESS) 00148 goto fail; 00149 00150 /* Get data from default value */ 00151 dwSize = 4 * sizeof(WCHAR); 00152 lRet = RegQueryValueExW(hKey, 00153 NULL, 00154 NULL, 00155 NULL, 00156 (LPBYTE)szSel, 00157 &dwSize); 00158 if (lRet != ERROR_SUCCESS) 00159 goto fail; 00160 00161 dwSize = 0; 00162 lRet = RegQueryValueExW(hKey, 00163 szSel, 00164 NULL, 00165 NULL, 00166 NULL, 00167 &dwSize); 00168 if (lRet != ERROR_SUCCESS) 00169 goto fail; 00170 00171 (*lpAddress) = (LPWSTR)HeapAlloc(GetProcessHeap(), 00172 0, 00173 dwSize); 00174 if ((*lpAddress) == NULL) 00175 { 00176 lRet = ERROR_NOT_ENOUGH_MEMORY; 00177 goto fail; 00178 } 00179 00180 lRet = RegQueryValueExW(hKey, 00181 szSel, 00182 NULL, 00183 NULL, 00184 (LPBYTE)*lpAddress, 00185 &dwSize); 00186 if (lRet != ERROR_SUCCESS) 00187 goto fail; 00188 00189 RegCloseKey(hKey); 00190 00191 return TRUE; 00192 00193 fail: 00194 DisplayWin32Error(lRet); 00195 if (hKey) 00196 RegCloseKey(hKey); 00197 HeapFree(GetProcessHeap(), 0, *lpAddress); 00198 return FALSE; 00199 } 00200 00201 00202 /* Request the time from the current NTP server */ 00203 static ULONG 00204 GetTimeFromServer(VOID) 00205 { 00206 LPWSTR lpAddress = NULL; 00207 ULONG ulTime = 0; 00208 00209 if (GetNTPServerAddress(&lpAddress)) 00210 { 00211 ulTime = GetServerTime(lpAddress); 00212 00213 HeapFree(GetProcessHeap(), 00214 0, 00215 lpAddress); 00216 } 00217 00218 return ulTime; 00219 } 00220 00221 /* 00222 * NTP servers state the number of seconds passed since 00223 * 1st Jan, 1900. The time returned from the server 00224 * needs adding to that date to get the current Gregorian time 00225 */ 00226 static VOID 00227 UpdateSystemTime(ULONG ulTime) 00228 { 00229 FILETIME ftNew; 00230 LARGE_INTEGER li; 00231 SYSTEMTIME stNew; 00232 00233 /* Time at 1st Jan 1900 */ 00234 stNew.wYear = 1900; 00235 stNew.wMonth = 1; 00236 stNew.wDay = 1; 00237 stNew.wHour = 0; 00238 stNew.wMinute = 0; 00239 stNew.wSecond = 0; 00240 stNew.wMilliseconds = 0; 00241 00242 /* Convert to a file time */ 00243 if (!SystemTimeToFileTime(&stNew, &ftNew)) 00244 { 00245 DisplayWin32Error(GetLastError()); 00246 return; 00247 } 00248 00249 /* Add on the time passed since 1st Jan 1900 */ 00250 li = *(LARGE_INTEGER *)&ftNew; 00251 li.QuadPart += (LONGLONG)10000000 * ulTime; 00252 ftNew = * (FILETIME *)&li; 00253 00254 /* Convert back to a system time */ 00255 if (!FileTimeToSystemTime(&ftNew, &stNew)) 00256 { 00257 DisplayWin32Error(GetLastError()); 00258 return; 00259 } 00260 00261 if (!SystemSetLocalTime(&stNew)) 00262 DisplayWin32Error(GetLastError()); 00263 } 00264 00265 00266 static VOID 00267 EnableDialogText(HWND hwnd) 00268 { 00269 BOOL bChecked; 00270 UINT uCheck; 00271 00272 uCheck = (UINT)SendDlgItemMessageW(hwnd, IDC_AUTOSYNC, BM_GETCHECK, 0, 0); 00273 bChecked = (uCheck == BST_CHECKED) ? TRUE : FALSE; 00274 00275 EnableWindow(GetDlgItem(hwnd, IDC_SERVERTEXT), bChecked); 00276 EnableWindow(GetDlgItem(hwnd, IDC_SERVERLIST), bChecked); 00277 EnableWindow(GetDlgItem(hwnd, IDC_UPDATEBUTTON), bChecked); 00278 EnableWindow(GetDlgItem(hwnd, IDC_SUCSYNC), bChecked); 00279 EnableWindow(GetDlgItem(hwnd, IDC_NEXTSYNC), bChecked); 00280 } 00281 00282 00283 static VOID 00284 GetSyncSetting(HWND hwnd) 00285 { 00286 HKEY hKey; 00287 WCHAR szData[8]; 00288 DWORD dwSize; 00289 00290 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, 00291 L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Parameters", 00292 0, 00293 KEY_QUERY_VALUE, 00294 &hKey) == ERROR_SUCCESS) 00295 { 00296 dwSize = 8 * sizeof(WCHAR); 00297 if (RegQueryValueExW(hKey, 00298 L"Type", 00299 NULL, 00300 NULL, 00301 (LPBYTE)szData, 00302 &dwSize) == ERROR_SUCCESS) 00303 { 00304 if (wcscmp(szData, L"NTP") == 0) 00305 SendDlgItemMessageW(hwnd, IDC_AUTOSYNC, BM_SETCHECK, 0, 0); 00306 } 00307 00308 RegCloseKey(hKey); 00309 } 00310 } 00311 00312 00313 static VOID 00314 OnInitDialog(HWND hwnd) 00315 { 00316 GetSyncSetting(hwnd); 00317 EnableDialogText(hwnd); 00318 CreateNTPServerList(hwnd); 00319 } 00320 00321 00322 /* Property page dialog callback */ 00323 INT_PTR CALLBACK 00324 InetTimePageProc(HWND hwndDlg, 00325 UINT uMsg, 00326 WPARAM wParam, 00327 LPARAM lParam) 00328 { 00329 switch (uMsg) 00330 { 00331 case WM_INITDIALOG: 00332 OnInitDialog(hwndDlg); 00333 break; 00334 00335 case WM_COMMAND: 00336 switch(LOWORD(wParam)) 00337 { 00338 case IDC_UPDATEBUTTON: 00339 { 00340 ULONG ulTime; 00341 00342 SetNTPServer(hwndDlg); 00343 00344 ulTime = GetTimeFromServer(); 00345 if (ulTime != 0) 00346 UpdateSystemTime(ulTime); 00347 } 00348 break; 00349 00350 case IDC_SERVERLIST: 00351 if (HIWORD(wParam) == CBN_SELCHANGE) 00352 { 00353 /* Enable the 'Apply' button */ 00354 PropSheet_Changed(GetParent(hwndDlg), hwndDlg); 00355 } 00356 break; 00357 00358 case IDC_AUTOSYNC: 00359 if (HIWORD(wParam) == BN_CLICKED) 00360 { 00361 EnableDialogText(hwndDlg); 00362 00363 /* Enable the 'Apply' button */ 00364 PropSheet_Changed(GetParent(hwndDlg), hwndDlg); 00365 } 00366 break; 00367 } 00368 break; 00369 00370 case WM_DESTROY: 00371 break; 00372 00373 case WM_NOTIFY: 00374 { 00375 LPNMHDR lpnm = (LPNMHDR)lParam; 00376 00377 switch (lpnm->code) 00378 { 00379 case PSN_APPLY: 00380 SetNTPServer(hwndDlg); 00381 return TRUE; 00382 00383 default: 00384 break; 00385 } 00386 } 00387 break; 00388 } 00389 00390 return FALSE; 00391 } Generated on Sun May 27 2012 04:21:01 for ReactOS by
1.7.6.1
|