1 /*****************************************************************************
2 * coredialogs.m: Mac OS X Core Dialogs
3 *****************************************************************************
4 * Copyright (C) 2005-2009 the VideoLAN team
7 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8 * Felix Paul Kühne <fkuehne at videolan dot org>
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.
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.
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 *****************************************************************************/
26 #import "coredialogs.h"
29 /* for the icon in our custom error panel */
30 #import <ApplicationServices/ApplicationServices.h>
32 /*****************************************************************************
33 * VLCCoreDialogProvider implementation
34 *****************************************************************************/
35 @implementation VLCCoreDialogProvider
37 static VLCCoreDialogProvider *_o_sharedInstance = nil;
39 + (VLCCoreDialogProvider *)sharedInstance
41 return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
46 if( _o_sharedInstance )
50 _o_sharedInstance = [super init];
51 [[NSNotificationCenter defaultCenter] addObserver:self
52 selector:@selector(performDialogEvent:)
53 name: @"VLCNewCoreDialogEventNotification"
55 o_error_panel = [[VLCErrorPanel alloc] init];
56 b_progress_cancelled = NO;
59 return _o_sharedInstance;
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];
73 -(void)performDialogEvent: (NSNotification *)o_notification
75 NSValue *o_value = [[o_notification userInfo] objectForKey:@"VLCDialogPointer"];
76 NSString *o_type = [[o_notification userInfo] objectForKey:@"VLCDialogType"];
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];
87 msg_Err( VLCIntf, "unhandled dialog type: '%s'", [o_type UTF8String] );
90 -(void)showFatalDialog: (NSValue *)o_value
92 dialog_fatal_t *p_dialog = [o_value pointerValue];
93 /* do we need to block ? */
94 if( p_dialog->modal == YES )
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];
103 [o_error_panel addError: [NSString stringWithUTF8String: p_dialog->title] withMsg: [NSString stringWithUTF8String: p_dialog->message]];
104 [o_error_panel showPanel];
108 -(void)showQuestionDialog: (NSValue *)o_value
110 dialog_question_t *p_dialog = [o_value pointerValue];
112 NSString *o_yes, *o_no, *o_cancel;
113 NSInteger i_returnValue = 0;
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];
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];
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;
134 -(void)showLoginDialog: (NSValue *)o_value
136 dialog_login_t *p_dialog = [o_value pointerValue];
137 NSInteger i_returnValue = 0;
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: @""];
146 i_returnValue = [NSApp runModalForWindow: o_auth_win];
150 *p_dialog->username = strdup( [[o_auth_login_fld stringValue] UTF8String] );
151 *p_dialog->password = strdup( [[o_auth_pw_fld stringValue] UTF8String] );
155 *p_dialog->username = *p_dialog->password = NULL;
159 -(IBAction)loginDialogAction:(id)sender
161 if( [[sender title] isEqualToString: _NS("OK")] )
162 [NSApp stopModalWithCode: 1];
164 [NSApp stopModalWithCode: 0];
167 -(void)showProgressDialog: (NSValue *)o_value
169 dialog_progress_bar_t *p_dialog = [o_value pointerValue];
171 if( p_dialog->title != NULL )
173 [o_prog_win setTitle: [NSString stringWithUTF8String: p_dialog->title]];
174 [o_prog_title_txt setStringValue: [NSString stringWithUTF8String: p_dialog->title]];
178 [o_prog_win setTitle: @""];
179 [o_prog_title_txt setStringValue: @""];
181 if( p_dialog->cancel != NULL )
182 [o_prog_cancel_btn setTitle: [NSString stringWithUTF8String: p_dialog->cancel]];
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]];
188 [o_prog_description_txt setStringValue: @""];
189 [o_prog_bar setDoubleValue: 0];
190 [o_prog_bar startAnimation: self];
192 [o_prog_win makeKeyAndOrderFront: self];
195 -(void)updateProgressPanelWithText: (NSString *)string andNumber: (double)d_number
197 [o_prog_description_txt setStringValue: string];
198 [o_prog_bar setDoubleValue: d_number];
201 -(void)destroyProgressPanel
203 [o_prog_bar stopAnimation: self];
207 -(IBAction)progDialogAction:(id)sender
209 b_progress_cancelled = YES;
212 -(BOOL)progressCancelled
214 return b_progress_cancelled;
219 return o_error_panel;
224 [[NSNotificationCenter defaultCenter] removeObserver:self];
229 /*****************************************************************************
230 * VLCErrorPanel implementation
231 *****************************************************************************/
232 @implementation VLCErrorPanel
237 nib_loaded = [NSBundle loadNibNamed:@"InteractionErrorPanel" owner:self];
240 [o_window setTitle: _NS("Errors and Warnings")];
241 [o_cleanup_button setTitle: _NS("Clean up")];
242 [o_messages_btn setTitle: _NS("Show Details")];
244 /* init data sources */
245 o_errors = [[NSMutableArray alloc] init];
246 o_icons = [[NSMutableArray alloc] init];
260 [o_window makeKeyAndOrderFront: self];
263 -(void)addError: (NSString *)o_error withMsg:(NSString *)o_msg
265 /* format our string as desired */
266 NSMutableAttributedString * ourError;
267 ourError = [[NSMutableAttributedString alloc] initWithString:
268 [NSString stringWithFormat:@"%@\n%@", o_error, o_msg]
270 [NSDictionary dictionaryWithObject: [NSFont systemFontOfSize:11] forKey: NSFontAttributeName]];
272 addAttribute: NSFontAttributeName
273 value: [NSFont boldSystemFontOfSize:11]
274 range: NSMakeRange( 0, [o_error length])];
275 [o_errors addObject: ourError];
278 [o_icons addObject: [NSImage imageWithErrorIcon]];
280 [o_error_table reloadData];
283 -(IBAction)cleanupTable:(id)sender
285 [o_errors removeAllObjects];
286 [o_icons removeAllObjects];
287 [o_error_table reloadData];
290 -(IBAction)showMessages:(id)sender
292 [[VLCMain sharedInstance] showMessagesPanel: sender];
295 /*----------------------------------------------------------------------------
296 * data source methods
297 *---------------------------------------------------------------------------*/
298 - (NSInteger)numberOfRowsInTableView:(NSTableView *)theDataTable
300 return [o_errors count];
303 - (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn:
304 (NSTableColumn *)theTableColumn row: (NSInteger)row
306 if( [[theTableColumn identifier] isEqualToString: @"error_msg"] )
307 return [o_errors objectAtIndex: row];
309 if( [[theTableColumn identifier] isEqualToString: @"icon"] )
310 return [o_icons objectAtIndex: row];
312 return @"unknown identifier";