AccountSelectionManager.mm 2.74 KB
Newer Older
1 2 3
/*
 *  Copyright (C) 2015-2017 Savoir-faire Linux Inc.
 *  Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
4 5
 *  Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com>
 *  Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 *  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.
 */

// LRC
23 24
#import <api/newaccountmodel.h>
#import <api/account.h>
25 26 27

#import "AccountSelectionManager.h"

28 29 30 31 32
@implementation AccountSelectionManager {

    const lrc::api::NewAccountModel* accMdl_;

}
33 34 35

NSString* const savedUserAccountKey = @"savedUserSelectedAccountKey";

36 37 38
- (id) initWithAccountModel:(const lrc::api::NewAccountModel*) accMdl {
    accMdl_ = accMdl;
    return [self init];
39 40
}

41 42 43 44
- (void) saveAccountWithId:(NSString*)accId
{
    [[NSUserDefaults standardUserDefaults] setObject:accId forKey:savedUserAccountKey];
}
45

46 47 48 49
- (NSString*) getSavedAccountId
{
    return [[NSUserDefaults standardUserDefaults] stringForKey:savedUserAccountKey];
}
50

51 52 53 54 55
- (void) clearSelectedAccount
{
    [[NSUserDefaults standardUserDefaults] setObject:nil forKey:savedUserAccountKey];
}

56 57
- (const lrc::api::account::Info&) savedAccount
{
58 59
    NSString* savedAccountId = [self getSavedAccountId];
    if (savedAccountId == nil) {
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
        NSException* noAccEx = [NSException
                                exceptionWithName:@"NoAccountSavedException"
                                reason:@"No saved account"
                                userInfo:nil];
        @throw noAccEx;
    } else {
        try {
            return accMdl_->getAccountInfo(std::string([savedAccountId UTF8String]));
        } catch (std::out_of_range& e) {
            NSException* outOfRangeEx = [NSException
                                         exceptionWithName:@"outofrange"
                                         reason:@"out of range"
                                         userInfo:nil];
            @throw outOfRangeEx;
        }
    }

77 78 79 80 81 82
}

- (void) setSavedAccount:(const lrc::api::account::Info&) acc
{
    if (acc.profileInfo.type == lrc::api::profile::Type::INVALID)
        return;
83 84

    [self saveAccountWithId:@(acc.id.c_str())];
85 86 87
}

@end