blob: f9b914c698af8996ef089bbc6590d6de1e4dbcf7 [file] [log] [blame]
Alexandre Lision45f1f542016-08-25 15:16:17 -04001/*
2 * Copyright (C) 2016 Savoir-faire Linux Inc.
3 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19#import "ExportPasswordWC.h"
20
21//LRC
22#import <account.h>
23
24//Ring
25#import "views/ITProgressIndicator.h"
26@interface ExportPasswordWC() <NSTextFieldDelegate>{
27 __unsafe_unretained IBOutlet NSSecureTextField* passwordField;
28 __unsafe_unretained IBOutlet NSTextField* resultField;
29 __unsafe_unretained IBOutlet NSTextField* errorField;
30 __unsafe_unretained IBOutlet ITProgressIndicator* progressIndicator;
31}
32@end
33
34@implementation ExportPasswordWC {
35 struct {
36 unsigned int didStart:1;
37 unsigned int didComplete:1;
38 } delegateRespondsTo;
39}
40
41@synthesize account;
42QMetaObject::Connection accountConnection;
43
44
45#pragma mark - Initialize
46- (id)initWithDelegate:(id <ExportPasswordDelegate>) del actionCode:(NSInteger) code
47{
48 return [super initWithWindowNibName:@"ExportPasswordWindow" delegate:del actionCode:code];
49}
50
51- (void)windowDidLoad
52{
53 [super windowDidLoad];
54}
55
56- (void)setDelegate:(id <ExportPasswordDelegate>)aDelegate
57{
58 if (super.delegate != aDelegate) {
59 [super setDelegate: aDelegate];
60 delegateRespondsTo.didStart = [aDelegate respondsToSelector:@selector(didStartWithPassword:)];
61 delegateRespondsTo.didComplete = [aDelegate respondsToSelector:@selector(didCompleteWithPin:Password:)];
62 }
63}
64
65- (void)showError:(NSString*) errorMessage
66{
67 [errorField setStringValue:errorMessage];
68 [super showError];
69}
70
71- (void)showLoading
72{
73 [progressIndicator setNumberOfLines:30];
74 [progressIndicator setWidthOfLine:2];
75 [progressIndicator setLengthOfLine:5];
76 [progressIndicator setInnerMargin:20];
77 [super showLoading];
78}
79
80#pragma mark - Events Handlers
81- (IBAction)completeAction:(id)sender
82{
83 // Check to avoid exporting an old account (not supported by daemon)
84 if (account->needsMigration()) {
85 [self showError:NSLocalizedString(@"You have to migrate your account before exporting", @"Error shown to user")];
86 } else {
87 NSString* password = passwordField.stringValue;
88 [self showLoading];
89 QObject::disconnect(accountConnection);
90 accountConnection = QObject::connect(account,
91 &Account::exportOnRingEnded,
92 [=](Account::ExportOnRingStatus status,const QString &pin) {
93 NSLog(@"Export ended!");
94 switch (status) {
95 case Account::ExportOnRingStatus::SUCCESS:{
96 NSString *nsPin = pin.toNSString();
97 NSLog(@"Export ended with Success, pin is %@",nsPin);
98 [resultField setAttributedStringValue:[self formatPinMessage:nsPin]];
99 [self showFinal];
100 }
101 break;
102 case Account::ExportOnRingStatus::WRONG_PASSWORD:{
103 NSLog(@"Export ended with Wrong Password");
104 [self showError:NSLocalizedString(@"Export ended with Wrong Password", @"Error shown to the user" )];
105 }
106 break;
107 case Account::ExportOnRingStatus::NETWORK_ERROR:{
108 NSLog(@"Export ended with NetworkError!");
109 [self showError:NSLocalizedString(@"A network error occured during the export", @"Error shown to the user" )];
110 }
111 break;
112 default:{
113 NSLog(@"Export ended with Unknown status!");
114 [self showError:NSLocalizedString(@"An error occured during the export", @"Error shown to the user" )];
115 }
116 break;
117 }
118 });
119 account->exportOnRing(QString::fromNSString(password));
120 }
121}
122
123//TODO: Move String formatting to a dedicated Utility Classes
124- (NSAttributedString *)formatPinMessage:(NSString*) pin
125{
126 NSMutableAttributedString* hereIsThePin = [[NSMutableAttributedString alloc] initWithString:NSLocalizedString(@"Your generated pin:","Title shown to user to concat with Pin")];
127 NSMutableAttributedString* thePin = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@\n", pin]];
128 [thePin beginEditing];
129 NSRange range = NSMakeRange(0, [thePin length]);
130 [thePin addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Helvetica-Bold" size:12.0] range:range];
131 [hereIsThePin appendAttributedString:thePin];
132 NSMutableAttributedString* infos = [[NSMutableAttributedString alloc] initWithString:NSLocalizedString(@"This pin and the account password should be entered on your new device within 5 minutes. On most client, this is done from \"Existing Ring account\" menu. You may generate a new pin at any moment.","Infos on how to use the pin")];
133 [hereIsThePin appendAttributedString:infos];
134
135 return hereIsThePin;
136}
137
138@end