1 /*****************************************************************************
2 * dialogProvider.m: Minimal Dialog Provider for Mac OS X
3 *****************************************************************************
4 * Copyright (C) 2009-2010 the VideoLAN team
7 * Authors: Felix Paul Kühne <fkuehne at videolan dot org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #import <stdlib.h> /* malloc(), free() */
34 #import <vlc_common.h>
35 #import <vlc_plugin.h>
36 #import <vlc_dialog.h>
37 #import <vlc_interface.h>
39 #import <Cocoa/Cocoa.h>
40 #import "VLCLoginPanel.h"
41 #import "VLCProgressPanel.h"
43 /*****************************************************************************
45 *****************************************************************************/
46 static int OpenIntf(vlc_object_t *);
47 static void CloseIntf(vlc_object_t *);
48 static void Run(intf_thread_t * );
50 static int DisplayError(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
51 static int DisplayCritical(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
52 static int DisplayQuestion(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
53 static int DisplayLogin(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
54 static int DisplayProgressPanelAction(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
56 static void updateProgressPanel (void *, const char *, float);
57 static bool checkProgressPanel (void *);
58 static void destroyProgressPanel (void *);
60 @interface VLCDialogDisplayer : NSObject
62 VLCProgressPanel *_currentProgressBarPanel;
65 + (NSDictionary *)dictionaryForDialog:(const char *)title :(const char *)message :(const char *)yes :(const char *)no :(const char *)cancel;
67 - (void)displayError:(NSDictionary *)dialog;
68 - (void)displayCritical:(NSDictionary *)dialog;
69 - (NSNumber *)displayQuestion:(NSDictionary *)dialog;
70 - (NSDictionary *)displayLogin:(NSDictionary *)dialog;
72 - (void)displayProgressBar:(NSDictionary *)dict;
73 - (void)updateProgressPanel:(NSDictionary *)dict;
74 - (void)destroyProgressPanel;
75 - (NSNumber *)checkProgressPanel;
77 - (id)resultFromSelectorOnMainThread:(SEL)sel withObject:(id)object;
80 static inline NSDictionary *DictFromDialogFatal(dialog_fatal_t *dialog) {
81 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :NULL];
83 static inline NSDictionary *DictFromDialogLogin(dialog_login_t *dialog) {
84 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :NULL];
86 static inline NSDictionary *DictFromDialogQuestion(dialog_question_t *dialog) {
87 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :dialog->yes :dialog->no :dialog->cancel];
89 static inline NSDictionary *DictFromDialogProgressBar(dialog_progress_bar_t *dialog) {
90 return [VLCDialogDisplayer dictionaryForDialog:dialog->title :dialog->message :NULL :NULL :dialog->cancel];
95 VLCDialogDisplayer *displayer;
99 bool is_hidding_noaction_dialogs;
103 #define T_HIDE_NOACTION N_("Hide no user action dialogs")
104 #define LT_HIDE_NOACTION N_("Don't display dialogs that don't require user action (Critical and error panel).")
106 #define prefix "macosx-dialog-provider"
107 /*****************************************************************************
109 *****************************************************************************/
112 /* Minimal interface. see intf.m */
113 set_shortname("Mac OS X Dialogs")
114 add_shortcut("macosx_dialog_provider")
115 add_shortcut("miosx")
116 set_description("Minimal Mac OS X Dialog Provider")
117 set_capability("interface", 0)
119 /* This setting is interesting, because when used with a libvlc app
120 * it's almost certain that the client program will display error by
121 * itself. Moreover certain action might end up in an error, but
122 * the client wants to ignored them completely. */
123 add_bool(prefix "hide-no-user-action-dialogs", true, NULL, T_HIDE_NOACTION, LT_HIDE_NOACTION, false)
125 set_callbacks(OpenIntf, CloseIntf)
126 set_category(CAT_INTERFACE)
127 set_subcategory(SUBCAT_INTERFACE_MAIN)
130 /*****************************************************************************
131 * OpenIntf: initialize interface
132 *****************************************************************************/
133 int OpenIntf(vlc_object_t *p_this)
135 intf_thread_t *p_intf = (intf_thread_t*) p_this;
137 p_intf->p_sys = malloc(sizeof(intf_sys_t));
141 memset(p_intf->p_sys,0,sizeof(*p_intf->p_sys));
143 p_intf->p_sys->displayer = [[VLCDialogDisplayer alloc] init];
145 bool hide = var_CreateGetBool(p_intf, prefix "hide-no-user-action-dialogs");
146 p_intf->p_sys->is_hidding_noaction_dialogs = hide;
148 /* subscribe to various interactive dialogues */
152 var_Create(p_intf,"dialog-error",VLC_VAR_ADDRESS);
153 var_AddCallback(p_intf,"dialog-error",DisplayError,p_intf);
154 var_Create(p_intf,"dialog-critical",VLC_VAR_ADDRESS);
155 var_AddCallback(p_intf,"dialog-critical",DisplayCritical,p_intf);
157 var_Create(p_intf,"dialog-login",VLC_VAR_ADDRESS);
158 var_AddCallback(p_intf,"dialog-login",DisplayLogin,p_intf);
159 var_Create(p_intf,"dialog-question",VLC_VAR_ADDRESS);
160 var_AddCallback(p_intf,"dialog-question",DisplayQuestion,p_intf);
161 var_Create(p_intf,"dialog-progress-bar",VLC_VAR_ADDRESS);
162 var_AddCallback(p_intf,"dialog-progress-bar",DisplayProgressPanelAction,p_intf);
163 dialog_Register(p_intf);
165 msg_Dbg(p_intf,"Mac OS X dialog provider initialised");
170 /*****************************************************************************
171 * CloseIntf: destroy interface
172 *****************************************************************************/
173 void CloseIntf(vlc_object_t *p_this)
175 intf_thread_t *p_intf = (intf_thread_t*) p_this;
177 /* unsubscribe from the interactive dialogues */
178 dialog_Unregister(p_intf);
180 if (!p_intf->p_sys->is_hidding_noaction_dialogs)
182 var_DelCallback(p_intf,"dialog-error",DisplayError,p_intf);
183 var_DelCallback(p_intf,"dialog-critical",DisplayCritical,p_intf);
185 var_DelCallback(p_intf,"dialog-login",DisplayLogin,p_intf);
186 var_DelCallback(p_intf,"dialog-question",DisplayQuestion,p_intf);
187 var_DelCallback(p_intf,"dialog-progress-bar",DisplayProgressPanelAction,p_intf);
189 [p_intf->p_sys->displayer release];
191 msg_Dbg(p_intf,"Mac OS X dialog provider closed");
196 /*****************************************************************************
197 * Callbacks triggered by the "dialog-*" variables
198 *****************************************************************************/
199 static int DisplayError(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
201 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
202 dialog_fatal_t *dialog = value.p_address;
203 intf_thread_t *p_intf = (intf_thread_t*) p_this;
204 intf_sys_t *sys = p_intf->p_sys;
205 [sys->displayer performSelectorOnMainThread:@selector(displayError:) withObject:DictFromDialogFatal(dialog) waitUntilDone:NO];
210 static int DisplayCritical(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
212 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
213 dialog_fatal_t *dialog = value.p_address;
214 intf_thread_t *p_intf = (intf_thread_t*) p_this;
215 intf_sys_t *sys = p_intf->p_sys;
216 [sys->displayer performSelectorOnMainThread:@selector(displayCritical:) withObject:DictFromDialogFatal(dialog) waitUntilDone:NO];
221 static int DisplayQuestion(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
223 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
224 dialog_question_t *dialog = value.p_address;
225 intf_thread_t *p_intf = (intf_thread_t*) p_this;
226 intf_sys_t *sys = p_intf->p_sys;
227 dialog->answer = [[sys->displayer resultFromSelectorOnMainThread:@selector(displayQuestion:) withObject:DictFromDialogQuestion(dialog)] intValue];
232 static int DisplayLogin(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
234 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
235 dialog_login_t *dialog = value.p_address;
236 intf_thread_t *p_intf = (intf_thread_t*) p_this;
237 intf_sys_t *sys = p_intf->p_sys;
238 NSDictionary *dict = [sys->displayer resultFromSelectorOnMainThread:@selector(displayLogin:) withObject:DictFromDialogLogin(dialog)];
240 *dialog->username = strdup([[dict objectForKey:@"username"] UTF8String]);
241 *dialog->password = strdup([[dict objectForKey:@"password"] UTF8String]);
247 static int DisplayProgressPanelAction(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
249 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
250 dialog_progress_bar_t *dialog = value.p_address;
251 intf_thread_t *p_intf = (intf_thread_t*) p_this;
252 intf_sys_t *sys = p_intf->p_sys;
254 [sys->displayer performSelectorOnMainThread:@selector(displayProgressBar:) withObject:DictFromDialogProgressBar(dialog) waitUntilDone:YES];
256 dialog->pf_update = updateProgressPanel;
257 dialog->pf_check = checkProgressPanel;
258 dialog->pf_destroy = destroyProgressPanel;
259 dialog->p_sys = p_intf->p_sys;
265 void updateProgressPanel (void *priv, const char *text, float value)
267 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
268 intf_sys_t *sys = (intf_sys_t *)priv;
270 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
271 [NSNumber numberWithFloat:value], @"value",
272 text ? [NSString stringWithUTF8String:text] : nil, @"text",
275 [sys->displayer performSelectorOnMainThread:@selector(updateProgressPanel:) withObject:dict waitUntilDone:YES];
280 void destroyProgressPanel (void *priv)
282 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
283 intf_sys_t *sys = (intf_sys_t *)priv;
285 [sys->displayer performSelectorOnMainThread:@selector(destroyProgressPanel) withObject:nil waitUntilDone:YES];
290 bool checkProgressPanel (void *priv)
292 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
293 intf_sys_t *sys = (intf_sys_t *)priv;
296 ret = [[sys->displayer resultFromSelectorOnMainThread:@selector(checkProgressPanel) withObject:nil] boolValue];
303 @implementation VLCDialogDisplayer
306 assert(!_currentProgressBarPanel); // This has to be closed on main thread.
310 + (NSDictionary *)dictionaryForDialog:(const char *)title :(const char *)message :(const char *)yes :(const char *)no :(const char *)cancel
312 NSMutableDictionary *dict = [NSMutableDictionary dictionary];
314 [dict setObject:[NSString stringWithUTF8String:title] forKey:@"title"];
316 [dict setObject:[NSString stringWithUTF8String:message] forKey:@"message"];
318 [dict setObject:[NSString stringWithUTF8String:yes] forKey:@"yes"];
320 [dict setObject:[NSString stringWithUTF8String:no] forKey:@"no"];
322 [dict setObject:[NSString stringWithUTF8String:cancel] forKey:@"cancel"];
326 #define VLCAssertIsMainThread() assert([NSThread isMainThread])
328 - (void)displayError:(NSDictionary *)dialog
330 VLCAssertIsMainThread();
332 NSRunInformationalAlertPanel([dialog objectForKey:@"title"],
333 [dialog objectForKey:@"message"],
337 - (void)displayCritical:(NSDictionary *)dialog
339 VLCAssertIsMainThread();
341 NSRunCriticalAlertPanel([dialog objectForKey:@"title"],
342 [dialog objectForKey:@"message"],
346 - (NSNumber *)displayQuestion:(NSDictionary *)dialog
348 VLCAssertIsMainThread();
350 NSInteger alertRet = 0;
352 NSAlert *alert = [NSAlert alertWithMessageText:[dialog objectForKey:@"title"]
353 defaultButton:[dialog objectForKey:@"yes"]
354 alternateButton:[dialog objectForKey:@"no"]
355 otherButton:[dialog objectForKey:@"cancel"]
356 informativeTextWithFormat:[dialog objectForKey:@"message"]];
357 [alert setAlertStyle:NSInformationalAlertStyle];
358 alertRet = [alert runModal];
362 case NSAlertDefaultReturn:
365 case NSAlertAlternateReturn:
368 case NSAlertOtherReturn:
377 return [NSNumber numberWithInt:ret];
380 - (NSDictionary *)displayLogin:(NSDictionary *)dialog
382 VLCAssertIsMainThread();
384 VLCLoginPanel *panel = [[VLCLoginPanel alloc] init];
385 [panel createContentView];
386 [panel setDialogTitle:[dialog objectForKey:@"title"]];
387 [panel setDialogMessage:[dialog objectForKey:@"message"]];
389 NSInteger ret = [NSApp runModalForWindow:panel];
395 return [NSDictionary dictionaryWithObjectsAndKeys:
396 [panel userName], @"username",
397 [panel password], @"password",
401 - (void)displayProgressBar:(NSDictionary *)dialog
403 VLCAssertIsMainThread();
405 if(_currentProgressBarPanel)
406 [self destroyProgressPanel];
408 assert(!_currentProgressBarPanel);
409 _currentProgressBarPanel = [[VLCProgressPanel alloc] init];
410 [_currentProgressBarPanel createContentView];
411 [_currentProgressBarPanel setDialogTitle:[dialog objectForKey:@"title"]];
412 [_currentProgressBarPanel setDialogMessage:[dialog objectForKey:@"message"] ?: @""];
413 [_currentProgressBarPanel setCancelButtonLabel:[dialog objectForKey:@"cancel"]];
415 [_currentProgressBarPanel center];
416 [_currentProgressBarPanel makeKeyAndOrderFront:nil];
419 - (void)updateProgressPanel:(NSDictionary *)dict
421 VLCAssertIsMainThread();
423 assert(_currentProgressBarPanel);
424 [_currentProgressBarPanel setDialogMessage:[dict objectForKey:@"text"] ?: @""];
425 [_currentProgressBarPanel setProgressAsDouble:[[dict objectForKey:@"value"] doubleValue] * 1000.];
428 - (void)destroyProgressPanel
430 VLCAssertIsMainThread();
432 [_currentProgressBarPanel close];
433 [_currentProgressBarPanel release];
434 _currentProgressBarPanel = nil;
437 - (NSNumber *)checkProgressPanel
439 VLCAssertIsMainThread();
441 return [NSNumber numberWithBool:[_currentProgressBarPanel isCancelled]];
446 * Helper to execute a function on main thread and get its return value.
448 - (void)execute:(NSDictionary *)dict
450 SEL sel = [[dict objectForKey:@"sel"] pointerValue];
451 id *result = [[dict objectForKey:@"result"] pointerValue];
452 id object = [dict objectForKey:@"object"];
454 NSAssert(sel, @"Try to execute a NULL selector");
455 NSAssert(object, @"Try to execute from a nil object");
457 *result = [self performSelector:sel withObject:object];
458 [*result retain]; // Balanced in -resultFromSelectorOnMainThread
461 - (id)resultFromSelectorOnMainThread:(SEL)sel withObject:(id)object
464 NSAssert(sel, @"Try to execute a NULL selector");
465 NSAssert(sel, @"Try to execute from a nil object");
466 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
467 [NSValue valueWithPointer:sel], @"sel",
468 [NSValue valueWithPointer:&result], @"result",
469 object, @"object", nil];
470 [self performSelectorOnMainThread:@selector(execute:) withObject:dict waitUntilDone:YES];
471 return [result autorelease];