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>
33 /*****************************************************************************
35 *****************************************************************************/
37 bool b_progress_cancelled;
38 static void updateProgressPanel (void *, const char *, float);
39 static bool checkProgressPanel (void *);
40 static void destroyProgressPanel (void *);
42 /*****************************************************************************
43 * VLCCoreDialogProvider implementation
44 *****************************************************************************/
45 @implementation VLCCoreDialogProvider
47 static VLCCoreDialogProvider *_o_sharedInstance = nil;
49 + (VLCCoreDialogProvider *)sharedInstance
51 return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
56 if( _o_sharedInstance )
60 _o_sharedInstance = [super init];
61 [[NSNotificationCenter defaultCenter] addObserver:self
62 selector:@selector(performDialogEvent:)
63 name: @"VLCNewCoreDialogEventNotification"
65 [[NSNotificationCenter defaultCenter] addObserver: self
66 selector:@selector(performProgressBarEvent:)
67 name:@"VLCCoreDialogProgressBarUpdateNotification"
69 [[NSNotificationCenter defaultCenter] addObserver: self
70 selector:@selector(performProgressBarEvent:)
71 name:@"VLCCoreDialogProgressBarDestroyNotification"
73 o_error_panel = [[VLCErrorPanel alloc] init];
74 b_progress_cancelled = NO;
77 return _o_sharedInstance;
82 [o_auth_login_txt setStringValue: _NS("User name")];
83 [o_auth_pw_txt setStringValue: _NS("Password")];
84 [o_auth_cancel_btn setTitle: _NS("Cancel")];
85 [o_auth_ok_btn setTitle: _NS("OK")];
86 [o_prog_cancel_btn setTitle: _NS("Cancel")];
87 [o_prog_bar setUsesThreadedAnimation: YES];
91 -(void)performDialogEvent: (NSNotification *)o_notification
93 NSValue *o_value = [[o_notification userInfo] objectForKey:@"VLCDialogPointer"];
94 NSString *o_type = [[o_notification userInfo] objectForKey:@"VLCDialogType"];
96 if( [o_type isEqualToString: @"dialog-fatal"] )
97 [self showFatalDialog: o_value];
98 else if( [o_type isEqualToString: @"dialog-question"] )
99 [self showQuestionDialog: o_value];
100 else if( [o_type isEqualToString: @"dialog-login"] )
101 [self showLoginDialog: o_value];
102 else if( [o_type isEqualToString: @"dialog-progress-bar"] )
103 [self showProgressDialog: o_value];
105 msg_Err( VLCIntf, "unhandled dialog type: '%s'", [o_type UTF8String] );
108 -(void)showFatalDialog: (NSValue *)o_value
110 dialog_fatal_t *p_dialog = [o_value pointerValue];
111 /* do we need to block ? */
112 if( p_dialog->modal == YES )
115 o_alert = [NSAlert alertWithMessageText: [NSString stringWithUTF8String: p_dialog->title] defaultButton: _NS("OK") alternateButton: nil otherButton: nil informativeTextWithFormat: [NSString stringWithUTF8String: p_dialog->message]];
116 [o_alert setAlertStyle: NSCriticalAlertStyle];
121 [o_error_panel addError: [NSString stringWithUTF8String: p_dialog->title] withMsg: [NSString stringWithUTF8String: p_dialog->message]];
122 [o_error_panel showPanel];
126 -(void)showQuestionDialog: (NSValue *)o_value
128 dialog_question_t *p_dialog = [o_value pointerValue];
130 NSString *o_yes, *o_no, *o_cancel;
131 NSInteger i_returnValue = 0;
133 if( p_dialog->yes != NULL )
134 o_yes = [NSString stringWithUTF8String: p_dialog->yes];
135 if( p_dialog->no != NULL )
136 o_no = [NSString stringWithUTF8String: p_dialog->no];
137 if( p_dialog->cancel != NULL )
138 o_cancel = [NSString stringWithUTF8String: p_dialog->cancel];
140 o_alert = [NSAlert alertWithMessageText: [NSString stringWithUTF8String: p_dialog->title] defaultButton: o_yes alternateButton:o_no otherButton: o_cancel informativeTextWithFormat: [NSString stringWithUTF8String: p_dialog->message]];
141 [o_alert setAlertStyle: NSInformationalAlertStyle];
142 i_returnValue = [o_alert runModal];
144 if( i_returnValue == NSAlertDefaultReturn )
145 p_dialog->answer = 1;
146 if( i_returnValue == NSAlertAlternateReturn )
147 p_dialog->answer = 2;
148 if( i_returnValue == NSAlertOtherReturn )
149 p_dialog->answer = 3;
152 -(void)showLoginDialog: (NSValue *)o_value
154 dialog_login_t *p_dialog = [o_value pointerValue];
155 NSInteger i_returnValue = 0;
157 [o_auth_title_txt setStringValue: [NSString stringWithUTF8String: p_dialog->title]];
158 [o_auth_win setTitle: [NSString stringWithUTF8String: p_dialog->title]];
159 [o_auth_description_txt setStringValue: [NSString stringWithUTF8String: p_dialog->message]];
160 [o_auth_login_fld setStringValue: @""];
161 [o_auth_pw_fld setStringValue: @""];
164 i_returnValue = [NSApp runModalForWindow: o_auth_win];
168 *p_dialog->username = strdup( [[o_auth_login_fld stringValue] UTF8String] );
169 *p_dialog->password = strdup( [[o_auth_pw_fld stringValue] UTF8String] );
173 *p_dialog->username = *p_dialog->password = NULL;
177 -(IBAction)loginDialogAction:(id)sender
179 if( [[sender title] isEqualToString: _NS("OK")] )
180 [NSApp stopModalWithCode: 1];
182 [NSApp stopModalWithCode: 0];
185 -(void)showProgressDialog: (NSValue *)o_value
187 dialog_progress_bar_t *p_dialog = [o_value pointerValue];
189 if( p_dialog->title != NULL )
191 [o_prog_win setTitle: [NSString stringWithUTF8String: p_dialog->title]];
192 [o_prog_title_txt setStringValue: [NSString stringWithUTF8String: p_dialog->title]];
196 [o_prog_win setTitle: @""];
197 [o_prog_title_txt setStringValue: @""];
199 if( p_dialog->cancel != NULL )
200 [o_prog_cancel_btn setTitle: [NSString stringWithUTF8String: p_dialog->cancel]];
202 [o_prog_cancel_btn setTitle: _NS("Cancel")];
203 if( p_dialog->message != NULL )
204 [o_prog_description_txt setStringValue: [NSString stringWithUTF8String: p_dialog->message]];
206 [o_prog_description_txt setStringValue: @""];
207 [o_prog_bar setDoubleValue: 0];
209 p_dialog->pf_update = updateProgressPanel;
210 p_dialog->pf_check = checkProgressPanel;
211 p_dialog->pf_destroy = destroyProgressPanel;
212 p_dialog->p_sys = self;
214 [NSApp runModalForWindow: o_prog_win];
217 -(void)performProgressBarEvent: (NSNotification *)o_notification
219 NSLog( @"%@ received", [o_notification name] );
220 if( [[o_notification name] isEqualToString: @"VLCCoreDialogProgressBarUpdateNotification"] )
222 NSNumber *o_number = [[o_notification userInfo] objectForKey:@"IntValue"];
223 NSString *o_text = [[o_notification userInfo] objectForKey:@"Text"];
224 [o_prog_description_txt setStringValue: o_text];
225 [o_prog_bar setDoubleValue: [o_number doubleValue]];
227 if( [[o_notification name] isEqualToString: @"VLCCoreDialogProgressBarDestroyNotification"] )
229 [NSApp stopModalWithCode: 0];
234 void updateProgressPanel (void *priv, const char *text, float value)
236 NSLog( @"we were updated with %s (%f)", text, value );
239 o_txt = [NSString stringWithUTF8String: text];
240 [[NSNotificationCenter defaultCenter] postNotificationName: @"VLCCoreDialogProgressBarUpdateNotification" object:[[VLCMain sharedInstance] getCoreDialogProvider] userInfo:[NSDictionary dictionaryWithObjectsAndKeys: o_txt, @"Text", [NSNumber numberWithInt: ((int)(value * 1000.))], @"IntValue", nil]];
243 void destroyProgressPanel (void *priv)
245 NSLog( @"we should destroy" );
246 [[NSNotificationCenter defaultCenter] postNotificationName: @"VLCCoreDialogProgressBarDestroyNotification" object:[[VLCMain sharedInstance] getCoreDialogProvider] userInfo: nil];
249 bool checkProgressPanel (void *priv)
251 NSLog( @"we were checked" );
252 return b_progress_cancelled;
255 -(IBAction)progDialogAction:(id)sender
257 NSLog( @"buttonAction!" );
258 b_progress_cancelled = YES;
263 return o_error_panel;
268 [[NSNotificationCenter defaultCenter] removeObserver:self];
273 /*****************************************************************************
274 * VLCErrorPanel implementation
275 *****************************************************************************/
276 @implementation VLCErrorPanel
281 nib_loaded = [NSBundle loadNibNamed:@"InteractionErrorPanel" owner:self];
284 [o_window setTitle: _NS("Errors and Warnings")];
285 [o_cleanup_button setTitle: _NS("Clean up")];
286 [o_messages_btn setTitle: _NS("Show Details")];
288 /* init data sources */
289 o_errors = [[NSMutableArray alloc] init];
290 o_icons = [[NSMutableArray alloc] init];
304 [o_window makeKeyAndOrderFront: self];
307 -(void)addError: (NSString *)o_error withMsg:(NSString *)o_msg
309 /* format our string as desired */
310 NSMutableAttributedString * ourError;
311 ourError = [[NSMutableAttributedString alloc] initWithString:
312 [NSString stringWithFormat:@"%@\n%@", o_error, o_msg]
314 [NSDictionary dictionaryWithObject: [NSFont systemFontOfSize:11] forKey: NSFontAttributeName]];
316 addAttribute: NSFontAttributeName
317 value: [NSFont boldSystemFontOfSize:11]
318 range: NSMakeRange( 0, [o_error length])];
319 [o_errors addObject: ourError];
322 [o_icons addObject: [NSImage imageWithErrorIcon]];
324 [o_error_table reloadData];
327 -(IBAction)cleanupTable:(id)sender
329 [o_errors removeAllObjects];
330 [o_icons removeAllObjects];
331 [o_error_table reloadData];
334 -(IBAction)showMessages:(id)sender
336 [[VLCMain sharedInstance] showMessagesPanel: sender];
339 /*----------------------------------------------------------------------------
340 * data source methods
341 *---------------------------------------------------------------------------*/
342 - (NSInteger)numberOfRowsInTableView:(NSTableView *)theDataTable
344 return [o_errors count];
347 - (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn:
348 (NSTableColumn *)theTableColumn row: (NSInteger)row
350 if( [[theTableColumn identifier] isEqualToString: @"error_msg"] )
351 return [o_errors objectAtIndex: row];
353 if( [[theTableColumn identifier] isEqualToString: @"icon"] )
354 return [o_icons objectAtIndex: row];
356 return @"unknown identifier";