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 NSString *toNSStr(const char *str) {
33 return str != NULL ? [NSString stringWithUTF8String:str] : @"";
36 /*****************************************************************************
37 * VLCCoreDialogProvider implementation
38 *****************************************************************************/
39 @implementation VLCCoreDialogProvider
41 static VLCCoreDialogProvider *_o_sharedInstance = nil;
43 + (VLCCoreDialogProvider *)sharedInstance
45 return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
50 if (_o_sharedInstance)
53 _o_sharedInstance = [super init];
54 o_error_panel = [[VLCErrorPanel alloc] init];
55 b_progress_cancelled = NO;
58 return _o_sharedInstance;
63 [o_auth_login_txt setStringValue: _NS("User name")];
64 [o_auth_pw_txt setStringValue: _NS("Password")];
65 [o_auth_cancel_btn setTitle: _NS("Cancel")];
66 [o_auth_ok_btn setTitle: _NS("OK")];
67 [o_prog_cancel_btn setTitle: _NS("Cancel")];
68 [o_prog_bar setUsesThreadedAnimation: YES];
72 -(void)performEventWithObject: (NSValue *)o_value ofType: (const char*)type
74 NSString *o_type = [NSString stringWithUTF8String:type];
76 if ([o_type isEqualToString: @"dialog-error"])
77 [self performSelectorOnMainThread:@selector(showFatalDialog:) withObject:o_value waitUntilDone:YES];
78 else if ([o_type isEqualToString: @"dialog-critical"])
79 [self performSelectorOnMainThread:@selector(showFatalWaitDialog:) withObject:o_value waitUntilDone:YES];
80 else if ([o_type isEqualToString: @"dialog-question"])
81 [self performSelectorOnMainThread:@selector(showQuestionDialog:) withObject:o_value waitUntilDone:YES];
82 else if ([o_type isEqualToString: @"dialog-login"])
83 [self performSelectorOnMainThread:@selector(showLoginDialog:) withObject:o_value waitUntilDone:YES];
84 else if ([o_type isEqualToString: @"dialog-progress-bar"])
85 [self performSelectorOnMainThread:@selector(showProgressDialogOnMainThread:) withObject: o_value waitUntilDone:YES];
87 msg_Err(VLCIntf, "unhandled dialog type: '%s'", type);
90 -(void)showFatalDialog: (NSValue *)o_value
92 dialog_fatal_t *p_dialog = [o_value pointerValue];
94 [o_error_panel addError: toNSStr(p_dialog->title) withMsg: toNSStr(p_dialog->message)];
95 [o_error_panel showPanel];
98 -(void)showFatalWaitDialog: (NSValue *)o_value
100 dialog_fatal_t *p_dialog = [o_value pointerValue];
103 o_alert = [NSAlert alertWithMessageText: toNSStr(p_dialog->title) defaultButton: _NS("OK") alternateButton: nil otherButton: nil informativeTextWithFormat: @"%s", p_dialog->message];
104 [o_alert setAlertStyle: NSCriticalAlertStyle];
108 -(void)showQuestionDialog: (NSValue *)o_value
110 dialog_question_t *p_dialog = [o_value pointerValue];
112 NSInteger i_returnValue = 0;
114 o_alert = [NSAlert alertWithMessageText: toNSStr(p_dialog->title) defaultButton: toNSStr(p_dialog->yes) alternateButton: toNSStr(p_dialog->no) otherButton: toNSStr(p_dialog->cancel) informativeTextWithFormat: @"%s", p_dialog->message];
115 [o_alert setAlertStyle: NSInformationalAlertStyle];
116 i_returnValue = [o_alert runModal];
118 if (i_returnValue == NSAlertDefaultReturn)
119 p_dialog->answer = 1;
120 if (i_returnValue == NSAlertAlternateReturn)
121 p_dialog->answer = 2;
122 if (i_returnValue == NSAlertOtherReturn)
123 p_dialog->answer = 3;
126 -(void)showLoginDialog: (NSValue *)o_value
128 dialog_login_t *p_dialog = [o_value pointerValue];
129 NSInteger i_returnValue = 0;
131 [o_auth_title_txt setStringValue: toNSStr(p_dialog->title)];
132 [o_auth_win setTitle: toNSStr(p_dialog->title)];
133 [o_auth_description_txt setStringValue: toNSStr(p_dialog->message)];
134 [o_auth_login_fld setStringValue: @""];
135 [o_auth_pw_fld setStringValue: @""];
138 i_returnValue = [NSApp runModalForWindow: o_auth_win];
142 *p_dialog->username = strdup([[o_auth_login_fld stringValue] UTF8String]);
143 *p_dialog->password = strdup([[o_auth_pw_fld stringValue] UTF8String]);
145 *p_dialog->username = *p_dialog->password = NULL;
148 -(IBAction)loginDialogAction:(id)sender
150 if ([[sender title] isEqualToString: _NS("OK")])
151 [NSApp stopModalWithCode: 1];
153 [NSApp stopModalWithCode: 0];
156 -(void)showProgressDialogOnMainThread: (NSValue *)o_value
158 /* we work-around a Cocoa limitation here, since you cannot delay an execution
159 * on the main thread within a single call */
160 b_progress_cancelled = NO;
162 dialog_progress_bar_t *p_dialog = [o_value pointerValue];
163 if (!p_dialog || b_progress_cancelled)
166 [o_prog_win setTitle: toNSStr(p_dialog->title)];
167 [o_prog_title_txt setStringValue: toNSStr(p_dialog->title)];
169 if (p_dialog->cancel != NULL)
170 [o_prog_cancel_btn setTitle: [NSString stringWithUTF8String:p_dialog->cancel]];
172 [o_prog_cancel_btn setTitle: _NS("Cancel")];
174 [o_prog_description_txt setStringValue: toNSStr(p_dialog->message)];
177 [self performSelector:@selector(showProgressDialog:) withObject: o_value afterDelay:3.00];
180 -(void)showProgressDialog: (NSValue *)o_value
182 dialog_progress_bar_t *p_dialog = [o_value pointerValue];
184 if (!p_dialog || b_progress_cancelled)
187 [o_prog_bar setDoubleValue: 0];
188 [o_prog_bar setIndeterminate: YES];
189 [o_prog_bar startAnimation: self];
191 [o_prog_win makeKeyAndOrderFront: self];
194 -(void)updateProgressPanelWithText: (NSString *)string andNumber: (double)d_number
196 [o_prog_description_txt setStringValue: string];
198 [o_prog_bar setIndeterminate: NO];
199 [o_prog_bar setDoubleValue: d_number];
202 -(void)destroyProgressPanel
204 b_progress_cancelled = YES;
205 [o_prog_bar performSelectorOnMainThread:@selector(stopAnimation:) withObject:self waitUntilDone:YES];
206 [o_prog_win performSelectorOnMainThread:@selector(close) withObject:nil waitUntilDone:YES];
209 -(IBAction)progDialogAction:(id)sender
211 b_progress_cancelled = YES;
214 -(BOOL)progressCancelled
216 return b_progress_cancelled;
221 return o_error_panel;
226 /*****************************************************************************
227 * VLCErrorPanel implementation
228 *****************************************************************************/
229 @implementation VLCErrorPanel
235 b_nib_loaded = [NSBundle loadNibNamed:@"ErrorPanel" owner:self];
237 /* init data sources */
238 o_errors = [[NSMutableArray alloc] init];
239 o_icons = [[NSMutableArray alloc] init];
247 [o_window setTitle: _NS("Errors and Warnings")];
248 [o_cleanup_button setTitle: _NS("Clean up")];
249 [o_messages_btn setTitle: _NS("Show Details")];
261 [o_window makeKeyAndOrderFront: self];
264 -(void)addError: (NSString *)o_error withMsg:(NSString *)o_msg
266 /* format our string as desired */
267 NSMutableAttributedString * ourError;
268 ourError = [[NSMutableAttributedString alloc] initWithString:
269 [NSString stringWithFormat:@"%@\n%@", o_error, o_msg]
271 [NSDictionary dictionaryWithObject: [NSFont systemFontOfSize:11] forKey: NSFontAttributeName]];
273 addAttribute: NSFontAttributeName
274 value: [NSFont boldSystemFontOfSize:11]
275 range: NSMakeRange(0, [o_error length])];
276 [o_errors addObject: ourError];
279 [o_icons addObject: [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kAlertStopIcon)]];
281 [o_error_table reloadData];
284 -(IBAction)cleanupTable:(id)sender
286 [o_errors removeAllObjects];
287 [o_icons removeAllObjects];
288 [o_error_table reloadData];
291 -(IBAction)showMessages:(id)sender
293 [[VLCMain sharedInstance] showMessagesPanel: sender];
296 /*----------------------------------------------------------------------------
297 * data source methods
298 *---------------------------------------------------------------------------*/
299 - (NSInteger)numberOfRowsInTableView:(NSTableView *)theDataTable
301 return [o_errors count];
304 - (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn:
305 (NSTableColumn *)theTableColumn row: (NSInteger)row
307 if ([[theTableColumn identifier] isEqualToString: @"error_msg"])
308 return [o_errors objectAtIndex:row];
310 if ([[theTableColumn identifier] isEqualToString: @"icon"])
311 return [o_icons objectAtIndex:row];
313 return @"unknown identifier";