]> git.sesse.net Git - vlc/blob - modules/gui/macosx/coredialogs.m
62ed718530735c5cc622b60765882ac2eab1efa3
[vlc] / modules / gui / macosx / coredialogs.m
1 /*****************************************************************************
2  * coredialogs.m: Mac OS X Core Dialogs
3  *****************************************************************************
4  * Copyright (C) 2005-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8  *          Felix Paul 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 #import "intf.h"
26 #import "coredialogs.h"
27 #import "misc.h"
28
29 /* for the icon in our custom error panel */
30 #import <ApplicationServices/ApplicationServices.h>
31
32 /*****************************************************************************
33  * VLCCoreDialogProvider implementation
34  *****************************************************************************/
35 @implementation VLCCoreDialogProvider
36
37 static VLCCoreDialogProvider *_o_sharedInstance = nil;
38
39 + (VLCCoreDialogProvider *)sharedInstance
40 {
41     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
42 }
43
44 -(id)init
45 {
46     if( _o_sharedInstance )
47         [self dealloc];
48     else
49     {
50         _o_sharedInstance = [super init];
51         [[NSNotificationCenter defaultCenter] addObserver:self
52                                                  selector:@selector(performDialogEvent:)
53                                                      name: @"VLCNewCoreDialogEventNotification"
54                                                    object:self];
55         o_error_panel = [[VLCErrorPanel alloc] init];
56         b_progress_cancelled = NO;
57     }
58     
59     return _o_sharedInstance;
60 }
61
62 -(void)awakeFromNib
63 {
64     [o_auth_login_txt setStringValue: _NS("User name")];
65     [o_auth_pw_txt setStringValue: _NS("Password")];
66     [o_auth_cancel_btn setTitle: _NS("Cancel")];
67     [o_auth_ok_btn setTitle: _NS("OK")];
68     [o_prog_cancel_btn setTitle: _NS("Cancel")];
69     [o_prog_bar setUsesThreadedAnimation: YES];
70
71 }    
72
73 -(void)performDialogEvent: (NSNotification *)o_notification
74 {
75     NSValue *o_value = [[o_notification userInfo] objectForKey:@"VLCDialogPointer"];
76     NSString *o_type = [[o_notification userInfo] objectForKey:@"VLCDialogType"];
77
78     if( [o_type isEqualToString: @"dialog-fatal"] )
79         [self showFatalDialog: o_value];
80     else if( [o_type isEqualToString: @"dialog-question"] )
81         [self showQuestionDialog: o_value];
82     else if( [o_type isEqualToString: @"dialog-login"] )
83         [self showLoginDialog: o_value];
84     else if( [o_type isEqualToString: @"dialog-progress-bar"] )
85         [self showProgressDialog: o_value];
86     else
87         msg_Err( VLCIntf, "unhandled dialog type: '%s'", [o_type UTF8String] );
88 }
89
90 -(void)showFatalDialog: (NSValue *)o_value
91 {
92     dialog_fatal_t *p_dialog = [o_value pointerValue];
93     /* do we need to block ? */
94     if( p_dialog->modal == YES )
95     {
96         NSAlert *o_alert;
97         o_alert = [NSAlert alertWithMessageText: [NSString stringWithUTF8String: p_dialog->title] defaultButton: _NS("OK") alternateButton: nil otherButton: nil informativeTextWithFormat: [NSString stringWithUTF8String: p_dialog->message]];
98         [o_alert setAlertStyle: NSCriticalAlertStyle];
99         [o_alert runModal];
100     }
101     else
102     {
103         [o_error_panel addError: [NSString stringWithUTF8String: p_dialog->title] withMsg: [NSString stringWithUTF8String: p_dialog->message]];
104         [o_error_panel showPanel];
105     }
106 }
107
108 -(void)showQuestionDialog: (NSValue *)o_value
109 {
110     dialog_question_t *p_dialog = [o_value pointerValue];
111     NSAlert *o_alert;
112     NSString *o_yes, *o_no, *o_cancel;
113     NSInteger i_returnValue = 0;
114     
115     if( p_dialog->yes != NULL )
116         o_yes = [NSString stringWithUTF8String: p_dialog->yes];
117     if( p_dialog->no != NULL )
118         o_no = [NSString stringWithUTF8String: p_dialog->no];
119     if( p_dialog->cancel != NULL )
120         o_cancel = [NSString stringWithUTF8String: p_dialog->cancel];
121
122     o_alert = [NSAlert alertWithMessageText: [NSString stringWithUTF8String: p_dialog->title] defaultButton: o_yes alternateButton:o_no otherButton: o_cancel informativeTextWithFormat: [NSString stringWithUTF8String: p_dialog->message]];
123     [o_alert setAlertStyle: NSInformationalAlertStyle];
124     i_returnValue = [o_alert runModal];
125
126     if( i_returnValue == NSAlertDefaultReturn )
127         p_dialog->answer = 1;
128     if( i_returnValue == NSAlertAlternateReturn )
129         p_dialog->answer = 2;
130     if( i_returnValue == NSAlertOtherReturn )
131         p_dialog->answer = 3;
132 }
133
134 -(void)showLoginDialog: (NSValue *)o_value
135 {
136     dialog_login_t *p_dialog = [o_value pointerValue];
137     NSInteger i_returnValue = 0;
138
139     [o_auth_title_txt setStringValue: [NSString stringWithUTF8String: p_dialog->title]];
140     [o_auth_win setTitle: [NSString stringWithUTF8String: p_dialog->title]];
141     [o_auth_description_txt setStringValue: [NSString stringWithUTF8String: p_dialog->message]];
142     [o_auth_login_fld setStringValue: @""];
143     [o_auth_pw_fld setStringValue: @""];
144
145     [o_auth_win center];
146     i_returnValue = [NSApp runModalForWindow: o_auth_win];
147     [o_auth_win close];
148     if( i_returnValue )
149     {
150         *p_dialog->username = strdup( [[o_auth_login_fld stringValue] UTF8String] );
151         *p_dialog->password = strdup( [[o_auth_pw_fld stringValue] UTF8String] );
152     }
153     else
154     {
155          *p_dialog->username = *p_dialog->password = NULL;
156     }
157 }
158
159 -(IBAction)loginDialogAction:(id)sender
160 {
161     if( [[sender title] isEqualToString: _NS("OK")] )
162         [NSApp stopModalWithCode: 1];
163     else
164         [NSApp stopModalWithCode: 0];
165 }
166
167 -(void)showProgressDialog: (NSValue *)o_value
168 {
169     dialog_progress_bar_t *p_dialog = [o_value pointerValue];
170
171     if( p_dialog->title != NULL )
172     {
173         [o_prog_win setTitle: [NSString stringWithUTF8String: p_dialog->title]];
174         [o_prog_title_txt setStringValue: [NSString stringWithUTF8String: p_dialog->title]];
175     }
176     else
177     {
178         [o_prog_win setTitle: @""];
179         [o_prog_title_txt setStringValue: @""];
180     }
181     if( p_dialog->cancel != NULL )
182         [o_prog_cancel_btn setTitle: [NSString stringWithUTF8String: p_dialog->cancel]];
183     else
184         [o_prog_cancel_btn setTitle: _NS("Cancel")];
185     if( p_dialog->message != NULL )
186         [o_prog_description_txt setStringValue: [NSString stringWithUTF8String: p_dialog->message]];
187     else
188         [o_prog_description_txt setStringValue: @""];
189     [o_prog_bar setDoubleValue: 0];
190     [o_prog_bar startAnimation: self];
191
192     [o_prog_win makeKeyAndOrderFront: self];
193 }
194
195 -(void)updateProgressPanelWithText: (NSString *)string andNumber: (double)d_number
196 {
197     [o_prog_description_txt setStringValue: string];
198     [o_prog_bar setDoubleValue: d_number];
199 }
200
201 -(void)destroyProgressPanel
202 {
203     [o_prog_bar stopAnimation: self];
204     [o_prog_win close];
205 }
206
207 -(IBAction)progDialogAction:(id)sender
208 {
209     b_progress_cancelled = YES;
210 }
211
212 -(BOOL)progressCancelled
213 {
214     return b_progress_cancelled;
215 }
216
217 -(id)errorPanel
218 {
219     return o_error_panel;
220 }
221
222 -(void)dealloc
223 {
224     [[NSNotificationCenter defaultCenter] removeObserver:self];
225     [super dealloc];
226 }
227 @end
228
229 /*****************************************************************************
230  * VLCErrorPanel implementation
231  *****************************************************************************/
232 @implementation VLCErrorPanel
233 -(id)init
234 {
235     [super init];
236
237     nib_loaded = [NSBundle loadNibNamed:@"InteractionErrorPanel" owner:self];
238
239     /* init strings */
240     [o_window setTitle: _NS("Errors and Warnings")];
241     [o_cleanup_button setTitle: _NS("Clean up")];
242     [o_messages_btn setTitle: _NS("Show Details")];
243
244     /* init data sources */
245     o_errors = [[NSMutableArray alloc] init];
246     o_icons = [[NSMutableArray alloc] init];
247
248     return self;
249 }
250
251 -(void)dealloc
252 {
253     [o_errors release];
254     [o_icons release];
255     [super dealloc];
256 }
257
258 -(void)showPanel
259 {
260     [o_window makeKeyAndOrderFront: self];
261 }
262
263 -(void)addError: (NSString *)o_error withMsg:(NSString *)o_msg
264 {
265     /* format our string as desired */
266     NSMutableAttributedString * ourError;
267     ourError = [[NSMutableAttributedString alloc] initWithString:
268         [NSString stringWithFormat:@"%@\n%@", o_error, o_msg]
269         attributes:
270         [NSDictionary dictionaryWithObject: [NSFont systemFontOfSize:11] forKey: NSFontAttributeName]];
271     [ourError
272         addAttribute: NSFontAttributeName
273         value: [NSFont boldSystemFontOfSize:11]
274         range: NSMakeRange( 0, [o_error length])];
275     [o_errors addObject: ourError];
276     [ourError release];
277
278     [o_icons addObject: [NSImage imageWithErrorIcon]];
279
280     [o_error_table reloadData];
281 }
282
283 -(IBAction)cleanupTable:(id)sender
284 {
285     [o_errors removeAllObjects];
286     [o_icons removeAllObjects];
287     [o_error_table reloadData];
288 }
289
290 -(IBAction)showMessages:(id)sender
291 {
292     [[VLCMain sharedInstance] showMessagesPanel: sender];
293 }
294
295 /*----------------------------------------------------------------------------
296  * data source methods
297  *---------------------------------------------------------------------------*/
298 - (NSInteger)numberOfRowsInTableView:(NSTableView *)theDataTable
299 {
300     return [o_errors count];
301 }
302
303 - (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn:
304     (NSTableColumn *)theTableColumn row: (NSInteger)row
305 {
306     if( [[theTableColumn identifier] isEqualToString: @"error_msg"] )
307         return [o_errors objectAtIndex: row];
308
309     if( [[theTableColumn identifier] isEqualToString: @"icon"] )
310         return [o_icons objectAtIndex: row];
311
312     return @"unknown identifier";
313 }
314
315 @end