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> |
| 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 "RingWizardNewAccountVC.h" |
| 21 | |
| 22 | |
| 23 | //Cocoa |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 24 | #import <Quartz/Quartz.h> |
| 25 | |
| 26 | //Qt |
| 27 | #import <QUrl> |
| 28 | #import <QPixmap> |
| 29 | |
| 30 | //LRC |
| 31 | #import <accountmodel.h> |
| 32 | #import <protocolmodel.h> |
| 33 | #import <profilemodel.h> |
| 34 | #import <QItemSelectionModel> |
| 35 | #import <account.h> |
| 36 | #import <certificate.h> |
| 37 | #import <profilemodel.h> |
| 38 | #import <profile.h> |
| 39 | #import <person.h> |
| 40 | |
| 41 | #import "AppDelegate.h" |
| 42 | #import "Constants.h" |
| 43 | #import "views/NSImage+Extensions.h" |
| 44 | #import "views/NSColor+RingTheme.h" |
| 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; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 57 | __unsafe_unretained IBOutlet NSSecureTextField* passwordField; |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 58 | __unsafe_unretained IBOutlet NSSecureTextField* passwordRepeatField; |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 59 | __unsafe_unretained IBOutlet NSImageView* passwordCheck; |
| 60 | __unsafe_unretained IBOutlet NSImageView* passwordRepeatCheck; |
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 NSImageView* ivLookupResult; |
| 66 | __unsafe_unretained IBOutlet NSProgressIndicator* indicatorLookupResult; |
| 67 | |
| 68 | __unsafe_unretained IBOutlet NSPopover* helpBlockchainContainer; |
| 69 | __unsafe_unretained IBOutlet NSPopover* helpPasswordContainer; |
| 70 | |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 71 | Account* accountToCreate; |
| 72 | NSTimer* errorTimer; |
| 73 | QMetaObject::Connection stateChanged; |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 74 | QMetaObject::Connection registrationEnded; |
| 75 | QMetaObject::Connection registeredNameFound; |
| 76 | |
| 77 | BOOL lookupQueued; |
| 78 | NSString* usernameWaitingForLookupResult; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 79 | } |
| 80 | |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 81 | NSInteger const DISPLAY_NAME_TAG = 1; |
| 82 | NSInteger const BLOCKCHAIN_NAME_TAG = 2; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 83 | |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 84 | //ERROR CODE for textfields validations |
| 85 | NSInteger const ERROR_PASSWORD_TOO_SHORT = -1; |
| 86 | NSInteger const ERROR_REPEAT_MISMATCH = -2; |
| 87 | |
| 88 | |
| 89 | - (BOOL)produceError:(NSError**)error withCode:(NSInteger)code andMessage:(NSString*)message |
| 90 | { |
| 91 | if (error != NULL){ |
| 92 | NSDictionary *errorDetail = @{NSLocalizedDescriptionKey: message}; |
| 93 | *error = [NSError errorWithDomain:@"Input" code:code userInfo:errorDetail]; |
| 94 | } |
| 95 | return NO; |
| 96 | } |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 97 | |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 98 | - (IBAction)showBlockchainHelp:(id)sender |
| 99 | { |
| 100 | [helpBlockchainContainer showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMaxYEdge]; |
| 101 | } |
| 102 | |
| 103 | - (IBAction)showPasswordHelp:(id)sender |
| 104 | { |
| 105 | [helpPasswordContainer showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMaxYEdge]; |
| 106 | } |
| 107 | |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 108 | - (void)show |
| 109 | { |
| 110 | AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate]; |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 111 | [displayNameField setTag:DISPLAY_NAME_TAG]; |
| 112 | [registeredNameField setTag:BLOCKCHAIN_NAME_TAG]; |
| 113 | [displayNameField setStringValue: NSFullUserName()]; |
| 114 | [self controlTextDidChange:[NSNotification notificationWithName:@"PlaceHolder" object:displayNameField]]; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 115 | [photoView setWantsLayer: YES]; |
| 116 | photoView.layer.cornerRadius = photoView.frame.size.width / 2; |
| 117 | photoView.layer.masksToBounds = YES; |
Alexandre Lision | c2ad639 | 2016-11-08 11:20:34 -0500 | [diff] [blame] | 118 | self.signUpBlockchainState = YES; |
| 119 | [self toggleSignupRing:nil]; |
Kateryna Kostiuk | 87ae2bf | 2018-05-04 13:46:17 -0400 | [diff] [blame] | 120 | [addProfilePhotoImage setWantsLayer: YES]; |
| 121 | [photoView setBordered:YES]; |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 122 | |
| 123 | [self display:creationView]; |
| 124 | } |
| 125 | |
| 126 | - (void)removeSubviews |
| 127 | { |
| 128 | while ([self.view.subviews count] > 0){ |
| 129 | [[self.view.subviews firstObject] removeFromSuperview]; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | - (void)display:(NSView *)view |
| 134 | { |
| 135 | [self.delegate showView:view]; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | - (IBAction)editPhoto:(id)sender |
| 139 | { |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 140 | auto pictureTaker = [IKPictureTaker pictureTaker]; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 141 | |
| 142 | [pictureTaker beginPictureTakerSheetForWindow:[self.delegate window] |
| 143 | withDelegate:self |
| 144 | didEndSelector:@selector(pictureTakerDidEnd:returnCode:contextInfo:) |
| 145 | contextInfo:nil]; |
| 146 | |
| 147 | } |
| 148 | |
| 149 | - (void)pictureTakerDidEnd:(IKPictureTaker *) picker |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 150 | returnCode:(NSInteger) code |
| 151 | contextInfo:(void*) contextInfo |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 152 | { |
| 153 | if (auto outputImage = [picker outputImage]) { |
Kateryna Kostiuk | 87ae2bf | 2018-05-04 13:46:17 -0400 | [diff] [blame] | 154 | [photoView setBordered:NO]; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 155 | [photoView setImage:outputImage]; |
Kateryna Kostiuk | 87ae2bf | 2018-05-04 13:46:17 -0400 | [diff] [blame] | 156 | [addProfilePhotoImage setHidden:YES]; |
| 157 | } else if(!photoView.image) { |
| 158 | [photoView setBordered:YES]; |
| 159 | [addProfilePhotoImage setHidden:NO]; |
Alexandre Lision | c2ad639 | 2016-11-08 11:20:34 -0500 | [diff] [blame] | 160 | } |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 161 | } |
| 162 | |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 163 | #pragma mark - Input validation |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 164 | |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 165 | - (BOOL)isPasswordValid |
| 166 | { |
| 167 | return self.password.length >= 6; |
| 168 | } |
| 169 | |
| 170 | - (BOOL)isRepeatPasswordValid |
| 171 | { |
Anthony Léonard | 24110e8 | 2017-09-15 16:29:11 -0400 | [diff] [blame] | 172 | 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] | 173 | } |
| 174 | |
| 175 | - (BOOL)validateRepeatPassword:(NSError **)error |
| 176 | { |
| 177 | if (!self.isRepeatPasswordValid){ |
| 178 | return [self produceError:error |
| 179 | withCode:ERROR_REPEAT_MISMATCH |
| 180 | andMessage:NSLocalizedString(@"Passwords don't match", |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 181 | @"Indication for user")]; |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 182 | } |
| 183 | return YES; |
| 184 | } |
| 185 | |
| 186 | - (BOOL)validatePassword:(NSError **)error |
| 187 | { |
| 188 | if (!self.isRepeatPasswordValid){ |
| 189 | return [self produceError:error |
| 190 | withCode:ERROR_PASSWORD_TOO_SHORT |
| 191 | andMessage:NSLocalizedString(@"Password is too short", |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 192 | @"Indication for user")]; |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 193 | } |
| 194 | return YES; |
| 195 | } |
| 196 | |
| 197 | - (BOOL)validateUserInputPassword:(NSError **)error |
| 198 | { |
| 199 | return [self validatePassword:error] && [self validateRepeatPassword:error]; |
| 200 | } |
| 201 | |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 202 | - (IBAction)createRingAccount:(id)sender |
| 203 | { |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 204 | NSError *error = nil; |
| 205 | if (![self validateUserInputPassword:&error]){ |
| 206 | NSAlert* alert = [NSAlert alertWithMessageText:[error localizedDescription] |
| 207 | defaultButton:NSLocalizedString(@"Revise Input", |
| 208 | @"Button title") |
| 209 | alternateButton:nil |
| 210 | otherButton:nil |
| 211 | informativeTextWithFormat:@"%@",error]; |
| 212 | |
| 213 | [alert beginSheetModalForWindow:passwordField.window |
| 214 | modalDelegate:nil |
| 215 | didEndSelector:NULL |
| 216 | contextInfo:NULL]; |
| 217 | |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | [self display:loadingView]; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 222 | [progressBar startAnimation:nil]; |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 223 | |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 224 | NSString* displayName = displayNameField.stringValue; |
| 225 | if ([displayName isEqualToString:@""]) { |
| 226 | displayName = NSLocalizedString(@"Unknown", @"Name used when user leave field empty"); |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 227 | } |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 228 | |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 229 | accountToCreate = AccountModel::instance().add(QString::fromNSString(displayName), Account::Protocol::RING); |
| 230 | accountToCreate->setAlias([displayName UTF8String]); |
| 231 | accountToCreate->setDisplayName([displayName UTF8String]); |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 232 | |
| 233 | if (auto profile = ProfileModel::instance().selectedProfile()) { |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 234 | profile->person()->setFormattedName([displayName UTF8String]); |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 235 | QPixmap p; |
| 236 | auto smallImg = [NSImage imageResize:[photoView image] newSize:{100,100}]; |
| 237 | if (p.loadFromData(QByteArray::fromNSData([smallImg TIFFRepresentation]))) { |
| 238 | profile->person()->setPhoto(QVariant(p)); |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 239 | } |
| 240 | profile->save(); |
Kateryna Kostiuk | 87ae2bf | 2018-05-04 13:46:17 -0400 | [diff] [blame] | 241 | |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 242 | } |
| 243 | |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 244 | QModelIndex qIdx = AccountModel::instance().protocolModel()->selectionModel()->currentIndex(); |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 245 | |
| 246 | [self setCallback]; |
| 247 | |
| 248 | [self performSelector:@selector(saveAccount) withObject:nil afterDelay:1]; |
| 249 | [self registerDefaultPreferences]; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Set default values for preferences |
| 254 | */ |
| 255 | - (void)registerDefaultPreferences |
| 256 | { |
| 257 | // enable AutoStartup |
| 258 | LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); |
| 259 | if (loginItemsRef == nil) return; |
| 260 | CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; |
| 261 | LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL); |
| 262 | if (itemRef) CFRelease(itemRef); |
| 263 | |
| 264 | // enable Notifications |
| 265 | [[NSUserDefaults standardUserDefaults] setBool:YES forKey:Preferences::Notifications]; |
| 266 | } |
| 267 | |
| 268 | - (void)saveAccount |
| 269 | { |
| 270 | accountToCreate->setArchivePassword(QString::fromNSString(passwordField.stringValue)); |
| 271 | accountToCreate->setUpnpEnabled(YES); // Always active upnp |
| 272 | accountToCreate << Account::EditAction::SAVE; |
| 273 | } |
| 274 | |
| 275 | - (void)setCallback |
| 276 | { |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 277 | stateChanged = QObject::connect(&AccountModel::instance(), |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 278 | &AccountModel::accountStateChanged, |
| 279 | [=](Account *account, const Account::RegistrationState state) { |
| 280 | switch(state){ |
| 281 | case Account::RegistrationState::READY: |
| 282 | case Account::RegistrationState::TRYING: |
| 283 | case Account::RegistrationState::UNREGISTERED:{ |
| 284 | accountToCreate << Account::EditAction::RELOAD; |
| 285 | QObject::disconnect(stateChanged); |
| 286 | //try to register username |
| 287 | if (self.signUpBlockchainState == NSOnState){ |
| 288 | [self startNameRegistration:account]; |
| 289 | } else { |
| 290 | [self.delegate didCreateAccountWithSuccess:YES]; |
| 291 | } |
| 292 | break; |
| 293 | } |
| 294 | case Account::RegistrationState::ERROR: |
| 295 | QObject::disconnect(stateChanged); |
| 296 | [self.delegate didCreateAccountWithSuccess:NO]; |
| 297 | break; |
| 298 | case Account::RegistrationState::INITIALIZING: |
| 299 | case Account::RegistrationState::COUNT__:{ |
| 300 | //Do Nothing |
| 301 | break; |
| 302 | } |
| 303 | } |
| 304 | }); |
| 305 | } |
| 306 | |
| 307 | - (void) startNameRegistration:(Account*) account |
| 308 | { |
Alexandre Lision | e857c0c | 2016-11-03 10:13:00 -0400 | [diff] [blame] | 309 | // Dismiss this screen if after 30 seconds the name is still not registered |
| 310 | errorTimer = [NSTimer scheduledTimerWithTimeInterval:30 |
| 311 | target:self |
| 312 | selector:@selector(nameRegistrationTimeout) userInfo:nil |
| 313 | repeats:NO]; |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 314 | registrationEnded = QObject::connect(account, |
| 315 | &Account::nameRegistrationEnded, |
| 316 | [=] (NameDirectory::RegisterNameStatus status, const QString& name) { |
| 317 | QObject::disconnect(registrationEnded); |
| 318 | switch(status) { |
| 319 | case NameDirectory::RegisterNameStatus::WRONG_PASSWORD: |
| 320 | case NameDirectory::RegisterNameStatus::ALREADY_TAKEN: |
| 321 | case NameDirectory::RegisterNameStatus::NETWORK_ERROR: { |
| 322 | [self couldNotRegisterUsername]; |
| 323 | break; |
| 324 | } |
| 325 | case NameDirectory::RegisterNameStatus::SUCCESS: { |
| 326 | break; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | [self.delegate didCreateAccountWithSuccess:YES]; |
| 331 | }); |
| 332 | self.isUserNameAvailable = account->registerName(QString::fromNSString(self.password), |
| 333 | QString::fromNSString(self.registeredName)); |
| 334 | if (!self.isUserNameAvailable){ |
| 335 | NSLog(@"Could not initialize registerName operation"); |
| 336 | QObject::disconnect(registrationEnded); |
| 337 | [self.delegate didCreateAccountWithSuccess:YES]; |
| 338 | } |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 339 | } |
| 340 | |
Alexandre Lision | e857c0c | 2016-11-03 10:13:00 -0400 | [diff] [blame] | 341 | - (void)nameRegistrationTimeout |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 342 | { |
Alexandre Lision | e857c0c | 2016-11-03 10:13:00 -0400 | [diff] [blame] | 343 | // This callback is used when registration takes more than 30 seconds |
| 344 | // It skips the wizard and brings the main window |
| 345 | [self.delegate didCreateAccountWithSuccess:YES]; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | - (IBAction)cancel:(id)sender |
| 349 | { |
| 350 | [self.delegate didCreateAccountWithSuccess:NO]; |
| 351 | } |
| 352 | |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 353 | #pragma mark - UserNameRegistration delegate methods |
| 354 | |
| 355 | - (IBAction)toggleSignupRing:(id)sender |
| 356 | { |
| 357 | if (self.withBlockchain) { |
| 358 | [self lookupUserName]; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | - (void)couldNotRegisterUsername |
| 363 | { |
| 364 | // Do nothing |
| 365 | } |
| 366 | |
| 367 | - (BOOL)withBlockchain |
| 368 | { |
| 369 | return self.signUpBlockchainState == NSOnState; |
| 370 | } |
| 371 | |
| 372 | - (BOOL)userNameAvailableORNotBlockchain |
| 373 | { |
| 374 | return !self.withBlockchain || (self.registeredName.length > 0 && self.isUserNameAvailable); |
| 375 | } |
| 376 | |
| 377 | - (void)showLookUpAvailable:(BOOL)available andText:(NSString *)message |
| 378 | { |
| 379 | [ivLookupResult setImage:[NSImage imageNamed:(available?@"ic_action_accept":@"ic_action_cancel")]] ; |
| 380 | [ivLookupResult setHidden:NO]; |
| 381 | [ivLookupResult setToolTip:message]; |
| 382 | } |
| 383 | |
| 384 | - (void)onUsernameAvailabilityChangedWithNewAvailability:(BOOL)newAvailability |
| 385 | { |
| 386 | self.isUserNameAvailable = newAvailability; |
| 387 | } |
| 388 | |
| 389 | - (void)hideLookupSpinner |
| 390 | { |
| 391 | [indicatorLookupResult setHidden:YES]; |
| 392 | } |
| 393 | |
| 394 | - (void)showLookupSpinner |
| 395 | { |
| 396 | [ivLookupResult setHidden:YES]; |
| 397 | [indicatorLookupResult setHidden:NO]; |
| 398 | [indicatorLookupResult startAnimation:nil]; |
| 399 | } |
| 400 | |
| 401 | - (BOOL)lookupUserName |
| 402 | { |
| 403 | [self showLookupSpinner]; |
| 404 | QObject::disconnect(registeredNameFound); |
| 405 | registeredNameFound = QObject::connect( |
| 406 | &NameDirectory::instance(), |
| 407 | &NameDirectory::registeredNameFound, |
| 408 | [=] ( const Account* account, NameDirectory::LookupStatus status, const QString& address, const QString& name) { |
| 409 | NSLog(@"Name lookup ended"); |
| 410 | lookupQueued = NO; |
| 411 | //If this is the username we are waiting for, we can disconnect. |
| 412 | if (name.compare(QString::fromNSString(usernameWaitingForLookupResult)) == 0) { |
| 413 | QObject::disconnect(registeredNameFound); |
| 414 | } else { |
| 415 | //Keep waiting... |
| 416 | return; |
| 417 | } |
| 418 | |
| 419 | //We may now stop the spinner |
| 420 | [self hideLookupSpinner]; |
| 421 | |
| 422 | BOOL isAvailable = NO; |
| 423 | NSString* message; |
| 424 | switch(status) |
| 425 | { |
| 426 | case NameDirectory::LookupStatus::SUCCESS: |
| 427 | { |
| 428 | message = NSLocalizedString(@"The entered username is not available", |
| 429 | @"Text shown to user when his username is already registered"); |
| 430 | isAvailable = NO; |
| 431 | break; |
| 432 | } |
| 433 | case NameDirectory::LookupStatus::NOT_FOUND: |
| 434 | { |
| 435 | message = NSLocalizedString(@"The entered username is available", |
| 436 | @"Text shown to user when his username is available to be registered"); |
| 437 | isAvailable = YES; |
| 438 | break; |
| 439 | } |
| 440 | case NameDirectory::LookupStatus::INVALID_NAME: |
| 441 | { |
Alexandre Lision | 5dc5d31 | 2016-11-10 10:41:37 -0500 | [diff] [blame] | 442 | 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] | 443 | @"Text shown to user when his username is invalid to be registered"); |
| 444 | isAvailable = NO; |
| 445 | break; |
| 446 | } |
| 447 | case NameDirectory::LookupStatus::ERROR: |
| 448 | default: |
| 449 | { |
| 450 | message = NSLocalizedString(@"Failed to perform lookup", |
| 451 | @"Text shown to user when an error occur at registration"); |
| 452 | isAvailable = NO; |
| 453 | break; |
| 454 | } |
| 455 | } |
| 456 | [self showLookUpAvailable:isAvailable andText: message]; |
| 457 | [self onUsernameAvailabilityChangedWithNewAvailability:isAvailable]; |
| 458 | |
| 459 | }); |
| 460 | |
| 461 | //Start the lookup in a second so that the UI dosen't seem to freeze |
| 462 | BOOL result = NameDirectory::instance().lookupName(nullptr, QString(), QString::fromNSString(usernameWaitingForLookupResult)); |
| 463 | |
| 464 | } |
| 465 | |
Alexandre Lision | 5dc5d31 | 2016-11-10 10:41:37 -0500 | [diff] [blame] | 466 | #pragma mark - NSTextFieldDelegate delegate methods |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 467 | |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 468 | - (void)controlTextDidChange:(NSNotification *)notif |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 469 | { |
| 470 | NSTextField* textField = [notif object]; |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 471 | if (textField.tag != BLOCKCHAIN_NAME_TAG) { |
| 472 | return; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 473 | } |
Alexandre Lision | 882289b | 2016-10-31 16:10:39 -0400 | [diff] [blame] | 474 | NSString* alias = textField.stringValue; |
| 475 | |
| 476 | [self showLookupSpinner]; |
| 477 | [self onUsernameAvailabilityChangedWithNewAvailability:NO]; |
| 478 | [NSObject cancelPreviousPerformRequestsWithTarget:self]; |
| 479 | [self performSelector:@selector(lookUp:) withObject:alias afterDelay:0.5]; |
| 480 | } |
| 481 | |
| 482 | - (void) lookUp:(NSString*) name |
| 483 | { |
| 484 | if (self.withBlockchain && !lookupQueued) { |
| 485 | usernameWaitingForLookupResult = name; |
| 486 | lookupQueued = YES; |
| 487 | [self lookupUserName]; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | |
| 492 | + (NSSet *)keyPathsForValuesAffectingUserNameAvailableORNotBlockchain |
| 493 | { |
| 494 | return [NSSet setWithObjects: NSStringFromSelector(@selector(signUpBlockchainState)), |
| 495 | NSStringFromSelector(@selector(isUserNameAvailable)), |
| 496 | nil]; |
| 497 | } |
| 498 | |
| 499 | + (NSSet *)keyPathsForValuesAffectingWithBlockchain |
| 500 | { |
| 501 | return [NSSet setWithObjects: NSStringFromSelector(@selector(signUpBlockchainState)), |
| 502 | nil]; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 503 | } |
| 504 | |
Loïc Siret | 3652cfb | 2016-10-27 10:12:07 -0400 | [diff] [blame] | 505 | + (NSSet *)keyPathsForValuesAffectingIsPasswordValid |
| 506 | { |
| 507 | return [NSSet setWithObjects:@"password", nil]; |
| 508 | } |
| 509 | |
| 510 | + (NSSet *)keyPathsForValuesAffectingIsRepeatPasswordValid |
| 511 | { |
| 512 | return [NSSet setWithObjects:@"password", @"repeatPassword", nil]; |
| 513 | } |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 514 | @end |