Lars Martin Hambro <lars_martin4 AT hotmail DOT com>
[reactos.git] / reactos / base / applications / mscutils / servman / propsheet.c
1 /*
2 * PROJECT: ReactOS Services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/mscutils/servman/propsheet.c
5 * PURPOSE: Property dialog box message handler
6 * COPYRIGHT: Copyright 2006-2007 Ged Murphy <gedmurphy@reactos.org>
7 *
8 */
9
10 #include "precomp.h"
11
12 typedef struct _SERVICEPROPSHEET
13 {
14 PMAIN_WND_INFO Info;
15 ENUM_SERVICE_STATUS_PROCESS *pService;
16 } SERVICEPROPSHEET, *PSERVICEPROPSHEET;
17
18
19 static VOID
20 SetButtonStates(PSERVICEPROPSHEET dlgInfo,
21 HWND hwndDlg)
22 {
23 HWND hButton;
24 LPQUERY_SERVICE_CONFIG lpServiceConfig;
25 DWORD Flags, State;
26 UINT i;
27
28 Flags = dlgInfo->pService->ServiceStatusProcess.dwControlsAccepted;
29 State = dlgInfo->pService->ServiceStatusProcess.dwCurrentState;
30
31 for (i = IDC_START; i <= IDC_RESUME; i++)
32 {
33 hButton = GetDlgItem(hwndDlg, i);
34 EnableWindow (hButton, FALSE);
35 }
36
37 lpServiceConfig = GetServiceConfig(dlgInfo->pService->lpServiceName);
38 if (State == SERVICE_STOPPED &&
39 lpServiceConfig && lpServiceConfig->dwStartType != SERVICE_DISABLED)
40 {
41 hButton = GetDlgItem(hwndDlg, IDC_START);
42 EnableWindow (hButton, TRUE);
43 HeapFree(GetProcessHeap(), 0, lpServiceConfig);
44 }
45 else if ( (Flags & SERVICE_ACCEPT_STOP) && (State == SERVICE_RUNNING) )
46 {
47 hButton = GetDlgItem(hwndDlg, IDC_STOP);
48 EnableWindow (hButton, TRUE);
49 }
50 else if ( (Flags & SERVICE_ACCEPT_PAUSE_CONTINUE) && (State == SERVICE_RUNNING) )
51 {
52 hButton = GetDlgItem(hwndDlg, IDC_PAUSE);
53 EnableWindow (hButton, TRUE);
54 }
55
56 /* set the main toolbar */
57 SetMenuAndButtonStates(dlgInfo->Info);
58 }
59
60
61 static VOID
62 SetServiceStatusText(PSERVICEPROPSHEET dlgInfo,
63 HWND hwndDlg)
64 {
65 LPTSTR lpStatus;
66 UINT id;
67
68 if (dlgInfo->pService->ServiceStatusProcess.dwCurrentState == SERVICE_RUNNING)
69 {
70 id = IDS_SERVICES_STARTED;
71 }
72 else
73 {
74 id = IDS_SERVICES_STOPPED;
75 }
76
77 if (AllocAndLoadString(&lpStatus,
78 hInstance,
79 id))
80 {
81 SendDlgItemMessage(hwndDlg,
82 IDC_SERV_STATUS,
83 WM_SETTEXT,
84 0,
85 (LPARAM)lpStatus);
86 LocalFree(lpStatus);
87 }
88 }
89
90 /*
91 * Fills the 'startup type' combo box with possible
92 * values and sets it to value of the selected item
93 */
94 static VOID
95 SetStartupType(LPTSTR lpServiceName,
96 HWND hwndDlg)
97 {
98 HWND hList;
99 LPQUERY_SERVICE_CONFIG pServiceConfig;
100 LPTSTR lpBuf;
101 DWORD StartUp = 0;
102 UINT i;
103
104 hList = GetDlgItem(hwndDlg, IDC_START_TYPE);
105
106 for (i = IDS_SERVICES_AUTO; i <= IDS_SERVICES_DIS; i++)
107 {
108 if (AllocAndLoadString(&lpBuf,
109 hInstance,
110 i))
111 {
112 SendMessage(hList,
113 CB_ADDSTRING,
114 0,
115 (LPARAM)lpBuf);
116 LocalFree(lpBuf);
117 }
118 }
119
120 pServiceConfig = GetServiceConfig(lpServiceName);
121
122 if (pServiceConfig)
123 {
124 switch (pServiceConfig->dwStartType)
125 {
126 case SERVICE_AUTO_START: StartUp = 0; break;
127 case SERVICE_DEMAND_START: StartUp = 1; break;
128 case SERVICE_DISABLED: StartUp = 2; break;
129 }
130
131 SendMessage(hList,
132 CB_SETCURSEL,
133 StartUp,
134 0);
135
136 HeapFree(ProcessHeap,
137 0,
138 pServiceConfig);
139 }
140 }
141
142
143 /*
144 * Populates the General Properties dialog with
145 * the relevant service information
146 */
147 static VOID
148 InitGeneralPage(PSERVICEPROPSHEET dlgInfo,
149 HWND hwndDlg)
150 {
151 LPQUERY_SERVICE_CONFIG pServiceConfig;
152 LPTSTR lpDescription;
153
154 /* set the service name */
155 SendDlgItemMessage(hwndDlg,
156 IDC_SERV_NAME,
157 WM_SETTEXT,
158 0,
159 (LPARAM)dlgInfo->pService->lpServiceName);
160
161 /* set the display name */
162 SendDlgItemMessage(hwndDlg,
163 IDC_DISP_NAME,
164 WM_SETTEXT,
165 0,
166 (LPARAM)dlgInfo->pService->lpDisplayName);
167
168 /* set the description */
169 if ((lpDescription = GetServiceDescription(dlgInfo->pService->lpServiceName)))
170 {
171 SendDlgItemMessage(hwndDlg,
172 IDC_DESCRIPTION,
173 WM_SETTEXT,
174 0,
175 (LPARAM)lpDescription);
176
177 HeapFree(ProcessHeap,
178 0,
179 lpDescription);
180 }
181
182 pServiceConfig = GetServiceConfig(dlgInfo->pService->lpServiceName);
183 if (pServiceConfig)
184 {
185 SendDlgItemMessage(hwndDlg,
186 IDC_EXEPATH,
187 WM_SETTEXT,
188 0,
189 (LPARAM)pServiceConfig->lpBinaryPathName);
190 HeapFree(ProcessHeap,
191 0,
192 pServiceConfig);
193 }
194
195
196 /* set startup type */
197 SetStartupType(dlgInfo->pService->lpServiceName, hwndDlg);
198
199 SetServiceStatusText(dlgInfo,
200 hwndDlg);
201
202 if (dlgInfo->Info->bIsUserAnAdmin)
203 {
204 HWND hEdit = GetDlgItem(hwndDlg,
205 IDC_EDIT);
206 EnableWindow(hEdit,
207 TRUE);
208 }
209 }
210
211
212 VOID
213 SaveDlgInfo(PSERVICEPROPSHEET dlgInfo,
214 HWND hwndDlg)
215 {
216 LPQUERY_SERVICE_CONFIG pServiceConfig = NULL;
217 HWND hList;
218 DWORD StartUp;
219
220 pServiceConfig = HeapAlloc(ProcessHeap,
221 HEAP_ZERO_MEMORY,
222 sizeof(*pServiceConfig));
223 if (pServiceConfig)
224 {
225 pServiceConfig->dwServiceType = SERVICE_NO_CHANGE;
226 pServiceConfig->dwErrorControl = SERVICE_NO_CHANGE;
227
228 hList = GetDlgItem(hwndDlg, IDC_START_TYPE);
229 StartUp = SendMessage(hList,
230 CB_GETCURSEL,
231 0,
232 0);
233 switch (StartUp)
234 {
235 case 0: pServiceConfig->dwStartType = SERVICE_AUTO_START; break;
236 case 1: pServiceConfig->dwStartType = SERVICE_DEMAND_START; break;
237 case 2: pServiceConfig->dwStartType = SERVICE_DISABLED; break;
238 }
239
240 if (SetServiceConfig(pServiceConfig,
241 dlgInfo->pService->lpServiceName,
242 NULL))
243 {
244 ChangeListViewText(dlgInfo->Info,
245 dlgInfo->pService,
246 LVSTARTUP);
247 }
248
249 HeapFree(ProcessHeap,
250 0,
251 pServiceConfig);
252 }
253 }
254
255
256 /*
257 * General Property dialog callback.
258 * Controls messages to the General dialog
259 */
260 static INT_PTR CALLBACK
261 GeneralPageProc(HWND hwndDlg,
262 UINT uMsg,
263 WPARAM wParam,
264 LPARAM lParam)
265 {
266 PSERVICEPROPSHEET dlgInfo;
267
268 /* Get the window context */
269 dlgInfo = (PSERVICEPROPSHEET)GetWindowLongPtr(hwndDlg,
270 GWLP_USERDATA);
271 if (dlgInfo == NULL && uMsg != WM_INITDIALOG)
272 {
273 return FALSE;
274 }
275
276 switch (uMsg)
277 {
278 case WM_INITDIALOG:
279 {
280 dlgInfo = (PSERVICEPROPSHEET)(((LPPROPSHEETPAGE)lParam)->lParam);
281 if (dlgInfo != NULL)
282 {
283 SetWindowLongPtr(hwndDlg,
284 GWLP_USERDATA,
285 (LONG_PTR)dlgInfo);
286 InitGeneralPage(dlgInfo, hwndDlg);
287 SetButtonStates(dlgInfo, hwndDlg);
288 }
289 }
290 break;
291
292 case WM_COMMAND:
293 switch(LOWORD(wParam))
294 {
295 case IDC_START_TYPE:
296 if (HIWORD(wParam) == CBN_SELCHANGE)
297 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
298 break;
299
300 case IDC_START:
301 if (DoStart(dlgInfo->Info))
302 {
303 UpdateServiceStatus(dlgInfo->pService);
304 ChangeListViewText(dlgInfo->Info, dlgInfo->pService, LVSTATUS);
305 SetButtonStates(dlgInfo, hwndDlg);
306 SetServiceStatusText(dlgInfo, hwndDlg);
307 }
308 break;
309
310 case IDC_STOP:
311 if (DoStop(dlgInfo->Info))
312 {
313 UpdateServiceStatus(dlgInfo->pService);
314 ChangeListViewText(dlgInfo->Info, dlgInfo->pService, LVSTATUS);
315 SetButtonStates(dlgInfo, hwndDlg);
316 SetServiceStatusText(dlgInfo, hwndDlg);
317 }
318 break;
319
320 case IDC_PAUSE:
321 if (DoPause(dlgInfo->Info))
322 {
323 UpdateServiceStatus(dlgInfo->pService);
324 ChangeListViewText(dlgInfo->Info, dlgInfo->pService, LVSTATUS);
325 SetButtonStates(dlgInfo, hwndDlg);
326 SetServiceStatusText(dlgInfo, hwndDlg);
327 }
328 break;
329
330 case IDC_RESUME:
331 if (DoResume(dlgInfo->Info))
332 {
333 UpdateServiceStatus(dlgInfo->pService);
334 ChangeListViewText(dlgInfo->Info, dlgInfo->pService, LVSTATUS);
335 SetButtonStates(dlgInfo, hwndDlg);
336 SetServiceStatusText(dlgInfo, hwndDlg);
337 }
338 break;
339
340 case IDC_EDIT:
341 {
342 HWND hName, hDesc, hExePath;
343
344 hName = GetDlgItem(hwndDlg, IDC_DISP_NAME);
345 hDesc = GetDlgItem(hwndDlg, IDC_DESCRIPTION);
346 hExePath = GetDlgItem(hwndDlg, IDC_EXEPATH);
347
348 SendMessage(hName, EM_SETREADONLY, FALSE, 0);
349 SendMessage(hDesc, EM_SETREADONLY, FALSE, 0);
350 SendMessage(hExePath, EM_SETREADONLY, FALSE, 0);
351 }
352 break;
353
354 case IDC_START_PARAM:
355 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
356 break;
357 }
358 break;
359
360 case WM_NOTIFY:
361 {
362 LPNMHDR lpnm = (LPNMHDR)lParam;
363
364 switch (lpnm->code)
365 {
366 case PSN_APPLY:
367 SaveDlgInfo(dlgInfo, hwndDlg);
368 SetButtonStates(dlgInfo, hwndDlg);
369 break;
370 }
371 }
372 break;
373 }
374
375 return FALSE;
376 }
377 /*
378 static VOID
379 InitDependPage(PSERVICEPROPSHEET dlgInfo,
380 HWND hwndDlg)
381 {
382
383
384 }
385
386
387
388 *
389 * Dependancies Property dialog callback.
390 * Controls messages to the Dependancies dialog
391 *
392 static INT_PTR CALLBACK
393 DependanciesPageProc(HWND hwndDlg,
394 UINT uMsg,
395 WPARAM wParam,
396 LPARAM lParam)
397 {
398 PSERVICEPROPSHEET dlgInfo;
399
400 dlgInfo = (PSERVICEPROPSHEET)GetWindowLongPtr(hwndDlg,
401 GWLP_USERDATA);
402
403 if (dlgInfo == NULL && uMsg != WM_INITDIALOG)
404 {
405 return FALSE;
406 }
407
408 switch (uMsg)
409 {
410 case WM_INITDIALOG:
411 {
412 dlgInfo = (PSERVICEPROPSHEET)(((LPPROPSHEETPAGE)lParam)->lParam);
413 if (dlgInfo != NULL)
414 {
415 SetWindowLongPtr(hwndDlg,
416 GWLP_USERDATA,
417 (LONG_PTR)dlgInfo);
418
419 InitDependPage(dlgInfo, hwndDlg);
420 }
421 }
422 break;
423
424 case WM_COMMAND:
425 switch(LOWORD(wParam))
426 {
427
428 }
429 break;
430 }
431
432 return FALSE;
433 }
434 */
435
436 static VOID
437 InitPropSheetPage(PROPSHEETPAGE *psp,
438 PSERVICEPROPSHEET dlgInfo,
439 WORD idDlg,
440 DLGPROC DlgProc)
441 {
442 ZeroMemory(psp, sizeof(PROPSHEETPAGE));
443 psp->dwSize = sizeof(PROPSHEETPAGE);
444 psp->dwFlags = PSP_DEFAULT;
445 psp->hInstance = hInstance;
446 psp->pszTemplate = MAKEINTRESOURCE(idDlg);
447 psp->pfnDlgProc = DlgProc;
448 psp->lParam = (LPARAM)dlgInfo;
449 }
450
451
452 LONG APIENTRY
453 OpenPropSheet(PMAIN_WND_INFO Info)
454 {
455 PROPSHEETHEADER psh;
456 PROPSHEETPAGE psp[1];
457 PSERVICEPROPSHEET pServicePropSheet;
458 LONG Ret = 0;
459
460 ZeroMemory(&psh, sizeof(PROPSHEETHEADER));
461 psh.dwSize = sizeof(PROPSHEETHEADER);
462 psh.dwFlags = PSH_PROPSHEETPAGE | PSH_PROPTITLE | PSH_USECALLBACK;// | PSH_MODELESS;
463 psh.hwndParent = Info->hMainWnd;
464 psh.hInstance = hInstance;
465 psh.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SM_ICON));
466 psh.pszCaption = Info->pCurrentService->lpDisplayName;
467 psh.nPages = sizeof(psp) / sizeof(PROPSHEETPAGE);
468 psh.nStartPage = 0;
469 psh.ppsp = psp;
470
471
472 pServicePropSheet = HeapAlloc(ProcessHeap,
473 0,
474 sizeof(*pServicePropSheet));
475 if (pServicePropSheet)
476 {
477 /* save current service, as it could change while the dialog is open */
478 pServicePropSheet->pService = Info->pCurrentService;
479 pServicePropSheet->Info = Info;
480
481 InitPropSheetPage(&psp[0], pServicePropSheet, IDD_DLG_GENERAL, GeneralPageProc);
482 //InitPropSheetPage(&psp[1], Info, IDD_DLG_GENERAL, LogonPageProc);
483 //InitPropSheetPage(&psp[2], Info, IDD_DLG_GENERAL, RecoveryPageProc);
484 //InitPropSheetPage(&psp[1], pServicePropSheet, IDD_DLG_DEPEND, DependanciesPageProc);
485
486 Ret = (LONG)(PropertySheet(&psh) != -1);
487
488 HeapFree(ProcessHeap,
489 0,
490 pServicePropSheet);
491 }
492
493 return Ret;
494 }
495