From 67669d4516bc7a0b18f5fc2ab3cd644d017570e3 Mon Sep 17 00:00:00 2001
From: kkostiuk <kateryna.kostiuk@savoirfairelinux.com>
Date: Thu, 30 Sep 2021 09:43:54 -0400
Subject: [PATCH] account: choose newly added account

When new account added switch selected account to a new one.

Change-Id: I6d9613b370b5fbcfe69d51d1f46b39e55cebc046
---
 src/AddSIPAccountVC.h          |  3 ++-
 src/AddSIPAccountVC.mm         |  6 +++---
 src/ConnectToAccManagerVC.h    |  3 ++-
 src/ConnectToAccManagerVC.mm   |  4 ++--
 src/RingWindowController.mm    | 13 ++++++++++++-
 src/RingWizardLinkAccountVC.h  |  3 ++-
 src/RingWizardLinkAccountVC.mm |  4 ++--
 src/RingWizardWC.h             |  6 ++++++
 src/RingWizardWC.mm            | 22 ++++++++++++++++++++--
 9 files changed, 51 insertions(+), 13 deletions(-)

diff --git a/src/AddSIPAccountVC.h b/src/AddSIPAccountVC.h
index 402b4abc..11a4fae3 100644
--- a/src/AddSIPAccountVC.h
+++ b/src/AddSIPAccountVC.h
@@ -19,9 +19,10 @@
 
 #import <Cocoa/Cocoa.h>
 #import "LrcModelsProtocol.h"
+#include <qstring.h>
 
 @protocol AddSIPAccountDelegate <NSObject>
-- (void)completedWithSuccess:(BOOL)success;
+- (void)completedWithSuccess:(BOOL)success accountId:(const QString &)accountId;
 - (void)showView:(NSView*)view;
 @end
 
diff --git a/src/AddSIPAccountVC.mm b/src/AddSIPAccountVC.mm
index 67476640..447d7183 100644
--- a/src/AddSIPAccountVC.mm
+++ b/src/AddSIPAccountVC.mm
@@ -72,7 +72,7 @@ NSTimer* timeoutTimer;
 
 - (IBAction)cancel:(id)sender
 {
-    [self.delegate completedWithSuccess: NO];
+    [self.delegate completedWithSuccess: NO accountId:""];
 }
 
 - (IBAction)addAccount:(id)sender
@@ -101,7 +101,7 @@ NSTimer* timeoutTimer;
                                           }
                                           self.accountModel->setAccountConfig(accountID, accountProperties);
                                           QObject::disconnect(accountCreated);
-                                          [self.delegate completedWithSuccess: YES];
+                                          [self.delegate completedWithSuccess: YES accountId: accountID];
                                       });
     accountToCreate = self.accountModel->createNewAccount(lrc::api::profile::Type::SIP, QString::fromNSString(displayName), "", "", "", QString::fromNSString(userNameField.stringValue));
 
@@ -113,7 +113,7 @@ NSTimer* timeoutTimer;
 
 -(void) addingAccountTimeout {
     QObject::disconnect(accountCreated);
-    [self.delegate completedWithSuccess: YES];
+    [self.delegate completedWithSuccess: YES accountId: accountToCreate];
 }
 
 
diff --git a/src/ConnectToAccManagerVC.h b/src/ConnectToAccManagerVC.h
index 35657f83..f2a1eff4 100644
--- a/src/ConnectToAccManagerVC.h
+++ b/src/ConnectToAccManagerVC.h
@@ -19,9 +19,10 @@
 
 #import <Cocoa/Cocoa.h>
 #import "LrcModelsProtocol.h"
+#include <qstring.h>
 
 @protocol RingWizardAccManagerDelegate <NSObject>
-- (void)didSignInSuccess:(BOOL)success;
+- (void)didSignInSuccess:(BOOL)success accountId:(const QString &)accountId;
 - (void)showView:(NSView*)view;
 @end
 
diff --git a/src/ConnectToAccManagerVC.mm b/src/ConnectToAccManagerVC.mm
index 885936eb..db4ad554 100644
--- a/src/ConnectToAccManagerVC.mm
+++ b/src/ConnectToAccManagerVC.mm
@@ -78,7 +78,7 @@ QString accointId;
 
 - (IBAction)dismissViewWithError:(id)sender
 {
-    [self.delegate didSignInSuccess:NO];
+    [self.delegate didSignInSuccess:NO accountId:""];
 }
 
 - (IBAction)startAgain:(id)sender
@@ -97,7 +97,7 @@ QString accointId;
                                           if(accountID.compare(accointId) != 0) {
                                               return;
                                           }
-                                          [self.delegate didSignInSuccess:YES];
+                                          [self.delegate didSignInSuccess:YES accountId: accointId];
                                           lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(accountID);
                                           accountProperties.Ringtone.ringtonePath = QString::fromNSString(defaultRingtonePath());
                                           self.accountModel->setAccountConfig(accountID, accountProperties);
diff --git a/src/RingWindowController.mm b/src/RingWindowController.mm
index cc4fd3a6..5bcb0041 100644
--- a/src/RingWindowController.mm
+++ b/src/RingWindowController.mm
@@ -57,7 +57,7 @@ typedef NS_ENUM(NSInteger, ViewState) {
     LEAVE_MESSAGE,
 };
 
-@interface RingWindowController () <MigrateRingAccountsDelegate>
+@interface RingWindowController () <MigrateRingAccountsDelegate, AccountCreationDelegate>
 
 @property (retain) MigrateRingAccountsWC* migrateWC;
 @property RingWizardWC* wizard;
@@ -630,10 +630,21 @@ typedef NS_ENUM(NSInteger, ViewState) {
 - (void) createNewAccount {
     [self changeViewTo:SHOW_WELCOME_SCREEN];
     wizard = [[RingWizardWC alloc] initWithNibName:@"RingWizard" bundle: nil accountmodel: self.accountModel];
+    wizard.delegate = self;
     [wizard showChooseWithCancelButton: YES];
     [self.window beginSheet:wizard.window completionHandler:nil];
 }
 
+- (void)accountCreated:(QString)accountId {
+    [chooseAccountVC selectAccount: accountId.toNSString()];
+    [settingsVC setSelectedAccount: accountId];
+    auto& accInfo = self.accountModel->getAccountInfo(accountId);
+    [smartViewVC setConversationModel:accInfo.conversationModel.get()];
+    [smartViewVC selectConversationList];
+    [self updateRingID];
+    [self changeViewTo:SHOW_WELCOME_SCREEN];
+}
+
 - (NSRect)window:(NSWindow *)window willPositionSheet:(NSWindow *)sheet
        usingRect:(NSRect)rect
 {
diff --git a/src/RingWizardLinkAccountVC.h b/src/RingWizardLinkAccountVC.h
index ed5fd21f..c881d576 100644
--- a/src/RingWizardLinkAccountVC.h
+++ b/src/RingWizardLinkAccountVC.h
@@ -19,9 +19,10 @@
 
 #import <Cocoa/Cocoa.h>
 #import "LrcModelsProtocol.h"
+#import <qstring.h>
 
 @protocol RingWizardLinkDelegate <NSObject>
-- (void)didLinkAccountWithSuccess:(BOOL)success;
+- (void)didLinkAccountWithSuccess:(BOOL)success accountId:(const QString&)accountId;
 - (void)showView:(NSView*)view;
 @end
 
diff --git a/src/RingWizardLinkAccountVC.mm b/src/RingWizardLinkAccountVC.mm
index 6ba7309d..6cc3cef4 100644
--- a/src/RingWizardLinkAccountVC.mm
+++ b/src/RingWizardLinkAccountVC.mm
@@ -119,11 +119,11 @@
                                            if(accountID.compare(accountToCreate) != 0) {
                                                return;
                                            }
-                                          [self.delegate didLinkAccountWithSuccess:YES];
                                           lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(accountID);
                                           accountProperties.Ringtone.ringtonePath = QString::fromNSString(defaultRingtonePath());
                                           self.accountModel->setAccountConfig(accountID, accountProperties);
                                           [self registerDefaultPreferences];
+                                          [self.delegate didLinkAccountWithSuccess: YES accountId: accountToCreate];
                                           QObject::disconnect(accountCreated);
                                           QObject::disconnect(accountRemoved);
                                       });
@@ -156,7 +156,7 @@
 
 - (IBAction)dismissViewWithError:(id)sender
 {
-    [self.delegate didLinkAccountWithSuccess:NO];
+    [self.delegate didLinkAccountWithSuccess: NO accountId:""];
 }
 
 - (IBAction)pickBackupFile:(id)sender
diff --git a/src/RingWizardWC.h b/src/RingWizardWC.h
index 685a24ce..769a5589 100644
--- a/src/RingWizardWC.h
+++ b/src/RingWizardWC.h
@@ -25,6 +25,11 @@
 #import "AddSIPAccountVC.h"
 #import "ConnectToAccManagerVC.h"
 #import "AccountBackupVC.h"
+#include <qstring.h>
+
+@protocol AccountCreationDelegate <NSObject>
+- (void)accountCreated:(QString)accountId;
+@end
 
 @interface RingWizardWC : NSWindowController <NSWindowDelegate, NSPathControlDelegate,
     NSOpenSavePanelDelegate, RingWizardChooseDelegate, RingWizardNewDelegate,
@@ -35,4 +40,5 @@ LrcModelsProtocol>
 - (void)showLinkAccountVC;
 - (void)showSIPAccountVC;
 - (void)showConnectToAccountManager;
+@property (weak, nonatomic) id <AccountCreationDelegate> delegate;
 @end
diff --git a/src/RingWizardWC.mm b/src/RingWizardWC.mm
index 7a600d79..d5328d9e 100644
--- a/src/RingWizardWC.mm
+++ b/src/RingWizardWC.mm
@@ -204,19 +204,37 @@
         return;
     }
     [self showBackUpAccount: accountId];
+    if (success) {
+        [self.delegate accountCreated: accountId];
+    }
+}
+
+#pragma - AddSIPAccountDelegate methods
+
+- (void)completedWithSuccess:(BOOL)success accountId:(const QString &)accountId; {
+    [self completedWithSuccess:success];
+    if (success) {
+        [self.delegate accountCreated: accountId];
+    }
 }
 
 #pragma - WizardLinkAccountDelegate methods
 
-- (void)didLinkAccountWithSuccess:(BOOL)success
+- (void)didLinkAccountWithSuccess:(BOOL)success accountId:(const QString &)accountId
 {
     [self completedWithSuccess:success];
+    if (success) {
+        [self.delegate accountCreated: accountId];
+    }
 }
 
 #pragma - RingWizardAccManagerDelegate
 
-- (void)didSignInSuccess:(BOOL)success {
+- (void)didSignInSuccess:(BOOL)success accountId:(const QString &)accountId {
     [self completedWithSuccess:success];
+    if (success) {
+        [self.delegate accountCreated: accountId];
+    }
 }
 
 - (void)showBackUpAccount:(const QString&)accountId{
-- 
GitLab