PasswordChangeWC.mm 2.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/*
 *  Copyright (C) 2017 Savoir-faire Linux Inc.
 *  Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

#import "PasswordChangeWC.h"
#import <accountmodel.h>

@implementation PasswordChangeWC
{
    Account* account;
    __unsafe_unretained IBOutlet NSSecureTextField *oldPassword;
    __unsafe_unretained IBOutlet NSSecureTextField *newPassword;
    __unsafe_unretained IBOutlet NSSecureTextField *repeatedPassword;

    __unsafe_unretained IBOutlet NSImageView *repeatPasswordValid;

    __unsafe_unretained IBOutlet NSButton *acceptButton;

    IBOutlet NSPopover *wrongPasswordPopover;
}

-(id)initWithAccount:(Account*)acc
{
    account = acc;
    return [super initWithWindowNibName:@"PasswordChange"];
}

-(void)windowDidLoad
{
    [super windowDidLoad];
    if (account != nullptr) {
        const auto hasPassword = account->archiveHasPassword();

        [oldPassword setEnabled:hasPassword];
        [oldPassword setPlaceholderString:(hasPassword)?@"":NSLocalizedString(@"Account has no password", @"No password on this account text field placeholder")];
    }
}

-(IBAction)accept:(id)sender
{
    if (account->changePassword(QString::fromNSString([oldPassword stringValue]), QString::fromNSString([newPassword stringValue])))
    {
        AccountModel::instance().save();
        [self close];
    } else {
        [oldPassword setStringValue:@""];
        [wrongPasswordPopover showRelativeToRect:oldPassword.visibleRect ofView:oldPassword preferredEdge:NSMinYEdge];
    }
}

-(IBAction)cancel:(id)sender
{
    [self close];
}

#pragma mark - NSTextFieldDelegate methods

-(void)controlTextDidChange:(NSNotification *)obj
{
    if ([[newPassword stringValue] isEqualToString: [repeatedPassword stringValue]]) {
        [repeatPasswordValid setHidden:NO];
        [acceptButton setEnabled:YES];
    } else {
        [repeatPasswordValid setHidden:YES];
        [acceptButton setEnabled:NO];
    }
}

@end