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> |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 37 | #import <account.h> |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 38 | #import <interfaces/pixmapmanipulatori.h> |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 39 | |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 40 | #import "Constants.h" |
| 41 | #import "views/NSImage+Extensions.h" |
| 42 | #import "views/NSColor+RingTheme.h" |
Andreas Traczyk | 92c101e | 2018-07-03 14:43:53 -0400 | [diff] [blame] | 43 | #import "utils.h" |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 44 | |
| 45 | @interface RingWizardNewAccountVC () |
| 46 | @end |
| 47 | |
| 48 | @implementation RingWizardNewAccountVC |
| 49 | { |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 50 | __unsafe_unretained IBOutlet NSView* loadingView; |
| 51 | __unsafe_unretained IBOutlet NSView* creationView; |
| 52 | |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 53 | __unsafe_unretained IBOutlet NSButton* photoView; |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 54 | __unsafe_unretained IBOutlet NSTextField* displayNameField; |
| 55 | __unsafe_unretained IBOutlet NSTextField* registeredNameField; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 56 | __unsafe_unretained IBOutlet NSSecureTextField* passwordField; |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 57 | __unsafe_unretained IBOutlet NSSecureTextField* passwordRepeatField; |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 58 | __unsafe_unretained IBOutlet NSImageView* passwordCheck; |
| 59 | __unsafe_unretained IBOutlet NSImageView* passwordRepeatCheck; |
Kateryna Kostiuk | 87ae2bf | 2018-05-04 13:46:17 -0400 | [diff] [blame] | 60 | __unsafe_unretained IBOutlet NSImageView* addProfilePhotoImage; |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 61 | |
| 62 | __unsafe_unretained IBOutlet NSProgressIndicator* progressBar; |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 63 | |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 64 | __unsafe_unretained IBOutlet NSImageView* ivLookupResult; |
| 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; |
| 70 | __unsafe_unretained IBOutlet NSBox* passwordBox; |
| 71 | __unsafe_unretained IBOutlet NSButton* passwordButton; |
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 | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 79 | std::string 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; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 84 | |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 85 | //ERROR CODE for textfields validations |
| 86 | NSInteger const ERROR_PASSWORD_TOO_SHORT = -1; |
| 87 | NSInteger const ERROR_REPEAT_MISMATCH = -2; |
| 88 | |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 89 | @synthesize accountModel; |
| 90 | |
Kateryna Kostiuk | 23222fe | 2018-11-16 14:28:02 -0500 | [diff] [blame] | 91 | #define heightWithCancelAndAdvanced 468 |
| 92 | #define defaultHeight 408 |
| 93 | |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 94 | -(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel { |
| 95 | if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) |
| 96 | { |
| 97 | self.accountModel = accountModel; |
| 98 | } |
| 99 | return self; |
| 100 | } |
| 101 | |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 102 | - (BOOL)produceError:(NSError**)error withCode:(NSInteger)code andMessage:(NSString*)message |
| 103 | { |
| 104 | if (error != NULL){ |
| 105 | NSDictionary *errorDetail = @{NSLocalizedDescriptionKey: message}; |
| 106 | *error = [NSError errorWithDomain:@"Input" code:code userInfo:errorDetail]; |
| 107 | } |
| 108 | return NO; |
| 109 | } |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 110 | |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 111 | - (IBAction)showBlockchainHelp:(id)sender |
| 112 | { |
| 113 | [helpBlockchainContainer showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMaxYEdge]; |
| 114 | } |
| 115 | |
| 116 | - (IBAction)showPasswordHelp:(id)sender |
| 117 | { |
| 118 | [helpPasswordContainer showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMaxYEdge]; |
| 119 | } |
| 120 | |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 121 | - (void)show |
| 122 | { |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 123 | [displayNameField setTag:DISPLAY_NAME_TAG]; |
| 124 | [registeredNameField setTag:BLOCKCHAIN_NAME_TAG]; |
| 125 | [displayNameField setStringValue: NSFullUserName()]; |
| 126 | [self controlTextDidChange:[NSNotification notificationWithName:@"PlaceHolder" object:displayNameField]]; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 127 | [photoView setWantsLayer: YES]; |
| 128 | photoView.layer.cornerRadius = photoView.frame.size.width / 2; |
| 129 | photoView.layer.masksToBounds = YES; |
Alexandre Lision | c2ad639 | 2016-11-08 11:20:34 -0500 | [diff] [blame] | 130 | self.signUpBlockchainState = YES; |
| 131 | [self toggleSignupRing:nil]; |
Kateryna Kostiuk | 87ae2bf | 2018-05-04 13:46:17 -0400 | [diff] [blame] | 132 | [addProfilePhotoImage setWantsLayer: YES]; |
| 133 | [photoView setBordered:YES]; |
Kateryna Kostiuk | 23222fe | 2018-11-16 14:28:02 -0500 | [diff] [blame] | 134 | [passwordButton setState: NSControlStateValueOff]; |
| 135 | NSRect viewFrame = creationView.frame; |
| 136 | viewFrame.size.height = defaultHeight; |
| 137 | creationView.frame = viewFrame; |
| 138 | |
| 139 | buttonTopConstraint.constant = 25; |
| 140 | [passwordBox setHidden: YES]; |
| 141 | self.registeredName = @""; |
| 142 | self.password = @""; |
| 143 | self.repeatPassword = @""; |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 144 | |
| 145 | [self display:creationView]; |
| 146 | } |
| 147 | |
| 148 | - (void)removeSubviews |
| 149 | { |
| 150 | while ([self.view.subviews count] > 0){ |
| 151 | [[self.view.subviews firstObject] removeFromSuperview]; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | - (void)display:(NSView *)view |
| 156 | { |
| 157 | [self.delegate showView:view]; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | - (IBAction)editPhoto:(id)sender |
| 161 | { |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 162 | auto pictureTaker = [IKPictureTaker pictureTaker]; |
Kateryna Kostiuk | 91b44e3 | 2018-09-28 17:08:02 -0400 | [diff] [blame] | 163 | if (@available(macOS 10.14, *)) { |
| 164 | AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; |
| 165 | if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) |
| 166 | { |
| 167 | [pictureTaker setValue:0 forKey:IKPictureTakerAllowsVideoCaptureKey]; |
| 168 | } |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 169 | |
Kateryna Kostiuk | 91b44e3 | 2018-09-28 17:08:02 -0400 | [diff] [blame] | 170 | if(authStatus == AVAuthorizationStatusNotDetermined) |
| 171 | { |
| 172 | [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { |
| 173 | if(!granted){ |
| 174 | [pictureTaker setValue:0 forKey:IKPictureTakerAllowsVideoCaptureKey]; |
| 175 | } |
| 176 | }]; |
| 177 | } |
| 178 | } |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 179 | [pictureTaker beginPictureTakerSheetForWindow:[self.delegate window] |
| 180 | withDelegate:self |
| 181 | didEndSelector:@selector(pictureTakerDidEnd:returnCode:contextInfo:) |
| 182 | contextInfo:nil]; |
| 183 | |
| 184 | } |
| 185 | |
| 186 | - (void)pictureTakerDidEnd:(IKPictureTaker *) picker |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 187 | returnCode:(NSInteger) code |
| 188 | contextInfo:(void*) contextInfo |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 189 | { |
Kateryna Kostiuk | 256814e | 2018-09-04 14:47:33 -0400 | [diff] [blame] | 190 | //do nothing when editing canceled |
| 191 | if (code == 0) { |
| 192 | return; |
| 193 | } |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 194 | if (auto outputImage = [picker outputImage]) { |
Kateryna Kostiuk | 87ae2bf | 2018-05-04 13:46:17 -0400 | [diff] [blame] | 195 | [photoView setBordered:NO]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 196 | auto image = [picker inputImage]; |
| 197 | CGFloat newSize = MIN(image.size.height, image.size.width); |
| 198 | outputImage = [outputImage cropImageToSize:CGSizeMake(newSize, newSize)]; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 199 | [photoView setImage:outputImage]; |
Kateryna Kostiuk | 87ae2bf | 2018-05-04 13:46:17 -0400 | [diff] [blame] | 200 | [addProfilePhotoImage setHidden:YES]; |
| 201 | } else if(!photoView.image) { |
| 202 | [photoView setBordered:YES]; |
| 203 | [addProfilePhotoImage setHidden:NO]; |
Alexandre Lision | c2ad639 | 2016-11-08 11:20:34 -0500 | [diff] [blame] | 204 | } |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 205 | } |
| 206 | |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 207 | #pragma mark - Input validation |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 208 | |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 209 | - (BOOL)isPasswordValid |
| 210 | { |
| 211 | return self.password.length >= 6; |
| 212 | } |
| 213 | |
| 214 | - (BOOL)isRepeatPasswordValid |
| 215 | { |
Anthony Léonard | 24110e8 | 2017-09-15 16:29:11 -0400 | [diff] [blame] | 216 | 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] | 217 | } |
| 218 | |
| 219 | - (BOOL)validateRepeatPassword:(NSError **)error |
| 220 | { |
| 221 | if (!self.isRepeatPasswordValid){ |
| 222 | return [self produceError:error |
| 223 | withCode:ERROR_REPEAT_MISMATCH |
| 224 | andMessage:NSLocalizedString(@"Passwords don't match", |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 225 | @"Indication for user")]; |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 226 | } |
| 227 | return YES; |
| 228 | } |
| 229 | |
| 230 | - (BOOL)validatePassword:(NSError **)error |
| 231 | { |
| 232 | if (!self.isRepeatPasswordValid){ |
| 233 | return [self produceError:error |
| 234 | withCode:ERROR_PASSWORD_TOO_SHORT |
| 235 | andMessage:NSLocalizedString(@"Password is too short", |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 236 | @"Indication for user")]; |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 237 | } |
| 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 | { |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 248 | NSError *error = nil; |
| 249 | if (![self validateUserInputPassword:&error]){ |
| 250 | NSAlert* alert = [NSAlert alertWithMessageText:[error localizedDescription] |
| 251 | defaultButton:NSLocalizedString(@"Revise Input", |
| 252 | @"Button title") |
| 253 | alternateButton:nil |
| 254 | otherButton:nil |
| 255 | informativeTextWithFormat:@"%@",error]; |
| 256 | |
| 257 | [alert beginSheetModalForWindow:passwordField.window |
| 258 | modalDelegate:nil |
| 259 | didEndSelector:NULL |
| 260 | contextInfo:NULL]; |
| 261 | |
| 262 | return; |
| 263 | } |
| 264 | |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 265 | QObject::disconnect(accountCreated); |
| 266 | QObject::disconnect(accountRemoved); |
| 267 | accountCreated = QObject::connect(self.accountModel, |
| 268 | &lrc::api::NewAccountModel::accountAdded, |
| 269 | [self] (const std::string& accountID) { |
| 270 | if(accountID.compare(accountToCreate) != 0) { |
| 271 | return; |
| 272 | } |
| 273 | QObject::disconnect(accountCreated); |
| 274 | QObject::disconnect(accountRemoved); |
| 275 | //set account avatar |
| 276 | if([photoView image]) { |
| 277 | NSImage *avatarImage = [photoView image]; |
| 278 | auto imageToBytes = QByteArray::fromNSData([avatarImage TIFFRepresentation]).toBase64(); |
| 279 | std::string imageToString = std::string(imageToBytes.constData(), imageToBytes.length()); |
| 280 | self.accountModel->setAvatar(accountID, imageToString); |
| 281 | } |
| 282 | //register username |
| 283 | if (self.registeredName && ![self.registeredName isEqualToString:@""]) { |
| 284 | NSString *passwordString = self.password ? self.password: @""; |
| 285 | NSString *usernameString = self.registeredName; |
| 286 | self.accountModel->registerName(accountID, [passwordString UTF8String], [usernameString UTF8String]); |
| 287 | } |
| 288 | |
| 289 | [self registerDefaultPreferences]; |
| 290 | [self.delegate didCreateAccountWithSuccess:YES]; |
| 291 | }); |
| 292 | //if account creation failed remove loading view |
| 293 | accountRemoved = QObject::connect(self.accountModel, |
| 294 | &lrc::api::NewAccountModel::accountRemoved, |
| 295 | [self] (const std::string& accountID) { |
| 296 | if(accountID.compare(accountToCreate) != 0) { |
| 297 | return; |
| 298 | } |
| 299 | QObject::disconnect(accountCreated); |
| 300 | QObject::disconnect(accountRemoved); |
| 301 | [self.delegate didCreateAccountWithSuccess:NO]; |
| 302 | }); |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 303 | [self display:loadingView]; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 304 | [progressBar startAnimation:nil]; |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 305 | |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 306 | accountToCreate = self.accountModel->createNewAccount(lrc::api::profile::Type::RING, [displayNameField.stringValue UTF8String],"",[passwordField.stringValue UTF8String], ""); |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Set default values for preferences |
| 311 | */ |
| 312 | - (void)registerDefaultPreferences |
| 313 | { |
Andreas Traczyk | 92c101e | 2018-07-03 14:43:53 -0400 | [diff] [blame] | 314 | if (!appSandboxed()) { |
| 315 | // enable AutoStartup |
| 316 | LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); |
| 317 | if (loginItemsRef == nil) return; |
| 318 | CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; |
| 319 | LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL); |
| 320 | if (itemRef) CFRelease(itemRef); |
| 321 | } |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 322 | |
| 323 | // enable Notifications |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame^] | 324 | [[NSUserDefaults standardUserDefaults] setBool:YES forKey:Preferences::CallNotifications]; |
| 325 | [[NSUserDefaults standardUserDefaults] setBool:YES forKey:Preferences::MessagesNotifications]; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 326 | } |
| 327 | |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 328 | - (IBAction)cancel:(id)sender |
| 329 | { |
| 330 | [self.delegate didCreateAccountWithSuccess:NO]; |
| 331 | } |
| 332 | |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 333 | #pragma mark - UserNameRegistration delegate methods |
| 334 | |
| 335 | - (IBAction)toggleSignupRing:(id)sender |
| 336 | { |
| 337 | if (self.withBlockchain) { |
| 338 | [self lookupUserName]; |
| 339 | } |
| 340 | } |
| 341 | |
Kateryna Kostiuk | 23222fe | 2018-11-16 14:28:02 -0500 | [diff] [blame] | 342 | - (IBAction)togglePasswordButton:(NSButton *)sender |
| 343 | { |
| 344 | NSRect viewFrame = creationView.frame; |
| 345 | if([sender state] == NSControlStateValueOn) { |
| 346 | viewFrame.size.height = heightWithCancelAndAdvanced; |
| 347 | [self.delegate updateFrame: heightWithCancelAndAdvanced]; |
| 348 | creationView.frame = viewFrame; |
| 349 | buttonTopConstraint.constant = 85; |
| 350 | [passwordBox setHidden: NO]; |
| 351 | } else { |
| 352 | buttonTopConstraint.priority = 100; |
| 353 | viewFrame.size.height = defaultHeight; |
| 354 | [self.delegate updateFrame: defaultHeight]; |
| 355 | creationView.frame = viewFrame; |
| 356 | buttonTopConstraint.constant = 25; |
| 357 | buttonTopConstraint.priority = 999; |
| 358 | [passwordBox setHidden: YES]; |
| 359 | } |
| 360 | } |
| 361 | |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 362 | - (BOOL)withBlockchain |
| 363 | { |
| 364 | return self.signUpBlockchainState == NSOnState; |
| 365 | } |
| 366 | |
| 367 | - (BOOL)userNameAvailableORNotBlockchain |
| 368 | { |
| 369 | return !self.withBlockchain || (self.registeredName.length > 0 && self.isUserNameAvailable); |
| 370 | } |
| 371 | |
| 372 | - (void)showLookUpAvailable:(BOOL)available andText:(NSString *)message |
| 373 | { |
| 374 | [ivLookupResult setImage:[NSImage imageNamed:(available?@"ic_action_accept":@"ic_action_cancel")]] ; |
| 375 | [ivLookupResult setHidden:NO]; |
| 376 | [ivLookupResult setToolTip:message]; |
| 377 | } |
| 378 | |
| 379 | - (void)onUsernameAvailabilityChangedWithNewAvailability:(BOOL)newAvailability |
| 380 | { |
| 381 | self.isUserNameAvailable = newAvailability; |
| 382 | } |
| 383 | |
| 384 | - (void)hideLookupSpinner |
| 385 | { |
| 386 | [indicatorLookupResult setHidden:YES]; |
| 387 | } |
| 388 | |
| 389 | - (void)showLookupSpinner |
| 390 | { |
| 391 | [ivLookupResult setHidden:YES]; |
| 392 | [indicatorLookupResult setHidden:NO]; |
| 393 | [indicatorLookupResult startAnimation:nil]; |
| 394 | } |
| 395 | |
| 396 | - (BOOL)lookupUserName |
| 397 | { |
| 398 | [self showLookupSpinner]; |
| 399 | QObject::disconnect(registeredNameFound); |
| 400 | registeredNameFound = QObject::connect( |
| 401 | &NameDirectory::instance(), |
| 402 | &NameDirectory::registeredNameFound, |
| 403 | [=] ( const Account* account, NameDirectory::LookupStatus status, const QString& address, const QString& name) { |
| 404 | NSLog(@"Name lookup ended"); |
| 405 | lookupQueued = NO; |
| 406 | //If this is the username we are waiting for, we can disconnect. |
| 407 | if (name.compare(QString::fromNSString(usernameWaitingForLookupResult)) == 0) { |
| 408 | QObject::disconnect(registeredNameFound); |
| 409 | } else { |
| 410 | //Keep waiting... |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | //We may now stop the spinner |
| 415 | [self hideLookupSpinner]; |
| 416 | |
| 417 | BOOL isAvailable = NO; |
| 418 | NSString* message; |
| 419 | switch(status) |
| 420 | { |
| 421 | case NameDirectory::LookupStatus::SUCCESS: |
| 422 | { |
| 423 | message = NSLocalizedString(@"The entered username is not available", |
| 424 | @"Text shown to user when his username is already registered"); |
| 425 | isAvailable = NO; |
| 426 | break; |
| 427 | } |
| 428 | case NameDirectory::LookupStatus::NOT_FOUND: |
| 429 | { |
| 430 | message = NSLocalizedString(@"The entered username is available", |
| 431 | @"Text shown to user when his username is available to be registered"); |
| 432 | isAvailable = YES; |
| 433 | break; |
| 434 | } |
| 435 | case NameDirectory::LookupStatus::INVALID_NAME: |
| 436 | { |
Alexandre Lision | 5dc5d31 | 2016-11-10 10:41:37 -0500 | [diff] [blame] | 437 | message = NSLocalizedString(@"The entered username is invalid. It must have at least 3 characters and contain only lowercase alphanumeric characters.", |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 438 | @"Text shown to user when his username is invalid to be registered"); |
| 439 | isAvailable = NO; |
| 440 | break; |
| 441 | } |
| 442 | case NameDirectory::LookupStatus::ERROR: |
| 443 | default: |
| 444 | { |
| 445 | message = NSLocalizedString(@"Failed to perform lookup", |
| 446 | @"Text shown to user when an error occur at registration"); |
| 447 | isAvailable = NO; |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | [self showLookUpAvailable:isAvailable andText: message]; |
| 452 | [self onUsernameAvailabilityChangedWithNewAvailability:isAvailable]; |
| 453 | |
| 454 | }); |
| 455 | |
| 456 | //Start the lookup in a second so that the UI dosen't seem to freeze |
| 457 | BOOL result = NameDirectory::instance().lookupName(nullptr, QString(), QString::fromNSString(usernameWaitingForLookupResult)); |
| 458 | |
| 459 | } |
| 460 | |
Alexandre Lision | 5dc5d31 | 2016-11-10 10:41:37 -0500 | [diff] [blame] | 461 | #pragma mark - NSTextFieldDelegate delegate methods |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 462 | |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 463 | - (void)controlTextDidChange:(NSNotification *)notif |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 464 | { |
| 465 | NSTextField* textField = [notif object]; |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 466 | if (textField.tag != BLOCKCHAIN_NAME_TAG) { |
| 467 | return; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 468 | } |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 469 | NSString* alias = textField.stringValue; |
| 470 | |
| 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 |