coredialogs.m 9.95 KB
Newer Older
1
/*****************************************************************************
2
 * coredialogs.m: Mac OS X Core Dialogs
3
 *****************************************************************************
4
 * Copyright (C) 2005-2009 the VideoLAN team
5
 * $Id$
6 7
 *
 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8
 *          Felix Paul Kühne <fkuehne at videolan dot org>
9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
14
 *
15 16 17 18 19 20 21
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24
 *****************************************************************************/

Felix Paul Kühne's avatar
Felix Paul Kühne committed
25
#import "intf.h"
26
#import "coredialogs.h"
27
#import "misc.h"
28

29
/* for the icon in our custom error panel */
30
#import <ApplicationServices/ApplicationServices.h>
31

32
/*****************************************************************************
33
 * VLCCoreDialogProvider implementation
34
 *****************************************************************************/
35 36 37 38 39 40 41 42
@implementation VLCCoreDialogProvider

static VLCCoreDialogProvider *_o_sharedInstance = nil;

+ (VLCCoreDialogProvider *)sharedInstance
{
    return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
}
43 44 45

-(id)init
{
46 47 48 49 50 51
    if( _o_sharedInstance )
        [self dealloc];
    else
    {
        _o_sharedInstance = [super init];
        o_error_panel = [[VLCErrorPanel alloc] init];
52
        b_progress_cancelled = NO;
53 54 55 56
    }
    
    return _o_sharedInstance;
}
57

58 59 60 61 62 63 64 65
-(void)awakeFromNib
{
    [o_auth_login_txt setStringValue: _NS("User name")];
    [o_auth_pw_txt setStringValue: _NS("Password")];
    [o_auth_cancel_btn setTitle: _NS("Cancel")];
    [o_auth_ok_btn setTitle: _NS("OK")];
    [o_prog_cancel_btn setTitle: _NS("Cancel")];
    [o_prog_bar setUsesThreadedAnimation: YES];
66

67
}
68

69
-(void)performEventWithObject: (NSValue *)o_value ofType: (const char*)type
70
{
71
    NSString *o_type = [NSString stringWithUTF8String:type];
72

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];
83
    else
84
        msg_Err( VLCIntf, "unhandled dialog type: '%s'", type );
85 86
}

87 88 89
-(void)showFatalDialog: (NSValue *)o_value
{
    dialog_fatal_t *p_dialog = [o_value pointerValue];
90 91 92 93 94 95 96 97 98 99 100 101 102

    [o_error_panel addError: [NSString stringWithUTF8String: p_dialog->title] withMsg: [NSString stringWithUTF8String: p_dialog->message]];
    [o_error_panel showPanel];
}

-(void)showFatalWaitDialog: (NSValue *)o_value
{
    dialog_fatal_t *p_dialog = [o_value pointerValue];
    NSAlert *o_alert;

    o_alert = [NSAlert alertWithMessageText: [NSString stringWithUTF8String: p_dialog->title] defaultButton: _NS("OK") alternateButton: nil otherButton: nil informativeTextWithFormat: [NSString stringWithUTF8String: p_dialog->message]];
    [o_alert setAlertStyle: NSCriticalAlertStyle];
    [o_alert runModal];
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
}

-(void)showQuestionDialog: (NSValue *)o_value
{
    dialog_question_t *p_dialog = [o_value pointerValue];
    NSAlert *o_alert;
    NSString *o_yes, *o_no, *o_cancel;
    NSInteger i_returnValue = 0;
    
    if( p_dialog->yes != NULL )
        o_yes = [NSString stringWithUTF8String: p_dialog->yes];
    if( p_dialog->no != NULL )
        o_no = [NSString stringWithUTF8String: p_dialog->no];
    if( p_dialog->cancel != NULL )
        o_cancel = [NSString stringWithUTF8String: p_dialog->cancel];

    o_alert = [NSAlert alertWithMessageText: [NSString stringWithUTF8String: p_dialog->title] defaultButton: o_yes alternateButton:o_no otherButton: o_cancel informativeTextWithFormat: [NSString stringWithUTF8String: p_dialog->message]];
    [o_alert setAlertStyle: NSInformationalAlertStyle];
    i_returnValue = [o_alert runModal];

    if( i_returnValue == NSAlertDefaultReturn )
        p_dialog->answer = 1;
    if( i_returnValue == NSAlertAlternateReturn )
        p_dialog->answer = 2;
    if( i_returnValue == NSAlertOtherReturn )
        p_dialog->answer = 3;
}

131
-(void)showLoginDialog: (NSValue *)o_value
132
{
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    dialog_login_t *p_dialog = [o_value pointerValue];
    NSInteger i_returnValue = 0;

    [o_auth_title_txt setStringValue: [NSString stringWithUTF8String: p_dialog->title]];
    [o_auth_win setTitle: [NSString stringWithUTF8String: p_dialog->title]];
    [o_auth_description_txt setStringValue: [NSString stringWithUTF8String: p_dialog->message]];
    [o_auth_login_fld setStringValue: @""];
    [o_auth_pw_fld setStringValue: @""];

    [o_auth_win center];
    i_returnValue = [NSApp runModalForWindow: o_auth_win];
    [o_auth_win close];
    if( i_returnValue )
    {
        *p_dialog->username = strdup( [[o_auth_login_fld stringValue] UTF8String] );
        *p_dialog->password = strdup( [[o_auth_pw_fld stringValue] UTF8String] );
    }
    else
    {
         *p_dialog->username = *p_dialog->password = NULL;
    }
154 155
}

156
-(IBAction)loginDialogAction:(id)sender
157
{
158 159 160 161
    if( [[sender title] isEqualToString: _NS("OK")] )
        [NSApp stopModalWithCode: 1];
    else
        [NSApp stopModalWithCode: 0];
162 163
}

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
-(void)showProgressDialog: (NSValue *)o_value
{
    dialog_progress_bar_t *p_dialog = [o_value pointerValue];

    if( p_dialog->title != NULL )
    {
        [o_prog_win setTitle: [NSString stringWithUTF8String: p_dialog->title]];
        [o_prog_title_txt setStringValue: [NSString stringWithUTF8String: p_dialog->title]];
    }
    else
    {
        [o_prog_win setTitle: @""];
        [o_prog_title_txt setStringValue: @""];
    }
    if( p_dialog->cancel != NULL )
        [o_prog_cancel_btn setTitle: [NSString stringWithUTF8String: p_dialog->cancel]];
    else
        [o_prog_cancel_btn setTitle: _NS("Cancel")];
    if( p_dialog->message != NULL )
        [o_prog_description_txt setStringValue: [NSString stringWithUTF8String: p_dialog->message]];
    else
        [o_prog_description_txt setStringValue: @""];
    [o_prog_bar setDoubleValue: 0];
187
    [o_prog_bar startAnimation: self];
188

189
    [o_prog_win makeKeyAndOrderFront: self];
190 191
}

192
-(void)updateProgressPanelWithText: (NSString *)string andNumber: (double)d_number
193
{
194 195
    [o_prog_description_txt setStringValue: string];
    [o_prog_bar setDoubleValue: d_number];
196 197
}

198
-(void)destroyProgressPanel
199
{
200 201
    [o_prog_bar stopAnimation: self];
    [o_prog_win close];
202 203
}

204
-(IBAction)progDialogAction:(id)sender
205
{
206
    b_progress_cancelled = YES;
207 208
}

209
-(BOOL)progressCancelled
210 211 212 213
{
    return b_progress_cancelled;
}

214
-(id)errorPanel
215 216 217 218
{
    return o_error_panel;
}

219 220
@end

221
/*****************************************************************************
222
 * VLCErrorPanel implementation
223
 *****************************************************************************/
224
@implementation VLCErrorPanel
225 226 227
-(id)init
{
    [super init];
228

229 230 231 232 233 234 235 236 237
    if( !b_nib_loaded )
    {
        b_nib_loaded = [NSBundle loadNibNamed:@"ErrorPanel" owner:self];
    
        /* init strings */
        [o_window setTitle: _NS("Errors and Warnings")];
        [o_cleanup_button setTitle: _NS("Clean up")];
        [o_messages_btn setTitle: _NS("Show Details")];
    }
238 239

    /* init data sources */
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
    o_errors = [[NSMutableArray alloc] init];
    o_icons = [[NSMutableArray alloc] init];

    return self;
}

-(void)dealloc
{
    [o_errors release];
    [o_icons release];
    [super dealloc];
}

-(void)showPanel
{
    [o_window makeKeyAndOrderFront: self];
}

-(void)addError: (NSString *)o_error withMsg:(NSString *)o_msg
{
    /* format our string as desired */
    NSMutableAttributedString * ourError;
    ourError = [[NSMutableAttributedString alloc] initWithString:
        [NSString stringWithFormat:@"%@\n%@", o_error, o_msg]
264
        attributes:
265
        [NSDictionary dictionaryWithObject: [NSFont systemFontOfSize:11] forKey: NSFontAttributeName]];
266
    [ourError
267
        addAttribute: NSFontAttributeName
268
        value: [NSFont boldSystemFontOfSize:11]
269 270 271 272
        range: NSMakeRange( 0, [o_error length])];
    [o_errors addObject: ourError];
    [ourError release];

273
    [o_icons addObject: [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kAlertStopIcon)]];
274 275 276 277 278 279 280 281 282 283 284

    [o_error_table reloadData];
}

-(IBAction)cleanupTable:(id)sender
{
    [o_errors removeAllObjects];
    [o_icons removeAllObjects];
    [o_error_table reloadData];
}

285 286 287 288 289
-(IBAction)showMessages:(id)sender
{
    [[VLCMain sharedInstance] showMessagesPanel: sender];
}

290 291 292
/*----------------------------------------------------------------------------
 * data source methods
 *---------------------------------------------------------------------------*/
293
- (NSInteger)numberOfRowsInTableView:(NSTableView *)theDataTable
294 295 296 297 298
{
    return [o_errors count];
}

- (id)tableView:(NSTableView *)theDataTable objectValueForTableColumn:
299
    (NSTableColumn *)theTableColumn row: (NSInteger)row
300 301 302 303 304 305 306 307 308 309 310
{
    if( [[theTableColumn identifier] isEqualToString: @"error_msg"] )
        return [o_errors objectAtIndex: row];

    if( [[theTableColumn identifier] isEqualToString: @"icon"] )
        return [o_icons objectAtIndex: row];

    return @"unknown identifier";
}

@end