PasswordChangeWC.mm 3.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 *  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"
21 22 23
#import <api/lrc.h>
#import <api/account.h>
#import <api/newaccountmodel.h>
24 25 26 27

@implementation PasswordChangeWC
{
    __unsafe_unretained IBOutlet NSSecureTextField *oldPassword;
28
    __unsafe_unretained IBOutlet NSTextField *oldPasswordTitle;
29 30
    __unsafe_unretained IBOutlet NSSecureTextField *newPassword;
    __unsafe_unretained IBOutlet NSSecureTextField *repeatedPassword;
31
    __unsafe_unretained IBOutlet NSLayoutConstraint *newPasswordTopConstraint;
32 33 34 35 36 37 38 39

    __unsafe_unretained IBOutlet NSImageView *repeatPasswordValid;

    __unsafe_unretained IBOutlet NSButton *acceptButton;

    IBOutlet NSPopover *wrongPasswordPopover;
}

40 41 42
@synthesize accountModel;

-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel
43
{
44 45 46 47 48
    if (self = [self initWithWindowNibName:nibNameOrNil])
    {
        self.accountModel= accountModel;
    }
    return self;
49 50 51 52 53
}

-(void)windowDidLoad
{
    [super windowDidLoad];
54 55
    lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
    BOOL hasPassword = accountProperties.archiveHasPassword;
56 57 58
    [oldPassword setHidden: !hasPassword];
    [oldPasswordTitle setHidden: !hasPassword];
    newPasswordTopConstraint.constant = hasPassword ? 15.0 : -oldPasswordTitle.frame.size.height;
59 60 61 62
}

-(IBAction)accept:(id)sender
{
63 64 65 66 67 68
    if (self.accountModel->changeAccountPassword(self.selectedAccountID, [[oldPassword stringValue] UTF8String], [[newPassword stringValue] UTF8String])) {
        lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
        BOOL haspassword = ![[newPassword stringValue] isEqualToString:@""];
        accountProperties.archiveHasPassword = haspassword;
        self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
        [self.delegate paswordCreatedWithSuccess: haspassword];
69 70 71
        [self close];
    } else {
        [oldPassword setStringValue:@""];
72
        [oldPassword setPlaceholderString:@"Enter your old password"];
73 74 75 76 77 78 79 80 81
        [wrongPasswordPopover showRelativeToRect:oldPassword.visibleRect ofView:oldPassword preferredEdge:NSMinYEdge];
    }
}

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

82 83 84 85 86
-(void) close {
    [NSApp endSheet:self.window];
    [self.window orderOut:self];
}

87 88 89 90 91 92 93 94 95 96 97 98 99 100
#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