Anthony Léonard | 1f70f72 | 2017-10-02 10:53:32 -0400 | [diff] [blame] | 1 | /* |
Sébastien Blin | 029ffa8 | 2019-01-02 17:43:48 -0500 | [diff] [blame] | 2 | * Copyright (C) 2017-2019 Savoir-faire Linux Inc. |
Anthony Léonard | 1f70f72 | 2017-10-02 10:53:32 -0400 | [diff] [blame] | 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; |
Kateryna Kostiuk | bdaa274 | 2018-09-04 15:49:30 -0400 | [diff] [blame] | 28 | __unsafe_unretained IBOutlet NSTextField *oldPasswordTitle; |
Anthony Léonard | 1f70f72 | 2017-10-02 10:53:32 -0400 | [diff] [blame] | 29 | __unsafe_unretained IBOutlet NSSecureTextField *newPassword; |
| 30 | __unsafe_unretained IBOutlet NSSecureTextField *repeatedPassword; |
Kateryna Kostiuk | bdaa274 | 2018-09-04 15:49:30 -0400 | [diff] [blame] | 31 | __unsafe_unretained IBOutlet NSLayoutConstraint *newPasswordTopConstraint; |
Anthony Léonard | 1f70f72 | 2017-10-02 10:53:32 -0400 | [diff] [blame] | 32 | |
| 33 | __unsafe_unretained IBOutlet NSImageView *repeatPasswordValid; |
| 34 | |
| 35 | __unsafe_unretained IBOutlet NSButton *acceptButton; |
| 36 | |
| 37 | IBOutlet NSPopover *wrongPasswordPopover; |
| 38 | } |
| 39 | |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 40 | @synthesize accountModel; |
| 41 | |
| 42 | -(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] | 43 | { |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 44 | if (self = [self initWithWindowNibName:nibNameOrNil]) |
| 45 | { |
| 46 | self.accountModel= accountModel; |
| 47 | } |
| 48 | return self; |
Anthony Léonard | 1f70f72 | 2017-10-02 10:53:32 -0400 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | -(void)windowDidLoad |
| 52 | { |
| 53 | [super windowDidLoad]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 54 | lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID); |
| 55 | BOOL hasPassword = accountProperties.archiveHasPassword; |
Kateryna Kostiuk | bdaa274 | 2018-09-04 15:49:30 -0400 | [diff] [blame] | 56 | [oldPassword setHidden: !hasPassword]; |
| 57 | [oldPasswordTitle setHidden: !hasPassword]; |
| 58 | newPasswordTopConstraint.constant = hasPassword ? 15.0 : -oldPasswordTitle.frame.size.height; |
Anthony Léonard | 1f70f72 | 2017-10-02 10:53:32 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | -(IBAction)accept:(id)sender |
| 62 | { |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 63 | if (self.accountModel->changeAccountPassword(self.selectedAccountID, [[oldPassword stringValue] UTF8String], [[newPassword stringValue] UTF8String])) { |
| 64 | lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID); |
| 65 | BOOL haspassword = ![[newPassword stringValue] isEqualToString:@""]; |
| 66 | accountProperties.archiveHasPassword = haspassword; |
| 67 | self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties); |
| 68 | [self.delegate paswordCreatedWithSuccess: haspassword]; |
Anthony Léonard | 1f70f72 | 2017-10-02 10:53:32 -0400 | [diff] [blame] | 69 | [self close]; |
| 70 | } else { |
| 71 | [oldPassword setStringValue:@""]; |
Kateryna Kostiuk | bdaa274 | 2018-09-04 15:49:30 -0400 | [diff] [blame] | 72 | [oldPassword setPlaceholderString:@"Enter your old password"]; |
Anthony Léonard | 1f70f72 | 2017-10-02 10:53:32 -0400 | [diff] [blame] | 73 | [wrongPasswordPopover showRelativeToRect:oldPassword.visibleRect ofView:oldPassword preferredEdge:NSMinYEdge]; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | -(IBAction)cancel:(id)sender |
| 78 | { |
| 79 | [self close]; |
| 80 | } |
| 81 | |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 82 | -(void) close { |
| 83 | [NSApp endSheet:self.window]; |
| 84 | [self.window orderOut:self]; |
| 85 | } |
| 86 | |
Anthony Léonard | 1f70f72 | 2017-10-02 10:53:32 -0400 | [diff] [blame] | 87 | #pragma mark - NSTextFieldDelegate methods |
| 88 | |
| 89 | -(void)controlTextDidChange:(NSNotification *)obj |
| 90 | { |
| 91 | if ([[newPassword stringValue] isEqualToString: [repeatedPassword stringValue]]) { |
| 92 | [repeatPasswordValid setHidden:NO]; |
| 93 | [acceptButton setEnabled:YES]; |
| 94 | } else { |
| 95 | [repeatPasswordValid setHidden:YES]; |
| 96 | [acceptButton setEnabled:NO]; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | @end |