msi: Publish the SelectionPath event in the SetTargetPath event.
[wine/dibdrv.git] / dlls / msi / events.c
blob53a6f6237df07c93951c4322fbf78d5ac12d8503
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"
35 #include "action.h"
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msi);
42 typedef UINT (*EVENTHANDLER)(MSIPACKAGE*,LPCWSTR,msi_dialog *);
44 struct _events {
45 LPCSTR event;
46 EVENTHANDLER handler;
49 struct subscriber {
50 struct list entry;
51 msi_dialog *dialog;
52 LPWSTR event;
53 LPWSTR control;
54 LPWSTR attribute;
57 UINT ControlEvent_HandleControlEvent(MSIPACKAGE *, LPCWSTR, LPCWSTR, msi_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_CleanupSubscriptions(package);
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 )
190 feature->ActionRequest = INSTALLSTATE_LOCAL;
191 feature->Action = INSTALLSTATE_LOCAL;
193 ACTION_UpdateComponentStates(package,argument);
195 return ERROR_SUCCESS;
198 static UINT ControlEvent_Remove(MSIPACKAGE* package, LPCWSTR argument,
199 msi_dialog* dialog)
201 static const WCHAR szAll[] = {'A','L','L',0};
202 MSIFEATURE *feature = NULL;
204 if (lstrcmpW(szAll,argument))
206 MSI_SetFeatureStateW(package,argument,INSTALLSTATE_ABSENT);
208 else
210 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
212 feature->ActionRequest = INSTALLSTATE_ABSENT;
213 feature->Action= INSTALLSTATE_ABSENT;
215 ACTION_UpdateComponentStates(package,argument);
217 return ERROR_SUCCESS;
220 static UINT ControlEvent_AddSource(MSIPACKAGE* package, LPCWSTR argument,
221 msi_dialog* dialog)
223 static const WCHAR szAll[] = {'A','L','L',0};
224 MSIFEATURE *feature = NULL;
226 if (lstrcmpW(szAll,argument))
228 MSI_SetFeatureStateW(package,argument,INSTALLSTATE_SOURCE);
230 else
232 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
234 feature->ActionRequest = INSTALLSTATE_SOURCE;
235 feature->Action = INSTALLSTATE_SOURCE;
237 ACTION_UpdateComponentStates(package,argument);
239 return ERROR_SUCCESS;
242 static UINT ControlEvent_SetTargetPath(MSIPACKAGE* package, LPCWSTR argument,
243 msi_dialog* dialog)
245 LPWSTR path = msi_dup_property( package, argument );
246 MSIRECORD *rec = MSI_CreateRecord( 1 );
247 UINT r;
249 static const WCHAR szSelectionPath[] = {'S','e','l','e','c','t','i','o','n','P','a','t','h',0};
251 MSI_RecordSetStringW( rec, 1, path );
252 ControlEvent_FireSubscribedEvent( package, szSelectionPath, rec );
254 /* failure to set the path halts the executing of control events */
255 r = MSI_SetTargetPathW(package, argument, path);
256 msi_free(path);
257 msi_free(&rec->hdr);
258 return r;
261 static UINT ControlEvent_Reset(MSIPACKAGE* package, LPCWSTR argument,
262 msi_dialog* dialog)
264 msi_dialog_reset(dialog);
265 return ERROR_SUCCESS;
269 * Subscribed events
271 static void free_subscriber( struct subscriber *sub )
273 msi_free(sub->event);
274 msi_free(sub->control);
275 msi_free(sub->attribute);
276 msi_free(sub);
279 VOID ControlEvent_SubscribeToEvent( MSIPACKAGE *package, msi_dialog *dialog,
280 LPCWSTR event, LPCWSTR control, LPCWSTR attribute )
282 struct subscriber *sub;
284 sub = msi_alloc(sizeof (*sub));
285 if( !sub )
286 return;
287 sub->dialog = dialog;
288 sub->event = strdupW(event);
289 sub->control = strdupW(control);
290 sub->attribute = strdupW(attribute);
291 list_add_tail( &package->subscriptions, &sub->entry );
294 VOID ControlEvent_UnSubscribeToEvent( MSIPACKAGE *package, LPCWSTR event,
295 LPCWSTR control, LPCWSTR attribute )
297 struct list *i, *t;
298 struct subscriber *sub;
300 LIST_FOR_EACH_SAFE( i, t, &package->subscriptions )
302 sub = LIST_ENTRY( i, struct subscriber, entry );
304 if( lstrcmpiW(sub->control,control) )
305 continue;
306 if( lstrcmpiW(sub->attribute,attribute) )
307 continue;
308 if( lstrcmpiW(sub->event,event) )
309 continue;
310 list_remove( &sub->entry );
311 free_subscriber( sub );
315 VOID ControlEvent_FireSubscribedEvent( MSIPACKAGE *package, LPCWSTR event,
316 MSIRECORD *rec )
318 struct subscriber *sub;
320 TRACE("Firing Event %s\n",debugstr_w(event));
322 LIST_FOR_EACH_ENTRY( sub, &package->subscriptions, struct subscriber, entry )
324 if (lstrcmpiW(sub->event, event))
325 continue;
326 msi_dialog_handle_event( sub->dialog, sub->control,
327 sub->attribute, rec );
331 VOID ControlEvent_CleanupSubscriptions(MSIPACKAGE *package)
333 struct list *i, *t;
334 struct subscriber *sub;
336 LIST_FOR_EACH_SAFE( i, t, &package->subscriptions )
338 sub = LIST_ENTRY( i, struct subscriber, entry );
340 list_remove( &sub->entry );
341 free_subscriber( sub );
346 * ACTION_DialogBox()
348 * Return ERROR_SUCCESS if dialog is process and ERROR_FUNCTION_FAILED
349 * if the given parameter is not a dialog box
351 UINT ACTION_DialogBox( MSIPACKAGE* package, LPCWSTR szDialogName )
353 UINT r = ERROR_SUCCESS;
355 if( package->next_dialog )
356 ERR("Already a next dialog... ignoring it\n");
357 package->next_dialog = NULL;
360 * Dialogs are chained by filling in the next_dialog member
361 * of the package structure, then terminating the current dialog.
362 * The code below sees the next_dialog member set, and runs the
363 * next dialog.
364 * We fall out of the loop below if we come across a modeless
365 * dialog, as it returns ERROR_IO_PENDING when we try to run
366 * its message loop.
368 r = event_do_dialog( package, szDialogName, NULL, TRUE );
369 while( r == ERROR_SUCCESS && package->next_dialog )
371 LPWSTR name = package->next_dialog;
373 package->next_dialog = NULL;
374 r = event_do_dialog( package, name, NULL, TRUE );
375 msi_free( name );
378 if( r == ERROR_IO_PENDING )
379 r = ERROR_SUCCESS;
381 return r;
384 static UINT ControlEvent_SetInstallLevel(MSIPACKAGE* package, LPCWSTR argument,
385 msi_dialog* dialog)
387 int iInstallLevel = atolW(argument);
389 TRACE("Setting install level: %i\n", iInstallLevel);
391 return MSI_SetInstallLevel( package, iInstallLevel );
394 static UINT ControlEvent_DirectoryListUp(MSIPACKAGE *package, LPCWSTR argument,
395 msi_dialog *dialog)
397 return msi_dialog_directorylist_up( dialog );
400 static const struct _events Events[] = {
401 { "EndDialog",ControlEvent_EndDialog },
402 { "NewDialog",ControlEvent_NewDialog },
403 { "SpawnDialog",ControlEvent_SpawnDialog },
404 { "SpawnWaitDialog",ControlEvent_SpawnWaitDialog },
405 { "DoAction",ControlEvent_DoAction },
406 { "AddLocal",ControlEvent_AddLocal },
407 { "Remove",ControlEvent_Remove },
408 { "AddSource",ControlEvent_AddSource },
409 { "SetTargetPath",ControlEvent_SetTargetPath },
410 { "Reset",ControlEvent_Reset },
411 { "SetInstallLevel",ControlEvent_SetInstallLevel },
412 { "DirectoryListUp",ControlEvent_DirectoryListUp },
413 { NULL,NULL },
416 UINT ControlEvent_HandleControlEvent(MSIPACKAGE *package, LPCWSTR event,
417 LPCWSTR argument, msi_dialog* dialog)
419 int i = 0;
420 UINT rc = ERROR_SUCCESS;
422 TRACE("Handling Control Event %s\n",debugstr_w(event));
423 if (!event)
424 return rc;
426 while( Events[i].event != NULL)
428 LPWSTR wevent = strdupAtoW(Events[i].event);
429 if (lstrcmpW(wevent,event)==0)
431 msi_free(wevent);
432 rc = Events[i].handler(package,argument,dialog);
433 return rc;
435 msi_free(wevent);
436 i++;
438 FIXME("unhandled control event %s arg(%s)\n",
439 debugstr_w(event), debugstr_w(argument));
440 return rc;