blob: e2adf4e8694301da8d2edb0428ee1ff87ee4c324 [file] [log] [blame]
Alexandre Lisionc1f96662016-03-23 17:24:19 -04001/*
2 * Copyright (C) 2016 Savoir-faire Linux Inc.
3 * Author: Alexandre Lision <alexandre.lision@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#import "PathPasswordWC.h"
20
21#import "views/ITProgressIndicator.h"
22
23@interface PathPasswordWC() <NSTextFieldDelegate>{
24
25 __unsafe_unretained IBOutlet NSView* errorContainer;
26 __unsafe_unretained IBOutlet NSTextField* errorLabel;
27
28 __unsafe_unretained IBOutlet ITProgressIndicator* progressView;
29
30 __unsafe_unretained IBOutlet NSView* pathPasswordContainer;
31 __unsafe_unretained IBOutlet NSSecureTextField* passwordField;
32 __unsafe_unretained IBOutlet NSPathControl* path;
33
34}
35
36@end
37
38@implementation PathPasswordWC {
39 struct {
40 unsigned int didComplete:1;
41 unsigned int didCompleteWithActionCode:1;
42 } delegateRespondsTo;
43}
44
45- (id)initWithDelegate:(id <PathPasswordDelegate>) del actionCode:(NSInteger) code
46{
47 if ((self = [super initWithWindowNibName:@"PathPasswordWindow"]) != nil) {
48 [self setDelegate:del];
49 self.actionCode = code;
50 }
51 return self;
52}
53
54- (void)windowDidLoad
55{
56 [super windowDidLoad];
57 [path setURL: [NSURL fileURLWithPath:NSHomeDirectory()]];
58 [progressView setNumberOfLines:30];
59 [progressView setWidthOfLine:2];
60 [progressView setLengthOfLine:5];
61 [progressView setInnerMargin:20];
62 [progressView setHidden:YES];
63}
64
65- (void)setDelegate:(id <PathPasswordDelegate>)aDelegate
66{
67 if (self.delegate != aDelegate) {
68 _delegate = aDelegate;
69 delegateRespondsTo.didComplete = [self.delegate respondsToSelector:@selector(didCompleteWithPath:Password:)];
70 delegateRespondsTo.didCompleteWithActionCode = [self.delegate respondsToSelector:@selector(didCompleteWithPath:Password:ActionCode:)];
71 }
72}
73
74- (void) setAllowFileSelection:(BOOL) b
75{
76 _allowFileSelection = b;
77 [path setAllowedTypes:_allowFileSelection ? nil : [NSArray arrayWithObject:@"public.folder"]];
78}
79
80- (IBAction) cancelPressed:(id)sender
81{
82 [NSApp endSheet:self.window];
83 [self.window orderOut:self];
84}
85
86- (IBAction)completeAction:(id)sender
87{
88 if (delegateRespondsTo.didComplete)
89 [self.delegate didCompleteWithPath:path.URL Password:passwordField.stringValue];
90 else if (delegateRespondsTo.didCompleteWithActionCode)
91 [self.delegate didCompleteWithPath:path.URL Password:passwordField.stringValue ActionCode:self.actionCode];
92}
93
94- (void)showLoading
95{
96 [progressView setHidden:NO];
97 [pathPasswordContainer setHidden:YES];
98 [errorContainer setHidden:YES];
99 [progressView setAnimates:YES];
100}
101
102- (void)showError:(NSString*) error
103{
104 [progressView setHidden:YES];
105 [pathPasswordContainer setHidden:YES];
106 [errorContainer setHidden:NO];
107 [errorLabel setStringValue:error];
108}
109
110@end