blob: 5422d865d87c5887c409e33e4e440796b4d6b75f [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];
Kateryna Kostiuke8d06572019-01-09 13:21:38 -050056 NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDownloadsDirectory, NSUserDomainMask, YES);
57 if(paths.count <= 0) {
58 return;
59 }
60 NSString * downloadPath = [paths objectAtIndex:0];
61 [path setURL: [NSURL URLWithString: downloadPath]];
Alexandre Lision886cde12016-10-25 17:39:49 -040062}
63
64- (void)setDelegate:(id <BackupAccountDelegate>)aDelegate
65{
66 if (self.delegate != aDelegate) {
67 [super setDelegate: aDelegate];
68 delegateRespondsTo.didCompleteExport = [self.delegate respondsToSelector:@selector(didCompleteExportWithPath:)];
69 }
70}
71
Alexandre Lision886cde12016-10-25 17:39:49 -040072- (void) setAllowFileSelection:(BOOL) b
73{
74 _allowFileSelection = b;
75 [path setAllowedTypes:_allowFileSelection ? nil : [NSArray arrayWithObject:@"public.folder"]];
76}
77
78- (IBAction)completeAction:(id)sender
79{
Kateryna Kostiuk14366812018-08-24 16:30:20 -040080 auto finalURL = [path.URL URLByAppendingPathComponent:[@"Account_" stringByAppendingString: @(std::string(self.selectedAccountID + ".gz").c_str())]];
Alexandre Lision886cde12016-10-25 17:39:49 -040081 [self showLoading];
Kateryna Kostiuk14366812018-08-24 16:30:20 -040082 if (self.accountModel->exportToFile(self.selectedAccountID, finalURL.path.UTF8String)) {
83 if (delegateRespondsTo.didCompleteExport) {
84 [((id<BackupAccountDelegate>)self.delegate) didCompleteExportWithPath:finalURL];
Alexandre Lision886cde12016-10-25 17:39:49 -040085 }
Kateryna Kostiuk14366812018-08-24 16:30:20 -040086 [self close];
87 [self.window.sheetParent endSheet: self.window];
88 } else {
89 [self showError];
90 }
Alexandre Lision886cde12016-10-25 17:39:49 -040091}
92
93- (void)showLoading
94{
95 [progressIndicator setNumberOfLines:30];
96 [progressIndicator setWidthOfLine:2];
97 [progressIndicator setLengthOfLine:5];
98 [progressIndicator setInnerMargin:20];
99 [super showLoading];
100}
101
Kateryna Kostiuk14366812018-08-24 16:30:20 -0400102- (IBAction)pathControlSingleClick:(id)sender {
103 [path setURL:[[path clickedPathComponentCell] URL]];
104}
105
106#pragma mark - NSPathControlDelegate
107
108- (void)pathControl:(NSPathControl *)pathControl willPopUpMenu:(NSMenu *)menu {
109 while ([[menu itemArray] count] >= 4) {
110 [menu removeItemAtIndex:3];
111 }
112}
113- (void)pathControl:(NSPathControl *)pathControl willDisplayOpenPanel:(NSOpenPanel *)openPanel
114{
Kateryna Kostiuk14366812018-08-24 16:30:20 -0400115 [openPanel setAllowsMultipleSelection:NO];
116 [openPanel setResolvesAliases:YES];
Kateryna Kostiuke8d06572019-01-09 13:21:38 -0500117 NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDownloadsDirectory, NSUserDomainMask, YES);
118 if(paths.count <= 0) {
119 return;
120 }
121 NSString * downloadPath = [paths objectAtIndex:0];
122 [openPanel setDirectoryURL:[NSURL URLWithString: downloadPath]];
Kateryna Kostiuk14366812018-08-24 16:30:20 -0400123 [openPanel setDelegate:self];
124}
125
Alexandre Lision886cde12016-10-25 17:39:49 -0400126@end