blob: 56e737b46613948f2fd16b376ca44d75992e161c [file] [log] [blame]
Alexandre Lision624b1a82016-09-11 19:29:01 -04001/*
Sébastien Blin029ffa82019-01-02 17:43:48 -05002 * Copyright (C) 2016-2019 Savoir-faire Linux Inc.
Alexandre Lision624b1a82016-09-11 19:29:01 -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
20#import "MigrateRingAccountsWC.h"
21
22//LRC
23#import <accountmodel.h>
24
25//RING
26#import "views/ITProgressIndicator.h"
27
28@interface MigrateRingAccountsWC() <NSTextFieldDelegate>{
29 __unsafe_unretained IBOutlet NSSecureTextField* passwordField;
30 __unsafe_unretained IBOutlet NSSecureTextField* passwordConfirmField;
31 __unsafe_unretained IBOutlet NSTextField* infoField;
32 __unsafe_unretained IBOutlet NSTextField* errorField;
33 __unsafe_unretained IBOutlet ITProgressIndicator* progressIndicator;
34}
35
36- (IBAction)onClickComplete:(id)sender;
37@end
38
39@implementation MigrateRingAccountsWC{
40 struct {
41 unsigned int didComplete:1;
42 unsigned int didCompleteWithError:1;
43 } delegateRespondsTo;
44}
45
46NSTimer* errorTimer;
47QMetaObject::Connection stateChanged;
48
49#pragma mark - Initialise / Setters
50- (id)initWithDelegate:(id <MigrateRingAccountsDelegate>) del actionCode:(NSInteger) code
51{
52 return [super initWithWindowNibName:@"MigrateRingAccountsWindow" delegate:del actionCode:code];
53}
54
55- (void) awakeFromNib{
56 [self setInfoMessageForAccount];
57}
58
59- (void)setDelegate:(id <MigrateRingAccountsDelegate>)aDelegate
60{
61 if (super.delegate != aDelegate) {
62 [super setDelegate: aDelegate];
63 delegateRespondsTo.didCompleteWithError = [self.delegate respondsToSelector:@selector(migrationDidCompleteWithError)];
64 delegateRespondsTo.didComplete = [self.delegate respondsToSelector:@selector(migrationDidComplete)];
65 }
66}
67
68- (void) setAccount:(Account *)aAccount
69{
70 _account = aAccount;
71}
72
73- (void) setInfoMessageForAccount
74{
75 NSMutableAttributedString* infoMessage = [[NSMutableAttributedString alloc] initWithString:NSLocalizedString(@"The following account needs to be migrated to the new Ring account format:",@"Text shown to the user")];
76 [infoMessage appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
77 [infoMessage appendAttributedString:[[NSAttributedString alloc] initWithString:NSLocalizedString(@"Alias : ",@"Text shown to the user")]];
78 const CGFloat fontSize = 13;
79 NSDictionary *attrs = @{
80 NSFontAttributeName:[NSFont boldSystemFontOfSize:fontSize]
81 };
82 [infoMessage appendAttributedString:[[NSAttributedString alloc] initWithString:self.account->alias().toNSString() attributes:attrs]];
83 [infoMessage appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
Alexandre Lision8b3850a2016-11-06 12:06:38 -050084 [infoMessage appendAttributedString:[[NSAttributedString alloc] initWithString:NSLocalizedString(@"RingID : ",@"Text shown to the user")]];
85 [infoMessage appendAttributedString:[[NSAttributedString alloc] initWithString:self.account->username().toNSString() attributes:attrs]];
Alexandre Lision624b1a82016-09-11 19:29:01 -040086 [infoMessage appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
87 [infoMessage appendAttributedString:[[NSAttributedString alloc] initWithString:NSLocalizedString(@"To proceed with the migration, you must choose a password for your account. This password will be used to encrypt your master key. It will be required for adding new devices to your Ring account. If you are not ready to choose a password, you may close Ring and resume the migration later.",@"Text shown to the user")]];
88 [infoField setAttributedStringValue:infoMessage];
89}
90
91
92- (void) showError:(NSString*) errorMessage
93{
94 [errorField setStringValue:errorMessage];
95 [super showError];
96}
97
98- (void)showLoading
99{
100 [progressIndicator setNumberOfLines:30];
101 [progressIndicator setWidthOfLine:2];
102 [progressIndicator setLengthOfLine:5];
103 [progressIndicator setInnerMargin:20];
104 [super showLoading];
105}
106
107#pragma mark - Events Handlers
108- (IBAction)removeAccount:(id)sender
109{
110 AccountModel::instance().remove(self.account);
111 AccountModel::instance().save();
112 [self cancelPressed:sender];
113}
114
115- (IBAction)startMigration:(NSButton *)sender
116{
117 if (![self validatePasswords]) {
118 [self showError:NSLocalizedString(@"Password and confirmation mismatch.",@"Text show to the user when password didn't match")];
119 } else {
120 [self showLoading];
121 errorTimer = [NSTimer scheduledTimerWithTimeInterval:30
122 target:self
123 selector:@selector(didCompleteWithError) userInfo:nil
124 repeats:NO];
125 stateChanged = QObject::connect(
126 self.account, &Account::stateChanged,
127 [=](Account::RegistrationState state){
128 switch(state){
129 case Account::RegistrationState::READY:
130 case Account::RegistrationState::TRYING:
131 case Account::RegistrationState::UNREGISTERED:{
132 self.account<< Account::EditAction::RELOAD;
133 QObject::disconnect(stateChanged);
134 [self didComplete];
135 break;
136 }
137 case Account::RegistrationState::ERROR:
138 case Account::RegistrationState::INITIALIZING:
139 case Account::RegistrationState::COUNT__:{
140 //DO Nothing
141 break;
142 }
143 }
144 });
145 self.account->setArchivePassword(QString::fromNSString(self.password));
146 self.account->performAction(Account::EditAction::SAVE);
147 }
148}
149
150- (BOOL)validatePasswords
151{
152 BOOL result = (self.password.length != 0 && [self.password isEqualToString:self.passwordConfirmation]);
153 NSLog(@"ValidatesPasswords : %s", result ? "true" : "false");
154 return result;
155}
156
157+ (NSSet *)keyPathsForValuesAffectingValidatePasswords
158{
159 return [NSSet setWithObjects:@"password", @"passwordConfirmation", nil];
160}
161
162
163#pragma mark - Delegates
164- (void)didComplete
165{
166 [errorTimer invalidate];
167 errorTimer = nil;
168 [self showFinal];
169}
170
171- (void)onClickComplete:(id)sender
172{
173 [self cancelPressed:sender];
174 if (delegateRespondsTo.didComplete)
175 [((id<MigrateRingAccountsDelegate>)self.delegate) migrationDidComplete];
176}
177
178
179- (void)didCompleteWithError
180{
181 [self showError:NSLocalizedString(@"Failed to migrate your account. You can retry by pressing Ok or delete your account.",@"Error message shown to user when it is impossible to migrate account")];
182}
183
184
185@end