Implement asn.1 encoding/decoding of times, with tests.
[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 void (*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 VOID 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 VOID 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 }
121
122 /*
123  * transition from one modal dialog to another modal dialog
124  */
125 static VOID 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 }
133
134 /*
135  * Create a new child dialog of an existing modal dialog
136  */
137 static VOID ControlEvent_SpawnDialog(MSIPACKAGE* package, LPCWSTR argument, 
138                               msi_dialog *dialog)
139 {
140     event_do_dialog( package, argument );
141     if( package->CurrentInstallState != ERROR_SUCCESS )
142         msi_dialog_end_dialog( dialog );
143 }
144
145 /*
146  * Creates a dialog that remains up for a period of time
147  * based on a condition
148  */
149 static VOID ControlEvent_SpawnWaitDialog(MSIPACKAGE* package, LPCWSTR argument, 
150                                   msi_dialog* dialog)
151 {
152     FIXME("Doing Nothing\n");
153 }
154
155 static VOID ControlEvent_DoAction(MSIPACKAGE* package, LPCWSTR argument, 
156                                   msi_dialog* dialog)
157 {
158     ACTION_PerformAction(package,argument,TRUE);
159 }
160
161 static VOID ControlEvent_AddLocal(MSIPACKAGE* package, LPCWSTR argument, 
162                                   msi_dialog* dialog)
163 {
164     static const WCHAR szAll[] = {'A','L','L',0};
165     int i;
166
167     if (lstrcmpW(szAll,argument))
168     {
169         MSI_SetFeatureStateW(package,argument,INSTALLSTATE_LOCAL);
170     }
171     else
172     {
173         for (i = 0; i < package->loaded_features; i++)
174         {
175             package->features[i].ActionRequest = INSTALLSTATE_LOCAL;
176             package->features[i].Action = INSTALLSTATE_LOCAL;
177         }
178         ACTION_UpdateComponentStates(package,argument);
179     }
180
181 }
182
183 static VOID ControlEvent_Remove(MSIPACKAGE* package, LPCWSTR argument, 
184                                 msi_dialog* dialog)
185 {
186     static const WCHAR szAll[] = {'A','L','L',0};
187     int i;
188
189     if (lstrcmpW(szAll,argument))
190     {
191         MSI_SetFeatureStateW(package,argument,INSTALLSTATE_ABSENT);
192     }
193     else
194     {
195         for (i = 0; i < package->loaded_features; i++)
196         {
197             package->features[i].ActionRequest = INSTALLSTATE_ABSENT;
198             package->features[i].Action= INSTALLSTATE_ABSENT;
199         }
200         ACTION_UpdateComponentStates(package,argument);
201     }
202 }
203
204 static VOID ControlEvent_AddSource(MSIPACKAGE* package, LPCWSTR argument, 
205                                    msi_dialog* dialog)
206 {
207     static const WCHAR szAll[] = {'A','L','L',0};
208     int i;
209
210     if (lstrcmpW(szAll,argument))
211     {
212         MSI_SetFeatureStateW(package,argument,INSTALLSTATE_SOURCE);
213     }
214     else
215     {
216         for (i = 0; i < package->loaded_features; i++)
217         {
218             package->features[i].ActionRequest = INSTALLSTATE_SOURCE;
219             package->features[i].Action = INSTALLSTATE_SOURCE;
220         }
221         ACTION_UpdateComponentStates(package,argument);
222     }
223 }
224
225
226 /*
227  * Subscribed events
228  */
229 static void free_subscriber( struct subscriber *sub )
230 {
231     HeapFree(GetProcessHeap(),0,sub->event);
232     HeapFree(GetProcessHeap(),0,sub->control);
233     HeapFree(GetProcessHeap(),0,sub->attribute);
234     HeapFree(GetProcessHeap(),0,sub);
235 }
236
237 VOID ControlEvent_SubscribeToEvent( MSIPACKAGE *package, LPCWSTR event,
238                                     LPCWSTR control, LPCWSTR attribute )
239 {
240     struct subscriber *sub;
241
242     sub = HeapAlloc(GetProcessHeap(),0,sizeof (*sub));
243     if( !sub )
244         return;
245     sub->event = strdupW(event);
246     sub->control = strdupW(control);
247     sub->attribute = strdupW(attribute);
248     list_add_tail( &package->subscriptions, &sub->entry );
249 }
250
251 VOID ControlEvent_UnSubscribeToEvent( MSIPACKAGE *package, LPCWSTR event,
252                                       LPCWSTR control, LPCWSTR attribute )
253 {
254     struct list *i, *t;
255     struct subscriber *sub;
256
257     LIST_FOR_EACH_SAFE( i, t, &package->subscriptions )
258     {
259         sub = LIST_ENTRY( i, struct subscriber, entry );
260
261         if( lstrcmpiW(sub->control,control) )
262             continue;
263         if( lstrcmpiW(sub->attribute,attribute) )
264             continue;
265         if( lstrcmpiW(sub->event,event) )
266             continue;
267         list_remove( &sub->entry );
268         free_subscriber( sub );
269     }
270 }
271
272 VOID ControlEvent_FireSubscribedEvent( MSIPACKAGE *package, LPCWSTR event, 
273                                        MSIRECORD *rec )
274 {
275     struct subscriber *sub;
276
277     TRACE("Firing Event %s\n",debugstr_w(event));
278
279     if (!package->dialog)
280         return;
281
282     LIST_FOR_EACH_ENTRY( sub, &package->subscriptions, struct subscriber, entry )
283     {
284         if (lstrcmpiW(sub->event, event))
285             continue;
286         msi_dialog_handle_event( package->dialog, sub->control,
287                                  sub->attribute, rec );
288     }
289 }
290
291 VOID ControlEvent_CleanupSubscriptions(MSIPACKAGE *package)
292 {
293     struct list *i, *t;
294     struct subscriber *sub;
295
296     LIST_FOR_EACH_SAFE( i, t, &package->subscriptions )
297     {
298         sub = LIST_ENTRY( i, struct subscriber, entry );
299
300         list_remove( &sub->entry );
301         free_subscriber( sub );
302     }
303 }
304
305 /*
306  * ACTION_DialogBox()
307  *
308  * Return ERROR_SUCCESS if dialog is process and ERROR_FUNCTION_FAILED
309  * if the given parameter is not a dialog box
310  */
311 UINT ACTION_DialogBox( MSIPACKAGE* package, LPCWSTR szDialogName )
312 {
313     UINT r = ERROR_SUCCESS;
314
315     if( package->next_dialog )
316         ERR("Already a next dialog... ignoring it\n");
317     package->next_dialog = NULL;
318
319     /*
320      * Dialogs are chained by filling in the next_dialog member
321      *  of the package structure, then terminating the current dialog.
322      *  The code below sees the next_dialog member set, and runs the
323      *  next dialog.
324      * We fall out of the loop below if we come across a modeless
325      *  dialog, as it returns ERROR_IO_PENDING when we try to run
326      *  its message loop.
327      */
328     r = event_do_dialog( package, szDialogName );
329     while( r == ERROR_SUCCESS && package->next_dialog )
330     {
331         LPWSTR name = package->next_dialog;
332
333         package->next_dialog = NULL;
334         r = event_do_dialog( package, name );
335         HeapFree( GetProcessHeap(), 0, name );
336     }
337
338     if( r == ERROR_IO_PENDING )
339         r = ERROR_SUCCESS;
340
341     return r;
342 }
343
344 struct _events Events[] = {
345     { "EndDialog",ControlEvent_EndDialog },
346     { "NewDialog",ControlEvent_NewDialog },
347     { "SpawnDialog",ControlEvent_SpawnDialog },
348     { "SpawnWaitDialog",ControlEvent_SpawnWaitDialog },
349     { "DoAction",ControlEvent_DoAction },
350     { "AddLocal",ControlEvent_AddLocal },
351     { "Remove",ControlEvent_Remove },
352     { "AddSource",ControlEvent_AddSource },
353     { NULL,NULL },
354 };
355
356 VOID ControlEvent_HandleControlEvent(MSIPACKAGE *package, LPCWSTR event,
357                                      LPCWSTR argument, msi_dialog* dialog)
358 {
359     int i = 0;
360
361     TRACE("Handling Control Event %s\n",debugstr_w(event));
362     if (!event)
363         return;
364
365     while( Events[i].event != NULL)
366     {
367         LPWSTR wevent = strdupAtoW(Events[i].event);
368         if (lstrcmpW(wevent,event)==0)
369         {
370             HeapFree(GetProcessHeap(),0,wevent);
371             Events[i].handler(package,argument,dialog);
372             return;
373         }
374         HeapFree(GetProcessHeap(),0,wevent);
375         i++;
376     }
377     FIXME("unhandled control event %s arg(%s)\n",
378           debugstr_w(event), debugstr_w(argument));
379 }