winscard: Add stub for SCardReleaseStartedEvent.
[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 #include <stdarg.h>
22 #include <stdio.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "winreg.h"
28 #include "msi.h"
29 #include "msipriv.h"
30
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(msi);
35
36 typedef UINT (*EVENTHANDLER)(MSIPACKAGE*,LPCWSTR,msi_dialog *);
37
38 struct _events {
39     LPCSTR event;
40     EVENTHANDLER handler;
41 };
42
43 struct subscriber {
44     struct list entry;
45     msi_dialog *dialog;
46     LPWSTR event;
47     LPWSTR control;
48     LPWSTR attribute;
49 };
50
51 static UINT ControlEvent_HandleControlEvent(MSIPACKAGE *, LPCWSTR, LPCWSTR, msi_dialog*);
52
53 /*
54  * Create a dialog box and run it if it's modal
55  */
56 static UINT event_do_dialog( MSIPACKAGE *package, LPCWSTR name, msi_dialog *parent, BOOL destroy_modeless )
57 {
58     msi_dialog *dialog;
59     UINT r;
60
61     /* create a new dialog */
62     dialog = msi_dialog_create( package, name, parent,
63                                 ControlEvent_HandleControlEvent );
64     if( dialog )
65     {
66         /* kill the current modeless dialog */
67         if( destroy_modeless && package->dialog )
68         {
69             msi_dialog_destroy( package->dialog );
70             package->dialog = NULL;
71         }
72
73         /* modeless dialogs return an error message */
74         r = msi_dialog_run_message_loop( dialog );
75         if( r == ERROR_SUCCESS )
76             msi_dialog_destroy( dialog );
77         else
78             package->dialog = dialog;
79     }
80     else
81         r = ERROR_FUNCTION_FAILED;
82
83     return r;
84 }
85
86
87 /*
88  * End a modal dialog box
89  */
90 static UINT ControlEvent_EndDialog(MSIPACKAGE* package, LPCWSTR argument, 
91                                    msi_dialog* dialog)
92 {
93     static const WCHAR szExit[] = {'E','x','i','t',0};
94     static const WCHAR szRetry[] = {'R','e','t','r','y',0};
95     static const WCHAR szIgnore[] = {'I','g','n','o','r','e',0};
96     static const WCHAR szReturn[] = {'R','e','t','u','r','n',0};
97
98     if (!strcmpW( argument, szExit ))
99         package->CurrentInstallState = ERROR_INSTALL_USEREXIT;
100     else if (!strcmpW( argument, szRetry ))
101         package->CurrentInstallState = ERROR_INSTALL_SUSPEND;
102     else if (!strcmpW( argument, szIgnore ))
103         package->CurrentInstallState = ERROR_SUCCESS;
104     else if (!strcmpW( argument, szReturn ))
105     {
106         msi_dialog *parent = msi_dialog_get_parent(dialog);
107         msi_free(package->next_dialog);
108         package->next_dialog = (parent) ? strdupW(msi_dialog_get_name(parent)) : NULL;
109         package->CurrentInstallState = ERROR_SUCCESS;
110     }
111     else
112     {
113         ERR("Unknown argument string %s\n",debugstr_w(argument));
114         package->CurrentInstallState = ERROR_FUNCTION_FAILED;
115     }
116
117     ControlEvent_CleanupDialogSubscriptions(package, msi_dialog_get_name( dialog ));
118     msi_dialog_end_dialog( dialog );
119     return ERROR_SUCCESS;
120 }
121
122 /*
123  * transition from one modal dialog to another modal dialog
124  */
125 static UINT ControlEvent_NewDialog(MSIPACKAGE* package, LPCWSTR argument, 
126                                    msi_dialog *dialog)
127 {
128     /* store the name of the next dialog, and signal this one to end */
129     package->next_dialog = strdupW(argument);
130     ControlEvent_CleanupSubscriptions(package);
131     msi_dialog_end_dialog( dialog );
132     return ERROR_SUCCESS;
133 }
134
135 /*
136  * Create a new child dialog of an existing modal dialog
137  */
138 static UINT ControlEvent_SpawnDialog(MSIPACKAGE* package, LPCWSTR argument, 
139                               msi_dialog *dialog)
140 {
141     /* don't destroy a modeless dialogs that might be our parent */
142     event_do_dialog( package, argument, dialog, FALSE );
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, -1);
163     return ERROR_SUCCESS;
164 }
165
166 static UINT ControlEvent_AddLocal(MSIPACKAGE* package, LPCWSTR argument, 
167                                   msi_dialog* dialog)
168 {
169     MSIFEATURE *feature = NULL;
170
171     if (strcmpW( szAll, argument ))
172     {
173         MSI_SetFeatureStateW(package,argument,INSTALLSTATE_LOCAL);
174     }
175     else
176     {
177         LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
178             msi_feature_set_state(package, feature, INSTALLSTATE_LOCAL);
179
180         ACTION_UpdateComponentStates(package,argument);
181     }
182     return ERROR_SUCCESS;
183 }
184
185 static UINT ControlEvent_Remove(MSIPACKAGE* package, LPCWSTR argument, 
186                                 msi_dialog* dialog)
187 {
188     MSIFEATURE *feature = NULL;
189
190     if (strcmpW( szAll, argument ))
191     {
192         MSI_SetFeatureStateW(package,argument,INSTALLSTATE_ABSENT);
193     }
194     else
195     {
196         LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
197             msi_feature_set_state(package, feature, INSTALLSTATE_ABSENT);
198
199         ACTION_UpdateComponentStates(package,argument);
200     }
201     return ERROR_SUCCESS;
202 }
203
204 static UINT ControlEvent_AddSource(MSIPACKAGE* package, LPCWSTR argument, 
205                                    msi_dialog* dialog)
206 {
207     MSIFEATURE *feature = NULL;
208
209     if (strcmpW( szAll, argument ))
210     {
211         MSI_SetFeatureStateW(package,argument,INSTALLSTATE_SOURCE);
212     }
213     else
214     {
215         LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
216             msi_feature_set_state(package, feature, INSTALLSTATE_SOURCE);
217         ACTION_UpdateComponentStates(package,argument);
218     }
219     return ERROR_SUCCESS;
220 }
221
222 static UINT ControlEvent_SetTargetPath(MSIPACKAGE* package, LPCWSTR argument, 
223                                    msi_dialog* dialog)
224 {
225     LPWSTR path = msi_dup_property( package->db, argument );
226     MSIRECORD *rec = MSI_CreateRecord( 1 );
227     UINT r;
228
229     static const WCHAR szSelectionPath[] = {'S','e','l','e','c','t','i','o','n','P','a','t','h',0};
230
231     MSI_RecordSetStringW( rec, 1, path );
232     ControlEvent_FireSubscribedEvent( package, szSelectionPath, rec );
233
234     /* failure to set the path halts the executing of control events */
235     r = MSI_SetTargetPathW(package, argument, path);
236     msi_free(path);
237     msi_free(&rec->hdr);
238     return r;
239 }
240
241 static UINT ControlEvent_Reset(MSIPACKAGE* package, LPCWSTR argument, 
242                                    msi_dialog* dialog)
243 {
244     msi_dialog_reset(dialog);
245     return ERROR_SUCCESS;
246 }
247
248 /*
249  * Subscribed events
250  */
251 static void free_subscriber( struct subscriber *sub )
252 {
253     msi_free(sub->event);
254     msi_free(sub->control);
255     msi_free(sub->attribute);
256     msi_free(sub);
257 }
258
259 VOID ControlEvent_SubscribeToEvent( MSIPACKAGE *package, msi_dialog *dialog,
260                                     LPCWSTR event, LPCWSTR control, LPCWSTR attribute )
261 {
262     struct subscriber *sub;
263
264     sub = msi_alloc(sizeof (*sub));
265     if( !sub )
266         return;
267     sub->dialog = dialog;
268     sub->event = strdupW(event);
269     sub->control = strdupW(control);
270     sub->attribute = strdupW(attribute);
271     list_add_tail( &package->subscriptions, &sub->entry );
272 }
273
274 VOID ControlEvent_FireSubscribedEvent( MSIPACKAGE *package, LPCWSTR event, 
275                                        MSIRECORD *rec )
276 {
277     struct subscriber *sub;
278
279     TRACE("Firing Event %s\n",debugstr_w(event));
280
281     LIST_FOR_EACH_ENTRY( sub, &package->subscriptions, struct subscriber, entry )
282     {
283         if (strcmpiW( sub->event, event ))
284             continue;
285         msi_dialog_handle_event( sub->dialog, sub->control, sub->attribute, rec );
286     }
287 }
288
289 VOID ControlEvent_CleanupDialogSubscriptions(MSIPACKAGE *package, LPWSTR dialog)
290 {
291     struct list *i, *t;
292     struct subscriber *sub;
293
294     LIST_FOR_EACH_SAFE( i, t, &package->subscriptions )
295     {
296         sub = LIST_ENTRY( i, struct subscriber, entry );
297
298         if (strcmpW( msi_dialog_get_name( sub->dialog ), dialog ))
299             continue;
300
301         list_remove( &sub->entry );
302         free_subscriber( sub );
303     }
304 }
305
306 VOID ControlEvent_CleanupSubscriptions(MSIPACKAGE *package)
307 {
308     struct list *i, *t;
309     struct subscriber *sub;
310
311     LIST_FOR_EACH_SAFE( i, t, &package->subscriptions )
312     {
313         sub = LIST_ENTRY( i, struct subscriber, entry );
314
315         list_remove( &sub->entry );
316         free_subscriber( sub );
317     }
318 }
319
320 /*
321  * ACTION_DialogBox()
322  *
323  * Return ERROR_SUCCESS if dialog is process and ERROR_FUNCTION_FAILED
324  * if the given parameter is not a dialog box
325  */
326 UINT ACTION_DialogBox( MSIPACKAGE* package, LPCWSTR szDialogName )
327 {
328     UINT r = ERROR_SUCCESS;
329
330     if( package->next_dialog )
331         ERR("Already a next dialog... ignoring it\n");
332     package->next_dialog = NULL;
333
334     /*
335      * Dialogs are chained by filling in the next_dialog member
336      *  of the package structure, then terminating the current dialog.
337      *  The code below sees the next_dialog member set, and runs the
338      *  next dialog.
339      * We fall out of the loop below if we come across a modeless
340      *  dialog, as it returns ERROR_IO_PENDING when we try to run
341      *  its message loop.
342      */
343     r = event_do_dialog( package, szDialogName, NULL, TRUE );
344     while( r == ERROR_SUCCESS && package->next_dialog )
345     {
346         LPWSTR name = package->next_dialog;
347
348         package->next_dialog = NULL;
349         r = event_do_dialog( package, name, NULL, TRUE );
350         msi_free( name );
351     }
352
353     if( r == ERROR_IO_PENDING )
354         r = ERROR_SUCCESS;
355
356     return r;
357 }
358
359 static UINT ControlEvent_SetInstallLevel(MSIPACKAGE* package, LPCWSTR argument,
360                                           msi_dialog* dialog)
361 {
362     int iInstallLevel = atolW(argument);
363
364     TRACE("Setting install level: %i\n", iInstallLevel);
365
366     return MSI_SetInstallLevel( package, iInstallLevel );
367 }
368
369 static UINT ControlEvent_DirectoryListUp(MSIPACKAGE *package, LPCWSTR argument,
370                                          msi_dialog *dialog)
371 {
372     return msi_dialog_directorylist_up( dialog );
373 }
374
375 static UINT ControlEvent_ReinstallMode(MSIPACKAGE *package, LPCWSTR argument,
376                                        msi_dialog *dialog)
377 {
378     return msi_set_property( package->db, szReinstallMode, argument );
379 }
380
381 static UINT ControlEvent_Reinstall( MSIPACKAGE *package, LPCWSTR argument,
382                                     msi_dialog *dialog )
383 {
384     return msi_set_property( package->db, szReinstall, argument );
385 }
386
387 static UINT ControlEvent_ValidateProductID(MSIPACKAGE *package, LPCWSTR argument,
388                                            msi_dialog *dialog)
389 {
390     LPWSTR key, template;
391     UINT ret = ERROR_SUCCESS;
392
393     template = msi_dup_property( package->db, szPIDTemplate );
394     key = msi_dup_property( package->db, szPIDKEY );
395
396     if (key && template)
397     {
398         FIXME( "partial stub: template %s key %s\n", debugstr_w(template), debugstr_w(key) );
399         ret = msi_set_property( package->db, szProductID, key );
400     }
401     msi_free( template );
402     msi_free( key );
403     return ret;
404 }
405
406 static const struct _events Events[] = {
407     { "EndDialog",ControlEvent_EndDialog },
408     { "NewDialog",ControlEvent_NewDialog },
409     { "SpawnDialog",ControlEvent_SpawnDialog },
410     { "SpawnWaitDialog",ControlEvent_SpawnWaitDialog },
411     { "DoAction",ControlEvent_DoAction },
412     { "AddLocal",ControlEvent_AddLocal },
413     { "Remove",ControlEvent_Remove },
414     { "AddSource",ControlEvent_AddSource },
415     { "SetTargetPath",ControlEvent_SetTargetPath },
416     { "Reset",ControlEvent_Reset },
417     { "SetInstallLevel",ControlEvent_SetInstallLevel },
418     { "DirectoryListUp",ControlEvent_DirectoryListUp },
419     { "SelectionBrowse",ControlEvent_SpawnDialog },
420     { "ReinstallMode",ControlEvent_ReinstallMode },
421     { "Reinstall",ControlEvent_Reinstall },
422     { "ValidateProductID",ControlEvent_ValidateProductID },
423     { NULL,NULL },
424 };
425
426 UINT ControlEvent_HandleControlEvent(MSIPACKAGE *package, LPCWSTR event,
427                                      LPCWSTR argument, msi_dialog* dialog)
428 {
429     int i = 0;
430     UINT rc = ERROR_SUCCESS;
431
432     TRACE("Handling Control Event %s\n",debugstr_w(event));
433     if (!event)
434         return rc;
435
436     while( Events[i].event != NULL)
437     {
438         LPWSTR wevent = strdupAtoW(Events[i].event);
439         if (!strcmpW( wevent, event ))
440         {
441             msi_free(wevent);
442             rc = Events[i].handler(package,argument,dialog);
443             return rc;
444         }
445         msi_free(wevent);
446         i++;
447     }
448     FIXME("unhandled control event %s arg(%s)\n",
449           debugstr_w(event), debugstr_w(argument));
450     return rc;
451 }