Anthony Léonard | 1f70f72 | 2017-10-02 10:53:32 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 Savoir-faire Linux Inc. |
| 3 | * Author: Anthony Léonard <anthony.leonard@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 "PasswordChangeWC.h" |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 21 | #import <api/lrc.h> |
| 22 | #import <api/account.h> |
| 23 | #import <api/newaccountmodel.h> |
Anthony Léonard | 1f70f72 | 2017-10-02 10:53:32 -0400 | [diff] [blame] | 24 | |
| 25 | @implementation PasswordChangeWC |
| 26 | { |
Anthony Léonard | 1f70f72 | 2017-10-02 10:53:32 -0400 | [diff] [blame] | 27 | __unsafe_unretained IBOutlet NSSecureTextField *oldPassword; |
| 28 | __unsafe_unretained IBOutlet NSSecureTextField *newPassword; |
| 29 | __unsafe_unretained IBOutlet NSSecureTextField *repeatedPassword; |
| 30 | |
| 31 | __unsafe_unretained IBOutlet NSImageView *repeatPasswordValid; |
| 32 | |
| 33 | __unsafe_unretained IBOutlet NSButton *acceptButton; |
| 34 | |
| 35 | IBOutlet NSPopover *wrongPasswordPopover; |
| 36 | } |
| 37 | |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 38 | @synthesize accountModel; |
| 39 | |
| 40 | -(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel |
Anthony Léonard | 1f70f72 | 2017-10-02 10:53:32 -0400 | [diff] [blame] | 41 | { |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 42 | if (self = [self initWithWindowNibName:nibNameOrNil]) |
| 43 | { |
| 44 | self.accountModel= accountModel; |
| 45 | } |
| 46 | return self; |
Anthony Léonard | 1f70f72 | 2017-10-02 10:53:32 -0400 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | -(void)windowDidLoad |
| 50 | { |
| 51 | [super windowDidLoad]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 52 | lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID); |
| 53 | BOOL hasPassword = accountProperties.archiveHasPassword; |
Anthony Léonard | 1f70f72 | 2017-10-02 10:53:32 -0400 | [diff] [blame] | 54 | |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 55 | [oldPassword setEnabled:hasPassword]; |
| 56 | [oldPassword setPlaceholderString:(hasPassword)?@"":NSLocalizedString(@"Account has no password", @"No password on this account text field placeholder")]; |
Anthony Léonard | 1f70f72 | 2017-10-02 10:53:32 -0400 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | -(IBAction)accept:(id)sender |
| 60 | { |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 61 | if (self.accountModel->changeAccountPassword(self.selectedAccountID, [[oldPassword stringValue] UTF8String], [[newPassword stringValue] UTF8String])) { |
| 62 | lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID); |
| 63 | BOOL haspassword = ![[newPassword stringValue] isEqualToString:@""]; |
| 64 | accountProperties.archiveHasPassword = haspassword; |
| 65 | self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties); |
| 66 | [self.delegate paswordCreatedWithSuccess: haspassword]; |
Anthony Léonard | 1f70f72 | 2017-10-02 10:53:32 -0400 | [diff] [blame] | 67 | [self close]; |
| 68 | } else { |
| 69 | [oldPassword setStringValue:@""]; |
| 70 | [wrongPasswordPopover showRelativeToRect:oldPassword.visibleRect ofView:oldPassword preferredEdge:NSMinYEdge]; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | -(IBAction)cancel:(id)sender |
| 75 | { |
| 76 | [self close]; |
| 77 | } |
| 78 | |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 79 | -(void) close { |
| 80 | [NSApp endSheet:self.window]; |
| 81 | [self.window orderOut:self]; |
| 82 | } |
| 83 | |
Anthony Léonard | 1f70f72 | 2017-10-02 10:53:32 -0400 | [diff] [blame] | 84 | #pragma mark - NSTextFieldDelegate methods |
| 85 | |
| 86 | -(void)controlTextDidChange:(NSNotification *)obj |
| 87 | { |
| 88 | if ([[newPassword stringValue] isEqualToString: [repeatedPassword stringValue]]) { |
| 89 | [repeatPasswordValid setHidden:NO]; |
| 90 | [acceptButton setEnabled:YES]; |
| 91 | } else { |
| 92 | [repeatPasswordValid setHidden:YES]; |
| 93 | [acceptButton setEnabled:NO]; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | @end |