VLCEmbeddedDialogProvider.m 15.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/*****************************************************************************
 * VLCEmbeddedDialogProvider.m: an implementation of the libvlc dialog API
 *****************************************************************************
 * Copyright (C) 2016 VideoLabs SAS
 * $Id$
 *
 * Authors: Felix Paul Kühne <fkuehne # videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#import "VLCEmbeddedDialogProvider.h"
#import "VLCLibrary.h"
#import "VLCiOSLegacyDialogProvider.h"
#import "VLCEmbeddedDialogProvider.h"

@interface VLCEmbeddedDialogProvider ()
{
    VLCLibrary *_libraryInstance;
}

- (void)displayError:(NSArray * _Nonnull)dialogData;
- (void)displayLoginDialog:(NSArray * _Nonnull)dialogData;
- (void)displayQuestion:(NSArray * _Nonnull)dialogData;
- (void)displayProgressDialog:(NSArray * _Nonnull)dialogData;
- (void)updateDisplayedProgressDialog:(NSArray * _Nonnull)dialogData;
39
- (void)dismissCurrentDialogViewController;
40 41 42

@end

43 44 45
static void displayErrorCallback(void *p_data,
                                 const char *psz_title,
                                 const char *psz_text)
46 47 48 49 50 51 52 53 54 55
{
    @autoreleasepool {
        VLCEmbeddedDialogProvider *dialogProvider = (__bridge VLCEmbeddedDialogProvider *)p_data;
        [dialogProvider performSelectorOnMainThread:@selector(displayError:)
                                         withObject:@[toNSStr(psz_title),
                                                      toNSStr(psz_text)]
                                      waitUntilDone:NO];
    }
}

56 57
static void displayLoginCallback(void *p_data,
                                 libvlc_dialog_id *p_id,
58 59 60
                                 const char *psz_title,
                                 const char *psz_text,
                                 const char *psz_default_username,
61
                                 bool b_ask_store)
62 63 64 65 66 67 68 69 70 71 72 73 74
{
    @autoreleasepool {
        VLCEmbeddedDialogProvider *dialogProvider = (__bridge VLCEmbeddedDialogProvider *)p_data;
        [dialogProvider performSelectorOnMainThread:@selector(displayLoginDialog:)
                                         withObject:@[[NSValue valueWithPointer:p_id],
                                                      toNSStr(psz_title),
                                                      toNSStr(psz_text),
                                                      toNSStr(psz_default_username),
                                                      @(b_ask_store)]
                                      waitUntilDone:NO];
    }
}

75 76
static void displayQuestionCallback(void *p_data,
                                    libvlc_dialog_id *p_id,
77 78 79 80 81
                                    const char *psz_title,
                                    const char *psz_text,
                                    libvlc_dialog_question_type i_type,
                                    const char *psz_cancel,
                                    const char *psz_action1,
82
                                    const char *psz_action2)
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
{
    @autoreleasepool {
        VLCEmbeddedDialogProvider *dialogProvider = (__bridge  VLCEmbeddedDialogProvider *)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];
    }
}

98 99
static void displayProgressCallback(void *p_data,
                                    libvlc_dialog_id *p_id,
100 101 102 103
                                    const char *psz_title,
                                    const char *psz_text,
                                    bool b_indeterminate,
                                    float f_position,
104
                                    const char *psz_cancel)
105 106 107 108 109 110 111 112 113 114 115 116 117 118
{
    @autoreleasepool {
        VLCEmbeddedDialogProvider *dialogProvider = (__bridge VLCEmbeddedDialogProvider *)p_data;
        [dialogProvider performSelectorOnMainThread:@selector(displayProgressDialog:)
                                         withObject:@[[NSValue valueWithPointer:p_id],
                                                      toNSStr(psz_title),
                                                      toNSStr(psz_text),
                                                      @(b_indeterminate),
                                                      @(f_position),
                                                      toNSStr(psz_cancel)]
                                      waitUntilDone:NO];
    }
}

119 120
static void cancelCallback(void *p_data,
                           libvlc_dialog_id *p_id)
121 122
{
    @autoreleasepool {
123 124 125 126
        VLCEmbeddedDialogProvider *dialogProvider = (__bridge VLCEmbeddedDialogProvider *)p_data;
        [dialogProvider performSelectorOnMainThread:@selector(dismissCurrentDialogViewController)
                                         withObject:nil
                                      waitUntilDone:NO];
127 128 129
    }
}

130 131 132 133
static void updateProgressCallback(void *p_data,
                                   libvlc_dialog_id *p_id,
                                   float f_position,
                                   const char *psz_text)
134 135 136 137 138
{
    @autoreleasepool {
        VLCEmbeddedDialogProvider *dialogProvider = (__bridge VLCEmbeddedDialogProvider *)p_data;
        [dialogProvider performSelectorOnMainThread:@selector(updateDisplayedProgressDialog:)
                                         withObject:@[[NSValue valueWithPointer:p_id],
139
                                                      @(f_position),
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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 187 188 189 190 191 192
                                                      toNSStr(psz_text)]
                                      waitUntilDone:NO];
    }
}

@implementation VLCEmbeddedDialogProvider

- (void)dealloc
{
    libvlc_dialog_set_callbacks(_libraryInstance.instance,
                                NULL,
                                NULL);
}

- (instancetype)initWithLibrary:(VLCLibrary *)library
{
    self = [super init];

    if (self != nil) {
        if (library == nil) {
            library = [VLCLibrary sharedLibrary];
        }

        _libraryInstance = library;

        /* callback setup */
        const libvlc_dialog_cbs cbs = {
            displayErrorCallback,
            displayLoginCallback,
            displayQuestionCallback,
            displayProgressCallback,
            cancelCallback,
            updateProgressCallback
        };

        libvlc_dialog_set_callbacks(_libraryInstance.instance,
                                    &cbs,
                                    (__bridge void *)self);
    }

    return self;
}

- (void)displayError:(NSArray * _Nonnull)dialogData
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:dialogData[0]
                                                                             message:dialogData[1]
                                                                      preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
                                                     style:UIAlertActionStyleDestructive
                                                   handler:nil];
    [alertController addAction:action];
193 194 195
    if ([alertController respondsToSelector:@selector(setPreferredAction:)]) {
        [alertController setPreferredAction:action];
    }
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    [[[[UIApplication sharedApplication].delegate.window rootViewController] presentedViewController] presentViewController:alertController
                                                                                                                   animated:YES
                                                                                                                 completion:nil];
}

- (void)displayLoginDialog:(NSArray * _Nonnull)dialogData
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:dialogData[1]
                                                                             message:dialogData[2]
                                                                      preferredStyle:UIAlertControllerStyleAlert];

    __block UITextField *usernameField = nil;
    __block UITextField *passwordField = nil;
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        usernameField = textField;
        textField.placeholder = NSLocalizedString(@"User", nil);
        if (![dialogData[3] isEqualToString:@""])
            textField.text = dialogData[3];
    }];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.secureTextEntry = YES;
        textField.placeholder = NSLocalizedString(@"Password", nil);
        passwordField = textField;
    }];

    UIAlertAction *loginAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Login", nil)
                                                          style:UIAlertActionStyleDefault
                                                        handler:^(UIAlertAction * _Nonnull action) {
                                                            NSString *username = usernameField.text;
                                                            NSString *password = passwordField.text;

                                                            libvlc_dialog_post_login([dialogData[0] pointerValue],
228 229
                                                                                     username ? [username UTF8String] : "",
                                                                                     password ? [password UTF8String] : "",
230 231 232
                                                                                     NO);
                                                        }];
    [alertController addAction:loginAction];
233 234 235
    if ([alertController respondsToSelector:@selector(setPreferredAction:)]) {
        [alertController setPreferredAction:loginAction];
    }
236 237 238 239

    [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil)
                                                        style:UIAlertActionStyleCancel
                                                      handler:^(UIAlertAction * _Nonnull action) {
240
                                                          libvlc_dialog_dismiss([dialogData[0] pointerValue]);
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 277 278 279 280 281
                                                      }]];
    if ([dialogData[4] boolValue]) {
        [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Save", nil)
                                                            style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * _Nonnull action) {
                                                              NSString *username = usernameField.text;
                                                              NSString *password = passwordField.text;

                                                              libvlc_dialog_post_login([dialogData[0] pointerValue],
                                                                                       username ? [username UTF8String] : NULL,
                                                                                       password ? [password UTF8String] : NULL,
                                                                                       YES);
                                                          }]];
    }

    [[[[UIApplication sharedApplication].delegate.window rootViewController] presentedViewController] presentViewController:alertController
                                                                                                                   animated:YES
                                                                                                                 completion:nil];
}

- (void)displayQuestion:(NSArray * _Nonnull)dialogData
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:dialogData[1]
                                                                             message:dialogData[2]
                                                                      preferredStyle:UIAlertControllerStyleAlert];

    if (![dialogData[4] isEqualToString:@""]) {
        [alertController addAction:[UIAlertAction actionWithTitle:dialogData[4]
                                                            style:UIAlertActionStyleCancel
                                                          handler:^(UIAlertAction * _Nonnull action) {
                                                              libvlc_dialog_post_action([dialogData[0] pointerValue], 3);
                                                          }]];
    }

    if (![dialogData[5] isEqualToString:@""]) {
        UIAlertAction *yesAction = [UIAlertAction actionWithTitle:dialogData[5]
                                                            style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * _Nonnull action) {
                                                              libvlc_dialog_post_action([dialogData[0] pointerValue], 1);
                                                          }];
        [alertController addAction:yesAction];
282 283 284
        if ([alertController respondsToSelector:@selector(setPreferredAction:)]) {
            [alertController setPreferredAction:yesAction];
        }
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
    }

    if (![dialogData[6] isEqualToString:@""]) {
        [alertController addAction:[UIAlertAction actionWithTitle:dialogData[6]
                                                            style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * _Nonnull action) {
                                                              libvlc_dialog_post_action([dialogData[0] pointerValue], 2);
                                                          }]];
    }

    [[[[UIApplication sharedApplication].delegate.window rootViewController] presentedViewController] presentViewController:alertController animated:YES completion:nil];
    
}

- (void)displayProgressDialog:(NSArray * _Nonnull)dialogData
{
    VKLog(@"%s: %@", __PRETTY_FUNCTION__, dialogData);
}

- (void)updateDisplayedProgressDialog:(NSArray * _Nonnull)dialogData
{
    VKLog(@"%s: %@", __PRETTY_FUNCTION__, dialogData);
}

309 310 311 312 313
- (void)dismissCurrentDialogViewController
{
    [[[[UIApplication sharedApplication].delegate.window rootViewController] presentedViewController] dismissViewControllerAnimated:YES completion:nil];
}

314
@end