blob: dbed71a02b635b44eb9d9fa7f431b33fc0ec80ee [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"
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;
28 __unsafe_unretained IBOutlet NSSecureTextField *newPassword;
29 __unsafe_unretained IBOutlet NSSecureTextField *repeatedPassword;
30
31 __unsafe_unretained IBOutlet NSImageView *repeatPasswordValid;
32
33 __unsafe_unretained IBOutlet NSButton *acceptButton;
34
35 IBOutlet NSPopover *wrongPasswordPopover;
36}
37
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040038@synthesize accountModel;
39
40-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel
Anthony Léonard1f70f722017-10-02 10:53:32 -040041{
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040042 if (self = [self initWithWindowNibName:nibNameOrNil])
43 {
44 self.accountModel= accountModel;
45 }
46 return self;
Anthony Léonard1f70f722017-10-02 10:53:32 -040047}
48
49-(void)windowDidLoad
50{
51 [super windowDidLoad];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040052 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
53 BOOL hasPassword = accountProperties.archiveHasPassword;
Anthony Léonard1f70f722017-10-02 10:53:32 -040054
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040055 [oldPassword setEnabled:hasPassword];
56 [oldPassword setPlaceholderString:(hasPassword)?@"":NSLocalizedString(@"Account has no password", @"No password on this account text field placeholder")];
Anthony Léonard1f70f722017-10-02 10:53:32 -040057}
58
59-(IBAction)accept:(id)sender
60{
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040061 if (self.accountModel->changeAccountPassword(self.selectedAccountID, [[oldPassword stringValue] UTF8String], [[newPassword stringValue] UTF8String])) {
62 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
63 BOOL haspassword = ![[newPassword stringValue] isEqualToString:@""];
64 accountProperties.archiveHasPassword = haspassword;
65 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
66 [self.delegate paswordCreatedWithSuccess: haspassword];
Anthony Léonard1f70f722017-10-02 10:53:32 -040067 [self close];
68 } else {
69 [oldPassword setStringValue:@""];
70 [wrongPasswordPopover showRelativeToRect:oldPassword.visibleRect ofView:oldPassword preferredEdge:NSMinYEdge];
71 }
72}
73
74-(IBAction)cancel:(id)sender
75{
76 [self close];
77}
78
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040079-(void) close {
80 [NSApp endSheet:self.window];
81 [self.window orderOut:self];
82}
83
Anthony Léonard1f70f722017-10-02 10:53:32 -040084#pragma mark - NSTextFieldDelegate methods
85
86-(void)controlTextDidChange:(NSNotification *)obj
87{
88 if ([[newPassword stringValue] isEqualToString: [repeatedPassword stringValue]]) {
89 [repeatPasswordValid setHidden:NO];
90 [acceptButton setEnabled:YES];
91 } else {
92 [repeatPasswordValid setHidden:YES];
93 [acceptButton setEnabled:NO];
94 }
95}
96
97@end