ExportPasswordWC.mm 6.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 *  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 "ExportPasswordWC.h"

//LRC
22 23
#import <api/lrc.h>
#import <api/newaccountmodel.h>
24
#import <account.h>
25
#import <api/account.h>
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

//Ring
#import "views/ITProgressIndicator.h"
@interface ExportPasswordWC() <NSTextFieldDelegate>{
    __unsafe_unretained IBOutlet NSSecureTextField* passwordField;
    __unsafe_unretained IBOutlet NSTextField* resultField;
    __unsafe_unretained IBOutlet NSTextField* errorField;
    __unsafe_unretained IBOutlet ITProgressIndicator* progressIndicator;
}
@end

@implementation ExportPasswordWC {

}

41 42
@synthesize accountModel;
QMetaObject::Connection accountConnection;
43

44
-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel
45
{
46 47 48
    if (self =  [self initWithWindowNibName: nibNameOrNil])
    {
        self.accountModel= accountModel;
49
    }
50
    return self;
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
}

- (void)showError:(NSString*) errorMessage
{
    [errorField setStringValue:errorMessage];
    [super showError];
}

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

#pragma mark - Events Handlers
- (IBAction)completeAction:(id)sender
{
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    NSString* password = passwordField.stringValue;
    [self showLoading];
    QObject::disconnect(accountConnection);
    accountConnection = QObject::connect(self.accountModel,
                                         &lrc::api::NewAccountModel::exportOnRingEnded,
                                         [self] (const std::string &accountID, lrc::api::account::ExportOnRingStatus status, const std::string &pin){
                                             if(accountID.compare(self.selectedAccountID) != 0) {
                                                 return;
                                             }
                                             switch (status) {
                                                 case lrc::api::account::ExportOnRingStatus::SUCCESS: {
                                                     NSString *nsPin = @(pin.c_str());
                                                     NSLog(@"Export ended with Success, pin is %@",nsPin);
                                                     [resultField setAttributedStringValue:[self formatPinMessage:nsPin]];
                                                     [self showFinal];
86
                                                 }
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
                                                     break;
                                                 case lrc::api::account::ExportOnRingStatus::WRONG_PASSWORD:{
                                                     NSLog(@"Export ended with wrong password");
                                                     [self showError:NSLocalizedString(@"The password you entered does not unlock this account", @"Error shown to the user" )];
                                                 }
                                                     break;
                                                 case lrc::api::account::ExportOnRingStatus::NETWORK_ERROR:{
                                                     NSLog(@"Export ended with NetworkError!");
                                                     [self showError:NSLocalizedString(@"A network error occured during the export", @"Error shown to the user" )];
                                                 }
                                                     break;
                                                 default:{
                                                     NSLog(@"Export ended with Unknown status!");
                                                     [self showError:NSLocalizedString(@"An error occured during the export", @"Error shown to the user" )];
                                                 }
                                                     break;
                                             }
                                              QObject::disconnect(accountConnection);
                                         });
    self.accountModel->exportOnRing(self.selectedAccountID, [password UTF8String]);
107 108 109
}

//TODO: Move String formatting to a dedicated Utility Classes
110
- (NSAttributedString*) formatPinMessage:(NSString*) pin
111
{
112
    NSMutableAttributedString* thePin = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"\n%@\n", pin]];
113 114
    [thePin beginEditing];
    NSRange range = NSMakeRange(0, [thePin length]);
115
    [thePin addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Helvetica-Bold" size:20.0] range:range];
116

117 118
    NSMutableParagraphStyle* mutParaStyle=[[NSMutableParagraphStyle alloc] init];
    [mutParaStyle setAlignment:NSCenterTextAlignment];
119

120 121 122 123 124 125 126
    [thePin addAttributes:[NSDictionary dictionaryWithObject:mutParaStyle forKey:NSParagraphStyleAttributeName] range:range];

    NSMutableAttributedString* infos = [[NSMutableAttributedString alloc] initWithString:NSLocalizedString(@"To complete the processs, you need to open Ring on the new device and choose the option \"Link this device to an account\". Your pin is valid for 10 minutes.","Title shown to user to concat with Pin")];
    [thePin appendAttributedString:infos];
    [thePin endEditing];

    return thePin;
127 128 129
}

@end