Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / msi / events.c
blob96fc6778877679c6026dbf1122565ff2802f001b
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2005 Aric Stewart for CodeWeavers
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.
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.
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
21 #include <stdarg.h>
22 #include <stdio.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "winreg.h"
28 #include "msi.h"
29 #include "msipriv.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(msi);
36 typedef UINT (*EVENTHANDLER)(MSIPACKAGE*,LPCWSTR,msi_dialog *);
38 struct _events {
39 LPCSTR event;
40 EVENTHANDLER handler;
43 struct subscriber {
44 struct list entry;
45 msi_dialog *dialog;
46 LPWSTR event;
47 LPWSTR control;
48 LPWSTR attribute;
51 static UINT ControlEvent_HandleControlEvent(MSIPACKAGE *, LPCWSTR, LPCWSTR, msi_dialog*);
54 * Create a dialog box and run it if it's modal
56 static UINT event_do_dialog( MSIPACKAGE *package, LPCWSTR name, msi_dialog *parent, BOOL destroy_modeless )
58 msi_dialog *dialog;
59 UINT r;
61 /* create a new dialog */
62 dialog = msi_dialog_create( package, name, parent,
63 ControlEvent_HandleControlEvent );
64 if( dialog )
66 /* kill the current modeless dialog */
67 if( destroy_modeless && package->dialog )
69 msi_dialog_destroy( package->dialog );
70 package->dialog = NULL;
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;
80 else
81 r = ERROR_FUNCTION_FAILED;
83 return r;
88 * End a modal dialog box
90 static UINT ControlEvent_EndDialog(MSIPACKAGE* package, LPCWSTR argument,
91 msi_dialog* dialog)
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};
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 ))
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;
111 else
113 ERR("Unknown argument string %s\n",debugstr_w(argument));
114 package->CurrentInstallState = ERROR_FUNCTION_FAILED;
117 ControlEvent_CleanupDialogSubscriptions(package, msi_dialog_get_name( dialog ));
118 msi_dialog_end_dialog( dialog );
119 return ERROR_SUCCESS;
123 * transition from one modal dialog to another modal dialog
125 static UINT ControlEvent_NewDialog(MSIPACKAGE* package, LPCWSTR argument,
126 msi_dialog *dialog)
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;
136 * Create a new child dialog of an existing modal dialog
138 static UINT ControlEvent_SpawnDialog(MSIPACKAGE* package, LPCWSTR argument,
139 msi_dialog *dialog)
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;
149 * Creates a dialog that remains up for a period of time
150 * based on a condition
152 static UINT ControlEvent_SpawnWaitDialog(MSIPACKAGE* package, LPCWSTR argument,
153 msi_dialog* dialog)
155 FIXME("Doing Nothing\n");
156 return ERROR_SUCCESS;
159 static UINT ControlEvent_DoAction(MSIPACKAGE* package, LPCWSTR argument,
160 msi_dialog* dialog)
162 ACTION_PerformAction(package, argument, -1);
163 return ERROR_SUCCESS;
166 static UINT ControlEvent_AddLocal(MSIPACKAGE* package, LPCWSTR argument,
167 msi_dialog* dialog)
169 MSIFEATURE *feature = NULL;
171 if (strcmpW( szAll, argument ))
173 MSI_SetFeatureStateW(package,argument,INSTALLSTATE_LOCAL);
175 else
177 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
178 msi_feature_set_state(package, feature, INSTALLSTATE_LOCAL);
180 ACTION_UpdateComponentStates(package,argument);
182 return ERROR_SUCCESS;
185 static UINT ControlEvent_Remove(MSIPACKAGE* package, LPCWSTR argument,
186 msi_dialog* dialog)
188 MSIFEATURE *feature = NULL;
190 if (strcmpW( szAll, argument ))
192 MSI_SetFeatureStateW(package,argument,INSTALLSTATE_ABSENT);
194 else
196 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
197 msi_feature_set_state(package, feature, INSTALLSTATE_ABSENT);
199 ACTION_UpdateComponentStates(package,argument);
201 return ERROR_SUCCESS;
204 static UINT ControlEvent_AddSource(MSIPACKAGE* package, LPCWSTR argument,
205 msi_dialog* dialog)
207 MSIFEATURE *feature = NULL;
209 if (strcmpW( szAll, argument ))
211 MSI_SetFeatureStateW(package,argument,INSTALLSTATE_SOURCE);
213 else
215 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
216 msi_feature_set_state(package, feature, INSTALLSTATE_SOURCE);
217 ACTION_UpdateComponentStates(package,argument);
219 return ERROR_SUCCESS;
222 static UINT ControlEvent_SetTargetPath(MSIPACKAGE* package, LPCWSTR argument,
223 msi_dialog* dialog)
225 LPWSTR path = msi_dup_property( package->db, argument );
226 MSIRECORD *rec = MSI_CreateRecord( 1 );
227 UINT r;
229 static const WCHAR szSelectionPath[] = {'S','e','l','e','c','t','i','o','n','P','a','t','h',0};
231 MSI_RecordSetStringW( rec, 1, path );
232 ControlEvent_FireSubscribedEvent( package, szSelectionPath, rec );
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;
241 static UINT ControlEvent_Reset(MSIPACKAGE* package, LPCWSTR argument,
242 msi_dialog* dialog)
244 msi_dialog_reset(dialog);
245 return ERROR_SUCCESS;
249 * Subscribed events
251 static void free_subscriber( struct subscriber *sub )
253 msi_free(sub->event);
254 msi_free(sub->control);
255 msi_free(sub->attribute);
256 msi_free(sub);
259 VOID ControlEvent_SubscribeToEvent( MSIPACKAGE *package, msi_dialog *dialog,
260 LPCWSTR event, LPCWSTR control, LPCWSTR attribute )
262 struct subscriber *sub;
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 );
274 VOID ControlEvent_FireSubscribedEvent( MSIPACKAGE *package, LPCWSTR event,
275 MSIRECORD *rec )
277 struct subscriber *sub;
279 TRACE("Firing Event %s\n",debugstr_w(event));
281 LIST_FOR_EACH_ENTRY( sub, &package->subscriptions, struct subscriber, entry )
283 if (strcmpiW( sub->event, event ))
284 continue;
285 msi_dialog_handle_event( sub->dialog, sub->control, sub->attribute, rec );
289 VOID ControlEvent_CleanupDialogSubscriptions(MSIPACKAGE *package, LPWSTR dialog)
291 struct list *i, *t;
292 struct subscriber *sub;
294 LIST_FOR_EACH_SAFE( i, t, &package->subscriptions )
296 sub = LIST_ENTRY( i, struct subscriber, entry );
298 if (strcmpW( msi_dialog_get_name( sub->dialog ), dialog ))
299 continue;
301 list_remove( &sub->entry );
302 free_subscriber( sub );
306 VOID ControlEvent_CleanupSubscriptions(MSIPACKAGE *package)
308 struct list *i, *t;
309 struct subscriber *sub;
311 LIST_FOR_EACH_SAFE( i, t, &package->subscriptions )
313 sub = LIST_ENTRY( i, struct subscriber, entry );
315 list_remove( &sub->entry );
316 free_subscriber( sub );
321 * ACTION_DialogBox()
323 * Return ERROR_SUCCESS if dialog is process and ERROR_FUNCTION_FAILED
324 * if the given parameter is not a dialog box
326 UINT ACTION_DialogBox( MSIPACKAGE* package, LPCWSTR szDialogName )
328 UINT r = ERROR_SUCCESS;
330 if( package->next_dialog )
331 ERR("Already a next dialog... ignoring it\n");
332 package->next_dialog = NULL;
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.
343 r = event_do_dialog( package, szDialogName, NULL, TRUE );
344 while( r == ERROR_SUCCESS && package->next_dialog )
346 LPWSTR name = package->next_dialog;
348 package->next_dialog = NULL;
349 r = event_do_dialog( package, name, NULL, TRUE );
350 msi_free( name );
353 if( r == ERROR_IO_PENDING )
354 r = ERROR_SUCCESS;
356 return r;
359 static UINT ControlEvent_SetInstallLevel(MSIPACKAGE* package, LPCWSTR argument,
360 msi_dialog* dialog)
362 int iInstallLevel = atolW(argument);
364 TRACE("Setting install level: %i\n", iInstallLevel);
366 return MSI_SetInstallLevel( package, iInstallLevel );
369 static UINT ControlEvent_DirectoryListUp(MSIPACKAGE *package, LPCWSTR argument,
370 msi_dialog *dialog)
372 return msi_dialog_directorylist_up( dialog );
375 static UINT ControlEvent_ReinstallMode(MSIPACKAGE *package, LPCWSTR argument,
376 msi_dialog *dialog)
378 return msi_set_property( package->db, szReinstallMode, argument );
381 static UINT ControlEvent_Reinstall( MSIPACKAGE *package, LPCWSTR argument,
382 msi_dialog *dialog )
384 return msi_set_property( package->db, szReinstall, argument );
387 static UINT ControlEvent_ValidateProductID(MSIPACKAGE *package, LPCWSTR argument,
388 msi_dialog *dialog)
390 LPWSTR key, template;
391 UINT ret = ERROR_SUCCESS;
393 template = msi_dup_property( package->db, szPIDTemplate );
394 key = msi_dup_property( package->db, szPIDKEY );
396 if (key && template)
398 FIXME( "partial stub: template %s key %s\n", debugstr_w(template), debugstr_w(key) );
399 ret = msi_set_property( package->db, szProductID, key );
401 msi_free( template );
402 msi_free( key );
403 return ret;
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 },
426 UINT ControlEvent_HandleControlEvent(MSIPACKAGE *package, LPCWSTR event,
427 LPCWSTR argument, msi_dialog* dialog)
429 int i = 0;
430 UINT rc = ERROR_SUCCESS;
432 TRACE("Handling Control Event %s\n",debugstr_w(event));
433 if (!event)
434 return rc;
436 while( Events[i].event != NULL)
438 LPWSTR wevent = strdupAtoW(Events[i].event);
439 if (!strcmpW( wevent, event ))
441 msi_free(wevent);
442 rc = Events[i].handler(package,argument,dialog);
443 return rc;
445 msi_free(wevent);
446 i++;
448 FIXME("unhandled control event %s arg(%s)\n",
449 debugstr_w(event), debugstr_w(argument));
450 return rc;