]> git.sesse.net Git - vlc/blob - modules/gui/macosx_dialog_provider/dialogProvider.m
osx dialog provider: implemented the progress bar dialog
[vlc] / modules / gui / macosx_dialog_provider / dialogProvider.m
1 /*****************************************************************************
2  * dialogProvider.m: Minimal Dialog Provider for Mac OS X
3  *****************************************************************************
4  * Copyright (C) 2009-2010 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Felix Paul Kühne <fkuehne at videolan dot org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #import <stdlib.h>                                      /* malloc(), free() */
28 #import <string.h>
29
30 #ifdef HAVE_CONFIG_H
31 # import "config.h"
32 #endif
33
34 #import <vlc_common.h>
35 #import <vlc_plugin.h>
36 #import <vlc_dialog.h>
37 #import <vlc_interface.h>
38
39 #import <Cocoa/Cocoa.h>
40 #import "VLCLoginPanel.h"
41 #import "VLCProgressPanel.h"
42
43 /*****************************************************************************
44  * Prototypes
45  *****************************************************************************/
46 static int  OpenIntf(vlc_object_t *);
47 static void CloseIntf(vlc_object_t *);
48 static void Run(intf_thread_t * );
49
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 * );
55
56 static void updateProgressPanel (void *, const char *, float);
57 static bool checkProgressPanel (void *);
58 static void destroyProgressPanel (void *);
59
60 struct intf_sys_t
61 {
62     VLCProgressPanel *currentProgressBarPanel;
63
64     vlc_mutex_t lock;
65     vlc_cond_t wait;
66 };
67
68 /*****************************************************************************
69  * Module descriptor
70  *****************************************************************************/
71
72 vlc_module_begin()
73 /* Minimal interface. see intf.m */
74 set_shortname("Mac OS X Dialogs")
75 add_shortcut("macosx_dialog_provider")
76 add_shortcut("miosx")
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)
82 vlc_module_end()
83
84 /*****************************************************************************
85  * OpenIntf: initialize interface
86  *****************************************************************************/
87 int OpenIntf(vlc_object_t *p_this)
88 {
89     intf_thread_t *p_intf = (intf_thread_t*) p_this;
90
91     p_intf->p_sys = malloc(sizeof(intf_sys_t));
92     if(!p_intf->p_sys)
93         return VLC_ENOMEM;
94
95     memset(p_intf->p_sys,0,sizeof(*p_intf->p_sys));
96
97     p_intf->pf_run = Run;
98
99     msg_Dbg(p_intf,"Opening Mac OS X dialog provider");
100     return VLC_SUCCESS;
101 }
102
103 /*****************************************************************************
104  * Run: waiting for the death
105  *****************************************************************************/
106 static void Run( intf_thread_t *p_intf )
107 {
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);
120
121     msg_Dbg(p_intf,"Mac OS X dialog provider initialised");
122
123     /* idle */
124     while(vlc_object_alive(p_intf))
125     {
126         sleep( 100000 );
127     }
128     
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);
136 }
137 /*****************************************************************************
138  * CloseIntf: destroy interface
139  *****************************************************************************/
140 void CloseIntf(vlc_object_t *p_this)
141 {
142     intf_thread_t *p_intf = (intf_thread_t*) p_this;
143
144     msg_Dbg(p_intf,"Mac OS X dialog provider closed");
145     free(p_intf->p_sys);
146 }
147
148
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)
153 {
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],
158                             @"OK", nil, nil);
159     [o_pool release];
160     return VLC_SUCCESS;
161 }
162
163 static int DisplayCritical(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
164 {
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],
169                             @"OK", nil, nil);
170     [o_pool release];
171     return VLC_SUCCESS;
172 }
173
174 static int DisplayQuestion(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
175 {
176     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
177     dialog_question_t *p_dialog = (dialog_question_t *)value.p_address;
178     NSAlert *o_alert;
179     NSString *o_yes, *o_no, *o_cancel;
180     NSInteger i_returnValue = 0;
181
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];
188
189     o_alert = [NSAlert alertWithMessageText:[NSString stringWithUTF8String:p_dialog->title]
190                               defaultButton:o_yes
191                             alternateButton:o_no 
192                                 otherButton:o_cancel
193                   informativeTextWithFormat:[NSString stringWithUTF8String:p_dialog->message]];
194     [o_alert setAlertStyle:NSInformationalAlertStyle];
195     i_returnValue = [o_alert runModal];
196
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;
203     [o_pool release];
204     return VLC_SUCCESS;
205 }
206
207 static int DisplayLogin(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
208 {
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]];
216     [thePanel center];
217     i_returnValue = [NSApp runModalForWindow:thePanel];
218     [thePanel close];
219     if (i_returnValue) {
220         *p_dialog->username = strdup( [[thePanel userName] UTF8String] );
221         *p_dialog->password = strdup( [[thePanel password] UTF8String] );
222     } else
223         *p_dialog->username = *p_dialog->password = NULL;
224     [o_pool release];
225     return VLC_SUCCESS;
226 }
227
228 static int DisplayProgressPanelAction(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
229 {
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;
234
235     if(p_sys->currentProgressBarPanel)
236         [p_sys->currentProgressBarPanel release];
237
238     p_sys->currentProgressBarPanel = [[VLCProgressPanel alloc] init];
239     [p_sys->currentProgressBarPanel createContentView];
240     if (p_dialog->title)
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];
248
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;
253
254     [o_pool release];
255     return VLC_SUCCESS;
256 }
257
258 void updateProgressPanel (void *priv, const char *text, float value)
259 {
260     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
261     intf_sys_t *p_sys = (intf_sys_t *)priv;
262
263     if (text)
264         [p_sys->currentProgressBarPanel setDialogMessage:[NSString stringWithUTF8String:text]];
265     else
266         [p_sys->currentProgressBarPanel setDialogMessage:@""];
267     [p_sys->currentProgressBarPanel setProgressAsDouble:(double)(value * 1000.)];
268
269     [o_pool release];
270 }
271
272 void destroyProgressPanel (void *priv)
273 {
274     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
275     intf_sys_t *p_sys = (intf_sys_t *)priv;
276
277     [p_sys->currentProgressBarPanel close];
278     [p_sys->currentProgressBarPanel release];
279
280     [o_pool release];
281 }
282
283 bool checkProgressPanel (void *priv)
284 {
285     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
286     intf_sys_t *p_sys = (intf_sys_t *)priv;
287     BOOL b_returned;
288
289     b_returned = [p_sys->currentProgressBarPanel isCancelled];
290
291     [o_pool release];
292     return b_returned;
293 }
294