coredialogs.m 11.5 KB
Newer Older
1
/*****************************************************************************
2
 * coredialogs.m: Mac OS X Core Dialogs
3
 *****************************************************************************
4
 * Copyright (C) 2005-2016 VLC authors and VideoLAN
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
@interface VLCCoreDialogProvider ()

34 35 36 37 38 39 40 41 42
- (void)displayError:(NSArray *)dialogData;

- (void)displayLoginDialog:(NSArray *)dialogData;

- (void)displayQuestion:(NSArray *)dialogData;

- (void)displayProgressDialog:(NSArray *)dialogData;

- (void)updateDisplayedProgressDialog:(NSArray *)dialogData;
43 44 45 46 47 48 49

@end


static void displayErrorCallback(const char *psz_title,
                                 const char *psz_text,
                                 void *p_data)
50 51
{
    @autoreleasepool {
52 53 54 55 56
        VLCCoreDialogProvider *dialogProvider = (__bridge VLCCoreDialogProvider *)p_data;
        [dialogProvider performSelectorOnMainThread:@selector(displayError:)
                                         withObject:@[toNSStr(psz_title),
                                                      toNSStr(psz_text)]
                                      waitUntilDone:NO];
57 58
    }
}
59

60 61 62 63 64 65 66 67 68
static void displayLoginCallback(vlc_dialog_id *p_id,
                                 const char *psz_title,
                                 const char *psz_text,
                                 const char *psz_default_username,
                                 bool b_ask_store,
                                 void *p_data)
{
    @autoreleasepool {
        VLCCoreDialogProvider *dialogProvider = (__bridge VLCCoreDialogProvider *)p_data;
69 70 71 72 73 74 75
        [dialogProvider performSelectorOnMainThread:@selector(displayLoginDialog:)
                                         withObject:@[[NSValue valueWithPointer:p_id],
                                                      toNSStr(psz_title),
                                                      toNSStr(psz_text),
                                                      toNSStr(psz_default_username),
                                                      @(b_ask_store)]
                                      waitUntilDone:NO];
76 77 78 79 80 81 82 83 84 85 86 87 88
    }
}

static void displayQuestionCallback(vlc_dialog_id *p_id,
                                    const char *psz_title,
                                    const char *psz_text,
                                    vlc_dialog_question_type i_type,
                                    const char *psz_cancel,
                                    const char *psz_action1,
                                    const char *psz_action2,
                                    void *p_data)
{
    @autoreleasepool {
89 90 91 92 93 94 95 96 97 98
        VLCCoreDialogProvider *dialogProvider = (__bridge  VLCCoreDialogProvider *)p_data;
        [dialogProvider performSelectorOnMainThread:@selector(displayQuestion:)
                                         withObject:@[[NSValue valueWithPointer:p_id],
                                                      toNSStr(psz_title),
                                                      toNSStr(psz_text),
                                                      @(i_type),
                                                      toNSStr(psz_cancel),
                                                      toNSStr(psz_action1),
                                                      toNSStr(psz_action2)]
                                      waitUntilDone:NO];
99 100 101 102 103 104 105 106 107 108 109 110 111
    }
}

static void displayProgressCallback(vlc_dialog_id *p_id,
                                    const char *psz_title,
                                    const char *psz_text,
                                    bool b_indeterminate,
                                    float f_position,
                                    const char *psz_cancel,
                                    void *p_data)
{
    @autoreleasepool {
        VLCCoreDialogProvider *dialogProvider = (__bridge VLCCoreDialogProvider *)p_data;
112 113 114 115 116 117 118 119
        [dialogProvider performSelectorOnMainThread:@selector(displayProgressDialog:)
                                         withObject:@[[NSValue valueWithPointer:p_id],
                                                      toNSStr(psz_title),
                                                      toNSStr(psz_text),
                                                      @(b_indeterminate),
                                                      @(f_position),
                                                      toNSStr(psz_cancel)]
                                      waitUntilDone:NO];
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    }
}

static void cancelCallback(vlc_dialog_id *p_id,
                           void *p_data)
{
    @autoreleasepool {
        [NSApp stopModalWithCode: 0];
    }
}

static void updateProgressCallback(vlc_dialog_id *p_id,
                                   float f_value,
                                   const char *psz_text,
                                   void *p_data)
{
    @autoreleasepool {
        VLCCoreDialogProvider *dialogProvider = (__bridge VLCCoreDialogProvider *)p_data;
138 139 140 141 142
        [dialogProvider performSelectorOnMainThread:@selector(updateDisplayedProgressDialog:)
                                         withObject:@[[NSValue valueWithPointer:p_id],
                                                      @(f_value),
                                                      toNSStr(psz_text)]
                                      waitUntilDone:NO];
143 144 145
    }
}

146 147
@implementation VLCCoreDialogProvider

148 149 150 151 152
- (instancetype)init
{
    self = [super init];

    if (self) {
153
        msg_Dbg(getIntf(), "Register dialog provider");
154 155
        [NSBundle loadNibNamed:@"CoreDialogs" owner: self];

156
        intf_thread_t *p_intf = getIntf();
157
        /* subscribe to various interactive dialogues */
158

159
        const vlc_dialog_cbs cbs = {
160 161 162 163 164 165 166 167 168
            displayErrorCallback,
            displayLoginCallback,
            displayQuestionCallback,
            displayProgressCallback,
            cancelCallback,
            updateProgressCallback
        };

        vlc_dialog_provider_set_callbacks(p_intf, &cbs, (__bridge void *)self);
169
    }
170

171 172 173 174 175
    return self;
}

- (void)dealloc
{
176
    msg_Dbg(getIntf(), "Deinitializing dialog provider");
177

178
    intf_thread_t *p_intf = getIntf();
179
    vlc_dialog_provider_set_callbacks(p_intf, NULL, NULL);
180 181
}

182 183
-(void)awakeFromNib
{
184
    _progressCancelled = NO;
185 186 187 188
    [authenticationLoginLabel setStringValue: _NS("Username")];
    [authenticationPasswordLabel setStringValue: _NS("Password")];
    [authenticationCancelButton setTitle: _NS("Cancel")];
    [authenticationOkButton setTitle: _NS("OK")];
189
    [authenticationStorePasswordCheckbox setTitle:_NS("Remember")];
190

191 192
    [progressCancelButton setTitle: _NS("Cancel")];
    [progressIndicator setUsesThreadedAnimation: YES];
193 194
}

195
- (void)displayError:(NSArray *)dialogData
196
{
197 198 199 200 201 202 203 204 205 206 207 208 209 210
    NSAlert *alert = [NSAlert alertWithMessageText:dialogData[0]
                                     defaultButton:_NS("OK")
                                   alternateButton:nil
                                       otherButton:nil
                         informativeTextWithFormat:@"%@", dialogData[1]];
    [alert setAlertStyle:NSCriticalAlertStyle];
    [alert runModal];
}

- (void)displayLoginDialog:(NSArray *)dialogData
{
    [authenticationTitleLabel setStringValue:dialogData[1]];
    authenticationWindow.title = dialogData[1];
    [authenticationDescriptionLabel setStringValue:dialogData[2]];
211

212
    [authenticationLoginTextField setStringValue:dialogData[3]];
213 214
    [authenticationPasswordTextField setStringValue:@""];

215
    authenticationStorePasswordCheckbox.hidden = ![dialogData[4] boolValue];
216 217
    authenticationStorePasswordCheckbox.state = NSOffState;

218 219 220 221 222 223 224
    [authenticationWindow center];
    NSInteger returnValue = [NSApp runModalForWindow:authenticationWindow];
    [authenticationWindow close];

    NSString *username = authenticationLoginTextField.stringValue;
    NSString *password = authenticationPasswordTextField.stringValue;

225
    vlc_dialog_id_post_login([dialogData[0] pointerValue],
226 227
                             username ? [username UTF8String] : NULL,
                             password ? [password UTF8String] : NULL,
228
                             authenticationStorePasswordCheckbox.state == NSOnState);
229 230
}

231
- (IBAction)authenticationDialogAction:(id)sender
232
{
233 234 235 236 237 238
    if ([[sender title] isEqualToString: _NS("OK")])
        [NSApp stopModalWithCode: 1];
    else
        [NSApp stopModalWithCode: 0];
}

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
- (void)displayQuestion:(NSArray *)dialogData
{
    NSAlert *alert = [NSAlert alertWithMessageText:dialogData[1]
                                     defaultButton:dialogData[5]
                                   alternateButton:dialogData[6]
                                       otherButton:dialogData[4]
                         informativeTextWithFormat:@"%@", dialogData[2]];

    switch ([dialogData[3] intValue]) {
        case VLC_DIALOG_QUESTION_WARNING:
            [alert setAlertStyle:NSWarningAlertStyle];
            break;
        case VLC_DIALOG_QUESTION_CRITICAL:
            [alert setAlertStyle:NSCriticalAlertStyle];
            break;
        default:
            [alert setAlertStyle:NSInformationalAlertStyle];
            break;
    }

    NSInteger returnValue = [alert runModal];
    switch (returnValue) {
        case NSAlertAlternateReturn:
            vlc_dialog_id_post_action([dialogData[0] pointerValue], 2);
            break;

        case NSAlertOtherReturn:
            vlc_dialog_id_post_action([dialogData[0] pointerValue], 3);
            break;

        default:
            vlc_dialog_id_post_action([dialogData[0] pointerValue], 1);
            break;
    }

}

- (void)displayProgressDialog:(NSArray *)dialogData
277
{
278 279
    progressTitleLabel.stringValue = dialogData[1];
    progressWindow.title = dialogData[1];
280

281
    progressDescriptionLabel.stringValue = dialogData[2];
282

283 284
    progressIndicator.indeterminate = [dialogData[3] boolValue];
    progressIndicator.doubleValue = [dialogData[4] doubleValue];
285

286 287
    if ([dialogData[5] length] > 0) {
        progressCancelButton.title = dialogData[5];
288 289 290 291 292 293 294
    } else {
        progressCancelButton.title = _NS("Cancel");
    }

    [progressIndicator startAnimation:self];

    [progressWindow center];
295
    NSInteger returnValue = [NSApp runModalForWindow:progressWindow];
296 297
    [progressWindow close];

298 299 300 301
    [progressIndicator stopAnimation:self];

    if (returnValue == -1)
        vlc_dialog_id_dismiss([dialogData[0] pointerValue]);
302 303
}

304 305
- (void)updateDisplayedProgressDialog:(NSArray *)dialogData

306 307
{
    if (!progressIndicator.indeterminate) {
308 309
        progressIndicator.doubleValue = [dialogData[1] doubleValue];
        progressDescriptionLabel.stringValue = dialogData[2];
310
    }
311 312
}

313
- (IBAction)progressDialogAction:(id)sender
314
{
315
    [NSApp stopModalWithCode: -1];
316 317 318
}

@end