From a16c9867458fb3c320b40615c007985dc360f131 Mon Sep 17 00:00:00 2001 From: Kateryna Kostiuk Date: Wed, 3 May 2017 13:30:14 -0400 Subject: [PATCH] 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: Alexandre Lision --- CMakeLists.txt | 4 +- src/AccountSelectionManager.h | 27 +++++++++++ src/AccountSelectionManager.mm | 84 ++++++++++++++++++++++++++++++++++ src/ChooseAccountVC.mm | 11 +++++ src/main.mm | 3 ++ 5 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 src/AccountSelectionManager.h create mode 100644 src/AccountSelectionManager.mm diff --git a/CMakeLists.txt b/CMakeLists.txt index 81909c91..9389c083 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/src/AccountSelectionManager.h b/src/AccountSelectionManager.h new file mode 100644 index 00000000..43861a31 --- /dev/null +++ b/src/AccountSelectionManager.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2015-2017 Savoir-faire Linux Inc. + * Author: Kateryna Kostiuk + * + * 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 + +@interface AccountSelectionManager : NSObject + +- (void) saveAccountWithIndex:(QModelIndex )index; +- (void) selectChosenAccount; + +@end diff --git a/src/AccountSelectionManager.mm b/src/AccountSelectionManager.mm new file mode 100644 index 00000000..335db25c --- /dev/null +++ b/src/AccountSelectionManager.mm @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2015-2017 Savoir-faire Linux Inc. + * Author: Kateryna Kostiuk + * + * 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 +#import +#import +#import + +#import "AccountSelectionManager.h" + +@implementation AccountSelectionManager + +NSString* const savedUserAccountKey = @"savedUserSelectedAccountKey"; + +- (void) saveAccountWithIndex:(QModelIndex )index { + if(!index.isValid()) { + return; + } + QByteArray accountID = index.data(static_cast(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 diff --git a/src/ChooseAccountVC.mm b/src/ChooseAccountVC.mm index 7ca77c7f..691dc50b 100644 --- a/src/ChooseAccountVC.mm +++ b/src/ChooseAccountVC.mm @@ -37,6 +37,7 @@ //RING #import "views/AccountMenuItemView.h" +#import "AccountSelectionManager.h" @interface ChooseAccountVC () @@ -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 diff --git a/src/main.mm b/src/main.mm index f13b5630..99ed8ec8 100644 --- a/src/main.mm +++ b/src/main.mm @@ -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(LoadOptions::FORCE_ENABLED); -- GitLab