Keep track of what sequence we are in and register unique
[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., 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 )
61 {
62     msi_dialog *dialog;
63     UINT r;
64
65     /* kill the current modeless dialog */
66     if( package->dialog )
67         msi_dialog_destroy( package->dialog );
68     package->dialog = NULL;
69
70     /* create a new dialog */
71     dialog = msi_dialog_create( package, name,
72                                 ControlEvent_HandleControlEvent );
73     if( dialog )
74     {
75         /* modeless dialogs return an error message */
76         r = msi_dialog_run_message_loop( dialog );
77         if( r == ERROR_SUCCESS )
78             msi_dialog_destroy( dialog );
79         else
80             package->dialog = dialog;
81     }
82     else
83         r = ERROR_FUNCTION_FAILED;
84
85     return r;
86 }
87
88
89 /*
90  * End a modal dialog box
91  */
92 static UINT ControlEvent_EndDialog(MSIPACKAGE* package, LPCWSTR argument, 
93                                    msi_dialog* dialog)
94 {
95     static const WCHAR szExit[] = {
96     'E','x','i','t',0};
97     static const WCHAR szRetry[] = {
98     'R','e','t','r','y',0};
99     static const WCHAR szIgnore[] = {
100     'I','g','n','o','r','e',0};
101     static const WCHAR szReturn[] = {
102     'R','e','t','u','r','n',0};
103
104     if (lstrcmpW(argument,szExit)==0)
105         package->CurrentInstallState = ERROR_INSTALL_USEREXIT;
106     else if (lstrcmpW(argument, szRetry) == 0)
107         package->CurrentInstallState = ERROR_INSTALL_SUSPEND;
108     else if (lstrcmpW(argument, szIgnore) == 0)
109         package->CurrentInstallState = -1;
110     else if (lstrcmpW(argument, szReturn) == 0)
111         package->CurrentInstallState = ERROR_SUCCESS;
112     else
113     {
114         ERR("Unknown argument string %s\n",debugstr_w(argument));
115         package->CurrentInstallState = ERROR_FUNCTION_FAILED;
116     }
117
118     ControlEvent_CleanupSubscriptions(package);
119     msi_dialog_end_dialog( dialog );
120     return ERROR_SUCCESS;
121 }
122
123 /*
124  * transition from one modal dialog to another modal dialog
125  */
126 static UINT ControlEvent_NewDialog(MSIPACKAGE* package, LPCWSTR argument, 
127                                    msi_dialog *dialog)
128 {
129     /* store the name of the next dialog, and signal this one to end */
130     package->next_dialog = strdupW(argument);
131     ControlEvent_CleanupSubscriptions(package);
132     msi_dialog_end_dialog( dialog );
133     return ERROR_SUCCESS;
134 }
135
136 /*
137  * Create a new child dialog of an existing modal dialog
138  */
139 static UINT ControlEvent_SpawnDialog(MSIPACKAGE* package, LPCWSTR argument, 
140                               msi_dialog *dialog)
141 {
142     event_do_dialog( package, argument );
143     if( package->CurrentInstallState != ERROR_SUCCESS )
144         msi_dialog_end_dialog( dialog );
145     return ERROR_SUCCESS;
146 }
147
148 /*
149  * Creates a dialog that remains up for a period of time
150  * based on a condition
151  */
152 static UINT ControlEvent_SpawnWaitDialog(MSIPACKAGE* package, LPCWSTR argument, 
153                                   msi_dialog* dialog)
154 {
155     FIXME("Doing Nothing\n");
156     return ERROR_SUCCESS;
157 }
158
159 static UINT ControlEvent_DoAction(MSIPACKAGE* package, LPCWSTR argument, 
160                                   msi_dialog* dialog)
161 {
162     ACTION_PerformAction(package,argument,TRUE);
163     return ERROR_SUCCESS;
164 }
165
166 static UINT ControlEvent_AddLocal(MSIPACKAGE* package, LPCWSTR argument, 
167                                   msi_dialog* dialog)
168 {
169     static const WCHAR szAll[] = {'A','L','L',0};
170     int i;
171
172     if (lstrcmpW(szAll,argument))
173     {
174         MSI_SetFeatureStateW(package,argument,INSTALLSTATE_LOCAL);
175     }
176     else
177     {
178         for (i = 0; i < package->loaded_features; i++)
179         {
180             package->features[i].ActionRequest = INSTALLSTATE_LOCAL;
181             package->features[i].Action = INSTALLSTATE_LOCAL;
182         }
183         ACTION_UpdateComponentStates(package,argument);
184     }
185     return ERROR_SUCCESS;
186 }
187
188 static UINT ControlEvent_Remove(MSIPACKAGE* package, LPCWSTR argument, 
189                                 msi_dialog* dialog)
190 {
191     static const WCHAR szAll[] = {'A','L','L',0};
192     int i;
193
194     if (lstrcmpW(szAll,argument))
195     {
196         MSI_SetFeatureStateW(package,argument,INSTALLSTATE_ABSENT);
197     }
198     else
199     {
200         for (i = 0; i < package->loaded_features; i++)
201         {
202             package->features[i].ActionRequest = INSTALLSTATE_ABSENT;
203             package->features[i].Action= INSTALLSTATE_ABSENT;
204         }
205         ACTION_UpdateComponentStates(package,argument);
206     }
207     return ERROR_SUCCESS;
208 }
209
210 static UINT ControlEvent_AddSource(MSIPACKAGE* package, LPCWSTR argument, 
211                                    msi_dialog* dialog)
212 {
213     static const WCHAR szAll[] = {'A','L','L',0};
214     int i;
215
216     if (lstrcmpW(szAll,argument))
217     {
218         MSI_SetFeatureStateW(package,argument,INSTALLSTATE_SOURCE);
219     }
220     else
221     {
222         for (i = 0; i < package->loaded_features; i++)
223         {
224             package->features[i].ActionRequest = INSTALLSTATE_SOURCE;
225             package->features[i].Action = INSTALLSTATE_SOURCE;
226         }
227         ACTION_UpdateComponentStates(package,argument);
228     }
229     return ERROR_SUCCESS;
230 }
231
232 static UINT ControlEvent_SetTargetPath(MSIPACKAGE* package, LPCWSTR argument, 
233                                    msi_dialog* dialog)
234 {
235     LPWSTR path = load_dynamic_property(package,argument, NULL);
236     /* failure to set the path halts the executing of control events */
237     return MSI_SetTargetPathW(package, argument, path);
238 }
239
240 /*
241  * Subscribed events
242  */
243 static void free_subscriber( struct subscriber *sub )
244 {
245     HeapFree(GetProcessHeap(),0,sub->event);
246     HeapFree(GetProcessHeap(),0,sub->control);
247     HeapFree(GetProcessHeap(),0,sub->attribute);
248     HeapFree(GetProcessHeap(),0,sub);
249 }
250
251 VOID ControlEvent_SubscribeToEvent( MSIPACKAGE *package, LPCWSTR event,
252                                     LPCWSTR control, LPCWSTR attribute )
253 {
254     struct subscriber *sub;
255
256     sub = HeapAlloc(GetProcessHeap(),0,sizeof (*sub));
257     if( !sub )
258         return;
259     sub->event = strdupW(event);
260     sub->control = strdupW(control);
261     sub->attribute = strdupW(attribute);
262     list_add_tail( &package->subscriptions, &sub->entry );
263 }
264
265 VOID ControlEvent_UnSubscribeToEvent( MSIPACKAGE *package, LPCWSTR event,
266                                       LPCWSTR control, LPCWSTR attribute )
267 {
268     struct list *i, *t;
269     struct subscriber *sub;
270
271     LIST_FOR_EACH_SAFE( i, t, &package->subscriptions )
272     {
273         sub = LIST_ENTRY( i, struct subscriber, entry );
274
275         if( lstrcmpiW(sub->control,control) )
276             continue;
277         if( lstrcmpiW(sub->attribute,attribute) )
278             continue;
279         if( lstrcmpiW(sub->event,event) )
280             continue;
281         list_remove( &sub->entry );
282         free_subscriber( sub );
283     }
284 }
285
286 VOID ControlEvent_FireSubscribedEvent( MSIPACKAGE *package, LPCWSTR event, 
287                                        MSIRECORD *rec )
288 {
289     struct subscriber *sub;
290
291     TRACE("Firing Event %s\n",debugstr_w(event));
292
293     if (!package->dialog)
294         return;
295
296     LIST_FOR_EACH_ENTRY( sub, &package->subscriptions, struct subscriber, entry )
297     {
298         if (lstrcmpiW(sub->event, event))
299             continue;
300         msi_dialog_handle_event( package->dialog, sub->control,
301                                  sub->attribute, rec );
302     }
303 }
304
305 VOID ControlEvent_CleanupSubscriptions(MSIPACKAGE *package)
306 {
307     struct list *i, *t;
308     struct subscriber *sub;
309
310     LIST_FOR_EACH_SAFE( i, t, &package->subscriptions )
311     {
312         sub = LIST_ENTRY( i, struct subscriber, entry );
313
314         list_remove( &sub->entry );
315         free_subscriber( sub );
316     }
317 }
318
319 /*
320  * ACTION_DialogBox()
321  *
322  * Return ERROR_SUCCESS if dialog is process and ERROR_FUNCTION_FAILED
323  * if the given parameter is not a dialog box
324  */
325 UINT ACTION_DialogBox( MSIPACKAGE* package, LPCWSTR szDialogName )
326 {
327     UINT r = ERROR_SUCCESS;
328
329     if( package->next_dialog )
330         ERR("Already a next dialog... ignoring it\n");
331     package->next_dialog = NULL;
332
333     /*
334      * Dialogs are chained by filling in the next_dialog member
335      *  of the package structure, then terminating the current dialog.
336      *  The code below sees the next_dialog member set, and runs the
337      *  next dialog.
338      * We fall out of the loop below if we come across a modeless
339      *  dialog, as it returns ERROR_IO_PENDING when we try to run
340      *  its message loop.
341      */
342     r = event_do_dialog( package, szDialogName );
343     while( r == ERROR_SUCCESS && package->next_dialog )
344     {
345         LPWSTR name = package->next_dialog;
346
347         package->next_dialog = NULL;
348         r = event_do_dialog( package, name );
349         HeapFree( GetProcessHeap(), 0, name );
350     }
351
352     if( r == ERROR_IO_PENDING )
353         r = ERROR_SUCCESS;
354
355     return r;
356 }
357
358 struct _events Events[] = {
359     { "EndDialog",ControlEvent_EndDialog },
360     { "NewDialog",ControlEvent_NewDialog },
361     { "SpawnDialog",ControlEvent_SpawnDialog },
362     { "SpawnWaitDialog",ControlEvent_SpawnWaitDialog },
363     { "DoAction",ControlEvent_DoAction },
364     { "AddLocal",ControlEvent_AddLocal },
365     { "Remove",ControlEvent_Remove },
366     { "AddSource",ControlEvent_AddSource },
367     { "SetTargetPath",ControlEvent_SetTargetPath },
368     { NULL,NULL },
369 };
370
371 UINT ControlEvent_HandleControlEvent(MSIPACKAGE *package, LPCWSTR event,
372                                      LPCWSTR argument, msi_dialog* dialog)
373 {
374     int i = 0;
375     UINT rc = ERROR_SUCCESS;
376
377     TRACE("Handling Control Event %s\n",debugstr_w(event));
378     if (!event)
379         return rc;
380
381     while( Events[i].event != NULL)
382     {
383         LPWSTR wevent = strdupAtoW(Events[i].event);
384         if (lstrcmpW(wevent,event)==0)
385         {
386             HeapFree(GetProcessHeap(),0,wevent);
387             rc = Events[i].handler(package,argument,dialog);
388             return rc;
389         }
390         HeapFree(GetProcessHeap(),0,wevent);
391         i++;
392     }
393     FIXME("unhandled control event %s arg(%s)\n",
394           debugstr_w(event), debugstr_w(argument));
395     return rc;
396 }