1 /*****************************************************************************
2 * coredialogs.m: Mac OS X Core Dialogs
3 *****************************************************************************
4 * Copyright (C) 2005-2011 VLC authors and VideoLAN
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 o_error_panel = [[VLCErrorPanel alloc] init];
52 b_progress_cancelled = NO;
55 return _o_sharedInstance;
60 [o_auth_login_txt setStringValue: _NS("User name")];
61 [o_auth_pw_txt setStringValue: _NS("Password")];
62 [o_auth_cancel_btn setTitle: _NS("Cancel")];
63 [o_auth_ok_btn setTitle: _NS("OK")];
64 [o_prog_cancel_btn setTitle: _NS("Cancel")];
65 [o_prog_bar setUsesThreadedAnimation: YES];
69 -(void)performEventWithObject: (NSValue *)o_value ofType: (const char*)type
71 NSString *o_type = [NSString stringWithUTF8String:type];
73 if( [o_type isEqualToString: @"dialog-error"] )
74 [self performSelectorOnMainThread:@selector(showFatalDialog:) withObject:o_value waitUntilDone:YES];
75 else if( [o_type isEqualToString: @"dialog-critical"] )
76 [self performSelectorOnMainThread:@selector(showFatalWaitDialog:) withObject:o_value waitUntilDone:YES];
77 else if( [o_type isEqualToString: @"dialog-question"] )
78 [self performSelectorOnMainThread:@selector(showQuestionDialog:) withObject:o_value waitUntilDone:YES];
79 else if( [o_type isEqualToString: @"dialog-login"] )
80 [self performSelectorOnMainThread:@selector(showLoginDialog:) withObject:o_value waitUntilDone:YES];
81 else if( [o_type isEqualToString: @"dialog-progress-bar"] )
82 [self performSelectorOnMainThread:@selector(showProgressDialog:) withObject:o_value waitUntilDone:YES];
84 msg_Err( VLCIntf, "unhandled dialog type: '%s'", type );
87 -(void)showFatalDialog: (NSValue *)o_value
89 dialog_fatal_t *p_dialog = [o_value pointerValue];
91 [o_error_panel addError: [NSString stringWithUTF8String: p_dialog->title] withMsg: [NSString stringWithUTF8String: p_dialog->message]];
92 [o_error_panel showPanel];
95 -(void)showFatalWaitDialog: (NSValue *)o_value
97 dialog_fatal_t *p_dialog = [o_value pointerValue];
100 o_alert = [NSAlert alertWithMessageText: [NSString stringWithUTF8String: p_dialog->title] defaultButton: _NS("OK") alternateButton: nil otherButton: nil informativeTextWithFormat: [NSString stringWithUTF8String: p_dialog->message]];
101 [o_alert setAlertStyle: NSCriticalAlertStyle];
105 -(void)showQuestionDialog: (NSValue *)o_value
107 dialog_question_t *p_dialog = [o_value pointerValue];
109 NSString *o_yes, *o_no, *o_cancel;
110 NSInteger i_returnValue = 0;
112 if( p_dialog->yes != NULL )
113 o_yes = [NSString stringWithUTF8String: p_dialog->yes];
114 if( p_dialog->no != NULL )
115 o_no = [NSString stringWithUTF8String: p_dialog->no];
116 if( p_dialog->cancel != NULL )
117 o_cancel = [NSString stringWithUTF8String: p_dialog->cancel];
119 o_alert = [NSAlert alertWithMessageText: [NSString stringWithUTF8String: p_dialog->title] defaultButton: o_yes alternateButton:o_no otherButton: o_cancel informativeTextWithFormat: [NSString stringWithUTF8String: p_dialog->message]];
120 [o_alert setAlertStyle: NSInformationalAlertStyle];
121 i_returnValue = [o_alert runModal];
123 if( i_returnValue == NSAlertDefaultReturn )
124 p_dialog->answer = 1;
125 if( i_returnValue == NSAlertAlternateReturn )
126 p_dialog->answer = 2;
127 if( i_returnValue == NSAlertOtherReturn )
128 p_dialog->answer = 3;
131 -(void)showLoginDialog: (NSValue *)o_value
133 dialog_login_t *p_dialog = [o_value pointerValue];
134 NSInteger i_returnValue = 0;
136 [o_auth_title_txt setStringValue: [NSString stringWithUTF8String: p_dialog->title]];
137 [o_auth_win setTitle: [NSString stringWithUTF8String: p_dialog->title]];
138 [o_auth_description_txt setStringValue: [NSString stringWithUTF8String: p_dialog->message]];
139 [o_auth_login_fld setStringValue: @""];
140 [o_auth_pw_fld setStringValue: @""];
143 i_returnValue = [NSApp runModalForWindow: o_auth_win];
147 *p_dialog->username = strdup( [[o_auth_login_fld stringValue] UTF8String] );
148 *p_dialog->password = strdup( [[o_auth_pw_fld stringValue] UTF8String] );
152 *p_dialog->username = *p_dialog->password = NULL;
156 -(IBAction)loginDialogAction:(id)sender
158 if( [[sender title] isEqualToString: _NS("OK")] )
159 [NSApp stopModalWithCode: 1];
161 [NSApp stopModalWithCode: 0];
164 -(void)showProgressDialog: (NSValue *)o_value
166 dialog_progress_bar_t *p_dialog = [o_value pointerValue];
168 if( p_dialog->title != NULL )
170 [o_prog_win setTitle: [NSString stringWithUTF8String: p_dialog->title]];
171 [o_prog_title_txt setStringValue: [NSString stringWithUTF8String: p_dialog->title]];
175 [o_prog_win setTitle: @""];
176 [o_prog_title_txt setStringValue: @""];
178 if( p_dialog->cancel != NULL )
179 [o_prog_cancel_btn setTitle: [NSString stringWithUTF8String: p_dialog->cancel]];
181 [o_prog_cancel_btn setTitle: _NS("Cancel")];
182 if( p_dialog->message != NULL )
183 [o_prog_description_txt setStringValue: [NSString stringWithUTF8String: p_dialog->message]];
185 [o_prog_description_txt setStringValue: @""];
186 [o_prog_bar setDoubleValue: 0];
187 [o_prog_bar setIndeterminate: YES];
188 [o_prog_bar startAnimation: self];
190 [o_prog_win makeKeyAndOrderFront: self];
193 -(void)updateProgressPanelWithText: (NSString *)string andNumber: (double)d_number
195 [o_prog_description_txt setStringValue: string];
197 [o_prog_bar setIndeterminate: NO];
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 /*****************************************************************************
225 * VLCErrorPanel implementation
226 *****************************************************************************/
227 @implementation VLCErrorPanel
233 b_nib_loaded = [NSBundle loadNibNamed:@"ErrorPanel" owner:self];
235 /* init data sources */
236 o_errors = [[NSMutableArray alloc] init];
237 o_icons = [[NSMutableArray alloc] init];
245 [o_window setTitle: _NS("Errors and Warnings")];
246 [o_cleanup_button setTitle: _NS("Clean up")];
247 [o_messages_btn setTitle: _NS("Show Details")];
259 [o_window makeKeyAndOrderFront: self];
262 -(void)addError: (NSString *)o_error withMsg:(NSString *)o_msg
264 /* format our string as desired */
265 NSMutableAttributedString * ourError;
266 ourError = [[NSMutableAttributedString alloc] initWithString:
267 [NSString stringWithFormat:@"%@\n%@", o_error, o_msg]
269 [NSDictionary dictionaryWithObject: [NSFont systemFontOfSize:11] forKey: NSFontAttributeName]];
271 addAttribute: NSFontAttributeName
272 value: [NSFont boldSystemFontOfSize:11]
273 range: NSMakeRange( 0, [o_error length])];
274 [o_errors addObject: ourError];
277 [o_icons addObject: [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kAlertStopIcon)]];
279 [o_error_table reloadData];
282 -(IBAction)cleanupTable:(id)sender
284 [o_errors removeAllObjects];
285 [o_icons removeAllObjects];
286 [o_error_table reloadData];
289 -(IBAction)showMessages:(id)sender
291 [[VLCMain sharedInstance] showMessagesPanel: sender];
294 /*----------------------------------------------------------------------------
295 * data source methods
296 *---------------------------------------------------------------------------*/
297 - (NSInteger)numberOfRowsInTableView:(NSTableView *)theDataTable
299 return [o_errors count];
302 - (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn:
303 (NSTableColumn *)theTableColumn row: (NSInteger)row
305 if( [[theTableColumn identifier] isEqualToString: @"error_msg"] )
306 return [o_errors objectAtIndex: row];
308 if( [[theTableColumn identifier] isEqualToString: @"icon"] )
309 return [o_icons objectAtIndex: row];
311 return @"unknown identifier";