2 * PROJECT: ReactOS Services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/mscutils/servman/progress.c
5 * PURPOSE: Progress dialog box message handler
6 * COPYRIGHT: Copyright 2006-2015 Ged Murphy <gedmurphy@reactos.org>
12 #define PROGRESS_RANGE 20
13 #define PROGRESS_STEP_MAX 15
15 typedef struct _PROGRESS_DATA
25 } PROGRESS_DATA
, *PPROGRESS_DATA
;
29 ResetProgressDialog(HWND hDlg
,
35 /* Load the label Id */
36 if (AllocAndLoadString(&lpProgStr
,
40 /* Write it to the dialog */
41 SendDlgItemMessageW(hDlg
,
50 /* Write the service name to the dialog */
51 SendDlgItemMessageW(hDlg
,
57 /* Set the progress bar to the start */
58 SendDlgItemMessageW(hDlg
,
65 unsigned int __stdcall
ActionThread(void* Param
)
67 PPROGRESS_DATA ProgressData
= (PPROGRESS_DATA
)Param
;
69 if (ProgressData
->Action
== ACTION_START
)
71 /* Setup the progress dialog for this action */
72 ResetProgressDialog(ProgressData
->hDlg
,
73 ProgressData
->ServiceName
,
74 IDS_PROGRESS_INFO_START
);
76 /* Start the service */
77 if (DoStartService(ProgressData
->ServiceName
,
78 ProgressData
->hProgress
,
81 /* We're done, slide the progress bar up to the top */
82 CompleteProgressBar(ProgressData
->hProgress
);
85 else if (ProgressData
->Action
== ACTION_STOP
|| ProgressData
->Action
== ACTION_RESTART
)
87 /* Check if there are and dependants to stop */
88 if (ProgressData
->StopDepends
&& ProgressData
->ServiceList
)
90 LPWSTR lpStr
= ProgressData
->ServiceList
;
92 /* Loop through all the services in the list */
95 /* Break when we hit the double null */
96 if (*lpStr
== L
'\0' && *(lpStr
+ 1) == L
'\0')
99 /* If this isn't our first time in the loop we'll
100 have been left on a null char */
104 ResetProgressDialog(ProgressData
->hDlg
,
106 IDS_PROGRESS_INFO_STOP
);
108 /* Stop the requested service */
109 if (DoStopService(ProgressData
->ServiceName
,
110 ProgressData
->hProgress
))
112 CompleteProgressBar(ProgressData
->hProgress
);
115 /* Move onto the next string */
116 while (*lpStr
!= L
'\0')
121 ResetProgressDialog(ProgressData
->hDlg
,
122 ProgressData
->ServiceName
,
123 IDS_PROGRESS_INFO_STOP
);
125 if (DoStopService(ProgressData
->ServiceName
,
126 ProgressData
->hProgress
))
128 CompleteProgressBar(ProgressData
->hProgress
);
132 /* If this was a restart, we'll need to start the service back up */
133 if (ProgressData
->Action
== ACTION_RESTART
)
135 /* Setup the progress dialog for this action */
136 ResetProgressDialog(ProgressData
->hDlg
,
137 ProgressData
->ServiceName
,
138 IDS_PROGRESS_INFO_START
);
140 /* Start the service */
141 if (DoStartService(ProgressData
->ServiceName
,
142 ProgressData
->hProgress
,
145 /* We're done, slide the progress bar up to the top */
146 CompleteProgressBar(ProgressData
->hProgress
);
150 else if (ProgressData
->Action
== ACTION_PAUSE
)
152 /* Setup the progress dialog for this action */
153 ResetProgressDialog(ProgressData
->hDlg
,
154 ProgressData
->ServiceName
,
155 IDS_PROGRESS_INFO_PAUSE
);
157 /* Pause the service */
158 if (DoControlService(ProgressData
->ServiceName
,
159 ProgressData
->hProgress
,
160 SERVICE_CONTROL_PAUSE
))
162 /* We're done, slide the progress bar up to the top */
163 CompleteProgressBar(ProgressData
->hProgress
);
166 else if (ProgressData
->Action
== ACTION_RESUME
)
168 /* Setup the progress dialog for this action */
169 ResetProgressDialog(ProgressData
->hDlg
,
170 ProgressData
->ServiceName
,
171 IDS_PROGRESS_INFO_RESUME
);
173 /* resume the service */
174 if (DoControlService(ProgressData
->ServiceName
,
175 ProgressData
->hProgress
,
176 SERVICE_CONTROL_CONTINUE
))
178 /* We're done, slide the progress bar up to the top */
179 CompleteProgressBar(ProgressData
->hProgress
);
184 EndDialog(ProgressData
->hDlg
, IDOK
);
191 InitDialog(HWND hDlg
,
196 PPROGRESS_DATA ProgressData
= (PPROGRESS_DATA
)lParam
;
199 ProgressData
->hDlg
= hDlg
;
201 /* Get a handle to the progress bar */
202 ProgressData
->hProgress
= GetDlgItem(hDlg
,
203 IDC_SERVCON_PROGRESS
);
204 if (!ProgressData
->hProgress
)
207 /* Set the progress bar range */
208 SendMessageW(ProgressData
->hProgress
,
211 MAKELPARAM(0, PROGRESS_RANGE
));
213 /* Set the progress bar step */
214 SendMessageW(ProgressData
->hProgress
,
219 /* Create a thread to handle the service control */
220 hThread
= (HANDLE
)_beginthreadex(NULL
, 0, &ActionThread
, ProgressData
, 0, NULL
);
221 if (!hThread
) return FALSE
;
223 CloseHandle(hThread
);
229 ProgressDialogProc(HWND hDlg
,
238 return InitDialog(hDlg
,
245 switch(LOWORD(wParam
))
248 EndDialog(hDlg
, wParam
);
262 CompleteProgressBar(HANDLE hProgress
)
264 HWND hProgBar
= (HWND
)hProgress
;
267 /* Get the current position */
268 Pos
= SendMessageW(hProgBar
,
273 /* Loop until we hit the max */
274 while (Pos
<= PROGRESS_RANGE
)
276 /* Increment the progress bar */
277 SendMessageW(hProgBar
,
282 /* Wait for 15ms to give it a smooth feel */
289 IncrementProgressBar(HANDLE hProgress
,
292 HWND hProgBar
= (HWND
)hProgress
;
295 /* Don't allow the progress to reach to complete*/
296 Position
= SendMessageW(hProgBar
,
300 if (Position
< PROGRESS_STEP_MAX
)
302 /* Do we want to increment the default amount? */
303 if (Step
== DEFAULT_STEP
)
305 /* Use the step value we set on create */
306 SendMessageW(hProgBar
,
313 /* Use the value passed */
314 SendMessageW(hProgBar
,
323 RunActionWithProgress(HWND hParent
,
329 PROGRESS_DATA ProgressData
;
338 /* Check if we'll be stopping the service */
339 if (Action
== ACTION_STOP
|| Action
== ACTION_RESTART
)
341 /* Does the service have any dependent services which need stopping first */
342 ServiceList
= GetListOfServicesToStop(ServiceName
);
345 /* Ask the user if they want to stop the dependants */
346 StopDepends
= CreateStopDependsDialog(hParent
,
351 /* Exit early if the user decided not to stop the dependants */
352 if (StopDepends
== FALSE
)
354 HeapFree(GetProcessHeap(), 0, ServiceList
);
360 ProgressData
.hDlg
= NULL
;
361 ProgressData
.ServiceName
= ServiceName
;
362 ProgressData
.Action
= Action
;
363 ProgressData
.StopDepends
= StopDepends
;
364 ProgressData
.ServiceList
= ServiceList
;
365 ProgressData
.Param
= Param
;
367 Result
= DialogBoxParamW(hInstance
,
368 MAKEINTRESOURCEW(IDD_DLG_PROGRESS
),
371 (LPARAM
)&ProgressData
);
374 HeapFree(GetProcessHeap(), 0, ServiceList
);
376 return (Result
== IDOK
);