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-error"] )
79 [self showFatalDialog: o_value];
80 else if( [o_type isEqualToString: @"dialog-critical"] )
81 [self showFatalWaitDialog: o_value];
82 else if( [o_type isEqualToString: @"dialog-question"] )
83 [self showQuestionDialog: o_value];
84 else if( [o_type isEqualToString: @"dialog-login"] )
85 [self showLoginDialog: o_value];
86 else if( [o_type isEqualToString: @"dialog-progress-bar"] )
87 [self showProgressDialog: o_value];
89 msg_Err( VLCIntf, "unhandled dialog type: '%s'", [o_type UTF8String] );
92 -(void)showFatalDialog: (NSValue *)o_value
94 dialog_fatal_t *p_dialog = [o_value pointerValue];
96 [o_error_panel addError: [NSString stringWithUTF8String: p_dialog->title] withMsg: [NSString stringWithUTF8String: p_dialog->message]];
97 [o_error_panel showPanel];
100 -(void)showFatalWaitDialog: (NSValue *)o_value
102 dialog_fatal_t *p_dialog = [o_value pointerValue];
105 o_alert = [NSAlert alertWithMessageText: [NSString stringWithUTF8String: p_dialog->title] defaultButton: _NS("OK") alternateButton: nil otherButton: nil informativeTextWithFormat: [NSString stringWithUTF8String: p_dialog->message]];
106 [o_alert setAlertStyle: NSCriticalAlertStyle];
110 -(void)showQuestionDialog: (NSValue *)o_value
112 dialog_question_t *p_dialog = [o_value pointerValue];
114 NSString *o_yes, *o_no, *o_cancel;
115 NSInteger i_returnValue = 0;
117 if( p_dialog->yes != NULL )
118 o_yes = [NSString stringWithUTF8String: p_dialog->yes];
119 if( p_dialog->no != NULL )
120 o_no = [NSString stringWithUTF8String: p_dialog->no];
121 if( p_dialog->cancel != NULL )
122 o_cancel = [NSString stringWithUTF8String: p_dialog->cancel];
124 o_alert = [NSAlert alertWithMessageText: [NSString stringWithUTF8String: p_dialog->title] defaultButton: o_yes alternateButton:o_no otherButton: o_cancel informativeTextWithFormat: [NSString stringWithUTF8String: p_dialog->message]];
125 [o_alert setAlertStyle: NSInformationalAlertStyle];
126 i_returnValue = [o_alert runModal];
128 if( i_returnValue == NSAlertDefaultReturn )
129 p_dialog->answer = 1;
130 if( i_returnValue == NSAlertAlternateReturn )
131 p_dialog->answer = 2;
132 if( i_returnValue == NSAlertOtherReturn )
133 p_dialog->answer = 3;
136 -(void)showLoginDialog: (NSValue *)o_value
138 dialog_login_t *p_dialog = [o_value pointerValue];
139 NSInteger i_returnValue = 0;
141 [o_auth_title_txt setStringValue: [NSString stringWithUTF8String: p_dialog->title]];
142 [o_auth_win setTitle: [NSString stringWithUTF8String: p_dialog->title]];
143 [o_auth_description_txt setStringValue: [NSString stringWithUTF8String: p_dialog->message]];
144 [o_auth_login_fld setStringValue: @""];
145 [o_auth_pw_fld setStringValue: @""];
148 i_returnValue = [NSApp runModalForWindow: o_auth_win];
152 *p_dialog->username = strdup( [[o_auth_login_fld stringValue] UTF8String] );
153 *p_dialog->password = strdup( [[o_auth_pw_fld stringValue] UTF8String] );
157 *p_dialog->username = *p_dialog->password = NULL;
161 -(IBAction)loginDialogAction:(id)sender
163 if( [[sender title] isEqualToString: _NS("OK")] )
164 [NSApp stopModalWithCode: 1];
166 [NSApp stopModalWithCode: 0];
169 -(void)showProgressDialog: (NSValue *)o_value
171 dialog_progress_bar_t *p_dialog = [o_value pointerValue];
173 if( p_dialog->title != NULL )
175 [o_prog_win setTitle: [NSString stringWithUTF8String: p_dialog->title]];
176 [o_prog_title_txt setStringValue: [NSString stringWithUTF8String: p_dialog->title]];
180 [o_prog_win setTitle: @""];
181 [o_prog_title_txt setStringValue: @""];
183 if( p_dialog->cancel != NULL )
184 [o_prog_cancel_btn setTitle: [NSString stringWithUTF8String: p_dialog->cancel]];
186 [o_prog_cancel_btn setTitle: _NS("Cancel")];
187 if( p_dialog->message != NULL )
188 [o_prog_description_txt setStringValue: [NSString stringWithUTF8String: p_dialog->message]];
190 [o_prog_description_txt setStringValue: @""];
191 [o_prog_bar setDoubleValue: 0];
192 [o_prog_bar startAnimation: self];
194 [o_prog_win makeKeyAndOrderFront: self];
197 -(void)updateProgressPanelWithText: (NSString *)string andNumber: (double)d_number
199 [o_prog_description_txt setStringValue: string];
200 [o_prog_bar setDoubleValue: d_number];
203 -(void)destroyProgressPanel
205 [o_prog_bar stopAnimation: self];
209 -(IBAction)progDialogAction:(id)sender
211 b_progress_cancelled = YES;
214 -(BOOL)progressCancelled
216 return b_progress_cancelled;
221 return o_error_panel;
226 [[NSNotificationCenter defaultCenter] removeObserver:self];
231 /*****************************************************************************
232 * VLCErrorPanel implementation
233 *****************************************************************************/
234 @implementation VLCErrorPanel
241 b_nib_loaded = [NSBundle loadNibNamed:@"ErrorPanel" owner:self];
244 [o_window setTitle: _NS("Errors and Warnings")];
245 [o_cleanup_button setTitle: _NS("Clean up")];
246 [o_messages_btn setTitle: _NS("Show Details")];
249 /* init data sources */
250 o_errors = [[NSMutableArray alloc] init];
251 o_icons = [[NSMutableArray alloc] init];
265 [o_window makeKeyAndOrderFront: self];
268 -(void)addError: (NSString *)o_error withMsg:(NSString *)o_msg
270 /* format our string as desired */
271 NSMutableAttributedString * ourError;
272 ourError = [[NSMutableAttributedString alloc] initWithString:
273 [NSString stringWithFormat:@"%@\n%@", o_error, o_msg]
275 [NSDictionary dictionaryWithObject: [NSFont systemFontOfSize:11] forKey: NSFontAttributeName]];
277 addAttribute: NSFontAttributeName
278 value: [NSFont boldSystemFontOfSize:11]
279 range: NSMakeRange( 0, [o_error length])];
280 [o_errors addObject: ourError];
283 [o_icons addObject: [NSImage imageWithErrorIcon]];
285 [o_error_table reloadData];
288 -(IBAction)cleanupTable:(id)sender
290 [o_errors removeAllObjects];
291 [o_icons removeAllObjects];
292 [o_error_table reloadData];
295 -(IBAction)showMessages:(id)sender
297 [[VLCMain sharedInstance] showMessagesPanel: sender];
300 /*----------------------------------------------------------------------------
301 * data source methods
302 *---------------------------------------------------------------------------*/
303 - (NSInteger)numberOfRowsInTableView:(NSTableView *)theDataTable
305 return [o_errors count];
308 - (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn:
309 (NSTableColumn *)theTableColumn row: (NSInteger)row
311 if( [[theTableColumn identifier] isEqualToString: @"error_msg"] )
312 return [o_errors objectAtIndex: row];
314 if( [[theTableColumn identifier] isEqualToString: @"icon"] )
315 return [o_icons objectAtIndex: row];
317 return @"unknown identifier";