Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 1 | /* |
| 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 "BackupAccountWC.h" |
| 20 | |
| 21 | //LRC |
| 22 | #import <accountmodel.h> |
| 23 | |
| 24 | //Ring |
| 25 | #import "views/ITProgressIndicator.h" |
| 26 | |
| 27 | @interface BackupAccountWC() <NSTextFieldDelegate> { |
| 28 | __unsafe_unretained IBOutlet NSPathControl* path; |
| 29 | __unsafe_unretained IBOutlet NSSecureTextField* passwordField; |
| 30 | __unsafe_unretained IBOutlet NSSecureTextField* passwordConfirmationField; |
| 31 | __unsafe_unretained IBOutlet ITProgressIndicator* progressIndicator; |
| 32 | |
| 33 | } |
| 34 | |
| 35 | @end |
| 36 | |
| 37 | @implementation BackupAccountWC { |
| 38 | struct { |
| 39 | unsigned int didCompleteExport:1; |
| 40 | } delegateRespondsTo; |
| 41 | } |
| 42 | @synthesize accounts; |
| 43 | |
| 44 | - (id)initWithDelegate:(id <LoadingWCDelegate>) del |
| 45 | { |
| 46 | return [self initWithDelegate:del actionCode:0]; |
| 47 | } |
| 48 | |
| 49 | - (id)initWithDelegate:(id <BackupAccountDelegate>) del actionCode:(NSInteger) code |
| 50 | { |
| 51 | return [super initWithWindowNibName:@"BackupAccountWindow" delegate:del actionCode:code]; |
| 52 | } |
| 53 | |
| 54 | - (void)windowDidLoad |
| 55 | { |
| 56 | [super windowDidLoad]; |
| 57 | [path setURL: [NSURL fileURLWithPath:NSHomeDirectory()]]; |
| 58 | } |
| 59 | |
| 60 | - (void)setDelegate:(id <BackupAccountDelegate>)aDelegate |
| 61 | { |
| 62 | if (self.delegate != aDelegate) { |
| 63 | [super setDelegate: aDelegate]; |
| 64 | delegateRespondsTo.didCompleteExport = [self.delegate respondsToSelector:@selector(didCompleteExportWithPath:)]; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | - (BOOL)validatePasswords |
| 69 | { |
| 70 | BOOL result = (self.password.length != 0 && [self.password isEqualToString:self.passwordConfirmation]); |
| 71 | NSLog(@"ValidatesPasswords : %s", result ? "true" : "false"); |
| 72 | return result; |
| 73 | } |
| 74 | |
| 75 | + (NSSet *)keyPathsForValuesAffectingValidatePasswords |
| 76 | { |
| 77 | return [NSSet setWithObjects:@"password", @"passwordConfirmation", nil]; |
| 78 | } |
| 79 | |
| 80 | - (void) setAllowFileSelection:(BOOL) b |
| 81 | { |
| 82 | _allowFileSelection = b; |
| 83 | [path setAllowedTypes:_allowFileSelection ? nil : [NSArray arrayWithObject:@"public.folder"]]; |
| 84 | } |
| 85 | |
| 86 | - (IBAction)completeAction:(id)sender |
| 87 | { |
| 88 | auto finalURL = [path.URL URLByAppendingPathComponent:@"accounts.ring"]; |
| 89 | [self showLoading]; |
| 90 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ |
| 91 | int result = AccountModel::instance().exportAccounts(accounts, finalURL.path.UTF8String, passwordField.stringValue.UTF8String); |
| 92 | switch (result) { |
| 93 | case 0: |
| 94 | if (delegateRespondsTo.didCompleteExport){ |
| 95 | [((id<BackupAccountDelegate>)self.delegate) didCompleteExportWithPath:finalURL]; |
| 96 | } |
| 97 | [self close]; |
| 98 | break; |
| 99 | default:{ |
| 100 | [self showError] ; |
| 101 | }break; |
| 102 | } |
| 103 | }); |
| 104 | } |
| 105 | |
| 106 | - (void)showLoading |
| 107 | { |
| 108 | [progressIndicator setNumberOfLines:30]; |
| 109 | [progressIndicator setWidthOfLine:2]; |
| 110 | [progressIndicator setLengthOfLine:5]; |
| 111 | [progressIndicator setInnerMargin:20]; |
| 112 | [super showLoading]; |
| 113 | } |
| 114 | |
| 115 | @end |