blob: 054442e452a27a9b6926f5edb3a67d3b9f96917c [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:{
Loïc Siret3652cfb2016-10-27 10:12:07 -0400103 NSLog(@"Export ended with wrong password");
104 [self showError:NSLocalizedString(@"The password you entered does not unlock this account", @"Error shown to the user" )];
Alexandre Lision45f1f542016-08-25 15:16:17 -0400105 }
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
Loïc Siret3652cfb2016-10-27 10:12:07 -0400124- (NSAttributedString*) formatPinMessage:(NSString*) pin
Alexandre Lision45f1f542016-08-25 15:16:17 -0400125{
Loïc Siret3652cfb2016-10-27 10:12:07 -0400126 NSMutableAttributedString* thePin = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"\n%@\n", pin]];
Alexandre Lision45f1f542016-08-25 15:16:17 -0400127 [thePin beginEditing];
128 NSRange range = NSMakeRange(0, [thePin length]);
Loïc Siret3652cfb2016-10-27 10:12:07 -0400129 [thePin addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Helvetica-Bold" size:20.0] range:range];
Alexandre Lision45f1f542016-08-25 15:16:17 -0400130
Loïc Siret3652cfb2016-10-27 10:12:07 -0400131 NSMutableParagraphStyle* mutParaStyle=[[NSMutableParagraphStyle alloc] init];
132 [mutParaStyle setAlignment:NSCenterTextAlignment];
133
134 [thePin addAttributes:[NSDictionary dictionaryWithObject:mutParaStyle forKey:NSParagraphStyleAttributeName] range:range];
135
136 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")];
137 [thePin appendAttributedString:infos];
138 [thePin endEditing];
139
140 return thePin;
Alexandre Lision45f1f542016-08-25 15:16:17 -0400141}
142
143@end