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