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 *);
62 VLCProgressPanel *currentProgressBarPanel;
68 /*****************************************************************************
70 *****************************************************************************/
73 /* Minimal interface. see intf.m */
74 set_shortname("Mac OS X Dialogs")
75 add_shortcut("macosx_dialog_provider")
77 set_description("Minimal Mac OS X Dialog Provider")
78 set_capability("interface", 50)
79 set_callbacks(OpenIntf, CloseIntf)
80 set_category(CAT_INTERFACE)
81 set_subcategory(SUBCAT_INTERFACE_MAIN)
84 /*****************************************************************************
85 * OpenIntf: initialize interface
86 *****************************************************************************/
87 int OpenIntf(vlc_object_t *p_this)
89 intf_thread_t *p_intf = (intf_thread_t*) p_this;
91 p_intf->p_sys = malloc(sizeof(intf_sys_t));
95 memset(p_intf->p_sys,0,sizeof(*p_intf->p_sys));
99 msg_Dbg(p_intf,"Opening Mac OS X dialog provider");
103 /*****************************************************************************
104 * Run: waiting for the death
105 *****************************************************************************/
106 static void Run( intf_thread_t *p_intf )
108 /* subscribe to various interactive dialogues */
109 var_Create(p_intf,"dialog-error",VLC_VAR_ADDRESS);
110 var_AddCallback(p_intf,"dialog-error",DisplayError,p_intf);
111 var_Create(p_intf,"dialog-critical",VLC_VAR_ADDRESS);
112 var_AddCallback(p_intf,"dialog-critical",DisplayCritical,p_intf);
113 var_Create(p_intf,"dialog-login",VLC_VAR_ADDRESS);
114 var_AddCallback(p_intf,"dialog-login",DisplayLogin,p_intf);
115 var_Create(p_intf,"dialog-question",VLC_VAR_ADDRESS);
116 var_AddCallback(p_intf,"dialog-question",DisplayQuestion,p_intf);
117 var_Create(p_intf,"dialog-progress-bar",VLC_VAR_ADDRESS);
118 var_AddCallback(p_intf,"dialog-progress-bar",DisplayProgressPanelAction,p_intf);
119 dialog_Register(p_intf);
121 msg_Dbg(p_intf,"Mac OS X dialog provider initialised");
124 while(vlc_object_alive(p_intf))
129 /* unsubscribe from the interactive dialogues */
130 dialog_Unregister(p_intf );
131 var_DelCallback(p_intf,"dialog-error",DisplayError,p_intf);
132 var_DelCallback(p_intf,"dialog-critical",DisplayCritical,p_intf);
133 var_DelCallback(p_intf,"dialog-login",DisplayLogin,p_intf);
134 var_DelCallback(p_intf,"dialog-question",DisplayQuestion,p_intf);
135 var_DelCallback(p_intf,"dialog-progress-bar",DisplayProgressPanelAction,p_intf);
137 /*****************************************************************************
138 * CloseIntf: destroy interface
139 *****************************************************************************/
140 void CloseIntf(vlc_object_t *p_this)
142 intf_thread_t *p_intf = (intf_thread_t*) p_this;
144 msg_Dbg(p_intf,"Mac OS X dialog provider closed");
149 /*****************************************************************************
150 * Callbacks triggered by the "dialog-*" variables
151 *****************************************************************************/
152 static int DisplayError(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
154 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
155 dialog_fatal_t *p_dialog = (dialog_fatal_t *)value.p_address;
156 NSRunInformationalAlertPanel([NSString stringWithUTF8String:p_dialog->title],
157 [NSString stringWithUTF8String:p_dialog->message],
163 static int DisplayCritical(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
165 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
166 dialog_fatal_t *p_dialog = (dialog_fatal_t *)value.p_address;
167 NSRunCriticalAlertPanel([NSString stringWithUTF8String:p_dialog->title],
168 [NSString stringWithUTF8String:p_dialog->message],
174 static int DisplayQuestion(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
176 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
177 dialog_question_t *p_dialog = (dialog_question_t *)value.p_address;
179 NSString *o_yes, *o_no, *o_cancel;
180 NSInteger i_returnValue = 0;
182 if (p_dialog->yes != NULL)
183 o_yes = [NSString stringWithUTF8String:p_dialog->yes];
184 if (p_dialog->no != NULL)
185 o_no = [NSString stringWithUTF8String:p_dialog->no];
186 if (p_dialog->cancel != NULL)
187 o_cancel = [NSString stringWithUTF8String:p_dialog->cancel];
189 o_alert = [NSAlert alertWithMessageText:[NSString stringWithUTF8String:p_dialog->title]
193 informativeTextWithFormat:[NSString stringWithUTF8String:p_dialog->message]];
194 [o_alert setAlertStyle:NSInformationalAlertStyle];
195 i_returnValue = [o_alert runModal];
197 if (i_returnValue == NSAlertDefaultReturn)
198 p_dialog->answer = 1;
199 if (i_returnValue == NSAlertAlternateReturn)
200 p_dialog->answer = 2;
201 if (i_returnValue == NSAlertOtherReturn)
202 p_dialog->answer = 3;
207 static int DisplayLogin(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
209 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
210 dialog_login_t *p_dialog = (dialog_login_t *)value.p_address;
211 NSInteger i_returnValue = 0;
212 VLCLoginPanel *thePanel = [[VLCLoginPanel alloc] init];
213 [thePanel createContentView];
214 [thePanel setDialogTitle:[NSString stringWithUTF8String:p_dialog->title]];
215 [thePanel setDialogMessage:[NSString stringWithUTF8String:p_dialog->message]];
217 i_returnValue = [NSApp runModalForWindow:thePanel];
220 *p_dialog->username = strdup( [[thePanel userName] UTF8String] );
221 *p_dialog->password = strdup( [[thePanel password] UTF8String] );
223 *p_dialog->username = *p_dialog->password = NULL;
228 static int DisplayProgressPanelAction(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
230 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
231 dialog_progress_bar_t * p_dialog = (dialog_progress_bar_t *)value.p_address;
232 intf_thread_t *p_intf = (intf_thread_t*) p_this;
233 intf_sys_t *p_sys = p_intf->p_sys;
235 if(p_sys->currentProgressBarPanel)
236 [p_sys->currentProgressBarPanel release];
238 p_sys->currentProgressBarPanel = [[VLCProgressPanel alloc] init];
239 [p_sys->currentProgressBarPanel createContentView];
241 [p_sys->currentProgressBarPanel setDialogTitle:[NSString stringWithUTF8String:p_dialog->title]];
242 if (p_dialog->message)
243 [p_sys->currentProgressBarPanel setDialogMessage:[NSString stringWithUTF8String:p_dialog->message]];
244 if (p_dialog->cancel)
245 [p_sys->currentProgressBarPanel setCancelButtonLabel:[NSString stringWithUTF8String:p_dialog->cancel]];
246 [p_sys->currentProgressBarPanel center];
247 [p_sys->currentProgressBarPanel makeKeyAndOrderFront:nil];
249 p_dialog->pf_update = updateProgressPanel;
250 p_dialog->pf_check = checkProgressPanel;
251 p_dialog->pf_destroy = destroyProgressPanel;
252 p_dialog->p_sys = p_intf->p_sys;
258 void updateProgressPanel (void *priv, const char *text, float value)
260 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
261 intf_sys_t *p_sys = (intf_sys_t *)priv;
264 [p_sys->currentProgressBarPanel setDialogMessage:[NSString stringWithUTF8String:text]];
266 [p_sys->currentProgressBarPanel setDialogMessage:@""];
267 [p_sys->currentProgressBarPanel setProgressAsDouble:(double)(value * 1000.)];
272 void destroyProgressPanel (void *priv)
274 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
275 intf_sys_t *p_sys = (intf_sys_t *)priv;
277 [p_sys->currentProgressBarPanel close];
278 [p_sys->currentProgressBarPanel release];
283 bool checkProgressPanel (void *priv)
285 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
286 intf_sys_t *p_sys = (intf_sys_t *)priv;
289 b_returned = [p_sys->currentProgressBarPanel isCancelled];