blob: 7be67ae0c00e8937f6e4fd1b39ca52e072dfea81 [file] [log] [blame]
Alexandre Lision886cde12016-10-25 17:39:49 -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 "RestoreAccountWC.h"
20
21//LRC
22#import <accountmodel.h>
23
24//Ring
25#import "views/ITProgressIndicator.h"
26
27@interface RestoreAccountWC() <NSTextFieldDelegate> {
28 __unsafe_unretained IBOutlet NSPathControl* path;
29 __unsafe_unretained IBOutlet NSSecureTextField* passwordField;
30 __unsafe_unretained IBOutlet ITProgressIndicator* progressIndicator;
31}
32
33@end
34
35@implementation RestoreAccountWC {
36 struct {
37 unsigned int didCompleteImport:1;
38 } delegateRespondsTo;
39}
40@synthesize accounts;
41
42- (id)initWithDelegate:(id <LoadingWCDelegate>) del
43{
44 return [self initWithDelegate:del actionCode:0];
45}
46
47- (id)initWithDelegate:(id <RestoreAccountDelegate>) del actionCode:(NSInteger) code
48{
49 return [super initWithWindowNibName:@"RestoreAccountWindow" delegate:del actionCode:code];
50}
51
52- (void)windowDidLoad
53{
54 [super windowDidLoad];
55 [path setURL: [NSURL fileURLWithPath:NSHomeDirectory()]];
56}
57
58- (void)setDelegate:(id <RestoreAccountDelegate>)aDelegate
59{
60 if (self.delegate != aDelegate) {
61 [super setDelegate: aDelegate];
62 delegateRespondsTo.didCompleteImport = [self.delegate respondsToSelector:@selector(didCompleteWithImport)];
63 }
64}
65
66- (void) setAllowFileSelection:(BOOL) b
67{
68 _allowFileSelection = b;
69 [path setAllowedTypes:_allowFileSelection ? nil : [NSArray arrayWithObject:@"public.folder"]];
70}
71
72- (IBAction)completeAction:(id)sender
73{
74 auto passwordString = passwordField.stringValue;
75 auto pathURL = path.URL;
76 [self showLoading];
77 SEL sel = @selector(importAccountsWithPath:andPassword:);
78 NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:sel]];
79 [inv setSelector:sel];
80 [inv setTarget:self];
81 //arguments 0 and 1 are self and _cmd respectively, automatically set by NSInvocation
82 [inv setArgument:&pathURL atIndex:2];
83 [inv setArgument:&passwordString atIndex:3];
84
85 // Schedule import for next iteration of event loop in order to let us start the loading anim
86 [inv performSelector:@selector(invoke) withObject:nil afterDelay:0];
87}
88
89- (void)showLoading
90{
91 [progressIndicator setNumberOfLines:30];
92 [progressIndicator setWidthOfLine:2];
93 [progressIndicator setLengthOfLine:5];
94 [progressIndicator setInnerMargin:20];
95 [super showLoading];
96}
97
98- (void) importAccountsWithPath:(NSURL*) urlPath andPassword:(NSString*) password
99{
100 int result = AccountModel::instance().importAccounts(urlPath.path.UTF8String, password.UTF8String);
101 switch (result) {
102 case 0:
103 if (delegateRespondsTo.didCompleteImport)
104 [((id<RestoreAccountDelegate>)self.delegate) didCompleteImport];
105 [self close];
106 break;
107 default:
108 {
109 [self showError];
110 }
111 break;
112 }
113}
114
115@end