blob: 7331d74ed03fbb37d0f1806e447beddf0d2e8814 [file] [log] [blame]
Anthony Léonard1f70f722017-10-02 10:53:32 -04001/*
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"
21#import <accountmodel.h>
22
23@implementation PasswordChangeWC
24{
25 Account* account;
26 __unsafe_unretained IBOutlet NSSecureTextField *oldPassword;
27 __unsafe_unretained IBOutlet NSSecureTextField *newPassword;
28 __unsafe_unretained IBOutlet NSSecureTextField *repeatedPassword;
29
30 __unsafe_unretained IBOutlet NSImageView *repeatPasswordValid;
31
32 __unsafe_unretained IBOutlet NSButton *acceptButton;
33
34 IBOutlet NSPopover *wrongPasswordPopover;
35}
36
37-(id)initWithAccount:(Account*)acc
38{
39 account = acc;
40 return [super initWithWindowNibName:@"PasswordChange"];
41}
42
43-(void)windowDidLoad
44{
45 [super windowDidLoad];
46 if (account != nullptr) {
47 const auto hasPassword = account->archiveHasPassword();
48
49 [oldPassword setEnabled:hasPassword];
50 [oldPassword setPlaceholderString:(hasPassword)?@"":NSLocalizedString(@"Account has no password", @"No password on this account text field placeholder")];
51 }
52}
53
54-(IBAction)accept:(id)sender
55{
56 if (account->changePassword(QString::fromNSString([oldPassword stringValue]), QString::fromNSString([newPassword stringValue])))
57 {
58 AccountModel::instance().save();
59 [self close];
60 } else {
61 [oldPassword setStringValue:@""];
62 [wrongPasswordPopover showRelativeToRect:oldPassword.visibleRect ofView:oldPassword preferredEdge:NSMinYEdge];
63 }
64}
65
66-(IBAction)cancel:(id)sender
67{
68 [self close];
69}
70
71#pragma mark - NSTextFieldDelegate methods
72
73-(void)controlTextDidChange:(NSNotification *)obj
74{
75 if ([[newPassword stringValue] isEqualToString: [repeatedPassword stringValue]]) {
76 [repeatPasswordValid setHidden:NO];
77 [acceptButton setEnabled:YES];
78 } else {
79 [repeatPasswordValid setHidden:YES];
80 [acceptButton setEnabled:NO];
81 }
82}
83
84@end