blob: 82bbf026e1b170fd12dad8f4918eff1f84bb786f [file] [log] [blame]
Alexandre Lision45f1f542016-08-25 15:16:17 -04001/*
Sébastien Blin029ffa82019-01-02 17:43:48 -05002 * Copyright (C) 2016-2019 Savoir-faire Linux Inc.
Alexandre Lision45f1f542016-08-25 15:16:17 -04003 * 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
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040022#import <api/lrc.h>
23#import <api/newaccountmodel.h>
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040024#import <api/account.h>
Alexandre Lision45f1f542016-08-25 15:16:17 -040025
26//Ring
27#import "views/ITProgressIndicator.h"
28@interface ExportPasswordWC() <NSTextFieldDelegate>{
29 __unsafe_unretained IBOutlet NSSecureTextField* passwordField;
30 __unsafe_unretained IBOutlet NSTextField* resultField;
31 __unsafe_unretained IBOutlet NSTextField* errorField;
32 __unsafe_unretained IBOutlet ITProgressIndicator* progressIndicator;
33}
34@end
35
36@implementation ExportPasswordWC {
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040037
Alexandre Lision45f1f542016-08-25 15:16:17 -040038}
39
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040040@synthesize accountModel;
Alexandre Lision45f1f542016-08-25 15:16:17 -040041QMetaObject::Connection accountConnection;
42
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040043-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel
Alexandre Lision45f1f542016-08-25 15:16:17 -040044{
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040045 if (self = [self initWithWindowNibName: nibNameOrNil])
46 {
47 self.accountModel= accountModel;
Alexandre Lision45f1f542016-08-25 15:16:17 -040048 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040049 return self;
Alexandre Lision45f1f542016-08-25 15:16:17 -040050}
51
52- (void)showError:(NSString*) errorMessage
53{
54 [errorField setStringValue:errorMessage];
55 [super showError];
56}
57
58- (void)showLoading
59{
60 [progressIndicator setNumberOfLines:30];
61 [progressIndicator setWidthOfLine:2];
62 [progressIndicator setLengthOfLine:5];
63 [progressIndicator setInnerMargin:20];
64 [super showLoading];
65}
66
67#pragma mark - Events Handlers
68- (IBAction)completeAction:(id)sender
69{
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040070 NSString* password = passwordField.stringValue;
71 [self showLoading];
72 QObject::disconnect(accountConnection);
73 accountConnection = QObject::connect(self.accountModel,
74 &lrc::api::NewAccountModel::exportOnRingEnded,
Kateryna Kostiukc867eb92020-03-08 13:15:17 -040075 [self] (const QString &accountID, lrc::api::account::ExportOnRingStatus status, const QString &pin){
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040076 if(accountID.compare(self.selectedAccountID) != 0) {
77 return;
78 }
79 switch (status) {
80 case lrc::api::account::ExportOnRingStatus::SUCCESS: {
Kateryna Kostiukc867eb92020-03-08 13:15:17 -040081 NSString *nsPin = pin.toNSString();
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040082 NSLog(@"Export ended with Success, pin is %@",nsPin);
83 [resultField setAttributedStringValue:[self formatPinMessage:nsPin]];
84 [self showFinal];
Alexandre Lision45f1f542016-08-25 15:16:17 -040085 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040086 break;
87 case lrc::api::account::ExportOnRingStatus::WRONG_PASSWORD:{
88 NSLog(@"Export ended with wrong password");
89 [self showError:NSLocalizedString(@"The password you entered does not unlock this account", @"Error shown to the user" )];
90 }
91 break;
92 case lrc::api::account::ExportOnRingStatus::NETWORK_ERROR:{
93 NSLog(@"Export ended with NetworkError!");
94 [self showError:NSLocalizedString(@"A network error occured during the export", @"Error shown to the user" )];
95 }
96 break;
97 default:{
98 NSLog(@"Export ended with Unknown status!");
99 [self showError:NSLocalizedString(@"An error occured during the export", @"Error shown to the user" )];
100 }
101 break;
102 }
103 QObject::disconnect(accountConnection);
104 });
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400105 self.accountModel->exportOnRing(self.selectedAccountID, QString::fromNSString(password));
Alexandre Lision45f1f542016-08-25 15:16:17 -0400106}
107
108//TODO: Move String formatting to a dedicated Utility Classes
Loïc Siret3652cfb2016-10-27 10:12:07 -0400109- (NSAttributedString*) formatPinMessage:(NSString*) pin
Alexandre Lision45f1f542016-08-25 15:16:17 -0400110{
Loïc Siret3652cfb2016-10-27 10:12:07 -0400111 NSMutableAttributedString* thePin = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"\n%@\n", pin]];
Alexandre Lision45f1f542016-08-25 15:16:17 -0400112 [thePin beginEditing];
113 NSRange range = NSMakeRange(0, [thePin length]);
Loïc Siret3652cfb2016-10-27 10:12:07 -0400114 [thePin addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Helvetica-Bold" size:20.0] range:range];
Alexandre Lision45f1f542016-08-25 15:16:17 -0400115
Loïc Siret3652cfb2016-10-27 10:12:07 -0400116 NSMutableParagraphStyle* mutParaStyle=[[NSMutableParagraphStyle alloc] init];
117 [mutParaStyle setAlignment:NSCenterTextAlignment];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400118
Loïc Siret3652cfb2016-10-27 10:12:07 -0400119 [thePin addAttributes:[NSDictionary dictionaryWithObject:mutParaStyle forKey:NSParagraphStyleAttributeName] range:range];
120
Kateryna Kostiuk85a334e2018-12-03 15:54:19 -0500121 NSMutableAttributedString* infos = [[NSMutableAttributedString alloc] initWithString:NSLocalizedString(@"To complete the processs, you need to open Jami 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")];
Loïc Siret3652cfb2016-10-27 10:12:07 -0400122 [thePin appendAttributedString:infos];
123 [thePin endEditing];
124
125 return thePin;
Alexandre Lision45f1f542016-08-25 15:16:17 -0400126}
127
128@end