]> git.sesse.net Git - vlc/blob - modules/gui/macosx/interaction.m
* first implementation of a widget-free authentication-dialogue (core and OSX only...
[vlc] / modules / gui / macosx / interaction.m
1 /*****************************************************************************
2  * interaction.h: Mac OS X interaction dialogs
3  *****************************************************************************
4  * Copyright (C) 2005-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8  *          Felix Kühne <fkuehne at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include "intf.h"
26 #import "interaction.h"
27
28 /*****************************************************************************
29  * VLCInteractionList implementation
30  *****************************************************************************/
31 @implementation VLCInteractionList
32
33 -(id)init
34 {
35     [super init];
36     o_interaction_list = [[NSMutableArray alloc] initWithCapacity:1];
37     [[NSNotificationCenter defaultCenter] addObserver:self
38         selector:@selector(newInteractionEvent:)
39         name: @"VLCNewInteractionEventNotification"
40         object:self];
41
42     return self;
43 }
44
45 -(void)newInteractionEvent: (NSNotification *)o_notification
46 {
47     VLCInteraction *o_interaction;
48     NSValue *o_value = [[o_notification userInfo] objectForKey:@"VLCDialogPointer"];
49     interaction_dialog_t *p_dialog = [o_value pointerValue];
50
51     switch( p_dialog->i_action )
52     {
53     case INTERACT_NEW:
54         [self addInteraction: p_dialog];
55         break;
56     case INTERACT_UPDATE:
57         o_interaction = (VLCInteraction *)p_dialog->p_private;
58         [o_interaction updateDialog];
59         break;
60     case INTERACT_HIDE:
61         o_interaction = (VLCInteraction *)p_dialog->p_private;
62         [o_interaction hideDialog];
63         break;
64     case INTERACT_DESTROY:
65         o_interaction = (VLCInteraction *)p_dialog->p_private;
66         [o_interaction destroyDialog];
67         [self removeInteraction:o_interaction];
68         p_dialog->i_status = DESTROYED_DIALOG;
69         break;
70     }
71 }
72
73 -(void)addInteraction: (interaction_dialog_t *)p_dialog
74 {
75
76     VLCInteraction *o_interaction = [[VLCInteraction alloc] initDialog: p_dialog];
77     
78     p_dialog->p_private = (void *)o_interaction;
79     [o_interaction_list addObject:[o_interaction autorelease]];
80     [o_interaction runDialog];
81 }
82
83 -(void)removeInteraction: (VLCInteraction *)o_interaction
84 {
85     [o_interaction_list removeObject:o_interaction];
86 }
87
88 -(void)dealloc
89 {
90     [[NSNotificationCenter defaultCenter] removeObserver:self];
91     [o_interaction_list removeAllObjects];
92     [o_interaction_list release];
93     [super dealloc];
94 }
95
96 @end
97
98 /*****************************************************************************
99  * VLCInteraction implementation
100  *****************************************************************************/
101 @implementation VLCInteraction
102
103 -(id)initDialog: (interaction_dialog_t *)_p_dialog
104 {
105     p_intf = VLCIntf;
106     [super init];
107     p_dialog = _p_dialog;
108     return self;
109 }
110
111 -(void)runDialog
112 {
113     int i = 0;
114     id o_window = NULL;
115     if( !p_dialog )
116         msg_Err( p_intf, "no available interaction framework" );
117
118     if( !nib_interact_loaded )
119     {
120         nib_interact_loaded = [NSBundle loadNibNamed:@"Interaction" owner:self];
121         [o_prog_cancel_btn setTitle: _NS("Cancel")];
122         [o_prog_bar setUsesThreadedAnimation: YES];
123         [o_auth_login_txt setStringValue: _NS("Login:")];
124         [o_auth_pw_txt setStringValue: _NS("Password:")];
125         [o_auth_cancel_btn setTitle: _NS("Cancel")];
126         [o_auth_ok_btn setTitle: _NS("OK")];
127     }
128
129     NSString *o_title = [NSString stringWithUTF8String:p_dialog->psz_title ? p_dialog->psz_title : "title"];
130     NSString *o_description = [NSString stringWithUTF8String:p_dialog->psz_description ? p_dialog->psz_description : ""];
131     
132     vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
133     if( p_vout != NULL )
134     {
135         NSEnumerator * o_enum = [[NSApp orderedWindows] objectEnumerator];
136
137         while( ( o_window = [o_enum nextObject] ) )
138         {
139             if( [[o_window className] isEqualToString: @"VLCWindow"] )
140             {
141                 vlc_object_release( (vlc_object_t *)p_vout );
142                 break;
143             }
144         }
145         vlc_object_release( (vlc_object_t *)p_vout );
146     }
147     else
148     {
149         o_window = [NSApp mainWindow];
150     }
151     
152     msg_Dbg( p_intf, "Title: %s", [o_title UTF8String] );
153     msg_Dbg( p_intf, "Description: %s", [o_description UTF8String] );
154     if( p_dialog->i_id == DIALOG_ERRORS )
155     {
156         for( i = 0; i < p_dialog->i_widgets; i++ )
157         {
158             msg_Err( p_intf, "Error: %s", p_dialog->pp_widgets[i]->psz_text );
159         }
160     }
161     else
162     {
163         for( i = 0; i < p_dialog->i_widgets; i++ )
164         {
165             msg_Dbg( p_intf, "widget: %s", p_dialog->pp_widgets[i]->psz_text );
166             o_description = [o_description stringByAppendingString: \
167                 [NSString stringWithUTF8String: \
168                     p_dialog->pp_widgets[i]->psz_text]];
169         }
170         if( p_dialog->i_flags & DIALOG_OK_CANCEL )
171         {
172             msg_Dbg( p_intf, "requested flag: DIALOG_OK_CANCEL" );
173             NSBeginInformationalAlertSheet( o_title, @"OK" , @"Cancel", nil, \
174                 o_window, self,@selector(sheetDidEnd: returnCode: contextInfo:),\
175                 NULL, nil, o_description );
176         }
177         else if( p_dialog->i_flags & DIALOG_YES_NO_CANCEL )
178         {
179             msg_Dbg( p_intf, "requested flag: DIALOG_YES_NO_CANCEL" );
180             NSBeginInformationalAlertSheet( o_title, @"Yes", @"No", @"Cancel", \
181                 o_window, self,@selector(sheetDidEnd: returnCode: contextInfo:),\
182                 NULL, nil, o_description );
183         }
184         else if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
185         {
186             msg_Dbg( p_intf, "requested flag: DIALOG_LOGIN_PW_OK_CANCEL" );
187             [o_auth_title setStringValue: o_title];
188             [o_auth_description setStringValue: o_description];
189             [o_auth_login_fld setStringValue: @""];
190             [o_auth_pw_fld setStringValue: @""];
191             [NSApp beginSheet: o_auth_win modalForWindow: o_window \
192                 modalDelegate: self didEndSelector: nil contextInfo: nil];
193             [o_auth_win makeKeyWindow];
194         }
195         else if( p_dialog->i_type & WIDGET_PROGRESS )
196         {
197             msg_Dbg( p_intf, "requested type: WIDGET_PROGRESS" );
198             [o_prog_title setStringValue: o_title];
199             [o_prog_description setStringValue: o_description];
200             [o_prog_bar setDoubleValue: 0];
201             [NSApp beginSheet: o_prog_win modalForWindow: o_window \
202                 modalDelegate: self didEndSelector: nil contextInfo: nil];
203             [o_prog_win makeKeyWindow];
204         }
205         else
206             msg_Warn( p_intf, "requested dialog type not implemented yet" );
207     }
208 }
209
210 - (void)sheetDidEnd:(NSWindow *)o_sheet returnCode:(int)i_return
211     contextInfo:(void *)o_context
212 {
213     vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
214     if( i_return == NSAlertDefaultReturn )
215     {
216         p_dialog->i_return = DIALOG_OK_YES;
217     }
218     else if( i_return == NSAlertAlternateReturn && ( p_dialog->i_flags & DIALOG_OK_CANCEL ) )
219     {
220         p_dialog->i_return = DIALOG_CANCELLED;
221     }
222     else if( i_return == NSAlertAlternateReturn )
223     {
224         p_dialog->i_return = DIALOG_NO;
225     }
226     else if( i_return == NSAlertOtherReturn )
227     {
228         p_dialog->i_return = DIALOG_CANCELLED;
229     }
230     p_dialog->i_status = ANSWERED_DIALOG;
231     vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
232 }
233
234 -(void)updateDialog
235 {
236     int i = 0;
237     for( i = 0 ; i< p_dialog->i_widgets; i++ )
238     {
239         if( p_dialog->i_type & WIDGET_PROGRESS )
240         {
241             [o_prog_bar setDoubleValue: \
242                 (double)(p_dialog->pp_widgets[i]->val.f_float)];
243
244             if( [o_prog_bar doubleValue] == 100.0 )
245             {
246                 /* we are done, let's hide */
247                 [self hideDialog];
248                 return;
249             }
250         }
251     }
252 }
253
254 -(void)hideDialog
255 {
256     msg_Dbg( p_intf, "hide event" );
257     if( p_dialog->i_type & WIDGET_PROGRESS )
258     {
259         [NSApp endSheet: o_prog_win];
260         [o_prog_win close];
261     }
262     if( p_dialog->i_flags & DIALOG_LOGIN_PW_OK_CANCEL )
263     {
264         [NSApp endSheet: o_auth_win];
265         [o_auth_win close];
266     }
267 }
268
269 -(void)destroyDialog
270 {
271     msg_Dbg( p_intf, "destroy event" );
272 }
273
274 - (IBAction)cancelAndClose:(id)sender
275 {
276     /* tell the core that the dialog was cancelled */
277     vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
278     p_dialog->i_return = DIALOG_CANCELLED;
279     p_dialog->i_status = ANSWERED_DIALOG;
280     vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
281     msg_Dbg( p_intf, "dialog cancelled" );
282 }
283
284 - (IBAction)okayAndClose:(id)sender
285 {
286     msg_Dbg( p_intf, "dialog's okay btn pressed, returning values" );
287     vlc_mutex_lock( &p_dialog->p_interaction->object_lock );
288     if( p_dialog->i_flags == DIALOG_LOGIN_PW_OK_CANCEL )
289     {
290         p_dialog->psz_returned[0] = strdup( [[o_auth_login_fld stringValue] UTF8String] );
291         p_dialog->psz_returned[1] = strdup( [[o_auth_pw_fld stringValue] UTF8String] );
292     }
293     p_dialog->i_return = DIALOG_OK_YES;
294     p_dialog->i_status = ANSWERED_DIALOG;
295     vlc_mutex_unlock( &p_dialog->p_interaction->object_lock );
296 }
297
298 @end