winex11: Synchronize the mouse cursor in update_mouse_state.
[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, msi_dialog *dialog )
167 {
168     MSIFEATURE *feature;
169
170     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
171     {
172         if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
173         {
174             if (feature->ActionRequest != INSTALLSTATE_LOCAL)
175                 msi_set_property( package->db, szPreselected, szOne );
176             MSI_SetFeatureStateW( package, feature->Feature, INSTALLSTATE_LOCAL );
177         }
178     }
179     return ERROR_SUCCESS;
180 }
181
182 static UINT ControlEvent_Remove( MSIPACKAGE *package, LPCWSTR argument, msi_dialog *dialog )
183 {
184     MSIFEATURE *feature;
185
186     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
187     {
188         if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
189         {
190             if (feature->ActionRequest != INSTALLSTATE_ABSENT)
191                 msi_set_property( package->db, szPreselected, szOne );
192             MSI_SetFeatureStateW( package, feature->Feature, INSTALLSTATE_ABSENT );
193         }
194     }
195     return ERROR_SUCCESS;
196 }
197
198 static UINT ControlEvent_AddSource( MSIPACKAGE *package, LPCWSTR argument, msi_dialog *dialog )
199 {
200     MSIFEATURE *feature;
201
202     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
203     {
204         if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
205         {
206             if (feature->ActionRequest != INSTALLSTATE_SOURCE)
207                 msi_set_property( package->db, szPreselected, szOne );
208             MSI_SetFeatureStateW( package, feature->Feature, INSTALLSTATE_SOURCE );
209         }
210     }
211     return ERROR_SUCCESS;
212 }
213
214 static UINT ControlEvent_SetTargetPath(MSIPACKAGE* package, LPCWSTR argument, 
215                                    msi_dialog* dialog)
216 {
217     LPWSTR path = msi_dup_property( package->db, argument );
218     MSIRECORD *rec = MSI_CreateRecord( 1 );
219     UINT r;
220
221     static const WCHAR szSelectionPath[] = {'S','e','l','e','c','t','i','o','n','P','a','t','h',0};
222
223     MSI_RecordSetStringW( rec, 1, path );
224     ControlEvent_FireSubscribedEvent( package, szSelectionPath, rec );
225
226     /* failure to set the path halts the executing of control events */
227     r = MSI_SetTargetPathW(package, argument, path);
228     msi_free(path);
229     msi_free(&rec->hdr);
230     return r;
231 }
232
233 static UINT ControlEvent_Reset(MSIPACKAGE* package, LPCWSTR argument, 
234                                    msi_dialog* dialog)
235 {
236     msi_dialog_reset(dialog);
237     return ERROR_SUCCESS;
238 }
239
240 /*
241  * Subscribed events
242  */
243 static void free_subscriber( struct subscriber *sub )
244 {
245     msi_free(sub->event);
246     msi_free(sub->control);
247     msi_free(sub->attribute);
248     msi_free(sub);
249 }
250
251 VOID ControlEvent_SubscribeToEvent( MSIPACKAGE *package, msi_dialog *dialog,
252                                     LPCWSTR event, LPCWSTR control, LPCWSTR attribute )
253 {
254     struct subscriber *sub;
255
256     sub = msi_alloc(sizeof (*sub));
257     if( !sub )
258         return;
259     sub->dialog = dialog;
260     sub->event = strdupW(event);
261     sub->control = strdupW(control);
262     sub->attribute = strdupW(attribute);
263     list_add_tail( &package->subscriptions, &sub->entry );
264 }
265
266 VOID ControlEvent_FireSubscribedEvent( MSIPACKAGE *package, LPCWSTR event, 
267                                        MSIRECORD *rec )
268 {
269     struct subscriber *sub;
270
271     TRACE("Firing Event %s\n",debugstr_w(event));
272
273     LIST_FOR_EACH_ENTRY( sub, &package->subscriptions, struct subscriber, entry )
274     {
275         if (strcmpiW( sub->event, event ))
276             continue;
277         msi_dialog_handle_event( sub->dialog, sub->control, sub->attribute, rec );
278     }
279 }
280
281 VOID ControlEvent_CleanupDialogSubscriptions(MSIPACKAGE *package, LPWSTR dialog)
282 {
283     struct list *i, *t;
284     struct subscriber *sub;
285
286     LIST_FOR_EACH_SAFE( i, t, &package->subscriptions )
287     {
288         sub = LIST_ENTRY( i, struct subscriber, entry );
289
290         if (strcmpW( msi_dialog_get_name( sub->dialog ), dialog ))
291             continue;
292
293         list_remove( &sub->entry );
294         free_subscriber( sub );
295     }
296 }
297
298 VOID ControlEvent_CleanupSubscriptions(MSIPACKAGE *package)
299 {
300     struct list *i, *t;
301     struct subscriber *sub;
302
303     LIST_FOR_EACH_SAFE( i, t, &package->subscriptions )
304     {
305         sub = LIST_ENTRY( i, struct subscriber, entry );
306
307         list_remove( &sub->entry );
308         free_subscriber( sub );
309     }
310 }
311
312 /*
313  * ACTION_DialogBox()
314  *
315  * Return ERROR_SUCCESS if dialog is process and ERROR_FUNCTION_FAILED
316  * if the given parameter is not a dialog box
317  */
318 UINT ACTION_DialogBox( MSIPACKAGE* package, LPCWSTR szDialogName )
319 {
320     UINT r = ERROR_SUCCESS;
321
322     if( package->next_dialog )
323         ERR("Already a next dialog... ignoring it\n");
324     package->next_dialog = NULL;
325
326     /*
327      * Dialogs are chained by filling in the next_dialog member
328      *  of the package structure, then terminating the current dialog.
329      *  The code below sees the next_dialog member set, and runs the
330      *  next dialog.
331      * We fall out of the loop below if we come across a modeless
332      *  dialog, as it returns ERROR_IO_PENDING when we try to run
333      *  its message loop.
334      */
335     r = event_do_dialog( package, szDialogName, NULL, TRUE );
336     while( r == ERROR_SUCCESS && package->next_dialog )
337     {
338         LPWSTR name = package->next_dialog;
339
340         package->next_dialog = NULL;
341         r = event_do_dialog( package, name, NULL, TRUE );
342         msi_free( name );
343     }
344
345     if( r == ERROR_IO_PENDING )
346         r = ERROR_SUCCESS;
347
348     return r;
349 }
350
351 static UINT ControlEvent_SetInstallLevel(MSIPACKAGE* package, LPCWSTR argument,
352                                           msi_dialog* dialog)
353 {
354     int iInstallLevel = atolW(argument);
355
356     TRACE("Setting install level: %i\n", iInstallLevel);
357
358     return MSI_SetInstallLevel( package, iInstallLevel );
359 }
360
361 static UINT ControlEvent_DirectoryListUp(MSIPACKAGE *package, LPCWSTR argument,
362                                          msi_dialog *dialog)
363 {
364     return msi_dialog_directorylist_up( dialog );
365 }
366
367 static UINT ControlEvent_ReinstallMode(MSIPACKAGE *package, LPCWSTR argument,
368                                        msi_dialog *dialog)
369 {
370     return msi_set_property( package->db, szReinstallMode, argument );
371 }
372
373 static UINT ControlEvent_Reinstall( MSIPACKAGE *package, LPCWSTR argument,
374                                     msi_dialog *dialog )
375 {
376     return msi_set_property( package->db, szReinstall, argument );
377 }
378
379 static UINT ControlEvent_ValidateProductID(MSIPACKAGE *package, LPCWSTR argument,
380                                            msi_dialog *dialog)
381 {
382     LPWSTR key, template;
383     UINT ret = ERROR_SUCCESS;
384
385     template = msi_dup_property( package->db, szPIDTemplate );
386     key = msi_dup_property( package->db, szPIDKEY );
387
388     if (key && template)
389     {
390         FIXME( "partial stub: template %s key %s\n", debugstr_w(template), debugstr_w(key) );
391         ret = msi_set_property( package->db, szProductID, key );
392     }
393     msi_free( template );
394     msi_free( key );
395     return ret;
396 }
397
398 static const struct _events Events[] = {
399     { "EndDialog",ControlEvent_EndDialog },
400     { "NewDialog",ControlEvent_NewDialog },
401     { "SpawnDialog",ControlEvent_SpawnDialog },
402     { "SpawnWaitDialog",ControlEvent_SpawnWaitDialog },
403     { "DoAction",ControlEvent_DoAction },
404     { "AddLocal",ControlEvent_AddLocal },
405     { "Remove",ControlEvent_Remove },
406     { "AddSource",ControlEvent_AddSource },
407     { "SetTargetPath",ControlEvent_SetTargetPath },
408     { "Reset",ControlEvent_Reset },
409     { "SetInstallLevel",ControlEvent_SetInstallLevel },
410     { "DirectoryListUp",ControlEvent_DirectoryListUp },
411     { "SelectionBrowse",ControlEvent_SpawnDialog },
412     { "ReinstallMode",ControlEvent_ReinstallMode },
413     { "Reinstall",ControlEvent_Reinstall },
414     { "ValidateProductID",ControlEvent_ValidateProductID },
415     { NULL,NULL },
416 };
417
418 UINT ControlEvent_HandleControlEvent(MSIPACKAGE *package, LPCWSTR event,
419                                      LPCWSTR argument, msi_dialog* dialog)
420 {
421     int i = 0;
422     UINT rc = ERROR_SUCCESS;
423
424     TRACE("Handling Control Event %s\n",debugstr_w(event));
425     if (!event)
426         return rc;
427
428     while( Events[i].event != NULL)
429     {
430         LPWSTR wevent = strdupAtoW(Events[i].event);
431         if (!strcmpW( wevent, event ))
432         {
433             msi_free(wevent);
434             rc = Events[i].handler(package,argument,dialog);
435             return rc;
436         }
437         msi_free(wevent);
438         i++;
439     }
440     FIXME("unhandled control event %s arg(%s)\n",
441           debugstr_w(event), debugstr_w(argument));
442     return rc;
443 }