Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
| 3 | * Author: Loïc Siret <loic.siret@savoirfairelinux.com> |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 4 | * Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com> |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | #import "RingWizardNewAccountVC.h" |
| 22 | |
| 23 | |
| 24 | //Cocoa |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 25 | #import <Quartz/Quartz.h> |
Kateryna Kostiuk | 91b44e3 | 2018-09-28 17:08:02 -0400 | [diff] [blame] | 26 | #import <AVFoundation/AVFoundation.h> |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 27 | |
| 28 | //Qt |
| 29 | #import <QUrl> |
| 30 | #import <QPixmap> |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 31 | #import <QSize> |
| 32 | #import <QtMacExtras/qmacfunctions.h> |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 33 | |
| 34 | //LRC |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 35 | #import <api/lrc.h> |
| 36 | #import <api/newaccountmodel.h> |
Kateryna Kostiuk | be7e2e4 | 2019-07-26 17:44:12 -0400 | [diff] [blame] | 37 | #import <api/account.h> |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 38 | #import <interfaces/pixmapmanipulatori.h> |
Kateryna Kostiuk | be7e2e4 | 2019-07-26 17:44:12 -0400 | [diff] [blame] | 39 | #import <namedirectory.h> |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 40 | |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 41 | #import "Constants.h" |
| 42 | #import "views/NSImage+Extensions.h" |
| 43 | #import "views/NSColor+RingTheme.h" |
Andreas Traczyk | 92c101e | 2018-07-03 14:43:53 -0400 | [diff] [blame] | 44 | #import "utils.h" |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 45 | |
| 46 | @interface RingWizardNewAccountVC () |
| 47 | @end |
| 48 | |
| 49 | @implementation RingWizardNewAccountVC |
| 50 | { |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 51 | __unsafe_unretained IBOutlet NSView* loadingView; |
| 52 | __unsafe_unretained IBOutlet NSView* creationView; |
| 53 | |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 54 | __unsafe_unretained IBOutlet NSButton* photoView; |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 55 | __unsafe_unretained IBOutlet NSTextField* displayNameField; |
| 56 | __unsafe_unretained IBOutlet NSTextField* registeredNameField; |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 57 | __unsafe_unretained IBOutlet NSTextField* registeredNameError; |
| 58 | __unsafe_unretained IBOutlet NSTextField* passwordError; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 59 | __unsafe_unretained IBOutlet NSSecureTextField* passwordField; |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 60 | __unsafe_unretained IBOutlet NSSecureTextField* passwordRepeatField; |
Kateryna Kostiuk | 87ae2bf | 2018-05-04 13:46:17 -0400 | [diff] [blame] | 61 | __unsafe_unretained IBOutlet NSImageView* addProfilePhotoImage; |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 62 | |
| 63 | __unsafe_unretained IBOutlet NSProgressIndicator* progressBar; |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 64 | |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 65 | __unsafe_unretained IBOutlet NSProgressIndicator* indicatorLookupResult; |
| 66 | |
| 67 | __unsafe_unretained IBOutlet NSPopover* helpBlockchainContainer; |
| 68 | __unsafe_unretained IBOutlet NSPopover* helpPasswordContainer; |
Kateryna Kostiuk | 23222fe | 2018-11-16 14:28:02 -0500 | [diff] [blame] | 69 | __unsafe_unretained IBOutlet NSLayoutConstraint* buttonTopConstraint; |
Kateryna Kostiuk | 23222fe | 2018-11-16 14:28:02 -0500 | [diff] [blame] | 70 | __unsafe_unretained IBOutlet NSButton* passwordButton; |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 71 | __unsafe_unretained IBOutlet NSStackView* repeatPasswordView; |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 72 | |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 73 | QMetaObject::Connection registeredNameFound; |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 74 | QMetaObject::Connection accountCreated; |
| 75 | QMetaObject::Connection accountRemoved; |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 76 | |
| 77 | BOOL lookupQueued; |
| 78 | NSString* usernameWaitingForLookupResult; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 79 | QString accountToCreate; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 80 | } |
| 81 | |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 82 | NSInteger const DISPLAY_NAME_TAG = 1; |
| 83 | NSInteger const BLOCKCHAIN_NAME_TAG = 2; |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 84 | NSInteger const PASSWORD_TAG = 3; |
| 85 | NSInteger const REPEAT_PASSWORD_TAG = 4; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 86 | |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 87 | //ERROR CODE for textfields validations |
| 88 | NSInteger const ERROR_PASSWORD_TOO_SHORT = -1; |
| 89 | NSInteger const ERROR_REPEAT_MISMATCH = -2; |
| 90 | |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 91 | @synthesize accountModel; |
| 92 | |
| 93 | -(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel { |
| 94 | if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) |
| 95 | { |
| 96 | self.accountModel = accountModel; |
| 97 | } |
| 98 | return self; |
| 99 | } |
| 100 | |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 101 | - (BOOL)produceError:(NSError**)error withCode:(NSInteger)code andMessage:(NSString*)message |
| 102 | { |
| 103 | if (error != NULL){ |
| 104 | NSDictionary *errorDetail = @{NSLocalizedDescriptionKey: message}; |
| 105 | *error = [NSError errorWithDomain:@"Input" code:code userInfo:errorDetail]; |
| 106 | } |
| 107 | return NO; |
| 108 | } |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 109 | |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 110 | - (IBAction)showBlockchainHelp:(id)sender |
| 111 | { |
| 112 | [helpBlockchainContainer showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMaxYEdge]; |
| 113 | } |
| 114 | |
| 115 | - (IBAction)showPasswordHelp:(id)sender |
| 116 | { |
| 117 | [helpPasswordContainer showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMaxYEdge]; |
| 118 | } |
| 119 | |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 120 | - (void)prepareViewToShow { |
| 121 | [self.view setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable]; |
| 122 | [creationView setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable]; |
Kateryna Kostiuk | 61d4116 | 2019-10-25 16:51:44 -0400 | [diff] [blame] | 123 | [loadingView setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable]; |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 124 | [passwordField setHidden: YES]; |
| 125 | [repeatPasswordView setHidden: YES]; |
| 126 | buttonTopConstraint.constant = 35; |
| 127 | } |
| 128 | |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 129 | - (void)show |
| 130 | { |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 131 | [displayNameField setTag:DISPLAY_NAME_TAG]; |
| 132 | [registeredNameField setTag:BLOCKCHAIN_NAME_TAG]; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 133 | [photoView setWantsLayer: YES]; |
| 134 | photoView.layer.cornerRadius = photoView.frame.size.width / 2; |
| 135 | photoView.layer.masksToBounds = YES; |
Alexandre Lision | c2ad639 | 2016-11-08 11:20:34 -0500 | [diff] [blame] | 136 | self.signUpBlockchainState = YES; |
| 137 | [self toggleSignupRing:nil]; |
Kateryna Kostiuk | 87ae2bf | 2018-05-04 13:46:17 -0400 | [diff] [blame] | 138 | [addProfilePhotoImage setWantsLayer: YES]; |
| 139 | [photoView setBordered:YES]; |
Kateryna Kostiuk | 23222fe | 2018-11-16 14:28:02 -0500 | [diff] [blame] | 140 | [passwordButton setState: NSControlStateValueOff]; |
Kateryna Kostiuk | 23222fe | 2018-11-16 14:28:02 -0500 | [diff] [blame] | 141 | self.registeredName = @""; |
| 142 | self.password = @""; |
| 143 | self.repeatPassword = @""; |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 144 | [self display:creationView]; |
| 145 | } |
| 146 | |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 147 | - (void)display:(NSView *)view |
| 148 | { |
| 149 | [self.delegate showView:view]; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | - (IBAction)editPhoto:(id)sender |
| 153 | { |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 154 | auto pictureTaker = [IKPictureTaker pictureTaker]; |
Kateryna Kostiuk | dc56324 | 2019-08-19 12:05:54 -0400 | [diff] [blame] | 155 | #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101400 |
Kateryna Kostiuk | 91b44e3 | 2018-09-28 17:08:02 -0400 | [diff] [blame] | 156 | if (@available(macOS 10.14, *)) { |
| 157 | AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; |
| 158 | if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) |
| 159 | { |
| 160 | [pictureTaker setValue:0 forKey:IKPictureTakerAllowsVideoCaptureKey]; |
| 161 | } |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 162 | |
Kateryna Kostiuk | 91b44e3 | 2018-09-28 17:08:02 -0400 | [diff] [blame] | 163 | if(authStatus == AVAuthorizationStatusNotDetermined) |
| 164 | { |
| 165 | [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { |
| 166 | if(!granted){ |
| 167 | [pictureTaker setValue:0 forKey:IKPictureTakerAllowsVideoCaptureKey]; |
| 168 | } |
| 169 | }]; |
| 170 | } |
| 171 | } |
Kateryna Kostiuk | dc56324 | 2019-08-19 12:05:54 -0400 | [diff] [blame] | 172 | #endif |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 173 | [pictureTaker beginPictureTakerSheetForWindow:[self.delegate window] |
| 174 | withDelegate:self |
| 175 | didEndSelector:@selector(pictureTakerDidEnd:returnCode:contextInfo:) |
| 176 | contextInfo:nil]; |
| 177 | |
| 178 | } |
| 179 | |
| 180 | - (void)pictureTakerDidEnd:(IKPictureTaker *) picker |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 181 | returnCode:(NSInteger) code |
| 182 | contextInfo:(void*) contextInfo |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 183 | { |
Kateryna Kostiuk | 256814e | 2018-09-04 14:47:33 -0400 | [diff] [blame] | 184 | //do nothing when editing canceled |
| 185 | if (code == 0) { |
| 186 | return; |
| 187 | } |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 188 | if (auto outputImage = [picker outputImage]) { |
Kateryna Kostiuk | 87ae2bf | 2018-05-04 13:46:17 -0400 | [diff] [blame] | 189 | [photoView setBordered:NO]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 190 | auto image = [picker inputImage]; |
Kateryna Kostiuk | 2f2ef95 | 2019-05-07 11:04:41 -0400 | [diff] [blame] | 191 | CGFloat newSize = MIN(MIN(image.size.height, image.size.width), MAX_IMAGE_SIZE); |
| 192 | outputImage = [outputImage imageResizeInsideMax: newSize]; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 193 | [photoView setImage:outputImage]; |
Kateryna Kostiuk | 87ae2bf | 2018-05-04 13:46:17 -0400 | [diff] [blame] | 194 | [addProfilePhotoImage setHidden:YES]; |
| 195 | } else if(!photoView.image) { |
| 196 | [photoView setBordered:YES]; |
| 197 | [addProfilePhotoImage setHidden:NO]; |
Alexandre Lision | c2ad639 | 2016-11-08 11:20:34 -0500 | [diff] [blame] | 198 | } |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 199 | } |
| 200 | |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 201 | #pragma mark - Input validation |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 202 | |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 203 | - (BOOL)isPasswordValid |
| 204 | { |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 205 | return self.password.length >= 6 || self.password.length == 0; |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | - (BOOL)isRepeatPasswordValid |
| 209 | { |
Anthony Léonard | 24110e8 | 2017-09-15 16:29:11 -0400 | [diff] [blame] | 210 | return [self.password isEqualToString:self.repeatPassword] || ([self.password length] == 0 && [self.repeatPassword length] == 0); |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | - (BOOL)validateRepeatPassword:(NSError **)error |
| 214 | { |
| 215 | if (!self.isRepeatPasswordValid){ |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 216 | passwordError.stringValue = NSLocalizedString(@"Passwords don't match", |
| 217 | @"Indication for user"); |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 218 | return [self produceError:error |
| 219 | withCode:ERROR_REPEAT_MISMATCH |
| 220 | andMessage:NSLocalizedString(@"Passwords don't match", |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 221 | @"Indication for user")]; |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 222 | } |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 223 | passwordError.stringValue = @""; |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 224 | return YES; |
| 225 | } |
| 226 | |
| 227 | - (BOOL)validatePassword:(NSError **)error |
| 228 | { |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 229 | if (!self.isPasswordValid){ |
| 230 | passwordError.stringValue = NSLocalizedString(@"Password is too short", |
| 231 | @"Indication for user"); |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 232 | return [self produceError:error |
| 233 | withCode:ERROR_PASSWORD_TOO_SHORT |
| 234 | andMessage:NSLocalizedString(@"Password is too short", |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 235 | @"Indication for user")]; |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 236 | } |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 237 | passwordError.stringValue = @""; |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 238 | return YES; |
| 239 | } |
| 240 | |
| 241 | - (BOOL)validateUserInputPassword:(NSError **)error |
| 242 | { |
| 243 | return [self validatePassword:error] && [self validateRepeatPassword:error]; |
| 244 | } |
| 245 | |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 246 | - (IBAction)createRingAccount:(id)sender |
| 247 | { |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 248 | QObject::disconnect(accountCreated); |
| 249 | QObject::disconnect(accountRemoved); |
| 250 | accountCreated = QObject::connect(self.accountModel, |
| 251 | &lrc::api::NewAccountModel::accountAdded, |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 252 | [self] (const QString& accountID) { |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 253 | if(accountID.compare(accountToCreate) != 0) { |
| 254 | return; |
| 255 | } |
| 256 | QObject::disconnect(accountCreated); |
| 257 | QObject::disconnect(accountRemoved); |
| 258 | //set account avatar |
| 259 | if([photoView image]) { |
| 260 | NSImage *avatarImage = [photoView image]; |
| 261 | auto imageToBytes = QByteArray::fromNSData([avatarImage TIFFRepresentation]).toBase64(); |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 262 | self.accountModel->setAvatar(accountID, QString(imageToBytes)); |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 263 | } |
| 264 | //register username |
| 265 | if (self.registeredName && ![self.registeredName isEqualToString:@""]) { |
| 266 | NSString *passwordString = self.password ? self.password: @""; |
| 267 | NSString *usernameString = self.registeredName; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 268 | self.accountModel->registerName(accountID, |
| 269 | QString::fromNSString(passwordString), |
| 270 | QString::fromNSString(usernameString)); |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 271 | } |
Kateryna Kostiuk | 0c0801e | 2019-07-18 20:10:51 -0400 | [diff] [blame] | 272 | lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(accountID); |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 273 | accountProperties.Ringtone.ringtonePath = QString::fromNSString(defaultRingtonePath()); |
Kateryna Kostiuk | 0c0801e | 2019-07-18 20:10:51 -0400 | [diff] [blame] | 274 | self.accountModel->setAccountConfig(accountID, accountProperties); |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 275 | [self registerDefaultPreferences]; |
Kateryna Kostiuk | 61d4116 | 2019-10-25 16:51:44 -0400 | [diff] [blame] | 276 | [self.delegate didCreateAccountWithSuccess:YES accountId: accountToCreate]; |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 277 | }); |
| 278 | //if account creation failed remove loading view |
| 279 | accountRemoved = QObject::connect(self.accountModel, |
| 280 | &lrc::api::NewAccountModel::accountRemoved, |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 281 | [self] (const QString& accountID) { |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 282 | if(accountID.compare(accountToCreate) != 0) { |
| 283 | return; |
| 284 | } |
| 285 | QObject::disconnect(accountCreated); |
| 286 | QObject::disconnect(accountRemoved); |
Kateryna Kostiuk | 61d4116 | 2019-10-25 16:51:44 -0400 | [diff] [blame] | 287 | [self.delegate didCreateAccountWithSuccess:NO accountId: accountToCreate]; |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 288 | }); |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 289 | [self display:loadingView]; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 290 | [progressBar startAnimation:nil]; |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 291 | |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 292 | accountToCreate = self.accountModel->createNewAccount(lrc::api::profile::Type::RING, QString::fromNSString(displayNameField.stringValue),"",QString::fromNSString(passwordField.stringValue), ""); |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Set default values for preferences |
| 297 | */ |
| 298 | - (void)registerDefaultPreferences |
| 299 | { |
Andreas Traczyk | 92c101e | 2018-07-03 14:43:53 -0400 | [diff] [blame] | 300 | if (!appSandboxed()) { |
| 301 | // enable AutoStartup |
| 302 | LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); |
| 303 | if (loginItemsRef == nil) return; |
| 304 | CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; |
| 305 | LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL); |
| 306 | if (itemRef) CFRelease(itemRef); |
| 307 | } |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 308 | |
| 309 | // enable Notifications |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 310 | [[NSUserDefaults standardUserDefaults] setBool:YES forKey:Preferences::CallNotifications]; |
| 311 | [[NSUserDefaults standardUserDefaults] setBool:YES forKey:Preferences::MessagesNotifications]; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 312 | } |
| 313 | |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 314 | - (IBAction)cancel:(id)sender |
| 315 | { |
Kateryna Kostiuk | 61d4116 | 2019-10-25 16:51:44 -0400 | [diff] [blame] | 316 | [self.delegate didCreateAccountWithSuccess:NO accountId: accountToCreate]; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 317 | } |
| 318 | |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 319 | #pragma mark - UserNameRegistration delegate methods |
| 320 | |
| 321 | - (IBAction)toggleSignupRing:(id)sender |
| 322 | { |
| 323 | if (self.withBlockchain) { |
| 324 | [self lookupUserName]; |
| 325 | } |
| 326 | } |
| 327 | |
Kateryna Kostiuk | 23222fe | 2018-11-16 14:28:02 -0500 | [diff] [blame] | 328 | - (IBAction)togglePasswordButton:(NSButton *)sender |
| 329 | { |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 330 | [passwordField setHidden: !passwordField.hidden]; |
| 331 | [repeatPasswordView setHidden: !repeatPasswordView.hidden]; |
| 332 | buttonTopConstraint.constant = repeatPasswordView.hidden ? 35 : 25; |
| 333 | [self display:creationView]; |
Kateryna Kostiuk | 23222fe | 2018-11-16 14:28:02 -0500 | [diff] [blame] | 334 | } |
| 335 | |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 336 | - (BOOL)withBlockchain |
| 337 | { |
| 338 | return self.signUpBlockchainState == NSOnState; |
| 339 | } |
| 340 | |
| 341 | - (BOOL)userNameAvailableORNotBlockchain |
| 342 | { |
| 343 | return !self.withBlockchain || (self.registeredName.length > 0 && self.isUserNameAvailable); |
| 344 | } |
| 345 | |
| 346 | - (void)showLookUpAvailable:(BOOL)available andText:(NSString *)message |
| 347 | { |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 348 | if (registeredNameField.stringValue.length > 0) { |
| 349 | registeredNameError.stringValue = message; |
| 350 | } |
| 351 | [indicatorLookupResult setHidden:YES]; |
| 352 | [indicatorLookupResult stopAnimation:nil]; |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | - (void)onUsernameAvailabilityChangedWithNewAvailability:(BOOL)newAvailability |
| 356 | { |
| 357 | self.isUserNameAvailable = newAvailability; |
| 358 | } |
| 359 | |
| 360 | - (void)hideLookupSpinner |
| 361 | { |
| 362 | [indicatorLookupResult setHidden:YES]; |
| 363 | } |
| 364 | |
| 365 | - (void)showLookupSpinner |
| 366 | { |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 367 | registeredNameError.stringValue = @""; |
| 368 | if (registeredNameField.stringValue.length > 0) { |
| 369 | [indicatorLookupResult setHidden:NO]; |
| 370 | [indicatorLookupResult startAnimation:nil]; |
| 371 | } |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | - (BOOL)lookupUserName |
| 375 | { |
| 376 | [self showLookupSpinner]; |
| 377 | QObject::disconnect(registeredNameFound); |
| 378 | registeredNameFound = QObject::connect( |
| 379 | &NameDirectory::instance(), |
| 380 | &NameDirectory::registeredNameFound, |
Kateryna Kostiuk | be7e2e4 | 2019-07-26 17:44:12 -0400 | [diff] [blame] | 381 | [=] (NameDirectory::LookupStatus status, |
| 382 | const QString& address, const QString& name) { |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 383 | NSLog(@"Name lookup ended"); |
| 384 | lookupQueued = NO; |
| 385 | //If this is the username we are waiting for, we can disconnect. |
| 386 | if (name.compare(QString::fromNSString(usernameWaitingForLookupResult)) == 0) { |
| 387 | QObject::disconnect(registeredNameFound); |
| 388 | } else { |
| 389 | //Keep waiting... |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | //We may now stop the spinner |
| 394 | [self hideLookupSpinner]; |
| 395 | |
| 396 | BOOL isAvailable = NO; |
| 397 | NSString* message; |
| 398 | switch(status) |
| 399 | { |
| 400 | case NameDirectory::LookupStatus::SUCCESS: |
| 401 | { |
| 402 | message = NSLocalizedString(@"The entered username is not available", |
| 403 | @"Text shown to user when his username is already registered"); |
| 404 | isAvailable = NO; |
| 405 | break; |
| 406 | } |
| 407 | case NameDirectory::LookupStatus::NOT_FOUND: |
| 408 | { |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 409 | message = @""; |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 410 | isAvailable = YES; |
| 411 | break; |
| 412 | } |
| 413 | case NameDirectory::LookupStatus::INVALID_NAME: |
| 414 | { |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 415 | message = NSLocalizedString(@"Invalid username.", |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 416 | @"Text shown to user when his username is invalid to be registered"); |
| 417 | isAvailable = NO; |
| 418 | break; |
| 419 | } |
| 420 | case NameDirectory::LookupStatus::ERROR: |
| 421 | default: |
| 422 | { |
| 423 | message = NSLocalizedString(@"Failed to perform lookup", |
| 424 | @"Text shown to user when an error occur at registration"); |
| 425 | isAvailable = NO; |
| 426 | break; |
| 427 | } |
| 428 | } |
| 429 | [self showLookUpAvailable:isAvailable andText: message]; |
| 430 | [self onUsernameAvailabilityChangedWithNewAvailability:isAvailable]; |
| 431 | |
| 432 | }); |
| 433 | |
| 434 | //Start the lookup in a second so that the UI dosen't seem to freeze |
Kateryna Kostiuk | be7e2e4 | 2019-07-26 17:44:12 -0400 | [diff] [blame] | 435 | BOOL result = NameDirectory::instance().lookupName(QString(), QString::fromNSString(usernameWaitingForLookupResult)); |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 436 | |
| 437 | } |
| 438 | |
Alexandre Lision | 5dc5d31 | 2016-11-10 10:41:37 -0500 | [diff] [blame] | 439 | #pragma mark - NSTextFieldDelegate delegate methods |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 440 | |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 441 | - (void)controlTextDidChange:(NSNotification *)notif |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 442 | { |
| 443 | NSTextField* textField = [notif object]; |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 444 | if (textField.tag == PASSWORD_TAG) { |
| 445 | NSError *error = nil; |
| 446 | [self validatePassword: &error]; |
| 447 | return; |
| 448 | } |
| 449 | if (textField.tag == REPEAT_PASSWORD_TAG) { |
| 450 | NSError *error = nil; |
| 451 | [self validateRepeatPassword: &error]; |
| 452 | return; |
| 453 | } |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 454 | if (textField.tag != BLOCKCHAIN_NAME_TAG) { |
| 455 | return; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 456 | } |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 457 | NSString* alias = textField.stringValue; |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 458 | [self showLookupSpinner]; |
| 459 | [self onUsernameAvailabilityChangedWithNewAvailability:NO]; |
| 460 | [NSObject cancelPreviousPerformRequestsWithTarget:self]; |
| 461 | [self performSelector:@selector(lookUp:) withObject:alias afterDelay:0.5]; |
| 462 | } |
| 463 | |
| 464 | - (void) lookUp:(NSString*) name |
| 465 | { |
| 466 | if (self.withBlockchain && !lookupQueued) { |
| 467 | usernameWaitingForLookupResult = name; |
| 468 | lookupQueued = YES; |
| 469 | [self lookupUserName]; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | |
| 474 | + (NSSet *)keyPathsForValuesAffectingUserNameAvailableORNotBlockchain |
| 475 | { |
| 476 | return [NSSet setWithObjects: NSStringFromSelector(@selector(signUpBlockchainState)), |
| 477 | NSStringFromSelector(@selector(isUserNameAvailable)), |
| 478 | nil]; |
| 479 | } |
| 480 | |
| 481 | + (NSSet *)keyPathsForValuesAffectingWithBlockchain |
| 482 | { |
| 483 | return [NSSet setWithObjects: NSStringFromSelector(@selector(signUpBlockchainState)), |
| 484 | nil]; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 485 | } |
| 486 | |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 487 | + (NSSet *)keyPathsForValuesAffectingIsPasswordValid |
| 488 | { |
| 489 | return [NSSet setWithObjects:@"password", nil]; |
| 490 | } |
| 491 | |
| 492 | + (NSSet *)keyPathsForValuesAffectingIsRepeatPasswordValid |
| 493 | { |
| 494 | return [NSSet setWithObjects:@"password", @"repeatPassword", nil]; |
| 495 | } |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 496 | @end |