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