b5b69d05819790faa862b3ef93129d61c490d3ea
[reactos.git] / reactos / dll / win32 / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #define WIN32_NO_STATUS
22 #define _INC_WINDOWS
23 #define COM_NO_WINDOWS_H
24
25 //#include <stdarg.h>
26 //#include <stdio.h>
27
28 //#include <windef.h>
29 //#include "winbase.h"
30 //#include "winerror.h"
31 //#include "winreg.h"
32 //#include "msi.h"
33 #include "msipriv.h"
34
35 #include <wine/debug.h>
36 #include <wine/unicode.h>
37
38 WINE_DEFAULT_DEBUG_CHANNEL(msi);
39
40 typedef UINT (*EVENTHANDLER)(MSIPACKAGE*,LPCWSTR,msi_dialog *);
41
42 struct control_events
43 {
44 const WCHAR *event;
45 EVENTHANDLER handler;
46 };
47
48 struct subscriber {
49 struct list entry;
50 msi_dialog *dialog;
51 LPWSTR event;
52 LPWSTR control;
53 LPWSTR attribute;
54 };
55
56 static UINT ControlEvent_HandleControlEvent(MSIPACKAGE *, LPCWSTR, LPCWSTR, msi_dialog*);
57
58 /*
59 * Create a dialog box and run it if it's modal
60 */
61 static UINT event_do_dialog( MSIPACKAGE *package, LPCWSTR name, msi_dialog *parent, BOOL destroy_modeless )
62 {
63 msi_dialog *dialog;
64 UINT r;
65
66 /* create a new dialog */
67 dialog = msi_dialog_create( package, name, parent,
68 ControlEvent_HandleControlEvent );
69 if( dialog )
70 {
71 /* kill the current modeless dialog */
72 if( destroy_modeless && package->dialog )
73 {
74 msi_dialog_destroy( package->dialog );
75 package->dialog = NULL;
76 }
77
78 /* modeless dialogs return an error message */
79 r = msi_dialog_run_message_loop( dialog );
80 if( r == ERROR_SUCCESS )
81 msi_dialog_destroy( dialog );
82 else
83 package->dialog = dialog;
84 }
85 else
86 r = ERROR_FUNCTION_FAILED;
87
88 return r;
89 }
90
91
92 /*
93 * End a modal dialog box
94 */
95 static UINT ControlEvent_EndDialog(MSIPACKAGE* package, LPCWSTR argument,
96 msi_dialog* dialog)
97 {
98 static const WCHAR szExit[] = {'E','x','i','t',0};
99 static const WCHAR szRetry[] = {'R','e','t','r','y',0};
100 static const WCHAR szIgnore[] = {'I','g','n','o','r','e',0};
101 static const WCHAR szReturn[] = {'R','e','t','u','r','n',0};
102
103 if (!strcmpW( argument, szExit ))
104 package->CurrentInstallState = ERROR_INSTALL_USEREXIT;
105 else if (!strcmpW( argument, szRetry ))
106 package->CurrentInstallState = ERROR_INSTALL_SUSPEND;
107 else if (!strcmpW( argument, szIgnore ))
108 package->CurrentInstallState = ERROR_SUCCESS;
109 else if (!strcmpW( argument, szReturn ))
110 {
111 msi_dialog *parent = msi_dialog_get_parent(dialog);
112 msi_free(package->next_dialog);
113 package->next_dialog = (parent) ? strdupW(msi_dialog_get_name(parent)) : NULL;
114 package->CurrentInstallState = ERROR_SUCCESS;
115 }
116 else
117 {
118 ERR("Unknown argument string %s\n",debugstr_w(argument));
119 package->CurrentInstallState = ERROR_FUNCTION_FAILED;
120 }
121
122 ControlEvent_CleanupDialogSubscriptions(package, msi_dialog_get_name( dialog ));
123 msi_dialog_end_dialog( dialog );
124 return ERROR_SUCCESS;
125 }
126
127 /*
128 * transition from one modal dialog to another modal dialog
129 */
130 static UINT ControlEvent_NewDialog(MSIPACKAGE* package, LPCWSTR argument,
131 msi_dialog *dialog)
132 {
133 /* store the name of the next dialog, and signal this one to end */
134 package->next_dialog = strdupW(argument);
135 ControlEvent_CleanupSubscriptions(package);
136 msi_dialog_end_dialog( dialog );
137 return ERROR_SUCCESS;
138 }
139
140 /*
141 * Create a new child dialog of an existing modal dialog
142 */
143 static UINT ControlEvent_SpawnDialog(MSIPACKAGE* package, LPCWSTR argument,
144 msi_dialog *dialog)
145 {
146 /* don't destroy a modeless dialogs that might be our parent */
147 event_do_dialog( package, argument, dialog, FALSE );
148 if( package->CurrentInstallState != ERROR_SUCCESS )
149 msi_dialog_end_dialog( dialog );
150 return ERROR_SUCCESS;
151 }
152
153 /*
154 * Creates a dialog that remains up for a period of time
155 * based on a condition
156 */
157 static UINT ControlEvent_SpawnWaitDialog(MSIPACKAGE* package, LPCWSTR argument,
158 msi_dialog* dialog)
159 {
160 FIXME("Doing Nothing\n");
161 return ERROR_SUCCESS;
162 }
163
164 static UINT ControlEvent_DoAction(MSIPACKAGE* package, LPCWSTR argument,
165 msi_dialog* dialog)
166 {
167 ACTION_PerformAction(package, argument, SCRIPT_NONE);
168 return ERROR_SUCCESS;
169 }
170
171 static UINT ControlEvent_AddLocal( MSIPACKAGE *package, LPCWSTR argument, msi_dialog *dialog )
172 {
173 MSIFEATURE *feature;
174
175 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
176 {
177 if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
178 {
179 if (feature->ActionRequest != INSTALLSTATE_LOCAL)
180 msi_set_property( package->db, szPreselected, szOne, -1 );
181 MSI_SetFeatureStateW( package, feature->Feature, INSTALLSTATE_LOCAL );
182 }
183 }
184 return ERROR_SUCCESS;
185 }
186
187 static UINT ControlEvent_Remove( MSIPACKAGE *package, LPCWSTR argument, msi_dialog *dialog )
188 {
189 MSIFEATURE *feature;
190
191 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
192 {
193 if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
194 {
195 if (feature->ActionRequest != INSTALLSTATE_ABSENT)
196 msi_set_property( package->db, szPreselected, szOne, -1 );
197 MSI_SetFeatureStateW( package, feature->Feature, INSTALLSTATE_ABSENT );
198 }
199 }
200 return ERROR_SUCCESS;
201 }
202
203 static UINT ControlEvent_AddSource( MSIPACKAGE *package, LPCWSTR argument, msi_dialog *dialog )
204 {
205 MSIFEATURE *feature;
206
207 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
208 {
209 if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
210 {
211 if (feature->ActionRequest != INSTALLSTATE_SOURCE)
212 msi_set_property( package->db, szPreselected, szOne, -1 );
213 MSI_SetFeatureStateW( package, feature->Feature, INSTALLSTATE_SOURCE );
214 }
215 }
216 return ERROR_SUCCESS;
217 }
218
219 static UINT ControlEvent_SetTargetPath(MSIPACKAGE* package, LPCWSTR argument,
220 msi_dialog* dialog)
221 {
222 static const WCHAR szSelectionPath[] = {'S','e','l','e','c','t','i','o','n','P','a','t','h',0};
223 LPWSTR path = msi_dup_property( package->db, argument );
224 MSIRECORD *rec = MSI_CreateRecord( 1 );
225 UINT r = ERROR_SUCCESS;
226
227 MSI_RecordSetStringW( rec, 1, path );
228 ControlEvent_FireSubscribedEvent( package, szSelectionPath, rec );
229 if (path)
230 {
231 /* failure to set the path halts the executing of control events */
232 r = MSI_SetTargetPathW(package, argument, path);
233 msi_free(path);
234 }
235 msi_free(&rec->hdr);
236 return r;
237 }
238
239 static UINT ControlEvent_Reset(MSIPACKAGE* package, LPCWSTR argument,
240 msi_dialog* dialog)
241 {
242 msi_dialog_reset(dialog);
243 return ERROR_SUCCESS;
244 }
245
246 /*
247 * Subscribed events
248 */
249 static void free_subscriber( struct subscriber *sub )
250 {
251 msi_free(sub->event);
252 msi_free(sub->control);
253 msi_free(sub->attribute);
254 msi_free(sub);
255 }
256
257 VOID ControlEvent_SubscribeToEvent( MSIPACKAGE *package, msi_dialog *dialog,
258 LPCWSTR event, LPCWSTR control, LPCWSTR attribute )
259 {
260 struct subscriber *sub;
261
262 TRACE("event %s control %s attribute %s\n", debugstr_w(event), debugstr_w(control), debugstr_w(attribute));
263
264 LIST_FOR_EACH_ENTRY( sub, &package->subscriptions, struct subscriber, entry )
265 {
266 if (!strcmpiW( sub->event, event ) &&
267 !strcmpiW( sub->control, control ) &&
268 !strcmpiW( sub->attribute, attribute ))
269 {
270 TRACE("already subscribed\n");
271 return;
272 };
273 }
274 if (!(sub = msi_alloc( sizeof(*sub) ))) return;
275 sub->dialog = dialog;
276 sub->event = strdupW(event);
277 sub->control = strdupW(control);
278 sub->attribute = strdupW(attribute);
279 list_add_tail( &package->subscriptions, &sub->entry );
280 }
281
282 VOID ControlEvent_FireSubscribedEvent( MSIPACKAGE *package, LPCWSTR event, MSIRECORD *rec )
283 {
284 struct subscriber *sub;
285
286 TRACE("Firing event %s\n", debugstr_w(event));
287
288 LIST_FOR_EACH_ENTRY( sub, &package->subscriptions, struct subscriber, entry )
289 {
290 if (strcmpiW( sub->event, event )) continue;
291 msi_dialog_handle_event( sub->dialog, sub->control, sub->attribute, rec );
292 }
293 }
294
295 VOID ControlEvent_CleanupDialogSubscriptions(MSIPACKAGE *package, LPWSTR dialog)
296 {
297 struct list *i, *t;
298 struct subscriber *sub;
299
300 LIST_FOR_EACH_SAFE( i, t, &package->subscriptions )
301 {
302 sub = LIST_ENTRY( i, struct subscriber, entry );
303
304 if (strcmpW( msi_dialog_get_name( sub->dialog ), dialog ))
305 continue;
306
307 list_remove( &sub->entry );
308 free_subscriber( sub );
309 }
310 }
311
312 VOID ControlEvent_CleanupSubscriptions(MSIPACKAGE *package)
313 {
314 struct list *i, *t;
315 struct subscriber *sub;
316
317 LIST_FOR_EACH_SAFE( i, t, &package->subscriptions )
318 {
319 sub = LIST_ENTRY( i, struct subscriber, entry );
320
321 list_remove( &sub->entry );
322 free_subscriber( sub );
323 }
324 }
325
326 /*
327 * ACTION_DialogBox()
328 *
329 * Return ERROR_SUCCESS if dialog is process and ERROR_FUNCTION_FAILED
330 * if the given parameter is not a dialog box
331 */
332 UINT ACTION_DialogBox( MSIPACKAGE* package, LPCWSTR szDialogName )
333 {
334 UINT r = ERROR_SUCCESS;
335
336 if( package->next_dialog )
337 ERR("Already a next dialog... ignoring it\n");
338 package->next_dialog = NULL;
339
340 /*
341 * Dialogs are chained by filling in the next_dialog member
342 * of the package structure, then terminating the current dialog.
343 * The code below sees the next_dialog member set, and runs the
344 * next dialog.
345 * We fall out of the loop below if we come across a modeless
346 * dialog, as it returns ERROR_IO_PENDING when we try to run
347 * its message loop.
348 */
349 r = event_do_dialog( package, szDialogName, NULL, TRUE );
350 while( r == ERROR_SUCCESS && package->next_dialog )
351 {
352 LPWSTR name = package->next_dialog;
353
354 package->next_dialog = NULL;
355 r = event_do_dialog( package, name, NULL, TRUE );
356 msi_free( name );
357 }
358
359 if( r == ERROR_IO_PENDING )
360 r = ERROR_SUCCESS;
361
362 return r;
363 }
364
365 static UINT ControlEvent_SetInstallLevel(MSIPACKAGE* package, LPCWSTR argument,
366 msi_dialog* dialog)
367 {
368 int iInstallLevel = atolW(argument);
369
370 TRACE("Setting install level: %i\n", iInstallLevel);
371
372 return MSI_SetInstallLevel( package, iInstallLevel );
373 }
374
375 static UINT ControlEvent_DirectoryListUp(MSIPACKAGE *package, LPCWSTR argument,
376 msi_dialog *dialog)
377 {
378 return msi_dialog_directorylist_up( dialog );
379 }
380
381 static UINT ControlEvent_ReinstallMode(MSIPACKAGE *package, LPCWSTR argument,
382 msi_dialog *dialog)
383 {
384 return msi_set_property( package->db, szReinstallMode, argument, -1 );
385 }
386
387 static UINT ControlEvent_Reinstall( MSIPACKAGE *package, LPCWSTR argument,
388 msi_dialog *dialog )
389 {
390 return msi_set_property( package->db, szReinstall, argument, -1 );
391 }
392
393 static UINT ControlEvent_ValidateProductID(MSIPACKAGE *package, LPCWSTR argument,
394 msi_dialog *dialog)
395 {
396 return msi_validate_product_id( package );
397 }
398
399 static const WCHAR end_dialogW[] = {'E','n','d','D','i','a','l','o','g',0};
400 static const WCHAR new_dialogW[] = {'N','e','w','D','i','a','l','o','g',0};
401 static const WCHAR spawn_dialogW[] = {'S','p','a','w','n','D','i','a','l','o','g',0};
402 static const WCHAR spawn_wait_dialogW[] = {'S','p','a','w','n','W','a','i','t','D','i','a','l','o','g',0};
403 static const WCHAR do_actionW[] = {'D','o','A','c','t','i','o','n',0};
404 static const WCHAR add_localW[] = {'A','d','d','L','o','c','a','l',0};
405 static const WCHAR removeW[] = {'R','e','m','o','v','e',0};
406 static const WCHAR add_sourceW[] = {'A','d','d','S','o','u','r','c','e',0};
407 static const WCHAR set_target_pathW[] = {'S','e','t','T','a','r','g','e','t','P','a','t','h',0};
408 static const WCHAR resetW[] = {'R','e','s','e','t',0};
409 static const WCHAR set_install_levelW[] = {'S','e','t','I','n','s','t','a','l','l','L','e','v','e','l',0};
410 static const WCHAR directory_list_upW[] = {'D','i','r','e','c','t','o','r','y','L','i','s','t','U','p',0};
411 static const WCHAR selection_browseW[] = {'S','e','l','e','c','t','i','o','n','B','r','o','w','s','e',0};
412 static const WCHAR reinstall_modeW[] = {'R','e','i','n','s','t','a','l','l','M','o','d','e',0};
413 static const WCHAR reinstallW[] = {'R','e','i','n','s','t','a','l','l',0};
414 static const WCHAR validate_product_idW[] = {'V','a','l','i','d','a','t','e','P','r','o','d','u','c','t','I','D',0};
415
416 static const struct control_events control_events[] =
417 {
418 { end_dialogW, ControlEvent_EndDialog },
419 { new_dialogW, ControlEvent_NewDialog },
420 { spawn_dialogW, ControlEvent_SpawnDialog },
421 { spawn_wait_dialogW, ControlEvent_SpawnWaitDialog },
422 { do_actionW, ControlEvent_DoAction },
423 { add_localW, ControlEvent_AddLocal },
424 { removeW, ControlEvent_Remove },
425 { add_sourceW, ControlEvent_AddSource },
426 { set_target_pathW, ControlEvent_SetTargetPath },
427 { resetW, ControlEvent_Reset },
428 { set_install_levelW, ControlEvent_SetInstallLevel },
429 { directory_list_upW, ControlEvent_DirectoryListUp },
430 { selection_browseW, ControlEvent_SpawnDialog },
431 { reinstall_modeW, ControlEvent_ReinstallMode },
432 { reinstallW, ControlEvent_Reinstall },
433 { validate_product_idW, ControlEvent_ValidateProductID },
434 { NULL, NULL }
435 };
436
437 UINT ControlEvent_HandleControlEvent( MSIPACKAGE *package, LPCWSTR event,
438 LPCWSTR argument, msi_dialog *dialog )
439 {
440 unsigned int i;
441
442 TRACE("handling control event %s\n", debugstr_w(event));
443
444 if (!event) return ERROR_SUCCESS;
445
446 for (i = 0; control_events[i].event; i++)
447 {
448 if (!strcmpW( control_events[i].event, event ))
449 return control_events[i].handler( package, argument, dialog );
450 }
451 FIXME("unhandled control event %s arg(%s)\n", debugstr_w(event), debugstr_w(argument));
452 return ERROR_SUCCESS;
453 }