]> git.sesse.net Git - vlc/blob - modules/gui/macosx_dialog_provider/dialogProvider.m
osx dialog provider: minor cleanup
[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->pf_run = Run;
90
91     msg_Dbg(p_intf,"Opening Mac OS X dialog provider");
92     return VLC_SUCCESS;
93 }
94
95 /*****************************************************************************
96  * Run: waiting for the death
97  *****************************************************************************/
98 static void Run( intf_thread_t *p_intf )
99 {
100     /* subscribe to various interactive dialogues */
101     var_Create(p_intf,"dialog-error",VLC_VAR_ADDRESS);
102     var_AddCallback(p_intf,"dialog-error",DisplayError,p_intf);
103     var_Create(p_intf,"dialog-critical",VLC_VAR_ADDRESS);
104     var_AddCallback(p_intf,"dialog-critical",DisplayCritical,p_intf);
105     var_Create(p_intf,"dialog-login",VLC_VAR_ADDRESS);
106     var_AddCallback(p_intf,"dialog-login",DisplayLogin,p_intf);
107     var_Create(p_intf,"dialog-question",VLC_VAR_ADDRESS);
108     var_AddCallback(p_intf,"dialog-question",DisplayQuestion,p_intf);
109     //    var_Create(p_intf,"dialog-progress-bar",VLC_VAR_ADDRESS);
110     //    var_AddCallback(p_intf,"dialog-progress-bar",DisplayProgressPanelAction,p_intf);
111     dialog_Register(p_intf);
112
113     msg_Dbg(p_intf,"Mac OS X dialog provider initialised");
114
115     /* idle */
116     while(vlc_object_alive(p_intf))
117     {
118         sleep( 100000 );
119     }
120     
121     /* unsubscribe from the interactive dialogues */
122     dialog_Unregister(p_intf );
123     var_DelCallback(p_intf,"dialog-error",DisplayError,p_intf);
124     var_DelCallback(p_intf,"dialog-critical",DisplayCritical,p_intf);
125     var_DelCallback(p_intf,"dialog-login",DisplayLogin,p_intf);
126     var_DelCallback(p_intf,"dialog-question",DisplayQuestion,p_intf);
127     //    var_DelCallback(p_intf,"dialog-progress-bar",DisplayProgressPanelAction,p_intf);
128 }
129 /*****************************************************************************
130  * CloseIntf: destroy interface
131  *****************************************************************************/
132 void CloseIntf(vlc_object_t *p_this)
133 {
134     intf_thread_t *p_intf = (intf_thread_t*) p_this;
135
136     msg_Dbg(p_intf,"Mac OS X dialog provider closed");
137     free(p_intf->p_sys);
138 }
139
140
141 /*****************************************************************************
142  * Callbacks triggered by the "dialog-*" variables
143  *****************************************************************************/
144 static int DisplayError(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
145 {
146     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
147     dialog_fatal_t *p_dialog = (dialog_fatal_t *)value.p_address;
148     NSRunInformationalAlertPanel([NSString stringWithUTF8String:p_dialog->title],
149                             [NSString stringWithUTF8String:p_dialog->message],
150                             @"OK", nil, nil);
151     [o_pool release];
152 }
153
154 static int DisplayCritical(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
155 {
156     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
157     dialog_fatal_t *p_dialog = (dialog_fatal_t *)value.p_address;
158     NSRunCriticalAlertPanel([NSString stringWithUTF8String:p_dialog->title],
159                             [NSString stringWithUTF8String:p_dialog->message],
160                             @"OK", nil, nil);
161     [o_pool release];
162 }
163
164 static int DisplayQuestion(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
165 {
166     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
167     dialog_question_t *p_dialog = (dialog_question_t *)value.p_address;
168     NSAlert *o_alert;
169     NSString *o_yes, *o_no, *o_cancel;
170     NSInteger i_returnValue = 0;
171
172     if (p_dialog->yes != NULL)
173         o_yes = [NSString stringWithUTF8String:p_dialog->yes];
174     if (p_dialog->no != NULL)
175         o_no = [NSString stringWithUTF8String:p_dialog->no];
176     if (p_dialog->cancel != NULL)
177         o_cancel = [NSString stringWithUTF8String:p_dialog->cancel];
178
179     o_alert = [NSAlert alertWithMessageText:[NSString stringWithUTF8String:p_dialog->title]
180                               defaultButton:o_yes
181                             alternateButton:o_no 
182                                 otherButton:o_cancel
183                   informativeTextWithFormat:[NSString stringWithUTF8String:p_dialog->message]];
184     [o_alert setAlertStyle:NSInformationalAlertStyle];
185     i_returnValue = [o_alert runModal];
186
187     if (i_returnValue == NSAlertDefaultReturn)
188         p_dialog->answer = 1;
189     if (i_returnValue == NSAlertAlternateReturn)
190         p_dialog->answer = 2;
191     if (i_returnValue == NSAlertOtherReturn)
192         p_dialog->answer = 3;
193     [o_pool release];
194 }
195
196 static int DisplayLogin(vlc_object_t *p_this, const char *type, vlc_value_t previous, vlc_value_t value, void *data)
197 {
198     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
199     dialog_login_t *p_dialog = (dialog_login_t *)value.p_address;
200     NSInteger i_returnValue = 0;
201     VLCLoginPanel *thePanel = [[VLCLoginPanel alloc] init];
202     [thePanel createContentView];
203     [thePanel setDialogTitle:[NSString stringWithUTF8String:p_dialog->title]];
204     [thePanel setDialogMessage:[NSString stringWithUTF8String:p_dialog->message]];
205     [thePanel center];
206     i_returnValue = [NSApp runModalForWindow: thePanel];
207     [thePanel close];
208     if( i_returnValue )
209     {
210         *p_dialog->username = strdup( [[thePanel userName] UTF8String] );
211         *p_dialog->password = strdup( [[thePanel password] UTF8String] );
212     }
213     else
214     {
215         *p_dialog->username = *p_dialog->password = NULL;
216     }    
217     [o_pool release];
218 }