BackupAccountWC.mm 3.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 *  Copyright (C) 2016 Savoir-faire Linux Inc.
 *  Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */
#import "BackupAccountWC.h"

//LRC
22 23 24
#import <api/lrc.h>
#import <api/newaccountmodel.h>
#import <account.h>
25 26 27 28 29 30 31

//Ring
#import "views/ITProgressIndicator.h"

@interface BackupAccountWC() <NSTextFieldDelegate> {
    __unsafe_unretained IBOutlet NSPathControl* path;
    __unsafe_unretained IBOutlet ITProgressIndicator* progressIndicator;
32
    __unsafe_unretained IBOutlet NSButton* cancelButton;
33 34 35 36 37 38 39 40 41 42
}

@end

@implementation BackupAccountWC {
    struct {
        unsigned int didCompleteExport:1;
    } delegateRespondsTo;
}

43
@synthesize accountModel;
44

45
-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel
46
{
47 48 49 50 51
    if (self = [self initWithWindowNibName:nibNameOrNil])
    {
        self.accountModel= accountModel;
    }
    return self;
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
}

- (void)windowDidLoad
{
    [super windowDidLoad];
    [path setURL: [NSURL fileURLWithPath:NSHomeDirectory()]];
}

- (void)setDelegate:(id <BackupAccountDelegate>)aDelegate
{
    if (self.delegate != aDelegate) {
        [super setDelegate: aDelegate];
        delegateRespondsTo.didCompleteExport = [self.delegate respondsToSelector:@selector(didCompleteExportWithPath:)];
    }
}

- (void) setAllowFileSelection:(BOOL) b
{
    _allowFileSelection = b;
    [path setAllowedTypes:_allowFileSelection ? nil : [NSArray arrayWithObject:@"public.folder"]];
}

- (IBAction)completeAction:(id)sender
{
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
76
    auto finalURL = [path.URL URLByAppendingPathComponent:[@"Account_" stringByAppendingString: @(std::string(self.selectedAccountID + ".gz").c_str())]];
77
    [self showLoading];
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
78 79 80
    if (self.accountModel->exportToFile(self.selectedAccountID, finalURL.path.UTF8String)) {
        if (delegateRespondsTo.didCompleteExport) {
            [((id<BackupAccountDelegate>)self.delegate) didCompleteExportWithPath:finalURL];
81
        }
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
82 83 84 85 86
        [self close];
        [self.window.sheetParent endSheet: self.window];
    } else {
        [self showError];
    }
87 88 89 90 91 92 93 94 95 96 97
}

- (void)showLoading
{
    [progressIndicator setNumberOfLines:30];
    [progressIndicator setWidthOfLine:2];
    [progressIndicator setLengthOfLine:5];
    [progressIndicator setInnerMargin:20];
    [super showLoading];
}

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
- (IBAction)pathControlSingleClick:(id)sender {
    [path setURL:[[path clickedPathComponentCell] URL]];
}

#pragma mark - NSPathControlDelegate

- (void)pathControl:(NSPathControl *)pathControl willPopUpMenu:(NSMenu *)menu {
    while ([[menu itemArray] count] >= 4) {
        [menu removeItemAtIndex:3];
    }
}
- (void)pathControl:(NSPathControl *)pathControl willDisplayOpenPanel:(NSOpenPanel *)openPanel
{
    NSLog(@"willDisplayOpenPanel");
    [openPanel setAllowsMultipleSelection:NO];
    [openPanel setResolvesAliases:YES];
    [openPanel setDirectory:NSHomeDirectory()];
    [openPanel setDelegate:self];
}

118
@end