Alexandre Lision | 45f1f54 | 2016-08-25 15:16:17 -0400 | [diff] [blame] | 1 | /* |
Sébastien Blin | 029ffa8 | 2019-01-02 17:43:48 -0500 | [diff] [blame] | 2 | * Copyright (C) 2016-2019 Savoir-faire Linux Inc. |
Alexandre Lision | 45f1f54 | 2016-08-25 15:16:17 -0400 | [diff] [blame] | 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 |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 22 | #import <api/lrc.h> |
| 23 | #import <api/newaccountmodel.h> |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 24 | #import <api/account.h> |
Alexandre Lision | 45f1f54 | 2016-08-25 15:16:17 -0400 | [diff] [blame] | 25 | |
| 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 Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 37 | |
Alexandre Lision | 45f1f54 | 2016-08-25 15:16:17 -0400 | [diff] [blame] | 38 | } |
| 39 | |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 40 | @synthesize accountModel; |
Alexandre Lision | 45f1f54 | 2016-08-25 15:16:17 -0400 | [diff] [blame] | 41 | QMetaObject::Connection accountConnection; |
| 42 | |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 43 | -(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel |
Alexandre Lision | 45f1f54 | 2016-08-25 15:16:17 -0400 | [diff] [blame] | 44 | { |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 45 | if (self = [self initWithWindowNibName: nibNameOrNil]) |
| 46 | { |
| 47 | self.accountModel= accountModel; |
Alexandre Lision | 45f1f54 | 2016-08-25 15:16:17 -0400 | [diff] [blame] | 48 | } |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 49 | return self; |
Alexandre Lision | 45f1f54 | 2016-08-25 15:16:17 -0400 | [diff] [blame] | 50 | } |
| 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 Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 70 | NSString* password = passwordField.stringValue; |
| 71 | [self showLoading]; |
| 72 | QObject::disconnect(accountConnection); |
| 73 | accountConnection = QObject::connect(self.accountModel, |
| 74 | &lrc::api::NewAccountModel::exportOnRingEnded, |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame^] | 75 | [self] (const QString &accountID, lrc::api::account::ExportOnRingStatus status, const QString &pin){ |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 76 | if(accountID.compare(self.selectedAccountID) != 0) { |
| 77 | return; |
| 78 | } |
| 79 | switch (status) { |
| 80 | case lrc::api::account::ExportOnRingStatus::SUCCESS: { |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame^] | 81 | NSString *nsPin = pin.toNSString(); |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 82 | NSLog(@"Export ended with Success, pin is %@",nsPin); |
| 83 | [resultField setAttributedStringValue:[self formatPinMessage:nsPin]]; |
| 84 | [self showFinal]; |
Alexandre Lision | 45f1f54 | 2016-08-25 15:16:17 -0400 | [diff] [blame] | 85 | } |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 86 | 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 Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame^] | 105 | self.accountModel->exportOnRing(self.selectedAccountID, QString::fromNSString(password)); |
Alexandre Lision | 45f1f54 | 2016-08-25 15:16:17 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | //TODO: Move String formatting to a dedicated Utility Classes |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 109 | - (NSAttributedString*) formatPinMessage:(NSString*) pin |
Alexandre Lision | 45f1f54 | 2016-08-25 15:16:17 -0400 | [diff] [blame] | 110 | { |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 111 | NSMutableAttributedString* thePin = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"\n%@\n", pin]]; |
Alexandre Lision | 45f1f54 | 2016-08-25 15:16:17 -0400 | [diff] [blame] | 112 | [thePin beginEditing]; |
| 113 | NSRange range = NSMakeRange(0, [thePin length]); |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 114 | [thePin addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Helvetica-Bold" size:20.0] range:range]; |
Alexandre Lision | 45f1f54 | 2016-08-25 15:16:17 -0400 | [diff] [blame] | 115 | |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 116 | NSMutableParagraphStyle* mutParaStyle=[[NSMutableParagraphStyle alloc] init]; |
| 117 | [mutParaStyle setAlignment:NSCenterTextAlignment]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 118 | |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 119 | [thePin addAttributes:[NSDictionary dictionaryWithObject:mutParaStyle forKey:NSParagraphStyleAttributeName] range:range]; |
| 120 | |
Kateryna Kostiuk | 85a334e | 2018-12-03 15:54:19 -0500 | [diff] [blame] | 121 | 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 Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 122 | [thePin appendAttributedString:infos]; |
| 123 | [thePin endEditing]; |
| 124 | |
| 125 | return thePin; |
Alexandre Lision | 45f1f54 | 2016-08-25 15:16:17 -0400 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @end |