RingWizardLinkAccountVC.mm 6.43 KB
Newer Older
1 2 3
/*
 *  Copyright (C) 2015-2016 Savoir-faire Linux Inc.
 *  Author: Loïc Siret <loic.siret@savoirfairelinux.com>
4
 *  Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 *  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 "RingWizardLinkAccountVC.h"
//Cocoa
#import <Quartz/Quartz.h>

//Qt
#import <QUrl>
#import <QPixmap>

//LRC
30 31
#import <api/lrc.h>
#import <api/newaccountmodel.h>
32 33

#import "Constants.h"
34
#import "views/NSImage+Extensions.h"
35
#import "utils.h"
36 37 38 39 40 41 42 43 44 45 46 47 48

@interface RingWizardLinkAccountVC ()

@end

@implementation RingWizardLinkAccountVC {
    __unsafe_unretained IBOutlet NSView* initialContainer;

    __unsafe_unretained IBOutlet NSView* loadingContainer;
    __unsafe_unretained IBOutlet NSProgressIndicator* progressBar;

    __unsafe_unretained IBOutlet NSView* errorContainer;

49 50 51 52 53
    __unsafe_unretained IBOutlet NSTextField* pinTextField;
    __unsafe_unretained IBOutlet NSButton* fileButton;
    __unsafe_unretained IBOutlet NSSecureTextField* passwordTextField;

    __unsafe_unretained IBOutlet NSButton* linkButton;
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
54 55
    __unsafe_unretained IBOutlet NSPopover* helpPINContainer;
    __unsafe_unretained IBOutlet NSPopover* helpArchiveFileContainer;
56 57
    NSString *fileButtonTitleBackup;

58 59 60
    QMetaObject::Connection accountCreated;
    QMetaObject::Connection accountRemoved;
    std::string accountToCreate;
61 62
}

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
63
@synthesize accountModel, backupFile;
64 65 66 67 68 69 70 71 72 73 74

-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel {
    if (self =  [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
    {
        self.accountModel = accountModel;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
75 76
    fileButtonTitleBackup = NSLocalizedString(@"Select archive",
                                              @"export account button title");
77 78
}

79
- (void)show
80
{
81
    [self.delegate showView:initialContainer];
82 83 84 85
    [fileButton setTitle:fileButtonTitleBackup];
    backupFile = nil;
    [pinTextField setStringValue:@""];
    [pinTextField setEnabled:YES];
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
86
    [linkButton setEnabled:NO];
87
    [passwordTextField setStringValue:@""];
88 89
}

90 91
- (void)showError
{
92 93 94
    [self.delegate showView:errorContainer];
    QObject::disconnect(accountCreated);
    QObject::disconnect(accountRemoved);
95 96 97 98
}
- (void)showLoading
{
    [progressBar startAnimation:nil];
99
    [self.delegate showView:loadingContainer];
100 101 102 103
}

- (IBAction)importRingAccount:(id)sender
{
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    QObject::disconnect(accountCreated);
    QObject::disconnect(accountRemoved);
    accountCreated = QObject::connect(self.accountModel,
                                      &lrc::api::NewAccountModel::accountAdded,
                                      [self] (const std::string& accountID) {
                                          [self.delegate didLinkAccountWithSuccess:YES];
                                          [self registerDefaultPreferences];
                                          QObject::disconnect(accountCreated);
                                          QObject::disconnect(accountRemoved);
                                      });
    // account that is invalid will be removed, connect the signal to show error message
    accountRemoved = QObject::connect(self.accountModel,
                                      &lrc::api::NewAccountModel::accountRemoved,
                                      [self] (const std::string& accountID) {
                                          if(accountID.compare(accountToCreate) == 0) {
                                              [self showError];
                                          }
                                      });
122

123 124 125 126 127
    [self showLoading];
    NSString *pin = backupFile ? @"" : (self.pinValue ? self.pinValue : @"");
    NSString *archivePath = backupFile ? [backupFile path] : @"";
    NSString *pathword = self.passwordValue ? self.passwordValue : @"";
    accountToCreate = self.accountModel->createNewAccount(lrc::api::profile::Type::RING, "",[archivePath UTF8String], [pathword UTF8String], [pin UTF8String]);
128 129 130 131 132 133 134
}

- (IBAction)dismissViewWithError:(id)sender
{
    [self.delegate didLinkAccountWithSuccess:NO];
}

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
- (IBAction)pickBackupFile:(id)sender
{
    NSOpenPanel* filePicker = [NSOpenPanel openPanel];
    [filePicker setCanChooseFiles:YES];
    [filePicker setCanChooseDirectories:NO];
    [filePicker setAllowsMultipleSelection:NO];

    if ([filePicker runModal] == NSFileHandlingPanelOKButton) {
        if ([[filePicker URLs] count] == 1) {
            backupFile = [[filePicker URLs] objectAtIndex:0];
            [fileButton setTitle:[backupFile lastPathComponent]];
            [pinTextField setEnabled:NO];
            [pinTextField setStringValue:@""];
            [linkButton setEnabled:YES];
        }
    }
151 152
}

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
153 154 155 156 157 158 159 160 161 162
- (IBAction)showPINHelp:(id)sender
{
    [helpPINContainer showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMaxYEdge];
}

- (IBAction)showArchiveFileHelp:(id)sender
{
    [helpArchiveFileContainer showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMaxYEdge];
}

163 164 165 166 167
/**
 * Set default values for preferences
 */
- (void)registerDefaultPreferences
{
168 169 170 171 172 173 174 175
    if (!appSandboxed()) {
        // enable AutoStartup
        LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
        if (loginItemsRef == nil) return;
        CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
        LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL);
        if (itemRef) CFRelease(itemRef);
    }
176 177

    // enable Notifications
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
178 179
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:Preferences::CallNotifications];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:Preferences::MessagesNotifications];
180 181
}
@end