Commit 49cb2918 authored by Anthony Léonard's avatar Anthony Léonard Committed by Guillaume Roguez

reimplement ChooseAccountVC with new account model

This controller is in charge of the account selector shown at the top
right of the client.

It now uses the new account model in LRC to display account available
on the machine. As the account selection is now to be managed on
client instead of LRC, a lot has changed in the AccountSelectionManager
too.

Finally, RingWindowController gives a reference of the account model
to the ChooseAccountVC has we don't use singleton that are accessible
from anywhere anymore.

Change-Id: I5c320923cd561dc44f600d388793a338af89adfd
Reviewed-by: Guillaume Roguez's avatarGuillaume Roguez <guillaume.roguez@savoirfairelinux.com>
parent 79597607
/*
* Copyright (C) 2015-2017 Savoir-faire Linux Inc.
* Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
* Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com>
* Author: Anthony Léonard <anthony.leonard@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
......@@ -19,9 +21,20 @@
#import <Foundation/Foundation.h>
namespace lrc {
namespace api {
class NewAccountModel;
namespace account {
struct Info;
}
}
}
@interface AccountSelectionManager : NSObject
- (void) saveAccountWithIndex:(QModelIndex )index;
- (void) selectChosenAccount;
@property const lrc::api::account::Info& savedAccount;
- (id) initWithAccountModel:(const lrc::api::NewAccountModel*) accMdl;
@end
/*
* Copyright (C) 2015-2017 Savoir-faire Linux Inc.
* Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
* Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com>
* Author: Anthony Léonard <anthony.leonard@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
......@@ -18,67 +20,45 @@
*/
// LRC
#import <accountmodel.h>
#import <account.h>
#import <AvailableAccountModel.h>
#import <QItemSelectionModel.h>
#import <api/newaccountmodel.h>
#import <api/account.h>
#import "AccountSelectionManager.h"
@implementation AccountSelectionManager
@implementation AccountSelectionManager {
const lrc::api::NewAccountModel* accMdl_;
}
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];
- (id) initWithAccountModel:(const lrc::api::NewAccountModel*) accMdl {
accMdl_ = accMdl;
return [self init];
}
- (void) saveAccountWithId:(NSString*)accId
{
[[NSUserDefaults standardUserDefaults] setObject:accId 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());
- (NSString*) getSavedAccountId
{
return [[NSUserDefaults standardUserDefaults] stringForKey:savedUserAccountKey];
}
}
if (savedIndex.isValid()) {
AvailableAccountModel::instance().selectionModel()->setCurrentIndex(savedIndex, QItemSelectionModel::ClearAndSelect);
}
- (const lrc::api::account::Info&) savedAccount
{
return accMdl_->getAccountInfo(std::string([[self getSavedAccountId] UTF8String]));
}
- (void) setSavedAccount:(const lrc::api::account::Info&) acc
{
if (acc.profileInfo.type == lrc::api::profile::Type::INVALID)
return;
else
saveAccountWithId:@(acc.id.c_str());
}
@end
/*
* Copyright (C) 2015-2017 Savoir-faire Linux Inc.
* Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
* Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com>
* Author: Anthony Léonard <anthony.leonard@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
......@@ -19,9 +21,22 @@
#import <Cocoa/Cocoa.h>
namespace lrc {
namespace api {
class NewAccountModel;
namespace account {
struct Info;
}
}
}
@interface ChooseAccountVC : NSViewController
@property (readonly) const lrc::api::account::Info& selectedAccount;
-(void) enable;
-(void) disable;
-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil model:(const lrc::api::NewAccountModel*) accMdl;
@end
This diff is collapsed.
......@@ -33,6 +33,7 @@
#import <recentmodel.h>
#import <AvailableAccountModel.h>
#import <api/lrc.h>
#import <api/account.h>
// Ring
#import "AppDelegate.h"
......@@ -94,7 +95,7 @@ NSString* const kTrustRequestMenuItemIdentifier = @"TrustRequestMenuItemIde
currentCallVC = [[CurrentCallVC alloc] initWithNibName:@"CurrentCall" bundle:nil];
offlineVC = [[ConversationVC alloc] initWithNibName:@"Conversation" bundle:nil];
// toolbar items
chooseAccountVC = [[ChooseAccountVC alloc] initWithNibName:@"ChooseAccount" bundle:nil];
chooseAccountVC = [[ChooseAccountVC alloc] initWithNibName:@"ChooseAccount" bundle:nil model:&(lrc_->getAccountModel())];
contactRequestVC = [[ContactRequestVC alloc] initWithNibName:@"ContactRequest" bundle:nil];
[callView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[[currentCallVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
......@@ -106,9 +107,18 @@ NSString* const kTrustRequestMenuItemIdentifier = @"TrustRequestMenuItemIde
[currentCallVC initFrame];
[offlineVC initFrame];
[self checkAccountsToMigrate];
// Fresh run, we need to make sure RingID appears
[shareButton sendActionOn:NSLeftMouseDownMask];
[self updateRingID];
// display accounts to select
NSToolbar *toolbar = self.window.toolbar;
toolbar.delegate = self;
[toolbar insertItemWithItemIdentifier:kChangeAccountToolBarItemIdentifier atIndex:1];
[toolbar insertItemWithItemIdentifier:kTrustRequestMenuItemIdentifier atIndex:2];
}
// TODO: Reimplement with new LRC signals
- (void) connect
{
// Update Ring ID label based on account model changes
......@@ -167,25 +177,25 @@ NSString* const kTrustRequestMenuItemIdentifier = @"TrustRequestMenuItemIde
*/
- (void) updateRingID
{
Account* finalChoice = nullptr;
auto& account = [chooseAccountVC selectedAccount];
[ringIDLabel setStringValue:@""];
QModelIndex index = AvailableAccountModel::instance().selectionModel()->currentIndex();
finalChoice = index.data(static_cast<int>(Account::Role::Object)).value<Account*>();
if(finalChoice == nil || (finalChoice->protocol() != Account::Protocol::RING)) {
if(account.profileInfo.type != lrc::api::profile::Type::RING) {
self.hideRingID = YES;
return;
}
self.hideRingID = NO;
auto name = finalChoice->registeredName();
auto& registeredName = account.registeredName;
auto& ringID = account.profileInfo.uri;
NSString* uriToDisplay = nullptr;
if (!name.isNull() && !name.isEmpty()) {
uriToDisplay = name.toNSString();
if (!registeredName.empty()) {
uriToDisplay = @(registeredName.c_str());
} else {
uriToDisplay = finalChoice->username().toNSString();
uriToDisplay = @(ringID.c_str());
}
[ringIDLabel setStringValue:uriToDisplay];
[self drawQRCode:finalChoice->username().toNSString()];
[self drawQRCode:@(ringID.c_str())];
}
- (IBAction)shareRingID:(id)sender {
......@@ -320,6 +330,7 @@ NSString* const kTrustRequestMenuItemIdentifier = @"TrustRequestMenuItemIde
#endif
}
// TODO: Reimplement as a blocking loop when new LRC models handle migration
- (void)checkAccountsToMigrate
{
auto ringList = AccountModel::instance().accountsToMigrate();
......
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="11762" systemVersion="16D30a" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="13771" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<development version="7000" identifier="xcode"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11762"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="13771"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="AccountMenuItemView">
......@@ -20,7 +20,7 @@
<rect key="frame" x="0.0" y="0.0" width="256" height="50"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
<textField verticalHuggingPriority="750" allowsCharacterPickerTouchBarItem="NO" translatesAutoresizingMaskIntoConstraints="NO" id="V91-eS-dUh">
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="V91-eS-dUh">
<rect key="frame" x="8" y="25" width="44" height="20"/>
<constraints>
<constraint firstAttribute="height" constant="20" id="VWk-wM-WSB"/>
......@@ -31,7 +31,7 @@
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" allowsCharacterPickerTouchBarItem="NO" translatesAutoresizingMaskIntoConstraints="NO" id="0PP-Di-b7L">
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="0PP-Di-b7L">
<rect key="frame" x="218" y="25" width="35" height="20"/>
<constraints>
<constraint firstAttribute="height" constant="20" id="byN-LO-mgw"/>
......@@ -42,7 +42,7 @@
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" allowsCharacterPickerTouchBarItem="NO" translatesAutoresizingMaskIntoConstraints="NO" id="tLn-uH-gZ6">
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="tLn-uH-gZ6">
<rect key="frame" x="8" y="4" width="127" height="20"/>
<constraints>
<constraint firstAttribute="height" constant="20" id="14e-51-tZ3"/>
......@@ -53,7 +53,7 @@
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" allowsCharacterPickerTouchBarItem="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ttQ-VU-CG6">
<textField hidden="YES" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="ttQ-VU-CG6">
<rect key="frame" x="133" y="4" width="120" height="20"/>
<constraints>
<constraint firstAttribute="height" constant="20" id="x9W-YM-xpS"/>
......
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