1 /*****************************************************************************
2 * coredialogs.m: Mac OS X Core Dialogs
3 *****************************************************************************
4 * Copyright (C) 2005-2012 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(showProgressDialogOnMainThread:) 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: @"%s", 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: @"%s", 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)showProgressDialogOnMainThread: (NSValue *)o_value
166 /* we work-around a Cocoa limitation here, since you cannot delay an execution
167 * on the main thread within a single call */
168 b_progress_cancelled = NO;
170 dialog_progress_bar_t *p_dialog = [o_value pointerValue];
172 if (!p_dialog || b_progress_cancelled)
175 if( p_dialog->title != NULL )
177 [o_prog_win setTitle: [NSString stringWithUTF8String: p_dialog->title]];
178 [o_prog_title_txt setStringValue: [NSString stringWithUTF8String: p_dialog->title]];
182 [o_prog_win setTitle: @""];
183 [o_prog_title_txt setStringValue: @""];
185 if( p_dialog->cancel != NULL )
186 [o_prog_cancel_btn setTitle: [NSString stringWithUTF8String: p_dialog->cancel]];
188 [o_prog_cancel_btn setTitle: _NS("Cancel")];
189 if( p_dialog->message != NULL )
190 [o_prog_description_txt setStringValue: [NSString stringWithUTF8String: p_dialog->message]];
192 [o_prog_description_txt setStringValue: @""];
195 [self performSelector:@selector(showProgressDialog:) withObject: o_value afterDelay:3.00];
198 -(void)showProgressDialog: (NSValue *)o_value
200 dialog_progress_bar_t *p_dialog = [o_value pointerValue];
202 if (!p_dialog || b_progress_cancelled)
205 [o_prog_bar setDoubleValue: 0];
206 [o_prog_bar setIndeterminate: YES];
207 [o_prog_bar startAnimation: self];
209 [o_prog_win makeKeyAndOrderFront: self];
212 -(void)updateProgressPanelWithText: (NSString *)string andNumber: (double)d_number
214 [o_prog_description_txt setStringValue: string];
216 [o_prog_bar setIndeterminate: NO];
217 [o_prog_bar setDoubleValue: d_number];
220 -(void)destroyProgressPanel
222 b_progress_cancelled = YES;
223 [o_prog_bar stopAnimation: self];
227 -(IBAction)progDialogAction:(id)sender
229 b_progress_cancelled = YES;
232 -(BOOL)progressCancelled
234 return b_progress_cancelled;
239 return o_error_panel;
244 /*****************************************************************************
245 * VLCErrorPanel implementation
246 *****************************************************************************/
247 @implementation VLCErrorPanel
253 b_nib_loaded = [NSBundle loadNibNamed:@"ErrorPanel" owner:self];
255 /* init data sources */
256 o_errors = [[NSMutableArray alloc] init];
257 o_icons = [[NSMutableArray alloc] init];
265 [o_window setTitle: _NS("Errors and Warnings")];
266 [o_cleanup_button setTitle: _NS("Clean up")];
267 [o_messages_btn setTitle: _NS("Show Details")];
279 [o_window makeKeyAndOrderFront: self];
282 -(void)addError: (NSString *)o_error withMsg:(NSString *)o_msg
284 /* format our string as desired */
285 NSMutableAttributedString * ourError;
286 ourError = [[NSMutableAttributedString alloc] initWithString:
287 [NSString stringWithFormat:@"%@\n%@", o_error, o_msg]
289 [NSDictionary dictionaryWithObject: [NSFont systemFontOfSize:11] forKey: NSFontAttributeName]];
291 addAttribute: NSFontAttributeName
292 value: [NSFont boldSystemFontOfSize:11]
293 range: NSMakeRange( 0, [o_error length])];
294 [o_errors addObject: ourError];
297 [o_icons addObject: [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kAlertStopIcon)]];
299 [o_error_table reloadData];
302 -(IBAction)cleanupTable:(id)sender
304 [o_errors removeAllObjects];
305 [o_icons removeAllObjects];
306 [o_error_table reloadData];
309 -(IBAction)showMessages:(id)sender
311 [[VLCMain sharedInstance] showMessagesPanel: sender];
314 /*----------------------------------------------------------------------------
315 * data source methods
316 *---------------------------------------------------------------------------*/
317 - (NSInteger)numberOfRowsInTableView:(NSTableView *)theDataTable
319 return [o_errors count];
322 - (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn:
323 (NSTableColumn *)theTableColumn row: (NSInteger)row
325 if( [[theTableColumn identifier] isEqualToString: @"error_msg"] )
326 return [o_errors objectAtIndex: row];
328 if( [[theTableColumn identifier] isEqualToString: @"icon"] )
329 return [o_icons objectAtIndex: row];
331 return @"unknown identifier";