]> git.sesse.net Git - vlc/blob - modules/gui/macosx_dialog_provider/dialogProvider.m
d997292fc9195e77a8d66ae8b3b9899a68cc79d7
[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
42 /*****************************************************************************
43  * Prototypes
44  *****************************************************************************/
45 static int  OpenIntf(vlc_object_t *);
46 static void CloseIntf(vlc_object_t *);
47 static void Run(intf_thread_t * );
48
49 static int DisplayError(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
50 static int DisplayCritical(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
51 static int DisplayQuestion(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
52 static int DisplayLogin(vlc_object_t *,const char *,vlc_value_t,vlc_value_t,void * );
53
54 struct intf_sys_t
55 {
56     vlc_mutex_t lock;
57     vlc_cond_t wait;
58 };
59
60 /*****************************************************************************
61  * Module descriptor
62  *****************************************************************************/
63
64 vlc_module_begin()
65 /* Minimal interface. see intf.m */
66 set_shortname("Mac OS X Dialogs")
67 add_shortcut("macosx_dialog_provider")
68 add_shortcut("miosx")
69 set_description("Minimal Mac OS X Dialog Provider")
70 set_capability("interface", 50)
71 set_callbacks(OpenIntf, CloseIntf)
72 set_category(CAT_INTERFACE)
73 set_subcategory(SUBCAT_INTERFACE_MAIN)
74 vlc_module_end()
75
76 /*****************************************************************************
77  * OpenIntf: initialize interface
78  *****************************************************************************/
79 int OpenIntf(vlc_object_t *p_this)
80 {
81     intf_thread_t *p_intf = (intf_thread_t*) p_this;
82
83     p_intf->p_sys = malloc(sizeof(intf_sys_t));
84     if(!p_intf->p_sys)
85         return VLC_ENOMEM;
86
87     memset(p_intf->p_sys,0,sizeof(*p_intf->p_sys));
88
89 //    p_intf->b_should_run_on_first_thread = true;
90     p_intf->pf_run = Run;
91
92     msg_Dbg(p_intf,"Opening Mac OS X dialog provider");
93     return VLC_SUCCESS;
94 }
95
96 /*****************************************************************************
97  * Run: waiting for the death
98  *****************************************************************************/
99 static void Run( intf_thread_t *p_intf )
100 {
101     /* subscribe to various interactive dialogues */
102     var_Create(p_intf,"dialog-error",VLC_VAR_ADDRESS);
103     var_AddCallback(p_intf,"dialog-error",DisplayError,p_intf);
104     var_Create(p_intf,"dialog-critical",VLC_VAR_ADDRESS);
105     var_AddCallback(p_intf,"dialog-critical",DisplayCritical,p_intf);
106     var_Create(p_intf,"dialog-login",VLC_VAR_ADDRESS);
107     var_AddCallback(p_intf,"dialog-login",DisplayLogin,p_intf);
108     var_Create(p_intf,"dialog-question",VLC_VAR_ADDRESS);
109     var_AddCallback(p_intf,"dialog-question",DisplayQuestion,p_intf);
110     //    var_Create(p_intf,"dialog-progress-bar",VLC_VAR_ADDRESS);
111     //    var_AddCallback(p_intf,"dialog-progress-bar",DisplayProgressPanelAction,p_intf);
112     dialog_Register(p_intf);
113
114     msg_Dbg(p_intf,"Mac OS X dialog provider initialised");
115
116     /* idle */
117     while(vlc_object_alive(p_intf))
118     {
119         sleep( 100000 );
120     }
121     
122     /* unsubscribe from the interactive dialogues */
123     dialog_Unregister(p_intf );
124     var_DelCallback(p_intf,"dialog-error",DisplayError,p_intf);
125     var_DelCallback(p_intf,"dialog-critical",DisplayCritical,p_intf);
126     var_DelCallback(p_intf,"dialog-login",DisplayLogin,p_intf);
127     var_DelCallback(p_intf,"dialog-question",DisplayQuestion,p_intf);
128     //    var_DelCallback(p_intf,"dialog-progress-bar",DisplayProgressPanelAction,p_intf);
129 }
130 /*****************************************************************************
131  * CloseIntf: destroy interface
132  *****************************************************************************/
133 void CloseIntf(vlc_object_t *p_this)
134 {
135     intf_thread_t *p_intf = (intf_thread_t*) p_this;
136
137     msg_Dbg(p_intf,"Mac OS X dialog provider closed");
138     free(p_intf->p_sys);
139 }
140
141
142 /*****************************************************************************
143  * Callbacks triggered by the "dialog-*" variables
144  *****************************************************************************/
145 static int DisplayError(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
146 {
147     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
148     dialog_fatal_t *p_dialog = (dialog_fatal_t *)value.p_address;
149     NSRunInformationalAlertPanel([NSString stringWithUTF8String:p_dialog->title],
150                             [NSString stringWithUTF8String:p_dialog->message],
151                             @"OK", nil, nil);
152     [o_pool release];
153 }
154
155 static int DisplayCritical(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
156 {
157     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
158     dialog_fatal_t *p_dialog = (dialog_fatal_t *)value.p_address;
159     NSRunCriticalAlertPanel([NSString stringWithUTF8String:p_dialog->title],
160                             [NSString stringWithUTF8String:p_dialog->message],
161                             @"OK", nil, nil);
162     [o_pool release];
163 }
164
165 static int DisplayQuestion(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
166 {
167     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
168     dialog_question_t *p_dialog = (dialog_question_t *)value.p_address;
169     NSAlert *o_alert;
170     NSString *o_yes, *o_no, *o_cancel;
171     NSInteger i_returnValue = 0;
172
173     if (p_dialog->yes != NULL)
174         o_yes = [NSString stringWithUTF8String:p_dialog->yes];
175     if (p_dialog->no != NULL)
176         o_no = [NSString stringWithUTF8String:p_dialog->no];
177     if (p_dialog->cancel != NULL)
178         o_cancel = [NSString stringWithUTF8String:p_dialog->cancel];
179
180     o_alert = [NSAlert alertWithMessageText:[NSString stringWithUTF8String:p_dialog->title]
181                               defaultButton:o_yes
182                             alternateButton:o_no 
183                                 otherButton:o_cancel
184                   informativeTextWithFormat:[NSString stringWithUTF8String:p_dialog->message]];
185     [o_alert setAlertStyle:NSInformationalAlertStyle];
186     i_returnValue = [o_alert runModal];
187
188     if (i_returnValue == NSAlertDefaultReturn)
189         p_dialog->answer = 1;
190     if (i_returnValue == NSAlertAlternateReturn)
191         p_dialog->answer = 2;
192     if (i_returnValue == NSAlertOtherReturn)
193         p_dialog->answer = 3;
194     [o_pool release];
195 }
196
197 static int DisplayLogin(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
198 {
199     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
200     dialog_login_t *p_dialog = (dialog_login_t *)value.p_address;
201     NSInteger i_returnValue = 0;
202     VLCLoginPanel *thePanel = [[VLCLoginPanel alloc] init];
203     [thePanel createContentView];
204     [thePanel setDialogTitle:[NSString stringWithUTF8String:p_dialog->title]];
205     [thePanel setDialogMessage:[NSString stringWithUTF8String:p_dialog->message]];
206     [thePanel center];
207     i_returnValue = [NSApp runModalForWindow: thePanel];
208     [thePanel close];
209     if( i_returnValue )
210     {
211         *p_dialog->username = strdup( [[thePanel userName] UTF8String] );
212         *p_dialog->password = strdup( [[thePanel password] UTF8String] );
213     }
214     else
215     {
216         *p_dialog->username = *p_dialog->password = NULL;
217     }    
218     [o_pool release];
219 }