Commit 72128c9c authored by Anthony Léonard's avatar Anthony Léonard Committed by Kateryna Kostiuk

fix account selection after first account creation

Account wasn't properly set due to a not handled exception. This is no
longer the case as the first account of the list is now selected by
default when no account is yet saved in user configuration.

Moreover, having an empty AccountModel shouldn't cause any crash as
this case is now handled with exceptions.

Change-Id: Ibb3e386d80e946b2507916b3e44cb28af62bc3c7
Reviewed-by: Kateryna Kostiuk's avatarKateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
parent 61e3dcfd
......@@ -94,17 +94,21 @@ NSMenuItem* selectedMenuItem;
-(const lrc::api::account::Info&) selectedAccount
{
const auto& account = [accountSelectionManager_ savedAccount];
if(account.profileInfo.type == lrc::api::profile::Type::INVALID){
try {
auto accountId = accMdl_->getAccountList().at(0);
const auto& fallbackAccount = accMdl_->getAccountInfo(accMdl_->getAccountList().at(0));
try {
return [accountSelectionManager_ savedAccount];
} catch (std::out_of_range& e) { // Is thrown when no account are saved yet
auto accountList = accMdl_->getAccountList();
if (!accountList.empty()) {
const auto& fallbackAccount = accMdl_->getAccountInfo(accountList.at(0));
return fallbackAccount;
} catch (std::out_of_range& e) { // Is thrown if account model has no account. We then return an invalid account
return account;
} else {
NSException* noAccEx = [NSException
exceptionWithName:@"NoAccountException"
reason:@"No account in AccountModel"
userInfo:nil];
@throw noAccEx;
}
}
return account;
}
-(void) updateMenu {
......@@ -144,14 +148,19 @@ NSMenuItem* selectedMenuItem;
-(void) updatePhoto
{
auto& account = [self selectedAccount];
if(account.profileInfo.type == lrc::api::profile::Type::INVALID)
return;
@try {
auto& account = [self selectedAccount];
if(account.profileInfo.type == lrc::api::profile::Type::INVALID)
return;
QByteArray ba = QByteArray::fromStdString(account.profileInfo.avatar);
QByteArray ba = QByteArray::fromStdString(account.profileInfo.avatar);
QVariant photo = GlobalInstances::pixmapManipulator().personPhoto(ba);
[profileImage setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(photo))];
QVariant photo = GlobalInstances::pixmapManipulator().personPhoto(ba);
[profileImage setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(photo))];
}
@catch (NSException *ex) {
NSLog(@"Caught exception %@: %@", [ex name], [ex reason]);
}
}
-(NSString*) nameForAccount:(const lrc::api::account::Info&) account {
......@@ -209,11 +218,16 @@ NSMenuItem* selectedMenuItem;
return;
}
[self.view setHidden:NO];
auto& account = [self selectedAccount];
if(account.profileInfo.type == lrc::api::profile::Type::INVALID){
return;
@try {
auto& account = [self selectedAccount];
if(account.profileInfo.type == lrc::api::profile::Type::INVALID){
return;
}
[accountSelectionButton selectItemWithTitle:[self itemTitleForAccount:account]];
}
@catch (NSException *ex) {
NSLog(@"Caught exception %@: %@", [ex name], [ex reason]);
}
[accountSelectionButton selectItemWithTitle:[self itemTitleForAccount:account]];
}
#pragma mark - NSPopUpButton item selection
......
......@@ -108,7 +108,12 @@ NSString* const kTrustRequestMenuItemIdentifier = @"TrustRequestMenuItemIde
[currentCallVC initFrame];
[offlineVC initFrame];
[smartViewVC setConversationModel: [chooseAccountVC selectedAccount].conversationModel.get()];
@try {
[smartViewVC setConversationModel: [chooseAccountVC selectedAccount].conversationModel.get()];
}
@catch (NSException *ex) {
NSLog(@"Caught exception %@: %@", [ex name], [ex reason]);
}
// Fresh run, we need to make sure RingID appears
[shareButton sendActionOn:NSLeftMouseDownMask];
......@@ -168,25 +173,32 @@ NSString* const kTrustRequestMenuItemIdentifier = @"TrustRequestMenuItemIde
*/
- (void) updateRingID
{
auto& account = [chooseAccountVC selectedAccount];
@try {
auto& account = [chooseAccountVC selectedAccount];
[ringIDLabel setStringValue:@""];
[ringIDLabel setStringValue:@""];
if(account.profileInfo.type != lrc::api::profile::Type::RING) {
self.hideRingID = YES;
return;
if(account.profileInfo.type != lrc::api::profile::Type::RING) {
self.hideRingID = YES;
return;
}
self.hideRingID = NO;
auto& registeredName = account.registeredName;
auto& ringID = account.profileInfo.uri;
NSString* uriToDisplay = nullptr;
if (!registeredName.empty()) {
uriToDisplay = @(registeredName.c_str());
} else {
uriToDisplay = @(ringID.c_str());
}
[ringIDLabel setStringValue:uriToDisplay];
[self drawQRCode:@(ringID.c_str())];
}
self.hideRingID = NO;
auto& registeredName = account.registeredName;
auto& ringID = account.profileInfo.uri;
NSString* uriToDisplay = nullptr;
if (!registeredName.empty()) {
uriToDisplay = @(registeredName.c_str());
} else {
uriToDisplay = @(ringID.c_str());
@catch (NSException *ex) {
NSLog(@"Caught exception %@: %@", [ex name], [ex reason]);
self.hideRingID = NO;
[ringIDLabel setStringValue:@"No account available"];
}
[ringIDLabel setStringValue:uriToDisplay];
[self drawQRCode:@(ringID.c_str())];
}
- (IBAction)shareRingID:(id)sender {
......
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