RestoreAccountWC.mm 3.58 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
/*
 *  Copyright (C) 2016 Savoir-faire Linux Inc.
 *  Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
 *
 *  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 3 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 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
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */
#import "RestoreAccountWC.h"

//LRC
#import <accountmodel.h>

//Ring
#import "views/ITProgressIndicator.h"

@interface RestoreAccountWC() <NSTextFieldDelegate> {
    __unsafe_unretained IBOutlet NSPathControl* path;
    __unsafe_unretained IBOutlet NSSecureTextField* passwordField;
    __unsafe_unretained IBOutlet ITProgressIndicator* progressIndicator;
}

@end

@implementation RestoreAccountWC {
    struct {
        unsigned int didCompleteImport:1;
    } delegateRespondsTo;
}
@synthesize accounts;

- (id)initWithDelegate:(id <LoadingWCDelegate>) del
{
    return [self initWithDelegate:del actionCode:0];
}

- (id)initWithDelegate:(id <RestoreAccountDelegate>) del actionCode:(NSInteger) code
{
    return [super initWithWindowNibName:@"RestoreAccountWindow" delegate:del actionCode:code];
}

- (void)windowDidLoad
{
    [super windowDidLoad];
    [path setURL: [NSURL fileURLWithPath:NSHomeDirectory()]];
}

- (void)setDelegate:(id <RestoreAccountDelegate>)aDelegate
{
    if (self.delegate != aDelegate) {
        [super setDelegate: aDelegate];
        delegateRespondsTo.didCompleteImport = [self.delegate respondsToSelector:@selector(didCompleteWithImport)];
    }
}

- (void) setAllowFileSelection:(BOOL) b
{
    _allowFileSelection = b;
    [path setAllowedTypes:_allowFileSelection ? nil : [NSArray arrayWithObject:@"public.folder"]];
}

- (IBAction)completeAction:(id)sender
{
    auto passwordString = passwordField.stringValue;
    auto pathURL = path.URL;
    [self showLoading];
    SEL sel = @selector(importAccountsWithPath:andPassword:);
    NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:sel]];
    [inv setSelector:sel];
    [inv setTarget:self];
    //arguments 0 and 1 are self and _cmd respectively, automatically set by NSInvocation
    [inv setArgument:&pathURL atIndex:2];
    [inv setArgument:&passwordString atIndex:3];

    // Schedule import for next iteration of event loop in order to let us start the loading anim
    [inv performSelector:@selector(invoke) withObject:nil afterDelay:0];
}

- (void)showLoading
{
    [progressIndicator setNumberOfLines:30];
    [progressIndicator setWidthOfLine:2];
    [progressIndicator setLengthOfLine:5];
    [progressIndicator setInnerMargin:20];
    [super showLoading];
}

- (void) importAccountsWithPath:(NSURL*) urlPath andPassword:(NSString*) password
{
    int result = AccountModel::instance().importAccounts(urlPath.path.UTF8String, password.UTF8String);
    switch (result) {
        case 0:
            if (delegateRespondsTo.didCompleteImport)
                [((id<RestoreAccountDelegate>)self.delegate) didCompleteImport];
            [self close];
            break;
        default:
        {
            [self showError];
        }
            break;
    }
}

@end