msi: Handle the SelectionBrowse event using ControlEvent_SpawnDialog.
[wine] / dlls / 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
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 #include "wine/unicode.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(msi);
41
42 typedef UINT (*EVENTHANDLER)(MSIPACKAGE*,LPCWSTR,msi_dialog *);
43
44 struct _events {
45     LPCSTR event;
46     EVENTHANDLER handler;
47 };
48
49 struct subscriber {
50     struct list entry;
51     msi_dialog *dialog;
52     LPWSTR event;
53     LPWSTR control;
54     LPWSTR attribute;
55 };
56
57 UINT ControlEvent_HandleControlEvent(MSIPACKAGE *, LPCWSTR, LPCWSTR, msi_dialog*);
58
59 /*
60  * Create a dialog box and run it if it's modal
61  */
62 static UINT event_do_dialog( MSIPACKAGE *package, LPCWSTR name, msi_dialog *parent, BOOL destroy_modeless )
63 {
64     msi_dialog *dialog;
65     UINT r;
66
67     /* create a new dialog */
68     dialog = msi_dialog_create( package, name, parent,
69                                 ControlEvent_HandleControlEvent );
70     if( dialog )
71     {
72         /* kill the current modeless dialog */
73         if( destroy_modeless && package->dialog )
74         {
75             msi_dialog_destroy( package->dialog );
76             package->dialog = NULL;
77         }
78
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;
85     }
86     else
87         r = ERROR_FUNCTION_FAILED;
88
89     return r;
90 }
91
92
93 /*
94  * End a modal dialog box
95  */
96 static UINT ControlEvent_EndDialog(MSIPACKAGE* package, LPCWSTR argument, 
97                                    msi_dialog* dialog)
98 {
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};
107
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)
115     {
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;
120     }
121     else
122     {
123         ERR("Unknown argument string %s\n",debugstr_w(argument));
124         package->CurrentInstallState = ERROR_FUNCTION_FAILED;
125     }
126
127     ControlEvent_CleanupSubscriptions(package);
128     msi_dialog_end_dialog( dialog );
129     return ERROR_SUCCESS;
130 }
131
132 /*
133  * transition from one modal dialog to another modal dialog
134  */
135 static UINT ControlEvent_NewDialog(MSIPACKAGE* package, LPCWSTR argument, 
136                                    msi_dialog *dialog)
137 {
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;
143 }
144
145 /*
146  * Create a new child dialog of an existing modal dialog
147  */
148 static UINT ControlEvent_SpawnDialog(MSIPACKAGE* package, LPCWSTR argument, 
149                               msi_dialog *dialog)
150 {
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;
156 }
157
158 /*
159  * Creates a dialog that remains up for a period of time
160  * based on a condition
161  */
162 static UINT ControlEvent_SpawnWaitDialog(MSIPACKAGE* package, LPCWSTR argument, 
163                                   msi_dialog* dialog)
164 {
165     FIXME("Doing Nothing\n");
166     return ERROR_SUCCESS;
167 }
168
169 static UINT ControlEvent_DoAction(MSIPACKAGE* package, LPCWSTR argument, 
170                                   msi_dialog* dialog)
171 {
172     ACTION_PerformAction(package,argument,TRUE);
173     return ERROR_SUCCESS;
174 }
175
176 static UINT ControlEvent_AddLocal(MSIPACKAGE* package, LPCWSTR argument, 
177                                   msi_dialog* dialog)
178 {
179     static const WCHAR szAll[] = {'A','L','L',0};
180     MSIFEATURE *feature = NULL;
181
182     if (lstrcmpW(szAll,argument))
183     {
184         MSI_SetFeatureStateW(package,argument,INSTALLSTATE_LOCAL);
185     }
186     else
187     {
188         LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
189         {
190             feature->ActionRequest = INSTALLSTATE_LOCAL;
191             feature->Action = INSTALLSTATE_LOCAL;
192         }
193         ACTION_UpdateComponentStates(package,argument);
194     }
195     return ERROR_SUCCESS;
196 }
197
198 static UINT ControlEvent_Remove(MSIPACKAGE* package, LPCWSTR argument, 
199                                 msi_dialog* dialog)
200 {
201     static const WCHAR szAll[] = {'A','L','L',0};
202     MSIFEATURE *feature = NULL;
203
204     if (lstrcmpW(szAll,argument))
205     {
206         MSI_SetFeatureStateW(package,argument,INSTALLSTATE_ABSENT);
207     }
208     else
209     {
210         LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
211         {
212             feature->ActionRequest = INSTALLSTATE_ABSENT;
213             feature->Action= INSTALLSTATE_ABSENT;
214         }
215         ACTION_UpdateComponentStates(package,argument);
216     }
217     return ERROR_SUCCESS;
218 }
219
220 static UINT ControlEvent_AddSource(MSIPACKAGE* package, LPCWSTR argument, 
221                                    msi_dialog* dialog)
222 {
223     static const WCHAR szAll[] = {'A','L','L',0};
224     MSIFEATURE *feature = NULL;
225
226     if (lstrcmpW(szAll,argument))
227     {
228         MSI_SetFeatureStateW(package,argument,INSTALLSTATE_SOURCE);
229     }
230     else
231     {
232         LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
233         {
234             feature->ActionRequest = INSTALLSTATE_SOURCE;
235             feature->Action = INSTALLSTATE_SOURCE;
236         }
237         ACTION_UpdateComponentStates(package,argument);
238     }
239     return ERROR_SUCCESS;
240 }
241
242 static UINT ControlEvent_SetTargetPath(MSIPACKAGE* package, LPCWSTR argument, 
243                                    msi_dialog* dialog)
244 {
245     LPWSTR path = msi_dup_property( package, argument );
246     MSIRECORD *rec = MSI_CreateRecord( 1 );
247     UINT r;
248
249     static const WCHAR szSelectionPath[] = {'S','e','l','e','c','t','i','o','n','P','a','t','h',0};
250
251     MSI_RecordSetStringW( rec, 1, path );
252     ControlEvent_FireSubscribedEvent( package, szSelectionPath, rec );
253
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;
259 }
260
261 static UINT ControlEvent_Reset(MSIPACKAGE* package, LPCWSTR argument, 
262                                    msi_dialog* dialog)
263 {
264     msi_dialog_reset(dialog);
265     return ERROR_SUCCESS;
266 }
267
268 /*
269  * Subscribed events
270  */
271 static void free_subscriber( struct subscriber *sub )
272 {
273     msi_free(sub->event);
274     msi_free(sub->control);
275     msi_free(sub->attribute);
276     msi_free(sub);
277 }
278
279 VOID ControlEvent_SubscribeToEvent( MSIPACKAGE *package, msi_dialog *dialog,
280                                     LPCWSTR event, LPCWSTR control, LPCWSTR attribute )
281 {
282     struct subscriber *sub;
283
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 );
292 }
293
294 VOID ControlEvent_UnSubscribeToEvent( MSIPACKAGE *package, LPCWSTR event,
295                                       LPCWSTR control, LPCWSTR attribute )
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( 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 );
312     }
313 }
314
315 VOID ControlEvent_FireSubscribedEvent( MSIPACKAGE *package, LPCWSTR event, 
316                                        MSIRECORD *rec )
317 {
318     struct subscriber *sub;
319
320     TRACE("Firing Event %s\n",debugstr_w(event));
321
322     LIST_FOR_EACH_ENTRY( sub, &package->subscriptions, struct subscriber, entry )
323     {
324         if (lstrcmpiW(sub->event, event))
325             continue;
326         msi_dialog_handle_event( sub->dialog, sub->control,
327                                  sub->attribute, rec );
328     }
329 }
330
331 VOID ControlEvent_CleanupSubscriptions(MSIPACKAGE *package)
332 {
333     struct list *i, *t;
334     struct subscriber *sub;
335
336     LIST_FOR_EACH_SAFE( i, t, &package->subscriptions )
337     {
338         sub = LIST_ENTRY( i, struct subscriber, entry );
339
340         list_remove( &sub->entry );
341         free_subscriber( sub );
342     }
343 }
344
345 /*
346  * ACTION_DialogBox()
347  *
348  * Return ERROR_SUCCESS if dialog is process and ERROR_FUNCTION_FAILED
349  * if the given parameter is not a dialog box
350  */
351 UINT ACTION_DialogBox( MSIPACKAGE* package, LPCWSTR szDialogName )
352 {
353     UINT r = ERROR_SUCCESS;
354
355     if( package->next_dialog )
356         ERR("Already a next dialog... ignoring it\n");
357     package->next_dialog = NULL;
358
359     /*
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.
367      */
368     r = event_do_dialog( package, szDialogName, NULL, TRUE );
369     while( r == ERROR_SUCCESS && package->next_dialog )
370     {
371         LPWSTR name = package->next_dialog;
372
373         package->next_dialog = NULL;
374         r = event_do_dialog( package, name, NULL, TRUE );
375         msi_free( name );
376     }
377
378     if( r == ERROR_IO_PENDING )
379         r = ERROR_SUCCESS;
380
381     return r;
382 }
383
384 static UINT ControlEvent_SetInstallLevel(MSIPACKAGE* package, LPCWSTR argument,
385                                           msi_dialog* dialog)
386 {
387     int iInstallLevel = atolW(argument);
388
389     TRACE("Setting install level: %i\n", iInstallLevel);
390
391     return MSI_SetInstallLevel( package, iInstallLevel );
392 }
393
394 static UINT ControlEvent_DirectoryListUp(MSIPACKAGE *package, LPCWSTR argument,
395                                          msi_dialog *dialog)
396 {
397     return msi_dialog_directorylist_up( dialog );
398 }
399
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     { "SelectionBrowse",ControlEvent_SpawnDialog },
414     { NULL,NULL },
415 };
416
417 UINT ControlEvent_HandleControlEvent(MSIPACKAGE *package, LPCWSTR event,
418                                      LPCWSTR argument, msi_dialog* dialog)
419 {
420     int i = 0;
421     UINT rc = ERROR_SUCCESS;
422
423     TRACE("Handling Control Event %s\n",debugstr_w(event));
424     if (!event)
425         return rc;
426
427     while( Events[i].event != NULL)
428     {
429         LPWSTR wevent = strdupAtoW(Events[i].event);
430         if (lstrcmpW(wevent,event)==0)
431         {
432             msi_free(wevent);
433             rc = Events[i].handler(package,argument,dialog);
434             return rc;
435         }
436         msi_free(wevent);
437         i++;
438     }
439     FIXME("unhandled control event %s arg(%s)\n",
440           debugstr_w(event), debugstr_w(argument));
441     return rc;
442 }