Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 1 | /* |
Sébastien Blin | 029ffa8 | 2019-01-02 17:43:48 -0500 | [diff] [blame^] | 2 | * Copyright (C) 2016-2019 Savoir-faire Linux Inc. |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 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 |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 22 | #import <api/lrc.h> |
| 23 | #import <api/newaccountmodel.h> |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 24 | |
| 25 | //Ring |
| 26 | #import "views/ITProgressIndicator.h" |
| 27 | |
| 28 | @interface BackupAccountWC() <NSTextFieldDelegate> { |
| 29 | __unsafe_unretained IBOutlet NSPathControl* path; |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 30 | __unsafe_unretained IBOutlet ITProgressIndicator* progressIndicator; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 31 | __unsafe_unretained IBOutlet NSButton* cancelButton; |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | @end |
| 35 | |
| 36 | @implementation BackupAccountWC { |
| 37 | struct { |
| 38 | unsigned int didCompleteExport:1; |
| 39 | } delegateRespondsTo; |
| 40 | } |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 41 | |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 42 | @synthesize accountModel; |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 43 | |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 44 | -(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 45 | { |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 46 | if (self = [self initWithWindowNibName:nibNameOrNil]) |
| 47 | { |
| 48 | self.accountModel= accountModel; |
| 49 | } |
| 50 | return self; |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | - (void)windowDidLoad |
| 54 | { |
| 55 | [super windowDidLoad]; |
| 56 | [path setURL: [NSURL fileURLWithPath:NSHomeDirectory()]]; |
| 57 | } |
| 58 | |
| 59 | - (void)setDelegate:(id <BackupAccountDelegate>)aDelegate |
| 60 | { |
| 61 | if (self.delegate != aDelegate) { |
| 62 | [super setDelegate: aDelegate]; |
| 63 | delegateRespondsTo.didCompleteExport = [self.delegate respondsToSelector:@selector(didCompleteExportWithPath:)]; |
| 64 | } |
| 65 | } |
| 66 | |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 67 | - (void) setAllowFileSelection:(BOOL) b |
| 68 | { |
| 69 | _allowFileSelection = b; |
| 70 | [path setAllowedTypes:_allowFileSelection ? nil : [NSArray arrayWithObject:@"public.folder"]]; |
| 71 | } |
| 72 | |
| 73 | - (IBAction)completeAction:(id)sender |
| 74 | { |
Kateryna Kostiuk | 1436681 | 2018-08-24 16:30:20 -0400 | [diff] [blame] | 75 | auto finalURL = [path.URL URLByAppendingPathComponent:[@"Account_" stringByAppendingString: @(std::string(self.selectedAccountID + ".gz").c_str())]]; |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 76 | [self showLoading]; |
Kateryna Kostiuk | 1436681 | 2018-08-24 16:30:20 -0400 | [diff] [blame] | 77 | if (self.accountModel->exportToFile(self.selectedAccountID, finalURL.path.UTF8String)) { |
| 78 | if (delegateRespondsTo.didCompleteExport) { |
| 79 | [((id<BackupAccountDelegate>)self.delegate) didCompleteExportWithPath:finalURL]; |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 80 | } |
Kateryna Kostiuk | 1436681 | 2018-08-24 16:30:20 -0400 | [diff] [blame] | 81 | [self close]; |
| 82 | [self.window.sheetParent endSheet: self.window]; |
| 83 | } else { |
| 84 | [self showError]; |
| 85 | } |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | - (void)showLoading |
| 89 | { |
| 90 | [progressIndicator setNumberOfLines:30]; |
| 91 | [progressIndicator setWidthOfLine:2]; |
| 92 | [progressIndicator setLengthOfLine:5]; |
| 93 | [progressIndicator setInnerMargin:20]; |
| 94 | [super showLoading]; |
| 95 | } |
| 96 | |
Kateryna Kostiuk | 1436681 | 2018-08-24 16:30:20 -0400 | [diff] [blame] | 97 | - (IBAction)pathControlSingleClick:(id)sender { |
| 98 | [path setURL:[[path clickedPathComponentCell] URL]]; |
| 99 | } |
| 100 | |
| 101 | #pragma mark - NSPathControlDelegate |
| 102 | |
| 103 | - (void)pathControl:(NSPathControl *)pathControl willPopUpMenu:(NSMenu *)menu { |
| 104 | while ([[menu itemArray] count] >= 4) { |
| 105 | [menu removeItemAtIndex:3]; |
| 106 | } |
| 107 | } |
| 108 | - (void)pathControl:(NSPathControl *)pathControl willDisplayOpenPanel:(NSOpenPanel *)openPanel |
| 109 | { |
| 110 | NSLog(@"willDisplayOpenPanel"); |
| 111 | [openPanel setAllowsMultipleSelection:NO]; |
| 112 | [openPanel setResolvesAliases:YES]; |
| 113 | [openPanel setDirectory:NSHomeDirectory()]; |
| 114 | [openPanel setDelegate:self]; |
| 115 | } |
| 116 | |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 117 | @end |