Sync with trunk r58687.
[reactos.git] / dll / cpl / sysdm / hardprof.c
1 /*
2 * PROJECT: ReactOS System Control Panel Applet
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/cpl/sysdm/hardprof.c
5 * PURPOSE: Modify hardware profiles
6 * COPYRIGHT: Copyright 2006 Ged Murphy <gedmurphy@gmail.com>
7 *
8 */
9
10 #include "precomp.h"
11 #include <debug.h>
12
13 typedef struct _PROFILE
14 {
15 WCHAR szFriendlyName[256];
16 } PROFILE, *PPROFILE;
17
18 typedef struct _PROFILEDATA
19 {
20 DWORD dwProfileCount;
21 PPROFILE pProfiles;
22 } PROFILEDATA, *PPROFILEDATA;
23
24
25 static
26 INT_PTR
27 CALLBACK
28 CopyProfileDlgProc(HWND hwndDlg,
29 UINT uMsg,
30 WPARAM wParam,
31 LPARAM lParam)
32 {
33 UNREFERENCED_PARAMETER(lParam);
34 UNREFERENCED_PARAMETER(wParam);
35 UNREFERENCED_PARAMETER(hwndDlg);
36
37 switch (uMsg)
38 {
39 case WM_COMMAND:
40 if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
41 {
42 EndDialog(hwndDlg,
43 LOWORD(wParam));
44 return TRUE;
45 }
46 break;
47 }
48 return FALSE;
49 }
50
51
52 static
53 INT_PTR
54 CALLBACK
55 RenameProfileDlgProc(HWND hwndDlg,
56 UINT uMsg,
57 WPARAM wParam,
58 LPARAM lParam)
59 {
60 UNREFERENCED_PARAMETER(lParam);
61 UNREFERENCED_PARAMETER(wParam);
62 UNREFERENCED_PARAMETER(hwndDlg);
63
64 switch (uMsg)
65 {
66 case WM_COMMAND:
67 if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
68 {
69 EndDialog(hwndDlg,
70 LOWORD(wParam));
71 return TRUE;
72 }
73 break;
74 }
75 return FALSE;
76 }
77
78
79 static
80 DWORD
81 GetUserWaitInterval(VOID)
82 {
83 DWORD dwWaitInterval = 30;
84 DWORD dwSize;
85 HKEY hKey;
86
87 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
88 L"System\\CurrentControlSet\\Control\\IDConfigDB",
89 0,
90 KEY_QUERY_VALUE,
91 &hKey))
92 return dwWaitInterval;
93
94 dwSize = sizeof(DWORD);
95 RegQueryValueExW(hKey,
96 L"UserWaitInterval",
97 NULL,
98 NULL,
99 (LPBYTE)&dwWaitInterval,
100 &dwSize);
101
102 RegCloseKey(hKey);
103
104 return dwWaitInterval;
105 }
106
107
108 static
109 VOID
110 SetUserWaitInterval(DWORD dwWaitInterval)
111 {
112 HKEY hKey;
113
114 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
115 L"System\\CurrentControlSet\\Control\\IDConfigDB",
116 0,
117 KEY_SET_VALUE,
118 &hKey))
119 return;
120
121 RegSetValueExW(hKey,
122 L"UserWaitInterval",
123 0,
124 REG_DWORD,
125 (LPBYTE)&dwWaitInterval,
126 sizeof(DWORD));
127
128 RegCloseKey(hKey);
129 }
130
131
132 static
133 BOOL
134 GetProfileCount(LPDWORD lpProfileCount)
135 {
136 HKEY hKey;
137 LONG lError;
138
139 *lpProfileCount = 0;
140
141 lError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
142 L"System\\CurrentControlSet\\Control\\IDConfigDB\\Hardware Profiles",
143 0,
144 KEY_READ,
145 &hKey);
146 if (lError != ERROR_SUCCESS)
147 return FALSE;
148
149 lError = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, lpProfileCount,
150 NULL, NULL, NULL, NULL, NULL, NULL, NULL);
151
152 RegCloseKey(hKey);
153
154 if (lError != ERROR_SUCCESS)
155 return FALSE;
156
157 return TRUE;
158 }
159
160
161 static
162 VOID
163 GetProfile(HWND hwndDlg, HKEY hKey, LPWSTR lpName, PPROFILE pProfile)
164 {
165 HKEY hProfileKey;
166 DWORD dwSize;
167 LONG lError;
168
169 lError = RegOpenKeyExW(hKey,
170 lpName,
171 0,
172 KEY_READ,
173 &hProfileKey);
174 if (lError != ERROR_SUCCESS)
175 return;
176
177 dwSize = 256 * sizeof(WCHAR);
178 lError = RegQueryValueExW(hProfileKey,
179 L"FriendlyName",
180 NULL,
181 NULL,
182 (LPBYTE)pProfile->szFriendlyName,
183 &dwSize);
184 if (lError == ERROR_SUCCESS)
185 {
186 DPRINT1("Profile: %S\n", pProfile->szFriendlyName);
187 SendDlgItemMessageW(hwndDlg, IDC_HRDPROFLSTBOX, LB_ADDSTRING, 0, (LPARAM)pProfile->szFriendlyName);
188 }
189
190 RegCloseKey(hProfileKey);
191 }
192
193
194 static
195 BOOL
196 GetProfiles(HWND hwndDlg)
197 {
198 PPROFILEDATA pProfileData;
199 WCHAR szName[8];
200 DWORD dwNameLength;
201 DWORD dwIndex = 0;
202 HKEY hKey;
203 LONG lError;
204
205 pProfileData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(PROFILEDATA));
206 if (pProfileData == NULL)
207 return FALSE;
208
209 if (!GetProfileCount(&pProfileData->dwProfileCount))
210 {
211 HeapFree(GetProcessHeap(), 0, pProfileData);
212 return FALSE;
213 }
214
215 pProfileData->pProfiles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
216 pProfileData->dwProfileCount * sizeof(PROFILE));
217 if (pProfileData->pProfiles == NULL)
218 {
219 HeapFree(GetProcessHeap(), 0, pProfileData);
220 return FALSE;
221 }
222
223 SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pProfileData);
224
225 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
226 L"System\\CurrentControlSet\\Control\\IDConfigDB\\Hardware Profiles",
227 0,
228 KEY_READ,
229 &hKey) != ERROR_SUCCESS)
230 return FALSE;
231
232 for (dwIndex = 0; dwIndex < pProfileData->dwProfileCount; dwIndex++)
233 {
234 dwNameLength = 8;
235 lError = RegEnumKeyExW(hKey,
236 dwIndex,
237 szName,
238 &dwNameLength,
239 NULL,
240 NULL,
241 NULL,
242 NULL);
243 if (lError != ERROR_SUCCESS)
244 break;
245
246 DPRINT("Profile name: %S\n", szName);
247
248 GetProfile(hwndDlg, hKey, szName, &pProfileData->pProfiles[dwIndex]);
249 }
250
251 RegCloseKey(hKey);
252
253 return TRUE;
254 }
255
256
257 static
258 BOOL
259 OnInitDialog(HWND hwndDlg)
260 {
261 DWORD dwWaitInterval;
262
263 DPRINT("OnInitDialog()\n");
264
265 SendMessage(GetDlgItem(hwndDlg, IDC_HRDPROFUP),
266 BM_SETIMAGE,(WPARAM)IMAGE_ICON,
267 (LPARAM)(HANDLE)LoadIcon(hApplet, MAKEINTRESOURCE(IDI_UP)));
268 SendMessage(GetDlgItem(hwndDlg, IDC_HRDPROFDWN),
269 BM_SETIMAGE,(WPARAM)IMAGE_ICON,
270 (LPARAM)(HANDLE)LoadIcon(hApplet, MAKEINTRESOURCE(IDI_DOWN)));
271
272 if (!GetProfiles(hwndDlg))
273 return FALSE;
274
275 SendDlgItemMessageW(hwndDlg, IDC_HRDPROFUPDWN, UDM_SETRANGE, (WPARAM)0, (LPARAM)MAKELONG((SHORT)500, 0));
276
277 dwWaitInterval = GetUserWaitInterval();
278 if (dwWaitInterval == (DWORD)-1)
279 {
280 CheckDlgButton(hwndDlg, IDC_HRDPROFWAIT, BST_CHECKED);
281 SendDlgItemMessageW(hwndDlg, IDC_HRDPROFUPDWN, UDM_SETPOS, 0, 30);
282 EnableWindow(GetDlgItem(hwndDlg, IDC_HRDPROFEDIT), FALSE);
283 }
284 else
285 {
286 CheckDlgButton(hwndDlg, IDC_HRDPROFSELECT, BST_CHECKED);
287 SendDlgItemMessageW(hwndDlg, IDC_HRDPROFUPDWN, UDM_SETPOS, 0, dwWaitInterval);
288 }
289
290 return TRUE;
291 }
292
293
294 static
295 VOID
296 OnOk(HWND hwndDlg)
297 {
298 DWORD dwWaitInterval;
299
300 if (IsDlgButtonChecked(hwndDlg, IDC_HRDPROFWAIT) == BST_CHECKED)
301 {
302 dwWaitInterval = (DWORD)-1;
303 }
304 else
305 {
306 dwWaitInterval = LOWORD(SendDlgItemMessageW(hwndDlg, IDC_HRDPROFUPDWN, UDM_GETPOS, 0, 0));
307 }
308
309 SetUserWaitInterval(dwWaitInterval);
310 }
311
312
313 /* Property page dialog callback */
314 INT_PTR
315 CALLBACK
316 HardProfDlgProc(HWND hwndDlg,
317 UINT uMsg,
318 WPARAM wParam,
319 LPARAM lParam)
320 {
321 PPROFILEDATA pProfileData;
322
323 UNREFERENCED_PARAMETER(lParam);
324 UNREFERENCED_PARAMETER(wParam);
325 UNREFERENCED_PARAMETER(hwndDlg);
326
327 pProfileData = (PPROFILEDATA)GetWindowLongPtr(hwndDlg, DWLP_USER);
328
329 switch (uMsg)
330 {
331 case WM_INITDIALOG:
332 return OnInitDialog(hwndDlg);
333
334 case WM_DESTROY:
335 if (pProfileData != NULL)
336 {
337 if (pProfileData->pProfiles != NULL)
338 HeapFree(GetProcessHeap(), 0, pProfileData->pProfiles);
339 HeapFree(GetProcessHeap(), 0, pProfileData);
340 }
341 break;
342
343 case WM_COMMAND:
344 switch (LOWORD(wParam))
345 {
346 case IDC_HRDPROFCOPY:
347 DialogBox(hApplet,
348 MAKEINTRESOURCE(IDD_COPYPROFILE),
349 hwndDlg,
350 (DLGPROC)CopyProfileDlgProc);
351 break;
352
353 case IDC_HRDPROFRENAME:
354 DialogBox(hApplet,
355 MAKEINTRESOURCE(IDD_RENAMEPROFILE),
356 hwndDlg,
357 (DLGPROC)RenameProfileDlgProc);
358 break;
359
360 case IDC_HRDPROFWAIT:
361 EnableWindow(GetDlgItem(hwndDlg, IDC_HRDPROFEDIT), FALSE);
362 return TRUE;
363
364 case IDC_HRDPROFSELECT:
365 EnableWindow(GetDlgItem(hwndDlg, IDC_HRDPROFEDIT), TRUE);
366 return TRUE;
367
368 case IDOK:
369 OnOk(hwndDlg);
370
371 case IDCANCEL:
372 EndDialog(hwndDlg,
373 LOWORD(wParam));
374 return TRUE;
375 }
376 break;
377 }
378
379 return FALSE;
380 }