netstat: Initial implementation.
[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 control_events
39 {
40     const WCHAR *event;
41     EVENTHANDLER handler;
42 };
43
44 struct subscriber {
45     struct list entry;
46     msi_dialog *dialog;
47     LPWSTR event;
48     LPWSTR control;
49     LPWSTR attribute;
50 };
51
52 static UINT ControlEvent_HandleControlEvent(MSIPACKAGE *, LPCWSTR, LPCWSTR, msi_dialog*);
53
54 /*
55  * Create a dialog box and run it if it's modal
56  */
57 static UINT event_do_dialog( MSIPACKAGE *package, LPCWSTR name, msi_dialog *parent, BOOL destroy_modeless )
58 {
59     msi_dialog *dialog;
60     UINT r;
61
62     /* create a new dialog */
63     dialog = msi_dialog_create( package, name, parent,
64                                 ControlEvent_HandleControlEvent );
65     if( dialog )
66     {
67         /* kill the current modeless dialog */
68         if( destroy_modeless && package->dialog )
69         {
70             msi_dialog_destroy( package->dialog );
71             package->dialog = NULL;
72         }
73
74         /* modeless dialogs return an error message */
75         r = msi_dialog_run_message_loop( dialog );
76         if( r == ERROR_SUCCESS )
77             msi_dialog_destroy( dialog );
78         else
79             package->dialog = dialog;
80     }
81     else
82         r = ERROR_FUNCTION_FAILED;
83
84     return r;
85 }
86
87
88 /*
89  * End a modal dialog box
90  */
91 static UINT ControlEvent_EndDialog(MSIPACKAGE* package, LPCWSTR argument, 
92                                    msi_dialog* dialog)
93 {
94     static const WCHAR szExit[] = {'E','x','i','t',0};
95     static const WCHAR szRetry[] = {'R','e','t','r','y',0};
96     static const WCHAR szIgnore[] = {'I','g','n','o','r','e',0};
97     static const WCHAR szReturn[] = {'R','e','t','u','r','n',0};
98
99     if (!strcmpW( argument, szExit ))
100         package->CurrentInstallState = ERROR_INSTALL_USEREXIT;
101     else if (!strcmpW( argument, szRetry ))
102         package->CurrentInstallState = ERROR_INSTALL_SUSPEND;
103     else if (!strcmpW( argument, szIgnore ))
104         package->CurrentInstallState = ERROR_SUCCESS;
105     else if (!strcmpW( argument, szReturn ))
106     {
107         msi_dialog *parent = msi_dialog_get_parent(dialog);
108         msi_free(package->next_dialog);
109         package->next_dialog = (parent) ? strdupW(msi_dialog_get_name(parent)) : NULL;
110         package->CurrentInstallState = ERROR_SUCCESS;
111     }
112     else
113     {
114         ERR("Unknown argument string %s\n",debugstr_w(argument));
115         package->CurrentInstallState = ERROR_FUNCTION_FAILED;
116     }
117
118     ControlEvent_CleanupDialogSubscriptions(package, msi_dialog_get_name( dialog ));
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     /* don't destroy a modeless dialogs that might be our parent */
143     event_do_dialog( package, argument, dialog, FALSE );
144     if( package->CurrentInstallState != ERROR_SUCCESS )
145         msi_dialog_end_dialog( dialog );
146     return ERROR_SUCCESS;
147 }
148
149 /*
150  * Creates a dialog that remains up for a period of time
151  * based on a condition
152  */
153 static UINT ControlEvent_SpawnWaitDialog(MSIPACKAGE* package, LPCWSTR argument, 
154                                   msi_dialog* dialog)
155 {
156     FIXME("Doing Nothing\n");
157     return ERROR_SUCCESS;
158 }
159
160 static UINT ControlEvent_DoAction(MSIPACKAGE* package, LPCWSTR argument, 
161                                   msi_dialog* dialog)
162 {
163     ACTION_PerformAction(package, argument, SCRIPT_NONE);
164     return ERROR_SUCCESS;
165 }
166
167 static UINT ControlEvent_AddLocal( MSIPACKAGE *package, LPCWSTR argument, msi_dialog *dialog )
168 {
169     MSIFEATURE *feature;
170
171     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
172     {
173         if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
174         {
175             if (feature->ActionRequest != INSTALLSTATE_LOCAL)
176                 msi_set_property( package->db, szPreselected, szOne, -1 );
177             MSI_SetFeatureStateW( package, feature->Feature, INSTALLSTATE_LOCAL );
178         }
179     }
180     return ERROR_SUCCESS;
181 }
182
183 static UINT ControlEvent_Remove( MSIPACKAGE *package, LPCWSTR argument, msi_dialog *dialog )
184 {
185     MSIFEATURE *feature;
186
187     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
188     {
189         if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
190         {
191             if (feature->ActionRequest != INSTALLSTATE_ABSENT)
192                 msi_set_property( package->db, szPreselected, szOne, -1 );
193             MSI_SetFeatureStateW( package, feature->Feature, INSTALLSTATE_ABSENT );
194         }
195     }
196     return ERROR_SUCCESS;
197 }
198
199 static UINT ControlEvent_AddSource( MSIPACKAGE *package, LPCWSTR argument, msi_dialog *dialog )
200 {
201     MSIFEATURE *feature;
202
203     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
204     {
205         if (!strcmpW( argument, feature->Feature ) || !strcmpW( argument, szAll ))
206         {
207             if (feature->ActionRequest != INSTALLSTATE_SOURCE)
208                 msi_set_property( package->db, szPreselected, szOne, -1 );
209             MSI_SetFeatureStateW( package, feature->Feature, INSTALLSTATE_SOURCE );
210         }
211     }
212     return ERROR_SUCCESS;
213 }
214
215 static UINT ControlEvent_SetTargetPath(MSIPACKAGE* package, LPCWSTR argument, 
216                                    msi_dialog* dialog)
217 {
218     static const WCHAR szSelectionPath[] = {'S','e','l','e','c','t','i','o','n','P','a','t','h',0};
219     LPWSTR path = msi_dup_property( package->db, argument );
220     MSIRECORD *rec = MSI_CreateRecord( 1 );
221     UINT r = ERROR_SUCCESS;
222
223     MSI_RecordSetStringW( rec, 1, path );
224     ControlEvent_FireSubscribedEvent( package, szSelectionPath, rec );
225     if (path)
226     {
227         /* failure to set the path halts the executing of control events */
228         r = MSI_SetTargetPathW(package, argument, path);
229         msi_free(path);
230     }
231     msi_free(&rec->hdr);
232     return r;
233 }
234
235 static UINT ControlEvent_Reset(MSIPACKAGE* package, LPCWSTR argument, 
236                                    msi_dialog* dialog)
237 {
238     msi_dialog_reset(dialog);
239     return ERROR_SUCCESS;
240 }
241
242 /*
243  * Subscribed events
244  */
245 static void free_subscriber( struct subscriber *sub )
246 {
247     msi_free(sub->event);
248     msi_free(sub->control);
249     msi_free(sub->attribute);
250     msi_free(sub);
251 }
252
253 VOID ControlEvent_SubscribeToEvent( MSIPACKAGE *package, msi_dialog *dialog,
254                                     LPCWSTR event, LPCWSTR control, LPCWSTR attribute )
255 {
256     struct subscriber *sub;
257
258     TRACE("event %s control %s attribute %s\n", debugstr_w(event), debugstr_w(control), debugstr_w(attribute));
259
260     LIST_FOR_EACH_ENTRY( sub, &package->subscriptions, struct subscriber, entry )
261     {
262         if (!strcmpiW( sub->event, event ) &&
263             !strcmpiW( sub->control, control ) &&
264             !strcmpiW( sub->attribute, attribute ))
265         {
266             TRACE("already subscribed\n");
267             return;
268         };
269     }
270     if (!(sub = msi_alloc( sizeof(*sub) ))) return;
271     sub->dialog = dialog;
272     sub->event = strdupW(event);
273     sub->control = strdupW(control);
274     sub->attribute = strdupW(attribute);
275     list_add_tail( &package->subscriptions, &sub->entry );
276 }
277
278 VOID ControlEvent_FireSubscribedEvent( MSIPACKAGE *package, LPCWSTR event, MSIRECORD *rec )
279 {
280     struct subscriber *sub;
281
282     TRACE("Firing event %s\n", debugstr_w(event));
283
284     LIST_FOR_EACH_ENTRY( sub, &package->subscriptions, struct subscriber, entry )
285     {
286         if (strcmpiW( sub->event, event )) continue;
287         msi_dialog_handle_event( sub->dialog, sub->control, sub->attribute, rec );
288     }
289 }
290
291 VOID ControlEvent_CleanupDialogSubscriptions(MSIPACKAGE *package, LPWSTR dialog)
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         if (strcmpW( msi_dialog_get_name( sub->dialog ), dialog ))
301             continue;
302
303         list_remove( &sub->entry );
304         free_subscriber( sub );
305     }
306 }
307
308 VOID ControlEvent_CleanupSubscriptions(MSIPACKAGE *package)
309 {
310     struct list *i, *t;
311     struct subscriber *sub;
312
313     LIST_FOR_EACH_SAFE( i, t, &package->subscriptions )
314     {
315         sub = LIST_ENTRY( i, struct subscriber, entry );
316
317         list_remove( &sub->entry );
318         free_subscriber( sub );
319     }
320 }
321
322 /*
323  * ACTION_DialogBox()
324  *
325  * Return ERROR_SUCCESS if dialog is process and ERROR_FUNCTION_FAILED
326  * if the given parameter is not a dialog box
327  */
328 UINT ACTION_DialogBox( MSIPACKAGE* package, LPCWSTR szDialogName )
329 {
330     UINT r = ERROR_SUCCESS;
331
332     if( package->next_dialog )
333         ERR("Already a next dialog... ignoring it\n");
334     package->next_dialog = NULL;
335
336     /*
337      * Dialogs are chained by filling in the next_dialog member
338      *  of the package structure, then terminating the current dialog.
339      *  The code below sees the next_dialog member set, and runs the
340      *  next dialog.
341      * We fall out of the loop below if we come across a modeless
342      *  dialog, as it returns ERROR_IO_PENDING when we try to run
343      *  its message loop.
344      */
345     r = event_do_dialog( package, szDialogName, NULL, TRUE );
346     while( r == ERROR_SUCCESS && package->next_dialog )
347     {
348         LPWSTR name = package->next_dialog;
349
350         package->next_dialog = NULL;
351         r = event_do_dialog( package, name, NULL, TRUE );
352         msi_free( name );
353     }
354
355     if( r == ERROR_IO_PENDING )
356         r = ERROR_SUCCESS;
357
358     return r;
359 }
360
361 static UINT ControlEvent_SetInstallLevel(MSIPACKAGE* package, LPCWSTR argument,
362                                           msi_dialog* dialog)
363 {
364     int iInstallLevel = atolW(argument);
365
366     TRACE("Setting install level: %i\n", iInstallLevel);
367
368     return MSI_SetInstallLevel( package, iInstallLevel );
369 }
370
371 static UINT ControlEvent_DirectoryListUp(MSIPACKAGE *package, LPCWSTR argument,
372                                          msi_dialog *dialog)
373 {
374     return msi_dialog_directorylist_up( dialog );
375 }
376
377 static UINT ControlEvent_ReinstallMode(MSIPACKAGE *package, LPCWSTR argument,
378                                        msi_dialog *dialog)
379 {
380     return msi_set_property( package->db, szReinstallMode, argument, -1 );
381 }
382
383 static UINT ControlEvent_Reinstall( MSIPACKAGE *package, LPCWSTR argument,
384                                     msi_dialog *dialog )
385 {
386     return msi_set_property( package->db, szReinstall, argument, -1 );
387 }
388
389 static UINT ControlEvent_ValidateProductID(MSIPACKAGE *package, LPCWSTR argument,
390                                            msi_dialog *dialog)
391 {
392     return msi_validate_product_id( package );
393 }
394
395 static const WCHAR end_dialogW[] = {'E','n','d','D','i','a','l','o','g',0};
396 static const WCHAR new_dialogW[] = {'N','e','w','D','i','a','l','o','g',0};
397 static const WCHAR spawn_dialogW[] = {'S','p','a','w','n','D','i','a','l','o','g',0};
398 static const WCHAR spawn_wait_dialogW[] = {'S','p','a','w','n','W','a','i','t','D','i','a','l','o','g',0};
399 static const WCHAR do_actionW[] = {'D','o','A','c','t','i','o','n',0};
400 static const WCHAR add_localW[] = {'A','d','d','L','o','c','a','l',0};
401 static const WCHAR removeW[] = {'R','e','m','o','v','e',0};
402 static const WCHAR add_sourceW[] = {'A','d','d','S','o','u','r','c','e',0};
403 static const WCHAR set_target_pathW[] = {'S','e','t','T','a','r','g','e','t','P','a','t','h',0};
404 static const WCHAR resetW[] = {'R','e','s','e','t',0};
405 static const WCHAR set_install_levelW[] = {'S','e','t','I','n','s','t','a','l','l','L','e','v','e','l',0};
406 static const WCHAR directory_list_upW[] = {'D','i','r','e','c','t','o','r','y','L','i','s','t','U','p',0};
407 static const WCHAR selection_browseW[] = {'S','e','l','e','c','t','i','o','n','B','r','o','w','s','e',0};
408 static const WCHAR reinstall_modeW[] = {'R','e','i','n','s','t','a','l','l','M','o','d','e',0};
409 static const WCHAR reinstallW[] = {'R','e','i','n','s','t','a','l','l',0};
410 static const WCHAR validate_product_idW[] = {'V','a','l','i','d','a','t','e','P','r','o','d','u','c','t','I','D',0};
411
412 static const struct control_events control_events[] =
413 {
414     { end_dialogW, ControlEvent_EndDialog },
415     { new_dialogW, ControlEvent_NewDialog },
416     { spawn_dialogW, ControlEvent_SpawnDialog },
417     { spawn_wait_dialogW, ControlEvent_SpawnWaitDialog },
418     { do_actionW, ControlEvent_DoAction },
419     { add_localW, ControlEvent_AddLocal },
420     { removeW, ControlEvent_Remove },
421     { add_sourceW, ControlEvent_AddSource },
422     { set_target_pathW, ControlEvent_SetTargetPath },
423     { resetW, ControlEvent_Reset },
424     { set_install_levelW, ControlEvent_SetInstallLevel },
425     { directory_list_upW, ControlEvent_DirectoryListUp },
426     { selection_browseW, ControlEvent_SpawnDialog },
427     { reinstall_modeW, ControlEvent_ReinstallMode },
428     { reinstallW, ControlEvent_Reinstall },
429     { validate_product_idW, ControlEvent_ValidateProductID },
430     { NULL, NULL }
431 };
432
433 UINT ControlEvent_HandleControlEvent( MSIPACKAGE *package, LPCWSTR event,
434                                       LPCWSTR argument, msi_dialog *dialog )
435 {
436     unsigned int i;
437
438     TRACE("handling control event %s\n", debugstr_w(event));
439
440     if (!event) return ERROR_SUCCESS;
441
442     for (i = 0; control_events[i].event; i++)
443     {
444         if (!strcmpW( control_events[i].event, event ))
445             return control_events[i].handler( package, argument, dialog );
446     }
447     FIXME("unhandled control event %s arg(%s)\n", debugstr_w(event), debugstr_w(argument));
448     return ERROR_SUCCESS;
449 }