blob: 335db25ce4cfd08a297cb69eb26a4dd4828b7a87 [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>
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// LRC
21#import <accountmodel.h>
22#import <account.h>
23#import <AvailableAccountModel.h>
24#import <QItemSelectionModel.h>
25
26#import "AccountSelectionManager.h"
27
28@implementation AccountSelectionManager
29
30NSString* const savedUserAccountKey = @"savedUserSelectedAccountKey";
31
32- (void) saveAccountWithIndex:(QModelIndex )index {
33 if(!index.isValid()) {
34 return;
35 }
36 QByteArray accountID = index.data(static_cast<int>(Account::Role::Id)).toByteArray();
37 if (accountID.isEmpty()) {
38 return;
39 }
40 NSString* accountToNSString = QString::QString(accountID).toNSString();
41 [[NSUserDefaults standardUserDefaults] setObject:accountToNSString forKey:savedUserAccountKey];
42}
43
44
45- (void) selectChosenAccount {
46 NSString* savedAccount = [[NSUserDefaults standardUserDefaults] stringForKey:savedUserAccountKey];
47 if(!savedAccount || savedAccount.length <= 0) {
48 return;
49 }
50 const char* secondName = [savedAccount UTF8String];
51 QByteArray assountToarray = QByteArray::QByteArray(secondName);
52 if (strlen(assountToarray) <= 0) {
53 return;
54 }
55 if (!(AccountModel::instance().getById(assountToarray))) {
56 return;
57 }
58 auto account = AccountModel::instance().getById(assountToarray);
59 QModelIndex savedIndex = QModelIndex::QModelIndex();
60 // first try to get saved account
61 savedIndex = AvailableAccountModel::instance().mapFromSource(account->index());
62 if (savedIndex.isValid()) {
63 AvailableAccountModel::instance().selectionModel()->setCurrentIndex(savedIndex, QItemSelectionModel::ClearAndSelect);
64 return;
65 }
66 // if account is not saved, try to select RING account
67 if (auto account = AvailableAccountModel::instance().currentDefaultAccount(URI::SchemeType::RING)) {
68 savedIndex = AvailableAccountModel::instance().mapFromSource(account->index());
69 }
70 if (savedIndex.isValid()) {
71 AvailableAccountModel::instance().selectionModel()->setCurrentIndex(savedIndex, QItemSelectionModel::ClearAndSelect);
72 return;
73 }
74 // if no RING account try to select SIP
75 if (auto account = AvailableAccountModel::instance().currentDefaultAccount(URI::SchemeType::SIP)) {
76 savedIndex = AvailableAccountModel::instance().mapFromSource(account->index());
77
78 }
79 if (savedIndex.isValid()) {
80 AvailableAccountModel::instance().selectionModel()->setCurrentIndex(savedIndex, QItemSelectionModel::ClearAndSelect);
81 }
82}
83
84@end