selected account: behaviour on conversation

This commit add a few changes to prevent bugs when account is changed
during call or conversation or when:

- Receiving an incoming call change selected account to be the same
as call destination account
- Making a call disable account selection
- Changing the selected account close current conversation

Tuleap: #1532
Change-Id: Ia37fabce0285a02233b35e69e73244e14d0a334a
Reviewed-by: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
diff --git a/src/AppDelegate.mm b/src/AppDelegate.mm
index ac8b630..c75002e 100644
--- a/src/AppDelegate.mm
+++ b/src/AppDelegate.mm
@@ -28,6 +28,8 @@
 #import <media/textrecording.h>
 #import <QItemSelectionModel>
 #import <account.h>
+#import <AvailableAccountModel.h>
+
 
 #if ENABLE_SPARKLE
 #import <Sparkle/Sparkle.h>
@@ -120,6 +122,14 @@
     QObject::connect(&CallModel::instance(),
                      &CallModel::incomingCall,
                      [=](Call* call) {
+                         // on incoming call set selected account match call destination account
+                         if (call->account()) {
+                             QModelIndex index = call->account()->index();
+                             index = AvailableAccountModel::instance().mapFromSource(index);
+
+                             AvailableAccountModel::instance().selectionModel()->setCurrentIndex(index,
+                                                                                                 QItemSelectionModel::ClearAndSelect);
+                         }
                          BOOL shouldComeToForeground = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::WindowBehaviour];
                          BOOL shouldNotify = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::Notifications];
                          if (shouldComeToForeground) {
diff --git a/src/ChooseAccountVC.h b/src/ChooseAccountVC.h
index 9c2e268..a0384e9 100644
--- a/src/ChooseAccountVC.h
+++ b/src/ChooseAccountVC.h
@@ -21,4 +21,7 @@
 
 @interface ChooseAccountVC : NSViewController
 
+-(void) enable;
+-(void) disable;
+
 @end
diff --git a/src/ChooseAccountVC.mm b/src/ChooseAccountVC.mm
index ac33ba0..422a33d 100644
--- a/src/ChooseAccountVC.mm
+++ b/src/ChooseAccountVC.mm
@@ -206,4 +206,11 @@
     selectedMenuItem = nil;
 }
 
+-(void) enable {
+    [accountSelectionButton setEnabled:YES];
+}
+-(void) disable {
+    [accountSelectionButton setEnabled:NO];
+}
+
 @end
diff --git a/src/RingWindowController.mm b/src/RingWindowController.mm
index 9f7e9ae..dec5330 100644
--- a/src/RingWindowController.mm
+++ b/src/RingWindowController.mm
@@ -108,15 +108,18 @@
                          if(!current.isValid()) {
                              [offlineVC animateOut];
                              [currentCallVC animateOut];
+                             [chooseAccountVC enable];
                              return;
                          }
 
                          if (!call) {
                              [currentCallVC animateOut];
                              [offlineVC animateIn];
+                             [chooseAccountVC enable];
                          } else {
                              [currentCallVC animateIn];
                              [offlineVC animateOut];
+                             [chooseAccountVC disable];
                          }
                      });
 
@@ -130,16 +133,19 @@
                          if (previous.isValid()) {
                              // We were already on a call
                              [currentCallVC animateOut];
+                             [chooseAccountVC enable];
                          } else {
                              // Make sure Conversation view hides when selecting a valid call
                              [currentCallVC animateIn];
                              [offlineVC animateOut];
+                             [chooseAccountVC disable];
                          }
                      });
     QObject::connect(AvailableAccountModel::instance().selectionModel(),
                      &QItemSelectionModel::currentChanged,
                      [self](const QModelIndex& idx){
                          [self updateRingID];
+                         [offlineVC animateOut];
                      });
 }