wined3d: Do not perform a NULL check on riid (Coverity).
[wine/multimedia.git] / dlls / msi / events.c
blobc946512ae36e9441145bad422b588bba4b60072e
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2005 Aric Stewart for CodeWeavers
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.
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.
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
23 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msi/setup/controlevent_overview.asp
26 #include <stdarg.h>
27 #include <stdio.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winerror.h"
32 #include "winreg.h"
33 #include "msi.h"
34 #include "msipriv.h"
36 #include "wine/debug.h"
37 #include "wine/unicode.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(msi);
41 typedef UINT (*EVENTHANDLER)(MSIPACKAGE*,LPCWSTR,msi_dialog *);
43 struct _events {
44 LPCSTR event;
45 EVENTHANDLER handler;
48 struct subscriber {
49 struct list entry;
50 msi_dialog *dialog;
51 LPWSTR event;
52 LPWSTR control;
53 LPWSTR attribute;
56 UINT ControlEvent_HandleControlEvent(MSIPACKAGE *, LPCWSTR, LPCWSTR, msi_dialog*);
57 static VOID ControlEvent_CleanupDialogSubscriptions(MSIPACKAGE *package, LPWSTR dialog);
60 * Create a dialog box and run it if it's modal
62 static UINT event_do_dialog( MSIPACKAGE *package, LPCWSTR name, msi_dialog *parent, BOOL destroy_modeless )
64 msi_dialog *dialog;
65 UINT r;
67 /* create a new dialog */
68 dialog = msi_dialog_create( package, name, parent,
69 ControlEvent_HandleControlEvent );
70 if( dialog )
72 /* kill the current modeless dialog */
73 if( destroy_modeless && package->dialog )
75 msi_dialog_destroy( package->dialog );
76 package->dialog = NULL;
79 /* modeless dialogs return an error message */
80 r = msi_dialog_run_message_loop( dialog );
81 if( r == ERROR_SUCCESS )
82 msi_dialog_destroy( dialog );
83 else
84 package->dialog = dialog;
86 else
87 r = ERROR_FUNCTION_FAILED;
89 return r;
94 * End a modal dialog box
96 static UINT ControlEvent_EndDialog(MSIPACKAGE* package, LPCWSTR argument,
97 msi_dialog* dialog)
99 static const WCHAR szExit[] = {
100 'E','x','i','t',0};
101 static const WCHAR szRetry[] = {
102 'R','e','t','r','y',0};
103 static const WCHAR szIgnore[] = {
104 'I','g','n','o','r','e',0};
105 static const WCHAR szReturn[] = {
106 'R','e','t','u','r','n',0};
108 if (lstrcmpW(argument,szExit)==0)
109 package->CurrentInstallState = ERROR_INSTALL_USEREXIT;
110 else if (lstrcmpW(argument, szRetry) == 0)
111 package->CurrentInstallState = ERROR_INSTALL_SUSPEND;
112 else if (lstrcmpW(argument, szIgnore) == 0)
113 package->CurrentInstallState = -1;
114 else if (lstrcmpW(argument, szReturn) == 0)
116 msi_dialog *parent = msi_dialog_get_parent(dialog);
117 msi_free(package->next_dialog);
118 package->next_dialog = (parent) ? strdupW(msi_dialog_get_name(parent)) : NULL;
119 package->CurrentInstallState = ERROR_SUCCESS;
121 else
123 ERR("Unknown argument string %s\n",debugstr_w(argument));
124 package->CurrentInstallState = ERROR_FUNCTION_FAILED;
127 ControlEvent_CleanupDialogSubscriptions(package, msi_dialog_get_name( dialog ));
128 msi_dialog_end_dialog( dialog );
129 return ERROR_SUCCESS;
133 * transition from one modal dialog to another modal dialog
135 static UINT ControlEvent_NewDialog(MSIPACKAGE* package, LPCWSTR argument,
136 msi_dialog *dialog)
138 /* store the name of the next dialog, and signal this one to end */
139 package->next_dialog = strdupW(argument);
140 ControlEvent_CleanupSubscriptions(package);
141 msi_dialog_end_dialog( dialog );
142 return ERROR_SUCCESS;
146 * Create a new child dialog of an existing modal dialog
148 static UINT ControlEvent_SpawnDialog(MSIPACKAGE* package, LPCWSTR argument,
149 msi_dialog *dialog)
151 /* don't destroy a modeless dialogs that might be our parent */
152 event_do_dialog( package, argument, dialog, FALSE );
153 if( package->CurrentInstallState != ERROR_SUCCESS )
154 msi_dialog_end_dialog( dialog );
155 return ERROR_SUCCESS;
159 * Creates a dialog that remains up for a period of time
160 * based on a condition
162 static UINT ControlEvent_SpawnWaitDialog(MSIPACKAGE* package, LPCWSTR argument,
163 msi_dialog* dialog)
165 FIXME("Doing Nothing\n");
166 return ERROR_SUCCESS;
169 static UINT ControlEvent_DoAction(MSIPACKAGE* package, LPCWSTR argument,
170 msi_dialog* dialog)
172 ACTION_PerformAction(package,argument,TRUE);
173 return ERROR_SUCCESS;
176 static UINT ControlEvent_AddLocal(MSIPACKAGE* package, LPCWSTR argument,
177 msi_dialog* dialog)
179 static const WCHAR szAll[] = {'A','L','L',0};
180 MSIFEATURE *feature = NULL;
182 if (lstrcmpW(szAll,argument))
184 MSI_SetFeatureStateW(package,argument,INSTALLSTATE_LOCAL);
186 else
188 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
189 msi_feature_set_state( feature, INSTALLSTATE_LOCAL );
191 ACTION_UpdateComponentStates(package,argument);
193 return ERROR_SUCCESS;
196 static UINT ControlEvent_Remove(MSIPACKAGE* package, LPCWSTR argument,
197 msi_dialog* dialog)
199 static const WCHAR szAll[] = {'A','L','L',0};
200 MSIFEATURE *feature = NULL;
202 if (lstrcmpW(szAll,argument))
204 MSI_SetFeatureStateW(package,argument,INSTALLSTATE_ABSENT);
206 else
208 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
209 msi_feature_set_state( feature, INSTALLSTATE_ABSENT );
211 ACTION_UpdateComponentStates(package,argument);
213 return ERROR_SUCCESS;
216 static UINT ControlEvent_AddSource(MSIPACKAGE* package, LPCWSTR argument,
217 msi_dialog* dialog)
219 static const WCHAR szAll[] = {'A','L','L',0};
220 MSIFEATURE *feature = NULL;
222 if (lstrcmpW(szAll,argument))
224 MSI_SetFeatureStateW(package,argument,INSTALLSTATE_SOURCE);
226 else
228 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
229 msi_feature_set_state( feature, INSTALLSTATE_SOURCE );
230 ACTION_UpdateComponentStates(package,argument);
232 return ERROR_SUCCESS;
235 static UINT ControlEvent_SetTargetPath(MSIPACKAGE* package, LPCWSTR argument,
236 msi_dialog* dialog)
238 LPWSTR path = msi_dup_property( package, argument );
239 MSIRECORD *rec = MSI_CreateRecord( 1 );
240 UINT r;
242 static const WCHAR szSelectionPath[] = {'S','e','l','e','c','t','i','o','n','P','a','t','h',0};
244 MSI_RecordSetStringW( rec, 1, path );
245 ControlEvent_FireSubscribedEvent( package, szSelectionPath, rec );
247 /* failure to set the path halts the executing of control events */
248 r = MSI_SetTargetPathW(package, argument, path);
249 msi_free(path);
250 msi_free(&rec->hdr);
251 return r;
254 static UINT ControlEvent_Reset(MSIPACKAGE* package, LPCWSTR argument,
255 msi_dialog* dialog)
257 msi_dialog_reset(dialog);
258 return ERROR_SUCCESS;
262 * Subscribed events
264 static void free_subscriber( struct subscriber *sub )
266 msi_free(sub->event);
267 msi_free(sub->control);
268 msi_free(sub->attribute);
269 msi_free(sub);
272 VOID ControlEvent_SubscribeToEvent( MSIPACKAGE *package, msi_dialog *dialog,
273 LPCWSTR event, LPCWSTR control, LPCWSTR attribute )
275 struct subscriber *sub;
277 sub = msi_alloc(sizeof (*sub));
278 if( !sub )
279 return;
280 sub->dialog = dialog;
281 sub->event = strdupW(event);
282 sub->control = strdupW(control);
283 sub->attribute = strdupW(attribute);
284 list_add_tail( &package->subscriptions, &sub->entry );
287 VOID ControlEvent_UnSubscribeToEvent( MSIPACKAGE *package, LPCWSTR event,
288 LPCWSTR control, LPCWSTR attribute )
290 struct list *i, *t;
291 struct subscriber *sub;
293 LIST_FOR_EACH_SAFE( i, t, &package->subscriptions )
295 sub = LIST_ENTRY( i, struct subscriber, entry );
297 if( lstrcmpiW(sub->control,control) )
298 continue;
299 if( lstrcmpiW(sub->attribute,attribute) )
300 continue;
301 if( lstrcmpiW(sub->event,event) )
302 continue;
303 list_remove( &sub->entry );
304 free_subscriber( sub );
308 VOID ControlEvent_FireSubscribedEvent( MSIPACKAGE *package, LPCWSTR event,
309 MSIRECORD *rec )
311 struct subscriber *sub;
313 TRACE("Firing Event %s\n",debugstr_w(event));
315 LIST_FOR_EACH_ENTRY( sub, &package->subscriptions, struct subscriber, entry )
317 if (lstrcmpiW(sub->event, event))
318 continue;
319 msi_dialog_handle_event( sub->dialog, sub->control,
320 sub->attribute, rec );
324 static VOID ControlEvent_CleanupDialogSubscriptions(MSIPACKAGE *package, LPWSTR dialog)
326 struct list *i, *t;
327 struct subscriber *sub;
329 LIST_FOR_EACH_SAFE( i, t, &package->subscriptions )
331 sub = LIST_ENTRY( i, struct subscriber, entry );
333 if ( lstrcmpW( msi_dialog_get_name( sub->dialog ), dialog ))
334 continue;
336 list_remove( &sub->entry );
337 free_subscriber( sub );
341 VOID ControlEvent_CleanupSubscriptions(MSIPACKAGE *package)
343 struct list *i, *t;
344 struct subscriber *sub;
346 LIST_FOR_EACH_SAFE( i, t, &package->subscriptions )
348 sub = LIST_ENTRY( i, struct subscriber, entry );
350 list_remove( &sub->entry );
351 free_subscriber( sub );
356 * ACTION_DialogBox()
358 * Return ERROR_SUCCESS if dialog is process and ERROR_FUNCTION_FAILED
359 * if the given parameter is not a dialog box
361 UINT ACTION_DialogBox( MSIPACKAGE* package, LPCWSTR szDialogName )
363 UINT r = ERROR_SUCCESS;
365 if( package->next_dialog )
366 ERR("Already a next dialog... ignoring it\n");
367 package->next_dialog = NULL;
370 * Dialogs are chained by filling in the next_dialog member
371 * of the package structure, then terminating the current dialog.
372 * The code below sees the next_dialog member set, and runs the
373 * next dialog.
374 * We fall out of the loop below if we come across a modeless
375 * dialog, as it returns ERROR_IO_PENDING when we try to run
376 * its message loop.
378 r = event_do_dialog( package, szDialogName, NULL, TRUE );
379 while( r == ERROR_SUCCESS && package->next_dialog )
381 LPWSTR name = package->next_dialog;
383 package->next_dialog = NULL;
384 r = event_do_dialog( package, name, NULL, TRUE );
385 msi_free( name );
388 if( r == ERROR_IO_PENDING )
389 r = ERROR_SUCCESS;
391 return r;
394 static UINT ControlEvent_SetInstallLevel(MSIPACKAGE* package, LPCWSTR argument,
395 msi_dialog* dialog)
397 int iInstallLevel = atolW(argument);
399 TRACE("Setting install level: %i\n", iInstallLevel);
401 return MSI_SetInstallLevel( package, iInstallLevel );
404 static UINT ControlEvent_DirectoryListUp(MSIPACKAGE *package, LPCWSTR argument,
405 msi_dialog *dialog)
407 return msi_dialog_directorylist_up( dialog );
410 static const struct _events Events[] = {
411 { "EndDialog",ControlEvent_EndDialog },
412 { "NewDialog",ControlEvent_NewDialog },
413 { "SpawnDialog",ControlEvent_SpawnDialog },
414 { "SpawnWaitDialog",ControlEvent_SpawnWaitDialog },
415 { "DoAction",ControlEvent_DoAction },
416 { "AddLocal",ControlEvent_AddLocal },
417 { "Remove",ControlEvent_Remove },
418 { "AddSource",ControlEvent_AddSource },
419 { "SetTargetPath",ControlEvent_SetTargetPath },
420 { "Reset",ControlEvent_Reset },
421 { "SetInstallLevel",ControlEvent_SetInstallLevel },
422 { "DirectoryListUp",ControlEvent_DirectoryListUp },
423 { "SelectionBrowse",ControlEvent_SpawnDialog },
424 { NULL,NULL },
427 UINT ControlEvent_HandleControlEvent(MSIPACKAGE *package, LPCWSTR event,
428 LPCWSTR argument, msi_dialog* dialog)
430 int i = 0;
431 UINT rc = ERROR_SUCCESS;
433 TRACE("Handling Control Event %s\n",debugstr_w(event));
434 if (!event)
435 return rc;
437 while( Events[i].event != NULL)
439 LPWSTR wevent = strdupAtoW(Events[i].event);
440 if (lstrcmpW(wevent,event)==0)
442 msi_free(wevent);
443 rc = Events[i].handler(package,argument,dialog);
444 return rc;
446 msi_free(wevent);
447 i++;
449 FIXME("unhandled control event %s arg(%s)\n",
450 debugstr_w(event), debugstr_w(argument));
451 return rc;