* Sync up to trunk head (r64939).
[reactos.git] / 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 LPTSTR 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 SendDlgItemMessage(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(LPTSTR lpServiceName,
93 HWND hwndDlg)
94 {
95 HWND hList;
96 LPQUERY_SERVICE_CONFIG pServiceConfig;
97 LPTSTR 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 SendMessage(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 SendMessage(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 LPTSTR lpDescription;
149
150 /* set the service name */
151 SendDlgItemMessage(hwndDlg,
152 IDC_SERV_NAME,
153 WM_SETTEXT,
154 0,
155 (LPARAM)dlgInfo->pService->lpServiceName);
156
157 /* set the display name */
158 SendDlgItemMessage(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 SendDlgItemMessage(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 SendDlgItemMessage(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 static
251 VOID
252 OnStart(HWND hwndDlg,
253 PSERVICEPROPSHEET dlgInfo)
254 {
255 WCHAR szStartParams[256];
256 LPWSTR lpStartParams = NULL;
257
258 if (GetDlgItemText(hwndDlg, IDC_START_PARAM, szStartParams, 256) > 0)
259 lpStartParams = szStartParams;
260
261 if (DoStart(dlgInfo->Info, lpStartParams))
262 {
263 UpdateServiceStatus(dlgInfo->pService);
264 ChangeListViewText(dlgInfo->Info, dlgInfo->pService, LVSTATUS);
265 SetButtonStates(dlgInfo, hwndDlg);
266 SetServiceStatusText(dlgInfo, hwndDlg);
267 }
268 }
269
270 /*
271 * General Property dialog callback.
272 * Controls messages to the General dialog
273 */
274 INT_PTR CALLBACK
275 GeneralPageProc(HWND hwndDlg,
276 UINT uMsg,
277 WPARAM wParam,
278 LPARAM lParam)
279 {
280 PSERVICEPROPSHEET dlgInfo;
281
282 /* Get the window context */
283 dlgInfo = (PSERVICEPROPSHEET)GetWindowLongPtr(hwndDlg,
284 GWLP_USERDATA);
285 if (dlgInfo == NULL && uMsg != WM_INITDIALOG)
286 {
287 return FALSE;
288 }
289
290 switch (uMsg)
291 {
292 case WM_INITDIALOG:
293 {
294 dlgInfo = (PSERVICEPROPSHEET)(((LPPROPSHEETPAGE)lParam)->lParam);
295 if (dlgInfo != NULL)
296 {
297 SetWindowLongPtr(hwndDlg,
298 GWLP_USERDATA,
299 (LONG_PTR)dlgInfo);
300 InitGeneralPage(dlgInfo, hwndDlg);
301 SetButtonStates(dlgInfo, hwndDlg);
302 }
303 }
304 break;
305
306 case WM_COMMAND:
307 switch(LOWORD(wParam))
308 {
309 case IDC_START_TYPE:
310 if (HIWORD(wParam) == CBN_SELCHANGE)
311 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
312 break;
313
314 case IDC_START:
315 OnStart(hwndDlg, dlgInfo);
316 break;
317
318 case IDC_STOP:
319 if (DoStop(dlgInfo->Info))
320 {
321 UpdateServiceStatus(dlgInfo->pService);
322 ChangeListViewText(dlgInfo->Info, dlgInfo->pService, LVSTATUS);
323 SetButtonStates(dlgInfo, hwndDlg);
324 SetServiceStatusText(dlgInfo, hwndDlg);
325 }
326 break;
327
328 case IDC_PAUSE:
329 if (DoPause(dlgInfo->Info))
330 {
331 UpdateServiceStatus(dlgInfo->pService);
332 ChangeListViewText(dlgInfo->Info, dlgInfo->pService, LVSTATUS);
333 SetButtonStates(dlgInfo, hwndDlg);
334 SetServiceStatusText(dlgInfo, hwndDlg);
335 }
336 break;
337
338 case IDC_RESUME:
339 if (DoResume(dlgInfo->Info))
340 {
341 UpdateServiceStatus(dlgInfo->pService);
342 ChangeListViewText(dlgInfo->Info, dlgInfo->pService, LVSTATUS);
343 SetButtonStates(dlgInfo, hwndDlg);
344 SetServiceStatusText(dlgInfo, hwndDlg);
345 }
346 break;
347
348 case IDC_EDIT:
349 {
350 HWND hName, hDesc, hExePath;
351
352 hName = GetDlgItem(hwndDlg, IDC_DISP_NAME);
353 hDesc = GetDlgItem(hwndDlg, IDC_DESCRIPTION);
354 hExePath = GetDlgItem(hwndDlg, IDC_EXEPATH);
355
356 SendMessage(hName, EM_SETREADONLY, FALSE, 0);
357 SendMessage(hDesc, EM_SETREADONLY, FALSE, 0);
358 SendMessage(hExePath, EM_SETREADONLY, FALSE, 0);
359 }
360 break;
361
362 case IDC_START_PARAM:
363 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
364 break;
365 }
366 break;
367
368 case WM_NOTIFY:
369 {
370 LPNMHDR lpnm = (LPNMHDR)lParam;
371
372 switch (lpnm->code)
373 {
374 case PSN_APPLY:
375 SaveDlgInfo(dlgInfo, hwndDlg);
376 SetButtonStates(dlgInfo, hwndDlg);
377 break;
378 }
379 }
380 break;
381 }
382
383 return FALSE;
384 }