blob: a7cccfcb7a179822f8c175a0d2ab4d4d9be1eb1f [file] [log] [blame]
Kateryna Kostiuka16c9862017-05-03 13:30:14 -04001/*
2 * Copyright (C) 2015-2017 Savoir-faire Linux Inc.
3 * Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
Anthony Léonard49cb2912017-11-13 16:15:39 -05004 * Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com>
5 * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com>
Kateryna Kostiuka16c9862017-05-03 13:30:14 -04006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22// LRC
Anthony Léonard49cb2912017-11-13 16:15:39 -050023#import <api/newaccountmodel.h>
24#import <api/account.h>
Kateryna Kostiuka16c9862017-05-03 13:30:14 -040025
26#import "AccountSelectionManager.h"
27
Anthony Léonard49cb2912017-11-13 16:15:39 -050028@implementation AccountSelectionManager {
29
30 const lrc::api::NewAccountModel* accMdl_;
31
32}
Kateryna Kostiuka16c9862017-05-03 13:30:14 -040033
34NSString* const savedUserAccountKey = @"savedUserSelectedAccountKey";
35
Anthony Léonard49cb2912017-11-13 16:15:39 -050036- (id) initWithAccountModel:(const lrc::api::NewAccountModel*) accMdl {
37 accMdl_ = accMdl;
38 return [self init];
Kateryna Kostiuka16c9862017-05-03 13:30:14 -040039}
40
Anthony Léonard49cb2912017-11-13 16:15:39 -050041- (void) saveAccountWithId:(NSString*)accId
42{
43 [[NSUserDefaults standardUserDefaults] setObject:accId forKey:savedUserAccountKey];
44}
Kateryna Kostiuka16c9862017-05-03 13:30:14 -040045
Anthony Léonard49cb2912017-11-13 16:15:39 -050046- (NSString*) getSavedAccountId
47{
48 return [[NSUserDefaults standardUserDefaults] stringForKey:savedUserAccountKey];
49}
Kateryna Kostiuka16c9862017-05-03 13:30:14 -040050
Anthony Léonard49cb2912017-11-13 16:15:39 -050051- (const lrc::api::account::Info&) savedAccount
52{
Anthony Léonardccb9d422018-02-13 16:00:23 -050053 NSString* savedAccountId = [self getSavedAccountId];
54 if (savedAccountId == nil) {
55 auto accId = accMdl_->getAccountList().front();
56 [self saveAccountWithId:@(accId.c_str())];
57 return accMdl_->getAccountInfo(accId);
58 } else
59 return accMdl_->getAccountInfo(std::string([savedAccountId UTF8String]));
Anthony Léonard49cb2912017-11-13 16:15:39 -050060}
61
62- (void) setSavedAccount:(const lrc::api::account::Info&) acc
63{
64 if (acc.profileInfo.type == lrc::api::profile::Type::INVALID)
65 return;
Anthony Léonardbe339112017-12-26 11:36:48 -050066
67 [self saveAccountWithId:@(acc.id.c_str())];
Kateryna Kostiuka16c9862017-05-03 13:30:14 -040068}
69
70@end