blob: 55b674df2144cea469d0cad2a25d44d4db481b95 [file] [log] [blame]
Alexandre Lision886cde12016-10-25 17:39:49 -04001/*
Sébastien Blin029ffa82019-01-02 17:43:48 -05002 * Copyright (C) 2016-2019 Savoir-faire Linux Inc.
Alexandre Lision886cde12016-10-25 17:39:49 -04003 * 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 Kostiuk1f8c1252018-07-30 18:18:57 -040022#import <api/lrc.h>
23#import <api/newaccountmodel.h>
Alexandre Lision886cde12016-10-25 17:39:49 -040024
25//Ring
26#import "views/ITProgressIndicator.h"
27
28@interface BackupAccountWC() <NSTextFieldDelegate> {
29 __unsafe_unretained IBOutlet NSPathControl* path;
Alexandre Lision886cde12016-10-25 17:39:49 -040030 __unsafe_unretained IBOutlet ITProgressIndicator* progressIndicator;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040031 __unsafe_unretained IBOutlet NSButton* cancelButton;
Alexandre Lision886cde12016-10-25 17:39:49 -040032}
33
34@end
35
36@implementation BackupAccountWC {
37 struct {
38 unsigned int didCompleteExport:1;
39 } delegateRespondsTo;
40}
Alexandre Lision886cde12016-10-25 17:39:49 -040041
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040042@synthesize accountModel;
Alexandre Lision886cde12016-10-25 17:39:49 -040043
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040044-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel
Alexandre Lision886cde12016-10-25 17:39:49 -040045{
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040046 if (self = [self initWithWindowNibName:nibNameOrNil])
47 {
48 self.accountModel= accountModel;
49 }
50 return self;
Alexandre Lision886cde12016-10-25 17:39:49 -040051}
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 Lision886cde12016-10-25 17:39:49 -040067- (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 Kostiuk14366812018-08-24 16:30:20 -040075 auto finalURL = [path.URL URLByAppendingPathComponent:[@"Account_" stringByAppendingString: @(std::string(self.selectedAccountID + ".gz").c_str())]];
Alexandre Lision886cde12016-10-25 17:39:49 -040076 [self showLoading];
Kateryna Kostiuk14366812018-08-24 16:30:20 -040077 if (self.accountModel->exportToFile(self.selectedAccountID, finalURL.path.UTF8String)) {
78 if (delegateRespondsTo.didCompleteExport) {
79 [((id<BackupAccountDelegate>)self.delegate) didCompleteExportWithPath:finalURL];
Alexandre Lision886cde12016-10-25 17:39:49 -040080 }
Kateryna Kostiuk14366812018-08-24 16:30:20 -040081 [self close];
82 [self.window.sheetParent endSheet: self.window];
83 } else {
84 [self showError];
85 }
Alexandre Lision886cde12016-10-25 17:39:49 -040086}
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 Kostiuk14366812018-08-24 16:30:20 -040097- (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 Lision886cde12016-10-25 17:39:49 -0400117@end