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