blob: 4f51d3995242f8932e8b8d25b307eb6e7cfa3917 [file] [log] [blame]
Kateryna Kostiuk61d41162019-10-25 16:51:44 -04001/*
2 * Copyright (C) 2019 Savoir-faire Linux Inc.
3 * Author: Kateryna Kostiuk <kateryna.kostiuk@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
20#import "AccountBackupVC.h"
21#import "Constants.h"
22
23//LRC
24#import <api/lrc.h>
25#import <api/newaccountmodel.h>
26
27@interface AccountBackupVC () {
28 __unsafe_unretained IBOutlet NSView* initialView;
29 __unsafe_unretained IBOutlet NSView* errorView;
30 __unsafe_unretained IBOutlet NSButton* skipBackupButton;
31}
32
33@end
34
35@implementation AccountBackupVC
36@synthesize accountModel, accountToBackup;
37
38-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel {
39 if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
40 {
41 self.accountModel = accountModel;
42 }
43 return self;
44}
45
46-(void)show {
47 [self.view setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
48 [initialView setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
49 [errorView setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
50 BOOL skipBackup = [[NSUserDefaults standardUserDefaults] boolForKey: SkipBackUpPage];
51 [skipBackupButton setState: !skipBackup];
52 [self.delegate showView: initialView];
53}
54
55- (IBAction)skip:(id)sender
56{
57 [self.delegate completedWithSuccess:YES];
58}
59
60- (IBAction)startAgain:(id)sender
61{
62 [self.delegate showView: initialView];
63}
64
65- (IBAction)alwaysSkipBackup:(id)sender
66{
67 [[NSUserDefaults standardUserDefaults] setBool:![sender state] forKey:SkipBackUpPage];
68}
69
70- (IBAction)exportAccount:(id)sender
71{
72 NSSavePanel* filePicker = [NSSavePanel savePanel];
Kateryna Kostiukc867eb92020-03-08 13:15:17 -040073 NSString* name = [self.accountToBackup.toNSString() stringByAppendingString: @".gz"];
Kateryna Kostiuk61d41162019-10-25 16:51:44 -040074 [filePicker setNameFieldStringValue: name];
75 if ([filePicker runModal] != NSFileHandlingPanelOKButton) {
76 return;
77 }
78 NSString *password = @"";
79 const char* fullPath = [[filePicker URL] fileSystemRepresentation];
80 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.accountToBackup);
81 if(accountProperties.archiveHasPassword) {
82 NSAlert *alert = [[NSAlert alloc] init];
83 [alert addButtonWithTitle:@"OK"];
84 [alert addButtonWithTitle:@"Cancel"];
85 [alert setMessageText: NSLocalizedString(@"Enter account password",
86 @"Backup enter password")];
87 NSTextField *input = [[NSSecureTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 20)];
88 [alert setAccessoryView:input];
89 if ([alert runModal] != NSAlertFirstButtonReturn) {
90 return;
91 }
92 password = [input stringValue];
93 }
Kateryna Kostiukc867eb92020-03-08 13:15:17 -040094 if (self.accountModel->exportToFile(self.accountToBackup, fullPath, QString::fromNSString(password))) {
Kateryna Kostiuk61d41162019-10-25 16:51:44 -040095 [self.delegate completedWithSuccess:YES];
96 } else {
97 [self.delegate showView: errorView];
98 }
99}
100
101@end