Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygensettings.c
Go to the documentation of this file.
00001 00002 #include <precomp.h> 00003 00004 /* update NUM_SETTINGS in precomp.h */ 00005 LPWSTR lpSettings[NUM_SETTINGS] = 00006 { 00007 L"desktopwidth", 00008 L"desktopheight", 00009 L"session bpp", 00010 L"full address", 00011 L"username", 00012 L"screen mode id", 00013 }; 00014 00015 VOID 00016 SaveAllSettings(PINFO pInfo) 00017 { 00018 INT ret; 00019 WCHAR szValue[MAXVALUE]; 00020 00021 /* server */ 00022 if (GetDlgItemText(pInfo->hGeneralPage, 00023 IDC_SERVERCOMBO, 00024 szValue, 00025 MAXVALUE)) 00026 { 00027 SetStringToSettings(pInfo->pRdpSettings, 00028 L"full address", 00029 szValue); 00030 } 00031 00032 /* resolution and fullscreen*/ 00033 ret = SendDlgItemMessage(pInfo->hDisplayPage, 00034 IDC_GEOSLIDER, 00035 TBM_GETPOS, 00036 0, 00037 0); 00038 if (ret != -1) 00039 { 00040 SetIntegerToSettings(pInfo->pRdpSettings, 00041 L"screen mode id", 00042 (ret == SendDlgItemMessageW(pInfo->hDisplayPage, IDC_GEOSLIDER, TBM_GETRANGEMAX, 0, 0)) ? 2 : 1); 00043 SetIntegerToSettings(pInfo->pRdpSettings, 00044 L"desktopwidth", 00045 pInfo->DisplayDeviceList->Resolutions[ret].dmPelsWidth); 00046 SetIntegerToSettings(pInfo->pRdpSettings, 00047 L"desktopheight", 00048 pInfo->DisplayDeviceList->Resolutions[ret].dmPelsHeight); 00049 } 00050 00051 /* bpp */ 00052 ret = SendDlgItemMessage(pInfo->hDisplayPage, 00053 IDC_BPPCOMBO, 00054 CB_GETCURSEL, 00055 0, 00056 0); 00057 if (ret != CB_ERR) 00058 { 00059 ret = SendDlgItemMessage(pInfo->hDisplayPage, 00060 IDC_BPPCOMBO, 00061 CB_GETITEMDATA, 00062 ret, 00063 0); 00064 if (ret != CB_ERR) 00065 { 00066 SetIntegerToSettings(pInfo->pRdpSettings, 00067 L"session bpp", 00068 ret); 00069 } 00070 } 00071 00072 /* user name */ 00073 if (GetDlgItemText(pInfo->hGeneralPage, 00074 IDC_NAMEEDIT, 00075 szValue, 00076 MAXVALUE)) 00077 { 00078 SetStringToSettings(pInfo->pRdpSettings, 00079 L"username", 00080 szValue); 00081 } 00082 } 00083 00084 00085 BOOL 00086 SetIntegerToSettings(PRDPSETTINGS pRdpSettings, 00087 LPWSTR lpKey, 00088 INT Value) 00089 { 00090 BOOL bRet = FALSE; 00091 00092 if (pRdpSettings) 00093 { 00094 INT i; 00095 00096 for (i = 0; i < pRdpSettings->NumSettings; i++) 00097 { 00098 if (wcscmp(pRdpSettings->pSettings[i].Key, lpKey) == 0) 00099 { 00100 if (pRdpSettings->pSettings[i].Type == 0) 00101 pRdpSettings->pSettings[i].Type = L'i'; 00102 00103 pRdpSettings->pSettings[i].Value.i = Value; 00104 bRet = TRUE; 00105 break; 00106 } 00107 } 00108 } 00109 00110 return bRet; 00111 } 00112 00113 00114 BOOL 00115 SetStringToSettings(PRDPSETTINGS pRdpSettings, 00116 LPWSTR lpKey, 00117 LPWSTR lpValue) 00118 { 00119 BOOL bRet = FALSE; 00120 00121 if (pRdpSettings) 00122 { 00123 INT i; 00124 00125 for (i = 0; i < pRdpSettings->NumSettings; i++) 00126 { 00127 if (wcscmp(pRdpSettings->pSettings[i].Key, lpKey) == 0) 00128 { 00129 if (pRdpSettings->pSettings[i].Type == 0) 00130 pRdpSettings->pSettings[i].Type = L's'; 00131 00132 wcscpy(pRdpSettings->pSettings[i].Value.s, lpValue); 00133 bRet = TRUE; 00134 break; 00135 } 00136 } 00137 } 00138 00139 return bRet; 00140 } 00141 00142 00143 INT 00144 GetIntegerFromSettings(PRDPSETTINGS pRdpSettings, 00145 LPWSTR lpKey) 00146 { 00147 INT Value = -1; 00148 00149 if (pRdpSettings) 00150 { 00151 INT i; 00152 00153 for (i = 0; i < pRdpSettings->NumSettings; i++) 00154 { 00155 if (wcscmp(pRdpSettings->pSettings[i].Key, lpKey) == 0) 00156 { 00157 if (pRdpSettings->pSettings[i].Type == L'i') 00158 { 00159 Value = pRdpSettings->pSettings[i].Value.i; 00160 break; 00161 } 00162 } 00163 } 00164 } 00165 00166 return Value; 00167 } 00168 00169 00170 LPWSTR 00171 GetStringFromSettings(PRDPSETTINGS pRdpSettings, 00172 LPWSTR lpKey) 00173 { 00174 LPWSTR lpValue = NULL; 00175 00176 if (pRdpSettings) 00177 { 00178 INT i; 00179 00180 for (i = 0; i < pRdpSettings->NumSettings; i++) 00181 { 00182 if (wcscmp(pRdpSettings->pSettings[i].Key, lpKey) == 0) 00183 { 00184 if (pRdpSettings->pSettings[i].Type == L's') 00185 { 00186 lpValue = pRdpSettings->pSettings[i].Value.s; 00187 break; 00188 } 00189 } 00190 } 00191 } 00192 00193 return lpValue; 00194 } 00195 00196 00197 static BOOL 00198 WriteRdpFile(HANDLE hFile, 00199 PRDPSETTINGS pRdpSettings) 00200 { 00201 WCHAR line[MAXKEY + MAXVALUE + 4]; 00202 DWORD BytesToWrite, BytesWritten; 00203 BOOL bRet; 00204 INT i, k; 00205 00206 for (i = 0; i < pRdpSettings->NumSettings; i++) 00207 { 00208 /* only write out values in the lpSettings struct */ 00209 for (k = 0; k < NUM_SETTINGS; k++) 00210 { 00211 if (wcscmp(lpSettings[k], pRdpSettings->pSettings[i].Key) == 0) 00212 { 00213 if (pRdpSettings->pSettings[i].Type == L'i') 00214 { 00215 _snwprintf(line, MAXKEY + MAXVALUE + 4, L"%s:i:%d\r\n", 00216 pRdpSettings->pSettings[i].Key, 00217 pRdpSettings->pSettings[i].Value.i); 00218 } 00219 else 00220 { 00221 _snwprintf(line, MAXKEY + MAXVALUE + 4, L"%s:s:%s\r\n", 00222 pRdpSettings->pSettings[i].Key, 00223 pRdpSettings->pSettings[i].Value.s); 00224 } 00225 00226 BytesToWrite = wcslen(line) * sizeof(WCHAR); 00227 00228 bRet = WriteFile(hFile, 00229 line, 00230 BytesToWrite, 00231 &BytesWritten, 00232 NULL); 00233 if (!bRet || BytesWritten == 0) 00234 return FALSE; 00235 } 00236 } 00237 } 00238 00239 return TRUE; 00240 } 00241 00242 00243 static VOID 00244 ParseSettings(PRDPSETTINGS pRdpSettings, 00245 LPWSTR lpBuffer) 00246 { 00247 LPWSTR lpStr = lpBuffer; 00248 WCHAR szSeps[] = L":\r\n"; 00249 WCHAR szNewline[] = L"\r\n"; 00250 LPWSTR lpToken; 00251 BOOL bFound; 00252 INT i; 00253 00254 /* move past unicode byte order */ 00255 if (lpStr[0] == 0xFEFF || lpStr[0] == 0xFFFE) 00256 lpStr += 1; 00257 00258 lpToken = wcstok(lpStr, szSeps); 00259 while (lpToken) 00260 { 00261 bFound = FALSE; 00262 00263 for (i = 0; i < pRdpSettings->NumSettings && !bFound; i++) 00264 { 00265 if (wcscmp(lpToken, pRdpSettings->pSettings[i].Key) == 0) 00266 { 00267 lpToken = wcstok(NULL, szSeps); 00268 if (lpToken[0] == L'i') 00269 { 00270 pRdpSettings->pSettings[i].Type = lpToken[0]; 00271 lpToken = wcstok(NULL, szSeps); 00272 if (lpToken != NULL) 00273 pRdpSettings->pSettings[i].Value.i = _wtoi(lpToken); 00274 } 00275 else if (lpToken[0] == L's') 00276 { 00277 pRdpSettings->pSettings[i].Type = lpToken[0]; 00278 lpToken = wcstok(NULL, szNewline); 00279 if (lpToken != NULL) 00280 wcscpy(pRdpSettings->pSettings[i].Value.s, lpToken); 00281 } 00282 bFound = TRUE; 00283 } 00284 } 00285 00286 /* move past the type and value */ 00287 if (!bFound) 00288 lpToken = wcstok(NULL, szNewline); 00289 00290 /* move to next key */ 00291 lpToken = wcstok(NULL, szSeps); 00292 } 00293 } 00294 00295 00296 static LPWSTR 00297 ReadRdpFile(HANDLE hFile) 00298 { 00299 LPWSTR lpBuffer = NULL; 00300 DWORD BytesToRead, BytesRead; 00301 BOOL bRes; 00302 00303 if (hFile) 00304 { 00305 BytesToRead = GetFileSize(hFile, NULL); 00306 if (BytesToRead) 00307 { 00308 lpBuffer = HeapAlloc(GetProcessHeap(), 00309 0, 00310 BytesToRead + 2); 00311 if (lpBuffer) 00312 { 00313 bRes = ReadFile(hFile, 00314 lpBuffer, 00315 BytesToRead, 00316 &BytesRead, 00317 NULL); 00318 if (bRes) 00319 { 00320 lpBuffer[BytesRead / 2] = 0; 00321 } 00322 else 00323 { 00324 HeapFree(GetProcessHeap(), 00325 0, 00326 lpBuffer); 00327 00328 lpBuffer = NULL; 00329 } 00330 } 00331 } 00332 } 00333 00334 return lpBuffer; 00335 } 00336 00337 00338 static HANDLE 00339 OpenRdpFile(LPWSTR path, BOOL bWrite) 00340 { 00341 HANDLE hFile = NULL; 00342 00343 if (path) 00344 { 00345 hFile = CreateFileW(path, 00346 bWrite ? GENERIC_WRITE : GENERIC_READ, 00347 0, 00348 NULL, 00349 bWrite ? CREATE_ALWAYS : OPEN_EXISTING, 00350 FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_HIDDEN, 00351 NULL); 00352 } 00353 00354 return hFile; 00355 } 00356 00357 00358 static VOID 00359 CloseRdpFile(HANDLE hFile) 00360 { 00361 if (hFile) 00362 CloseHandle(hFile); 00363 } 00364 00365 00366 BOOL 00367 SaveRdpSettingsToFile(LPWSTR lpFile, 00368 PRDPSETTINGS pRdpSettings) 00369 { 00370 WCHAR pszPath[MAX_PATH]; 00371 HANDLE hFile; 00372 BOOL bRet = FALSE; 00373 00374 /* use default file */ 00375 if (lpFile == NULL) 00376 { 00377 #ifndef __REACTOS__ 00378 HRESULT hr; 00379 LPITEMIDLIST lpidl= NULL; 00380 00381 hr = SHGetFolderLocation(NULL, 00382 CSIDL_PERSONAL, 00383 NULL, 00384 0, 00385 &lpidl); 00386 if (hr == S_OK) 00387 { 00388 if (SHGetPathFromIDListW(lpidl, pszPath)) 00389 { 00390 wcscat(pszPath, L"\\Default.rdp"); 00391 lpFile = pszPath; 00392 CoTaskMemFree(lpidl); 00393 } 00394 } 00395 #else 00396 wcscpy(pszPath, L"C:\\Default.rdp"); 00397 lpFile = pszPath; 00398 #endif 00399 } 00400 00401 if (lpFile) 00402 { 00403 hFile = OpenRdpFile(lpFile, TRUE); 00404 if (hFile) 00405 { 00406 if (WriteRdpFile(hFile, pRdpSettings)) 00407 { 00408 bRet = TRUE; 00409 } 00410 00411 CloseRdpFile(hFile); 00412 } 00413 } 00414 00415 return bRet; 00416 } 00417 00418 00419 BOOL 00420 LoadRdpSettingsFromFile(PRDPSETTINGS pRdpSettings, 00421 LPWSTR lpFile) 00422 { 00423 WCHAR pszPath[MAX_PATH]; 00424 HANDLE hFile; 00425 BOOL bRet = FALSE; 00426 00427 /* use default file */ 00428 if (lpFile == NULL) 00429 { 00430 #ifndef __REACTOS__ // remove when this is working 00431 HRESULT hr; 00432 LPITEMIDLIST lpidl= NULL; 00433 00434 hr = SHGetFolderLocation(NULL, 00435 CSIDL_PERSONAL, 00436 NULL, 00437 0, 00438 &lpidl); 00439 if (hr == S_OK) 00440 { 00441 if (SHGetPathFromIDListW(lpidl, pszPath)) 00442 { 00443 wcscat(pszPath, L"\\Default.rdp"); 00444 lpFile = pszPath; 00445 CoTaskMemFree(lpidl); 00446 } 00447 } 00448 #else 00449 wcscpy(pszPath, L"C:\\Default.rdp"); 00450 lpFile = pszPath; 00451 #endif 00452 } 00453 00454 if (lpFile) 00455 { 00456 LPWSTR lpBuffer = NULL; 00457 00458 hFile = OpenRdpFile(lpFile, FALSE); 00459 if (hFile) 00460 { 00461 lpBuffer = ReadRdpFile(hFile); 00462 if (lpBuffer) 00463 { 00464 ParseSettings(pRdpSettings, lpBuffer); 00465 00466 HeapFree(GetProcessHeap(), 00467 0, 00468 lpBuffer); 00469 00470 bRet = TRUE; 00471 } 00472 00473 CloseRdpFile(hFile); 00474 } 00475 } 00476 00477 return bRet; 00478 } 00479 00480 00481 BOOL 00482 InitRdpSettings(PRDPSETTINGS pRdpSettings) 00483 { 00484 BOOL bRet = FALSE; 00485 00486 pRdpSettings->pSettings = HeapAlloc(GetProcessHeap(), 00487 0, 00488 sizeof(SETTINGS) * NUM_SETTINGS); 00489 if (pRdpSettings->pSettings) 00490 { 00491 INT i; 00492 00493 for (i = 0; i < NUM_SETTINGS; i++) 00494 { 00495 wcscpy(pRdpSettings->pSettings[i].Key, lpSettings[i]); 00496 pRdpSettings->pSettings[i].Type = (WCHAR)0; 00497 pRdpSettings->pSettings[i].Value.i = 0; 00498 } 00499 00500 pRdpSettings->NumSettings = NUM_SETTINGS; 00501 00502 bRet = TRUE; 00503 } 00504 00505 return bRet; 00506 } Generated on Sun May 27 2012 04:16:29 for ReactOS by
1.7.6.1
|