blob: 67293921ac7257ab5b008cde3d0444d99561ead5 [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 "BackupAccountWC.h"
20
21//LRC
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040022#import <api/lrc.h>
23#import <api/newaccountmodel.h>
24#import <account.h>
Alexandre Lision886cde12016-10-25 17:39:49 -040025
26//Ring
27#import "views/ITProgressIndicator.h"
28
29@interface BackupAccountWC() <NSTextFieldDelegate> {
30 __unsafe_unretained IBOutlet NSPathControl* path;
Alexandre Lision886cde12016-10-25 17:39:49 -040031 __unsafe_unretained IBOutlet ITProgressIndicator* progressIndicator;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040032 __unsafe_unretained IBOutlet NSButton* cancelButton;
Alexandre Lision886cde12016-10-25 17:39:49 -040033}
34
35@end
36
37@implementation BackupAccountWC {
38 struct {
39 unsigned int didCompleteExport:1;
40 } delegateRespondsTo;
41}
Alexandre Lision886cde12016-10-25 17:39:49 -040042
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040043@synthesize accountModel;
Alexandre Lision886cde12016-10-25 17:39:49 -040044
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040045-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel
Alexandre Lision886cde12016-10-25 17:39:49 -040046{
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040047 if (self = [self initWithWindowNibName:nibNameOrNil])
48 {
49 self.accountModel= accountModel;
50 }
51 return self;
Alexandre Lision886cde12016-10-25 17:39:49 -040052}
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
Alexandre Lision886cde12016-10-25 17:39:49 -040068- (void) setAllowFileSelection:(BOOL) b
69{
70 _allowFileSelection = b;
71 [path setAllowedTypes:_allowFileSelection ? nil : [NSArray arrayWithObject:@"public.folder"]];
72}
73
74- (IBAction)completeAction:(id)sender
75{
Kateryna Kostiuk14366812018-08-24 16:30:20 -040076 auto finalURL = [path.URL URLByAppendingPathComponent:[@"Account_" stringByAppendingString: @(std::string(self.selectedAccountID + ".gz").c_str())]];
Alexandre Lision886cde12016-10-25 17:39:49 -040077 [self showLoading];
Kateryna Kostiuk14366812018-08-24 16:30:20 -040078 if (self.accountModel->exportToFile(self.selectedAccountID, finalURL.path.UTF8String)) {
79 if (delegateRespondsTo.didCompleteExport) {
80 [((id<BackupAccountDelegate>)self.delegate) didCompleteExportWithPath:finalURL];
Alexandre Lision886cde12016-10-25 17:39:49 -040081 }
Kateryna Kostiuk14366812018-08-24 16:30:20 -040082 [self close];
83 [self.window.sheetParent endSheet: self.window];
84 } else {
85 [self showError];
86 }
Alexandre Lision886cde12016-10-25 17:39:49 -040087}
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
Kateryna Kostiuk14366812018-08-24 16:30:20 -040098- (IBAction)pathControlSingleClick:(id)sender {
99 [path setURL:[[path clickedPathComponentCell] URL]];
100}
101
102#pragma mark - NSPathControlDelegate
103
104- (void)pathControl:(NSPathControl *)pathControl willPopUpMenu:(NSMenu *)menu {
105 while ([[menu itemArray] count] >= 4) {
106 [menu removeItemAtIndex:3];
107 }
108}
109- (void)pathControl:(NSPathControl *)pathControl willDisplayOpenPanel:(NSOpenPanel *)openPanel
110{
111 NSLog(@"willDisplayOpenPanel");
112 [openPanel setAllowsMultipleSelection:NO];
113 [openPanel setResolvesAliases:YES];
114 [openPanel setDirectory:NSHomeDirectory()];
115 [openPanel setDelegate:self];
116}
117
Alexandre Lision886cde12016-10-25 17:39:49 -0400118@end