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