- Merge from trunk up to r45543
[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 /* set the main toolbar */
52 SetMenuAndButtonStates(dlgInfo->Info);
53 }
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 /*
139 * Populates the General Properties dialog with
140 * the relevant service information
141 */
142 static VOID
143 InitGeneralPage(PSERVICEPROPSHEET dlgInfo,
144 HWND hwndDlg)
145 {
146 LPQUERY_SERVICE_CONFIG pServiceConfig;
147 LPTSTR lpDescription;
148
149 /* set the service name */
150 SendDlgItemMessage(hwndDlg,
151 IDC_SERV_NAME,
152 WM_SETTEXT,
153 0,
154 (LPARAM)dlgInfo->pService->lpServiceName);
155
156 /* set the display name */
157 SendDlgItemMessage(hwndDlg,
158 IDC_DISP_NAME,
159 WM_SETTEXT,
160 0,
161 (LPARAM)dlgInfo->pService->lpDisplayName);
162
163 /* set the description */
164 if ((lpDescription = GetServiceDescription(dlgInfo->pService->lpServiceName)))
165 {
166 SendDlgItemMessage(hwndDlg,
167 IDC_DESCRIPTION,
168 WM_SETTEXT,
169 0,
170 (LPARAM)lpDescription);
171
172 HeapFree(ProcessHeap,
173 0,
174 lpDescription);
175 }
176
177 pServiceConfig = GetServiceConfig(dlgInfo->pService->lpServiceName);
178 if (pServiceConfig)
179 {
180 SendDlgItemMessage(hwndDlg,
181 IDC_EXEPATH,
182 WM_SETTEXT,
183 0,
184 (LPARAM)pServiceConfig->lpBinaryPathName);
185 HeapFree(ProcessHeap,
186 0,
187 pServiceConfig);
188 }
189
190
191 /* set startup type */
192 SetStartupType(dlgInfo->pService->lpServiceName, hwndDlg);
193
194 SetServiceStatusText(dlgInfo,
195 hwndDlg);
196
197 if (dlgInfo->Info->bIsUserAnAdmin)
198 {
199 HWND hEdit = GetDlgItem(hwndDlg,
200 IDC_EDIT);
201 EnableWindow(hEdit,
202 TRUE);
203 }
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 /*
252 * General Property dialog callback.
253 * Controls messages to the General dialog
254 */
255 INT_PTR CALLBACK
256 GeneralPageProc(HWND hwndDlg,
257 UINT uMsg,
258 WPARAM wParam,
259 LPARAM lParam)
260 {
261 PSERVICEPROPSHEET dlgInfo;
262
263 /* Get the window context */
264 dlgInfo = (PSERVICEPROPSHEET)GetWindowLongPtr(hwndDlg,
265 GWLP_USERDATA);
266 if (dlgInfo == NULL && uMsg != WM_INITDIALOG)
267 {
268 return FALSE;
269 }
270
271 switch (uMsg)
272 {
273 case WM_INITDIALOG:
274 {
275 dlgInfo = (PSERVICEPROPSHEET)(((LPPROPSHEETPAGE)lParam)->lParam);
276 if (dlgInfo != NULL)
277 {
278 SetWindowLongPtr(hwndDlg,
279 GWLP_USERDATA,
280 (LONG_PTR)dlgInfo);
281 InitGeneralPage(dlgInfo, hwndDlg);
282 SetButtonStates(dlgInfo, hwndDlg);
283 }
284 }
285 break;
286
287 case WM_COMMAND:
288 switch(LOWORD(wParam))
289 {
290 case IDC_START_TYPE:
291 if (HIWORD(wParam) == CBN_SELCHANGE)
292 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
293 break;
294
295 case IDC_START:
296 if (DoStart(dlgInfo->Info))
297 {
298 UpdateServiceStatus(dlgInfo->pService);
299 ChangeListViewText(dlgInfo->Info, dlgInfo->pService, LVSTATUS);
300 SetButtonStates(dlgInfo, hwndDlg);
301 SetServiceStatusText(dlgInfo, hwndDlg);
302 }
303 break;
304
305 case IDC_STOP:
306 if (DoStop(dlgInfo->Info))
307 {
308 UpdateServiceStatus(dlgInfo->pService);
309 ChangeListViewText(dlgInfo->Info, dlgInfo->pService, LVSTATUS);
310 SetButtonStates(dlgInfo, hwndDlg);
311 SetServiceStatusText(dlgInfo, hwndDlg);
312 }
313 break;
314
315 case IDC_PAUSE:
316 if (DoPause(dlgInfo->Info))
317 {
318 UpdateServiceStatus(dlgInfo->pService);
319 ChangeListViewText(dlgInfo->Info, dlgInfo->pService, LVSTATUS);
320 SetButtonStates(dlgInfo, hwndDlg);
321 SetServiceStatusText(dlgInfo, hwndDlg);
322 }
323 break;
324
325 case IDC_RESUME:
326 if (DoResume(dlgInfo->Info))
327 {
328 UpdateServiceStatus(dlgInfo->pService);
329 ChangeListViewText(dlgInfo->Info, dlgInfo->pService, LVSTATUS);
330 SetButtonStates(dlgInfo, hwndDlg);
331 SetServiceStatusText(dlgInfo, hwndDlg);
332 }
333 break;
334
335 case IDC_EDIT:
336 {
337 HWND hName, hDesc, hExePath;
338
339 hName = GetDlgItem(hwndDlg, IDC_DISP_NAME);
340 hDesc = GetDlgItem(hwndDlg, IDC_DESCRIPTION);
341 hExePath = GetDlgItem(hwndDlg, IDC_EXEPATH);
342
343 SendMessage(hName, EM_SETREADONLY, FALSE, 0);
344 SendMessage(hDesc, EM_SETREADONLY, FALSE, 0);
345 SendMessage(hExePath, EM_SETREADONLY, FALSE, 0);
346 }
347 break;
348
349 case IDC_START_PARAM:
350 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
351 break;
352 }
353 break;
354
355 case WM_NOTIFY:
356 {
357 LPNMHDR lpnm = (LPNMHDR)lParam;
358
359 switch (lpnm->code)
360 {
361 case PSN_APPLY:
362 SaveDlgInfo(dlgInfo, hwndDlg);
363 SetButtonStates(dlgInfo, hwndDlg);
364 break;
365 }
366 }
367 break;
368 }
369
370 return FALSE;
371 }