blob: 9fdd320b8800fff9c4eef3339ab62f9d7cee53eb [file] [log] [blame]
Anthony Léonard1f70f722017-10-02 10:53:32 -04001/*
Sébastien Blin029ffa82019-01-02 17:43:48 -05002 * Copyright (C) 2017-2019 Savoir-faire Linux Inc.
Anthony Léonard1f70f722017-10-02 10:53:32 -04003 * 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 Kostiuk1f8c1252018-07-30 18:18:57 -040021#import <api/lrc.h>
22#import <api/account.h>
23#import <api/newaccountmodel.h>
Anthony Léonard1f70f722017-10-02 10:53:32 -040024
25@implementation PasswordChangeWC
26{
Anthony Léonard1f70f722017-10-02 10:53:32 -040027 __unsafe_unretained IBOutlet NSSecureTextField *oldPassword;
Kateryna Kostiukbdaa2742018-09-04 15:49:30 -040028 __unsafe_unretained IBOutlet NSTextField *oldPasswordTitle;
Anthony Léonard1f70f722017-10-02 10:53:32 -040029 __unsafe_unretained IBOutlet NSSecureTextField *newPassword;
30 __unsafe_unretained IBOutlet NSSecureTextField *repeatedPassword;
Kateryna Kostiukbdaa2742018-09-04 15:49:30 -040031 __unsafe_unretained IBOutlet NSLayoutConstraint *newPasswordTopConstraint;
Anthony Léonard1f70f722017-10-02 10:53:32 -040032
33 __unsafe_unretained IBOutlet NSImageView *repeatPasswordValid;
34
35 __unsafe_unretained IBOutlet NSButton *acceptButton;
36
37 IBOutlet NSPopover *wrongPasswordPopover;
38}
39
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040040@synthesize accountModel;
41
42-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel
Anthony Léonard1f70f722017-10-02 10:53:32 -040043{
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040044 if (self = [self initWithWindowNibName:nibNameOrNil])
45 {
46 self.accountModel= accountModel;
47 }
48 return self;
Anthony Léonard1f70f722017-10-02 10:53:32 -040049}
50
51-(void)windowDidLoad
52{
53 [super windowDidLoad];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040054 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
55 BOOL hasPassword = accountProperties.archiveHasPassword;
Kateryna Kostiukbdaa2742018-09-04 15:49:30 -040056 [oldPassword setHidden: !hasPassword];
57 [oldPasswordTitle setHidden: !hasPassword];
58 newPasswordTopConstraint.constant = hasPassword ? 15.0 : -oldPasswordTitle.frame.size.height;
Anthony Léonard1f70f722017-10-02 10:53:32 -040059}
60
61-(IBAction)accept:(id)sender
62{
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040063 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éonard1f70f722017-10-02 10:53:32 -040069 [self close];
70 } else {
71 [oldPassword setStringValue:@""];
Kateryna Kostiukbdaa2742018-09-04 15:49:30 -040072 [oldPassword setPlaceholderString:@"Enter your old password"];
Anthony Léonard1f70f722017-10-02 10:53:32 -040073 [wrongPasswordPopover showRelativeToRect:oldPassword.visibleRect ofView:oldPassword preferredEdge:NSMinYEdge];
74 }
75}
76
77-(IBAction)cancel:(id)sender
78{
79 [self close];
80}
81
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040082-(void) close {
83 [NSApp endSheet:self.window];
84 [self.window orderOut:self];
85}
86
Anthony Léonard1f70f722017-10-02 10:53:32 -040087#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