[BTRFS]
[reactos.git] / reactos / base / applications / mscutils / servman / propsheet_general.c
1 /*
2 * PROJECT: ReactOS Services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/mscutils/servman/propsheet_general.c
5 * PURPOSE: Property dialog box message handler
6 * COPYRIGHT: Copyright 2006-2009 Ged Murphy <gedmurphy@reactos.org>
7 *
8 */
9
10 #include "precomp.h"
11
12 static VOID
13 SetButtonStates(PSERVICEPROPSHEET dlgInfo,
14 HWND hwndDlg)
15 {
16 HWND hButton;
17 LPQUERY_SERVICE_CONFIG lpServiceConfig;
18 DWORD Flags, State;
19 UINT i;
20
21 Flags = dlgInfo->pService->ServiceStatusProcess.dwControlsAccepted;
22 State = dlgInfo->pService->ServiceStatusProcess.dwCurrentState;
23
24 for (i = IDC_START; i <= IDC_RESUME; i++)
25 {
26 hButton = GetDlgItem(hwndDlg, i);
27 EnableWindow (hButton, FALSE);
28 }
29
30 lpServiceConfig = GetServiceConfig(dlgInfo->pService->lpServiceName);
31 if (State == SERVICE_STOPPED &&
32 lpServiceConfig && lpServiceConfig->dwStartType != SERVICE_DISABLED)
33 {
34 hButton = GetDlgItem(hwndDlg, IDC_START);
35 EnableWindow (hButton, TRUE);
36 }
37 else if ( (Flags & SERVICE_ACCEPT_STOP) && (State == SERVICE_RUNNING) )
38 {
39 hButton = GetDlgItem(hwndDlg, IDC_STOP);
40 EnableWindow (hButton, TRUE);
41 }
42 else if ( (Flags & SERVICE_ACCEPT_PAUSE_CONTINUE) && (State == SERVICE_RUNNING) )
43 {
44 hButton = GetDlgItem(hwndDlg, IDC_PAUSE);
45 EnableWindow (hButton, TRUE);
46 }
47
48 if(lpServiceConfig)
49 HeapFree(GetProcessHeap(), 0, lpServiceConfig);
50
51 hButton = GetDlgItem(hwndDlg, IDC_START_PARAM);
52 EnableWindow(hButton, (State == SERVICE_STOPPED));
53
54 /* set the main toolbar */
55 SetMenuAndButtonStates(dlgInfo->Info);
56 }
57
58 static VOID
59 SetServiceStatusText(PSERVICEPROPSHEET dlgInfo,
60 HWND hwndDlg)
61 {
62 LPWSTR lpStatus;
63 UINT id;
64
65 if (dlgInfo->pService->ServiceStatusProcess.dwCurrentState == SERVICE_RUNNING)
66 {
67 id = IDS_SERVICES_STARTED;
68 }
69 else
70 {
71 id = IDS_SERVICES_STOPPED;
72 }
73
74 if (AllocAndLoadString(&lpStatus,
75 hInstance,
76 id))
77 {
78 SendDlgItemMessageW(hwndDlg,
79 IDC_SERV_STATUS,
80 WM_SETTEXT,
81 0,
82 (LPARAM)lpStatus);
83 LocalFree(lpStatus);
84 }
85 }
86
87 /*
88 * Fills the 'startup type' combo box with possible
89 * values and sets it to value of the selected item
90 */
91 static VOID
92 SetStartupType(LPWSTR lpServiceName,
93 HWND hwndDlg)
94 {
95 HWND hList;
96 LPQUERY_SERVICE_CONFIG pServiceConfig;
97 LPWSTR lpBuf;
98 DWORD StartUp = 0;
99 UINT i;
100
101 hList = GetDlgItem(hwndDlg, IDC_START_TYPE);
102
103 for (i = IDS_SERVICES_AUTO; i <= IDS_SERVICES_DIS; i++)
104 {
105 if (AllocAndLoadString(&lpBuf,
106 hInstance,
107 i))
108 {
109 SendMessageW(hList,
110 CB_ADDSTRING,
111 0,
112 (LPARAM)lpBuf);
113 LocalFree(lpBuf);
114 }
115 }
116
117 pServiceConfig = GetServiceConfig(lpServiceName);
118
119 if (pServiceConfig)
120 {
121 switch (pServiceConfig->dwStartType)
122 {
123 case SERVICE_AUTO_START: StartUp = 0; break;
124 case SERVICE_DEMAND_START: StartUp = 1; break;
125 case SERVICE_DISABLED: StartUp = 2; break;
126 }
127
128 SendMessageW(hList,
129 CB_SETCURSEL,
130 StartUp,
131 0);
132
133 HeapFree(ProcessHeap,
134 0,
135 pServiceConfig);
136 }
137 }
138
139 /*
140 * Populates the General Properties dialog with
141 * the relevant service information
142 */
143 static VOID
144 InitGeneralPage(PSERVICEPROPSHEET dlgInfo,
145 HWND hwndDlg)
146 {
147 LPQUERY_SERVICE_CONFIG pServiceConfig;
148 LPWSTR lpDescription;
149
150 /* set the service name */
151 SendDlgItemMessageW(hwndDlg,
152 IDC_SERV_NAME,
153 WM_SETTEXT,
154 0,
155 (LPARAM)dlgInfo->pService->lpServiceName);
156
157 /* set the display name */
158 SendDlgItemMessageW(hwndDlg,
159 IDC_DISP_NAME,
160 WM_SETTEXT,
161 0,
162 (LPARAM)dlgInfo->pService->lpDisplayName);
163
164 /* set the description */
165 if ((lpDescription = GetServiceDescription(dlgInfo->pService->lpServiceName)))
166 {
167 SendDlgItemMessageW(hwndDlg,
168 IDC_DESCRIPTION,
169 WM_SETTEXT,
170 0,
171 (LPARAM)lpDescription);
172
173 HeapFree(ProcessHeap,
174 0,
175 lpDescription);
176 }
177
178 pServiceConfig = GetServiceConfig(dlgInfo->pService->lpServiceName);
179 if (pServiceConfig)
180 {
181 SendDlgItemMessageW(hwndDlg,
182 IDC_EXEPATH,
183 WM_SETTEXT,
184 0,
185 (LPARAM)pServiceConfig->lpBinaryPathName);
186 HeapFree(ProcessHeap,
187 0,
188 pServiceConfig);
189 }
190
191
192 /* set startup type */
193 SetStartupType(dlgInfo->pService->lpServiceName, hwndDlg);
194
195 SetServiceStatusText(dlgInfo,
196 hwndDlg);
197
198 if (dlgInfo->Info->bIsUserAnAdmin)
199 {
200 HWND hEdit = GetDlgItem(hwndDlg,
201 IDC_EDIT);
202 EnableWindow(hEdit,
203 TRUE);
204 }
205 }
206
207 VOID
208 SaveDlgInfo(PSERVICEPROPSHEET dlgInfo,
209 HWND hwndDlg)
210 {
211 LPQUERY_SERVICE_CONFIG pServiceConfig = NULL;
212 HWND hList;
213 DWORD StartUp;
214
215 pServiceConfig = HeapAlloc(ProcessHeap,
216 HEAP_ZERO_MEMORY,
217 sizeof(*pServiceConfig));
218 if (pServiceConfig)
219 {
220 pServiceConfig->dwServiceType = SERVICE_NO_CHANGE;
221 pServiceConfig->dwErrorControl = SERVICE_NO_CHANGE;
222
223 hList = GetDlgItem(hwndDlg, IDC_START_TYPE);
224 StartUp = SendMessage(hList,
225 CB_GETCURSEL,
226 0,
227 0);
228 switch (StartUp)
229 {
230 case 0: pServiceConfig->dwStartType = SERVICE_AUTO_START; break;
231 case 1: pServiceConfig->dwStartType = SERVICE_DEMAND_START; break;
232 case 2: pServiceConfig->dwStartType = SERVICE_DISABLED; break;
233 }
234
235 if (SetServiceConfig(pServiceConfig,
236 dlgInfo->pService->lpServiceName,
237 NULL))
238 {
239 ChangeListViewText(dlgInfo->Info,
240 dlgInfo->pService,
241 LVSTARTUP);
242 }
243
244 HeapFree(ProcessHeap,
245 0,
246 pServiceConfig);
247 }
248 }
249
250 /*
251 * General Property dialog callback.
252 * Controls messages to the General dialog
253 */
254 INT_PTR CALLBACK
255 GeneralPageProc(HWND hwndDlg,
256 UINT uMsg,
257 WPARAM wParam,
258 LPARAM lParam)
259 {
260 PSERVICEPROPSHEET dlgInfo;
261
262 /* Get the window context */
263 dlgInfo = (PSERVICEPROPSHEET)GetWindowLongPtr(hwndDlg,
264 GWLP_USERDATA);
265 if (dlgInfo == NULL && uMsg != WM_INITDIALOG)
266 {
267 return FALSE;
268 }
269
270 switch (uMsg)
271 {
272 case WM_INITDIALOG:
273 {
274 dlgInfo = (PSERVICEPROPSHEET)(((LPPROPSHEETPAGE)lParam)->lParam);
275 if (dlgInfo != NULL)
276 {
277 SetWindowLongPtr(hwndDlg,
278 GWLP_USERDATA,
279 (LONG_PTR)dlgInfo);
280 InitGeneralPage(dlgInfo, hwndDlg);
281 SetButtonStates(dlgInfo, hwndDlg);
282 }
283 }
284 break;
285
286 case WM_COMMAND:
287 switch(LOWORD(wParam))
288 {
289 case IDC_START_TYPE:
290 if (HIWORD(wParam) == CBN_SELCHANGE)
291 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
292 break;
293
294 case IDC_START:
295 {
296 WCHAR szStartParams[256];
297 LPWSTR lpStartParams = NULL;
298
299 if (GetDlgItemText(hwndDlg, IDC_START_PARAM, szStartParams, 256) > 0)
300 lpStartParams = szStartParams;
301
302 RunActionWithProgress(hwndDlg,
303 dlgInfo->pService->lpServiceName,
304 dlgInfo->pService->lpDisplayName,
305 ACTION_START,
306 lpStartParams);
307
308 UpdateServiceStatus(dlgInfo->pService);
309 ChangeListViewText(dlgInfo->Info, dlgInfo->pService, LVSTATUS);
310 SetButtonStates(dlgInfo, hwndDlg);
311 SetServiceStatusText(dlgInfo, hwndDlg);
312 break;
313 }
314
315 case IDC_STOP:
316 RunActionWithProgress(hwndDlg,
317 dlgInfo->pService->lpServiceName,
318 dlgInfo->pService->lpDisplayName,
319 ACTION_STOP,
320 NULL);
321
322 UpdateServiceStatus(dlgInfo->pService);
323 ChangeListViewText(dlgInfo->Info, dlgInfo->pService, LVSTATUS);
324 SetButtonStates(dlgInfo, hwndDlg);
325 SetServiceStatusText(dlgInfo, hwndDlg);
326 break;
327
328 case IDC_PAUSE:
329 RunActionWithProgress(hwndDlg,
330 dlgInfo->pService->lpServiceName,
331 dlgInfo->pService->lpDisplayName,
332 ACTION_PAUSE,
333 NULL);
334
335 UpdateServiceStatus(dlgInfo->pService);
336 ChangeListViewText(dlgInfo->Info, dlgInfo->pService, LVSTATUS);
337 SetButtonStates(dlgInfo, hwndDlg);
338 SetServiceStatusText(dlgInfo, hwndDlg);
339 break;
340
341 case IDC_RESUME:
342 RunActionWithProgress(hwndDlg,
343 dlgInfo->pService->lpServiceName,
344 dlgInfo->pService->lpDisplayName,
345 ACTION_RESUME,
346 NULL);
347
348 UpdateServiceStatus(dlgInfo->pService);
349 ChangeListViewText(dlgInfo->Info, dlgInfo->pService, LVSTATUS);
350 SetButtonStates(dlgInfo, hwndDlg);
351 SetServiceStatusText(dlgInfo, hwndDlg);
352 break;
353
354 case IDC_EDIT:
355 {
356 HWND hName, hDesc, hExePath;
357
358 hName = GetDlgItem(hwndDlg, IDC_DISP_NAME);
359 hDesc = GetDlgItem(hwndDlg, IDC_DESCRIPTION);
360 hExePath = GetDlgItem(hwndDlg, IDC_EXEPATH);
361
362 SendMessage(hName, EM_SETREADONLY, FALSE, 0);
363 SendMessage(hDesc, EM_SETREADONLY, FALSE, 0);
364 SendMessage(hExePath, EM_SETREADONLY, FALSE, 0);
365 break;
366 }
367
368 case IDC_START_PARAM:
369 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
370 break;
371 }
372 break;
373
374 case WM_NOTIFY:
375 {
376 LPNMHDR lpnm = (LPNMHDR)lParam;
377
378 switch (lpnm->code)
379 {
380 case PSN_APPLY:
381 SaveDlgInfo(dlgInfo, hwndDlg);
382 SetButtonStates(dlgInfo, hwndDlg);
383 break;
384 }
385 }
386 break;
387 }
388
389 return FALSE;
390 }