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)
49 _o_sharedInstance = [super init];
50 o_error_panel = [[VLCErrorPanel alloc] init];
51 b_progress_cancelled = NO;
54 return _o_sharedInstance;
59 [o_auth_login_txt setStringValue: _NS("User name")];
60 [o_auth_pw_txt setStringValue: _NS("Password")];
61 [o_auth_cancel_btn setTitle: _NS("Cancel")];
62 [o_auth_ok_btn setTitle: _NS("OK")];
63 [o_prog_cancel_btn setTitle: _NS("Cancel")];
64 [o_prog_bar setUsesThreadedAnimation: YES];
68 -(void)performEventWithObject: (NSValue *)o_value ofType: (const char*)type
70 NSString *o_type = [NSString stringWithUTF8String:type];
72 if ([o_type isEqualToString: @"dialog-error"])
73 [self performSelectorOnMainThread:@selector(showFatalDialog:) withObject:o_value waitUntilDone:YES];
74 else if ([o_type isEqualToString: @"dialog-critical"])
75 [self performSelectorOnMainThread:@selector(showFatalWaitDialog:) withObject:o_value waitUntilDone:YES];
76 else if ([o_type isEqualToString: @"dialog-question"])
77 [self performSelectorOnMainThread:@selector(showQuestionDialog:) withObject:o_value waitUntilDone:YES];
78 else if ([o_type isEqualToString: @"dialog-login"])
79 [self performSelectorOnMainThread:@selector(showLoginDialog:) withObject:o_value waitUntilDone:YES];
80 else if ([o_type isEqualToString: @"dialog-progress-bar"])
81 [self performSelectorOnMainThread:@selector(showProgressDialogOnMainThread:) withObject: o_value waitUntilDone:YES];
83 msg_Err(VLCIntf, "unhandled dialog type: '%s'", type);
86 -(void)showFatalDialog: (NSValue *)o_value
88 dialog_fatal_t *p_dialog = [o_value pointerValue];
90 [o_error_panel addError: toNSStr(p_dialog->title) withMsg: toNSStr(p_dialog->message)];
91 [o_error_panel showPanel];
94 -(void)showFatalWaitDialog: (NSValue *)o_value
96 dialog_fatal_t *p_dialog = [o_value pointerValue];
99 o_alert = [NSAlert alertWithMessageText: toNSStr(p_dialog->title) defaultButton: _NS("OK") alternateButton: nil otherButton: nil informativeTextWithFormat: @"%@", toNSStr(p_dialog->message)];
100 [o_alert setAlertStyle: NSCriticalAlertStyle];
104 -(void)showQuestionDialog: (NSValue *)o_value
106 dialog_question_t *p_dialog = [o_value pointerValue];
108 NSInteger i_returnValue = 0;
110 o_alert = [NSAlert alertWithMessageText: toNSStr(p_dialog->title) defaultButton: toNSStr(p_dialog->yes) alternateButton: toNSStr(p_dialog->no) otherButton: toNSStr(p_dialog->cancel) informativeTextWithFormat:@"%@", toNSStr(p_dialog->message)];
111 [o_alert setAlertStyle: NSInformationalAlertStyle];
112 i_returnValue = [o_alert runModal];
114 if (i_returnValue == NSAlertDefaultReturn)
115 p_dialog->answer = 1;
116 if (i_returnValue == NSAlertAlternateReturn)
117 p_dialog->answer = 2;
118 if (i_returnValue == NSAlertOtherReturn)
119 p_dialog->answer = 3;
122 -(void)showLoginDialog: (NSValue *)o_value
124 dialog_login_t *p_dialog = [o_value pointerValue];
125 NSInteger i_returnValue = 0;
127 [o_auth_title_txt setStringValue: toNSStr(p_dialog->title)];
128 [o_auth_win setTitle: toNSStr(p_dialog->title)];
129 [o_auth_description_txt setStringValue: toNSStr(p_dialog->message)];
130 [o_auth_login_fld setStringValue: @""];
131 [o_auth_pw_fld setStringValue: @""];
134 i_returnValue = [NSApp runModalForWindow: o_auth_win];
138 *p_dialog->username = strdup([[o_auth_login_fld stringValue] UTF8String]);
139 *p_dialog->password = strdup([[o_auth_pw_fld stringValue] UTF8String]);
141 *p_dialog->username = *p_dialog->password = NULL;
144 -(IBAction)loginDialogAction:(id)sender
146 if ([[sender title] isEqualToString: _NS("OK")])
147 [NSApp stopModalWithCode: 1];
149 [NSApp stopModalWithCode: 0];
152 -(void)showProgressDialogOnMainThread: (NSValue *)o_value
154 /* we work-around a Cocoa limitation here, since you cannot delay an execution
155 * on the main thread within a single call */
156 b_progress_cancelled = NO;
158 dialog_progress_bar_t *p_dialog = [o_value pointerValue];
159 if (!p_dialog || b_progress_cancelled)
162 [o_prog_win setTitle: toNSStr(p_dialog->title)];
163 [o_prog_title_txt setStringValue: toNSStr(p_dialog->title)];
165 if (p_dialog->cancel != NULL)
166 [o_prog_cancel_btn setTitle: [NSString stringWithUTF8String:p_dialog->cancel]];
168 [o_prog_cancel_btn setTitle: _NS("Cancel")];
170 [o_prog_description_txt setStringValue: toNSStr(p_dialog->message)];
173 [self performSelector:@selector(showProgressDialog:) withObject: o_value afterDelay:3.00];
176 -(void)showProgressDialog: (NSValue *)o_value
178 dialog_progress_bar_t *p_dialog = [o_value pointerValue];
180 if (!p_dialog || b_progress_cancelled)
183 [o_prog_bar setDoubleValue: 0];
184 [o_prog_bar setIndeterminate: YES];
185 [o_prog_bar startAnimation: self];
187 [o_prog_win makeKeyAndOrderFront: self];
190 -(void)updateProgressPanelWithText: (NSString *)string andNumber: (double)d_number
192 [o_prog_description_txt setStringValue: string];
194 [o_prog_bar setIndeterminate: NO];
195 [o_prog_bar setDoubleValue: d_number];
198 -(void)destroyProgressPanel
200 b_progress_cancelled = YES;
201 [o_prog_bar performSelectorOnMainThread:@selector(stopAnimation:) withObject:self waitUntilDone:YES];
202 [o_prog_win performSelectorOnMainThread:@selector(close) withObject:nil waitUntilDone:YES];
205 -(IBAction)progDialogAction:(id)sender
207 b_progress_cancelled = YES;
210 -(BOOL)progressCancelled
212 return b_progress_cancelled;
217 return o_error_panel;
222 /*****************************************************************************
223 * VLCErrorPanel implementation
224 *****************************************************************************/
225 @implementation VLCErrorPanel
231 b_nib_loaded = [NSBundle loadNibNamed:@"ErrorPanel" owner:self];
233 /* init data sources */
234 o_errors = [[NSMutableArray alloc] init];
235 o_icons = [[NSMutableArray alloc] init];
243 [o_window setTitle: _NS("Errors and Warnings")];
244 [o_cleanup_button setTitle: _NS("Clean up")];
256 [o_window makeKeyAndOrderFront: self];
259 -(void)addError: (NSString *)o_error withMsg:(NSString *)o_msg
261 /* format our string as desired */
262 NSMutableAttributedString * ourError;
263 ourError = [[NSMutableAttributedString alloc] initWithString:
264 [NSString stringWithFormat:@"%@\n%@", o_error, o_msg]
266 [NSDictionary dictionaryWithObject: [NSFont systemFontOfSize:11] forKey: NSFontAttributeName]];
268 addAttribute: NSFontAttributeName
269 value: [NSFont boldSystemFontOfSize:11]
270 range: NSMakeRange(0, [o_error length])];
271 [o_errors addObject: ourError];
274 [o_icons addObject: [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kAlertStopIcon)]];
276 [o_error_table reloadData];
279 -(IBAction)cleanupTable:(id)sender
281 [o_errors removeAllObjects];
282 [o_icons removeAllObjects];
283 [o_error_table reloadData];
286 /*----------------------------------------------------------------------------
287 * data source methods
288 *---------------------------------------------------------------------------*/
289 - (NSInteger)numberOfRowsInTableView:(NSTableView *)theDataTable
291 return [o_errors count];
294 - (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn:
295 (NSTableColumn *)theTableColumn row: (NSInteger)row
297 if ([[theTableColumn identifier] isEqualToString: @"error_msg"])
298 return [o_errors objectAtIndex:row];
300 if ([[theTableColumn identifier] isEqualToString: @"icon"])
301 return [o_icons objectAtIndex:row];
303 return @"unknown identifier";