Commit a16c9867 authored by Kateryna Kostiuk's avatar Kateryna Kostiuk Committed by Alexandre Lision

selected account: save and restore

This commit save user chosen account in NSUserDefaults and restore
it on launch. If account is not saved then try to find registered
RING account, if it is not available search for SIP account.

Change-Id: I15294e163b4a69247532bf46dd2c115a7c7c4add
Reviewed-by: default avatarAlexandre Lision <alexandre.lision@savoirfairelinux.com>
parent a7248468
......@@ -204,7 +204,9 @@ SET(ringclient_OTHERS
src/INDSequentialTextSelectionManager.mm
src/INDSequentialTextSelectionManager.h
src/delegates/ImageManipulationDelegate.mm
src/delegates/ImageManipulationDelegate.h)
src/delegates/ImageManipulationDelegate.h
src/AccountSelectionManager.h
src/AccountSelectionManager.mm)
SET(ringclient_XIBS
......
/*
* Copyright (C) 2015-2017 Savoir-faire Linux Inc.
* Author: Kateryna Kostiuk <kateryna.kostiuk@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 <Foundation/Foundation.h>
@interface AccountSelectionManager : NSObject
- (void) saveAccountWithIndex:(QModelIndex )index;
- (void) selectChosenAccount;
@end
/*
* Copyright (C) 2015-2017 Savoir-faire Linux Inc.
* Author: Kateryna Kostiuk <kateryna.kostiuk@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.
*/
// LRC
#import <accountmodel.h>
#import <account.h>
#import <AvailableAccountModel.h>
#import <QItemSelectionModel.h>
#import "AccountSelectionManager.h"
@implementation AccountSelectionManager
NSString* const savedUserAccountKey = @"savedUserSelectedAccountKey";
- (void) saveAccountWithIndex:(QModelIndex )index {
if(!index.isValid()) {
return;
}
QByteArray accountID = index.data(static_cast<int>(Account::Role::Id)).toByteArray();
if (accountID.isEmpty()) {
return;
}
NSString* accountToNSString = QString::QString(accountID).toNSString();
[[NSUserDefaults standardUserDefaults] setObject:accountToNSString forKey:savedUserAccountKey];
}
- (void) selectChosenAccount {
NSString* savedAccount = [[NSUserDefaults standardUserDefaults] stringForKey:savedUserAccountKey];
if(!savedAccount || savedAccount.length <= 0) {
return;
}
const char* secondName = [savedAccount UTF8String];
QByteArray assountToarray = QByteArray::QByteArray(secondName);
if (strlen(assountToarray) <= 0) {
return;
}
if (!(AccountModel::instance().getById(assountToarray))) {
return;
}
auto account = AccountModel::instance().getById(assountToarray);
QModelIndex savedIndex = QModelIndex::QModelIndex();
// first try to get saved account
savedIndex = AvailableAccountModel::instance().mapFromSource(account->index());
if (savedIndex.isValid()) {
AvailableAccountModel::instance().selectionModel()->setCurrentIndex(savedIndex, QItemSelectionModel::ClearAndSelect);
return;
}
// if account is not saved, try to select RING account
if (auto account = AvailableAccountModel::instance().currentDefaultAccount(URI::SchemeType::RING)) {
savedIndex = AvailableAccountModel::instance().mapFromSource(account->index());
}
if (savedIndex.isValid()) {
AvailableAccountModel::instance().selectionModel()->setCurrentIndex(savedIndex, QItemSelectionModel::ClearAndSelect);
return;
}
// if no RING account try to select SIP
if (auto account = AvailableAccountModel::instance().currentDefaultAccount(URI::SchemeType::SIP)) {
savedIndex = AvailableAccountModel::instance().mapFromSource(account->index());
}
if (savedIndex.isValid()) {
AvailableAccountModel::instance().selectionModel()->setCurrentIndex(savedIndex, QItemSelectionModel::ClearAndSelect);
}
}
@end
......@@ -37,6 +37,7 @@
//RING
#import "views/AccountMenuItemView.h"
#import "AccountSelectionManager.h"
@interface ChooseAccountVC () <NSMenuDelegate>
......@@ -53,12 +54,14 @@ Boolean menuNeedsUpdate;
NSMenu* accountsMenu;
NSMenuItem* selectedMenuItem;
QMetaObject::Connection accountUpdate;
AccountSelectionManager* accountManager;
- (void)awakeFromNib
{
[profileImage setWantsLayer: YES];
profileImage.layer.cornerRadius = profileImage.frame.size.width / 2;
profileImage.layer.masksToBounds = YES;
accountManager = [[AccountSelectionManager alloc] init];
if (auto pro = ProfileModel::instance().selectedProfile()) {
auto photo = GlobalInstances::pixmapManipulator().contactPhoto(pro->person(), {140,140});
......@@ -78,6 +81,10 @@ QMetaObject::Connection accountUpdate;
QObject::connect(AvailableAccountModel::instance().selectionModel(),
&QItemSelectionModel::currentChanged,
[self](const QModelIndex& idx){
if(!idx.isValid()) {
return;
}
[accountManager saveAccountWithIndex:idx];
[self update];
});
QObject::connect(&AvailableAccountModel::instance(),
......@@ -194,7 +201,11 @@ QMetaObject::Connection accountUpdate;
- (IBAction)itemChanged:(id)sender {
NSInteger row = [(NSPopUpButton *)sender indexOfSelectedItem] / 2;
QModelIndex index = AvailableAccountModel::instance().selectionModel()->model()->index(row, 0);
if(!index.isValid()) {
return;
}
AvailableAccountModel::instance().selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect);
[accountManager saveAccountWithIndex:index];
}
#pragma mark - NSMenuDelegate
......
......@@ -40,6 +40,7 @@
#import "backends/AddressBookBackend.h"
#import "delegates/ImageManipulationDelegate.h"
#import "AccountSelectionManager.h"
int main(int argc, const char *argv[]) {
......@@ -66,6 +67,8 @@ int main(int argc, const char *argv[]) {
}
}
AccountSelectionManager* manager = [[AccountSelectionManager alloc] init];
manager.selectChosenAccount;
CallModel::instance();
CategorizedHistoryModel::instance().addCollection<LocalHistoryCollection>(LoadOptions::FORCE_ENABLED);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment