Sync to Wine-20050830:
[reactos.git] / reactos / lib / msi / events.c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2005 Aric Stewart for CodeWeavers
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21
22 /*
23 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msi/setup/controlevent_overview.asp
24 */
25
26 #include <stdarg.h>
27 #include <stdio.h>
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winerror.h"
32 #include "winreg.h"
33 #include "msi.h"
34 #include "msipriv.h"
35 #include "action.h"
36
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(msi);
40
41 typedef UINT (*EVENTHANDLER)(MSIPACKAGE*,LPCWSTR,msi_dialog *);
42
43 struct _events {
44 LPCSTR event;
45 EVENTHANDLER handler;
46 };
47
48 struct subscriber {
49 struct list entry;
50 LPWSTR event;
51 LPWSTR control;
52 LPWSTR attribute;
53 };
54
55 UINT ControlEvent_HandleControlEvent(MSIPACKAGE *, LPCWSTR, LPCWSTR, msi_dialog*);
56
57 /*
58 * Create a dialog box and run it if it's modal
59 */
60 static UINT event_do_dialog( MSIPACKAGE *package, LPCWSTR name, BOOL destroy_modeless )
61 {
62 msi_dialog *dialog;
63 UINT r;
64
65 /* create a new dialog */
66 dialog = msi_dialog_create( package, name,
67 ControlEvent_HandleControlEvent );
68 if( dialog )
69 {
70 /* kill the current modeless dialog */
71 if( destroy_modeless && package->dialog )
72 {
73 msi_dialog_destroy( package->dialog );
74 package->dialog = NULL;
75 }
76
77 /* modeless dialogs return an error message */
78 r = msi_dialog_run_message_loop( dialog );
79 if( r == ERROR_SUCCESS )
80 msi_dialog_destroy( dialog );
81 else
82 package->dialog = dialog;
83 }
84 else
85 r = ERROR_FUNCTION_FAILED;
86
87 return r;
88 }
89
90
91 /*
92 * End a modal dialog box
93 */
94 static UINT ControlEvent_EndDialog(MSIPACKAGE* package, LPCWSTR argument,
95 msi_dialog* dialog)
96 {
97 static const WCHAR szExit[] = {
98 'E','x','i','t',0};
99 static const WCHAR szRetry[] = {
100 'R','e','t','r','y',0};
101 static const WCHAR szIgnore[] = {
102 'I','g','n','o','r','e',0};
103 static const WCHAR szReturn[] = {
104 'R','e','t','u','r','n',0};
105
106 if (lstrcmpW(argument,szExit)==0)
107 package->CurrentInstallState = ERROR_INSTALL_USEREXIT;
108 else if (lstrcmpW(argument, szRetry) == 0)
109 package->CurrentInstallState = ERROR_INSTALL_SUSPEND;
110 else if (lstrcmpW(argument, szIgnore) == 0)
111 package->CurrentInstallState = -1;
112 else if (lstrcmpW(argument, szReturn) == 0)
113 package->CurrentInstallState = ERROR_SUCCESS;
114 else
115 {
116 ERR("Unknown argument string %s\n",debugstr_w(argument));
117 package->CurrentInstallState = ERROR_FUNCTION_FAILED;
118 }
119
120 ControlEvent_CleanupSubscriptions(package);
121 msi_dialog_end_dialog( dialog );
122 return ERROR_SUCCESS;
123 }
124
125 /*
126 * transition from one modal dialog to another modal dialog
127 */
128 static UINT ControlEvent_NewDialog(MSIPACKAGE* package, LPCWSTR argument,
129 msi_dialog *dialog)
130 {
131 /* store the name of the next dialog, and signal this one to end */
132 package->next_dialog = strdupW(argument);
133 ControlEvent_CleanupSubscriptions(package);
134 msi_dialog_end_dialog( dialog );
135 return ERROR_SUCCESS;
136 }
137
138 /*
139 * Create a new child dialog of an existing modal dialog
140 */
141 static UINT ControlEvent_SpawnDialog(MSIPACKAGE* package, LPCWSTR argument,
142 msi_dialog *dialog)
143 {
144 /* don't destroy a modeless dialogs that might be our parent */
145 event_do_dialog( package, argument, FALSE );
146 if( package->CurrentInstallState != ERROR_SUCCESS )
147 msi_dialog_end_dialog( dialog );
148 return ERROR_SUCCESS;
149 }
150
151 /*
152 * Creates a dialog that remains up for a period of time
153 * based on a condition
154 */
155 static UINT ControlEvent_SpawnWaitDialog(MSIPACKAGE* package, LPCWSTR argument,
156 msi_dialog* dialog)
157 {
158 FIXME("Doing Nothing\n");
159 return ERROR_SUCCESS;
160 }
161
162 static UINT ControlEvent_DoAction(MSIPACKAGE* package, LPCWSTR argument,
163 msi_dialog* dialog)
164 {
165 ACTION_PerformAction(package,argument,TRUE);
166 return ERROR_SUCCESS;
167 }
168
169 static UINT ControlEvent_AddLocal(MSIPACKAGE* package, LPCWSTR argument,
170 msi_dialog* dialog)
171 {
172 static const WCHAR szAll[] = {'A','L','L',0};
173 MSIFEATURE *feature = NULL;
174
175 if (lstrcmpW(szAll,argument))
176 {
177 MSI_SetFeatureStateW(package,argument,INSTALLSTATE_LOCAL);
178 }
179 else
180 {
181 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
182 {
183 feature->ActionRequest = INSTALLSTATE_LOCAL;
184 feature->Action = INSTALLSTATE_LOCAL;
185 }
186 ACTION_UpdateComponentStates(package,argument);
187 }
188 return ERROR_SUCCESS;
189 }
190
191 static UINT ControlEvent_Remove(MSIPACKAGE* package, LPCWSTR argument,
192 msi_dialog* dialog)
193 {
194 static const WCHAR szAll[] = {'A','L','L',0};
195 MSIFEATURE *feature = NULL;
196
197 if (lstrcmpW(szAll,argument))
198 {
199 MSI_SetFeatureStateW(package,argument,INSTALLSTATE_ABSENT);
200 }
201 else
202 {
203 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
204 {
205 feature->ActionRequest = INSTALLSTATE_ABSENT;
206 feature->Action= INSTALLSTATE_ABSENT;
207 }
208 ACTION_UpdateComponentStates(package,argument);
209 }
210 return ERROR_SUCCESS;
211 }
212
213 static UINT ControlEvent_AddSource(MSIPACKAGE* package, LPCWSTR argument,
214 msi_dialog* dialog)
215 {
216 static const WCHAR szAll[] = {'A','L','L',0};
217 MSIFEATURE *feature = NULL;
218
219 if (lstrcmpW(szAll,argument))
220 {
221 MSI_SetFeatureStateW(package,argument,INSTALLSTATE_SOURCE);
222 }
223 else
224 {
225 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
226 {
227 feature->ActionRequest = INSTALLSTATE_SOURCE;
228 feature->Action = INSTALLSTATE_SOURCE;
229 }
230 ACTION_UpdateComponentStates(package,argument);
231 }
232 return ERROR_SUCCESS;
233 }
234
235 static UINT ControlEvent_SetTargetPath(MSIPACKAGE* package, LPCWSTR argument,
236 msi_dialog* dialog)
237 {
238 LPWSTR path = load_dynamic_property(package,argument, NULL);
239 /* failure to set the path halts the executing of control events */
240 return MSI_SetTargetPathW(package, argument, path);
241 }
242
243 /*
244 * Subscribed events
245 */
246 static void free_subscriber( struct subscriber *sub )
247 {
248 HeapFree(GetProcessHeap(),0,sub->event);
249 HeapFree(GetProcessHeap(),0,sub->control);
250 HeapFree(GetProcessHeap(),0,sub->attribute);
251 HeapFree(GetProcessHeap(),0,sub);
252 }
253
254 VOID ControlEvent_SubscribeToEvent( MSIPACKAGE *package, LPCWSTR event,
255 LPCWSTR control, LPCWSTR attribute )
256 {
257 struct subscriber *sub;
258
259 sub = HeapAlloc(GetProcessHeap(),0,sizeof (*sub));
260 if( !sub )
261 return;
262 sub->event = strdupW(event);
263 sub->control = strdupW(control);
264 sub->attribute = strdupW(attribute);
265 list_add_tail( &package->subscriptions, &sub->entry );
266 }
267
268 VOID ControlEvent_UnSubscribeToEvent( MSIPACKAGE *package, LPCWSTR event,
269 LPCWSTR control, LPCWSTR attribute )
270 {
271 struct list *i, *t;
272 struct subscriber *sub;
273
274 LIST_FOR_EACH_SAFE( i, t, &package->subscriptions )
275 {
276 sub = LIST_ENTRY( i, struct subscriber, entry );
277
278 if( lstrcmpiW(sub->control,control) )
279 continue;
280 if( lstrcmpiW(sub->attribute,attribute) )
281 continue;
282 if( lstrcmpiW(sub->event,event) )
283 continue;
284 list_remove( &sub->entry );
285 free_subscriber( sub );
286 }
287 }
288
289 VOID ControlEvent_FireSubscribedEvent( MSIPACKAGE *package, LPCWSTR event,
290 MSIRECORD *rec )
291 {
292 struct subscriber *sub;
293
294 TRACE("Firing Event %s\n",debugstr_w(event));
295
296 if (!package->dialog)
297 return;
298
299 LIST_FOR_EACH_ENTRY( sub, &package->subscriptions, struct subscriber, entry )
300 {
301 if (lstrcmpiW(sub->event, event))
302 continue;
303 msi_dialog_handle_event( package->dialog, sub->control,
304 sub->attribute, rec );
305 }
306 }
307
308 VOID ControlEvent_CleanupSubscriptions(MSIPACKAGE *package)
309 {
310 struct list *i, *t;
311 struct subscriber *sub;
312
313 LIST_FOR_EACH_SAFE( i, t, &package->subscriptions )
314 {
315 sub = LIST_ENTRY( i, struct subscriber, entry );
316
317 list_remove( &sub->entry );
318 free_subscriber( sub );
319 }
320 }
321
322 /*
323 * ACTION_DialogBox()
324 *
325 * Return ERROR_SUCCESS if dialog is process and ERROR_FUNCTION_FAILED
326 * if the given parameter is not a dialog box
327 */
328 UINT ACTION_DialogBox( MSIPACKAGE* package, LPCWSTR szDialogName )
329 {
330 UINT r = ERROR_SUCCESS;
331
332 if( package->next_dialog )
333 ERR("Already a next dialog... ignoring it\n");
334 package->next_dialog = NULL;
335
336 /*
337 * Dialogs are chained by filling in the next_dialog member
338 * of the package structure, then terminating the current dialog.
339 * The code below sees the next_dialog member set, and runs the
340 * next dialog.
341 * We fall out of the loop below if we come across a modeless
342 * dialog, as it returns ERROR_IO_PENDING when we try to run
343 * its message loop.
344 */
345 r = event_do_dialog( package, szDialogName, TRUE );
346 while( r == ERROR_SUCCESS && package->next_dialog )
347 {
348 LPWSTR name = package->next_dialog;
349
350 package->next_dialog = NULL;
351 r = event_do_dialog( package, name, TRUE );
352 HeapFree( GetProcessHeap(), 0, name );
353 }
354
355 if( r == ERROR_IO_PENDING )
356 r = ERROR_SUCCESS;
357
358 return r;
359 }
360
361 struct _events Events[] = {
362 { "EndDialog",ControlEvent_EndDialog },
363 { "NewDialog",ControlEvent_NewDialog },
364 { "SpawnDialog",ControlEvent_SpawnDialog },
365 { "SpawnWaitDialog",ControlEvent_SpawnWaitDialog },
366 { "DoAction",ControlEvent_DoAction },
367 { "AddLocal",ControlEvent_AddLocal },
368 { "Remove",ControlEvent_Remove },
369 { "AddSource",ControlEvent_AddSource },
370 { "SetTargetPath",ControlEvent_SetTargetPath },
371 { NULL,NULL },
372 };
373
374 UINT ControlEvent_HandleControlEvent(MSIPACKAGE *package, LPCWSTR event,
375 LPCWSTR argument, msi_dialog* dialog)
376 {
377 int i = 0;
378 UINT rc = ERROR_SUCCESS;
379
380 TRACE("Handling Control Event %s\n",debugstr_w(event));
381 if (!event)
382 return rc;
383
384 while( Events[i].event != NULL)
385 {
386 LPWSTR wevent = strdupAtoW(Events[i].event);
387 if (lstrcmpW(wevent,event)==0)
388 {
389 HeapFree(GetProcessHeap(),0,wevent);
390 rc = Events[i].handler(package,argument,dialog);
391 return rc;
392 }
393 HeapFree(GetProcessHeap(),0,wevent);
394 i++;
395 }
396 FIXME("unhandled control event %s arg(%s)\n",
397 debugstr_w(event), debugstr_w(argument));
398 return rc;
399 }