account: Add selected account in Toolbar

This commit add dropdown list to toolbar to change currently selected account.
After selection it update ring id used to share account info.

Tuleap: #1532
Change-Id: I9fe3e65513ec45dce8bd53d8611f4daca6081663
Reviewed-by: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
diff --git a/src/RingWindowController.mm b/src/RingWindowController.mm
index 62e43d5..c545f09 100644
--- a/src/RingWindowController.mm
+++ b/src/RingWindowController.mm
@@ -42,8 +42,9 @@
 #import "views/IconButton.h"
 #import "views/NSColor+RingTheme.h"
 #import "views/BackgroundView.h"
+#import "ChooseAccountVC.h"
 
-@interface RingWindowController () <MigrateRingAccountsDelegate>
+@interface RingWindowController () <MigrateRingAccountsDelegate, NSToolbarDelegate>
 
 @property (retain) MigrateRingAccountsWC* migrateWC;
 
@@ -64,20 +65,23 @@
 
     CurrentCallVC* currentCallVC;
     ConversationVC* offlineVC;
+
+    ChooseAccountVC* chooseAccountVC;
 }
 
-QMetaObject::Connection accountUpdate;
 static NSString* const kPreferencesIdentifier = @"PreferencesIdentifier";
+NSString* const kChangeAccountToolBarItemIdentifier = @"ChangeAccountToolBarItemIdentifier";
 
 - (void)windowDidLoad {
     [super windowDidLoad];
     [self.window setMovableByWindowBackground:YES];
 
     [self.window setBackgroundColor:[NSColor colorWithRed:242.0/255 green:242.0/255 blue:242.0/255 alpha:1.0]];
+    self.window.titleVisibility = NSWindowTitleHidden;
 
     currentCallVC = [[CurrentCallVC alloc] initWithNibName:@"CurrentCall" bundle:nil];
     offlineVC = [[ConversationVC alloc] initWithNibName:@"Conversation" bundle:nil];
-
+    chooseAccountVC = [[ChooseAccountVC alloc] initWithNibName:@"ChooseAccount" bundle:nil];
     [callView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
     [[currentCallVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
     [[offlineVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
@@ -94,13 +98,6 @@
 - (void) connect
 {
     // Update Ring ID label based on account model changes
-    QObject::disconnect(accountUpdate);
-    accountUpdate = QObject::connect(&AccountModel::instance(),
-                                     &AccountModel::dataChanged,
-                                     [=] {
-                                         [self updateRingID];
-                                     });
-
     QObject::connect(RecentModel::instance().selectionModel(),
                      &QItemSelectionModel::currentChanged,
                      [=](const QModelIndex &current, const QModelIndex &previous) {
@@ -137,6 +134,14 @@
                              [offlineVC animateOut];
                          }
                      });
+    QObject::connect(AccountModel::instance().userSelectionModel(),
+                     &QItemSelectionModel::currentChanged,
+                     [=](const QModelIndex &current, const QModelIndex &previous) {
+                         if(!current.isValid())
+                             return;
+                         [self updateRingID];
+                     });
+
 }
 
 /**
@@ -150,23 +155,26 @@
     Account* finalChoice = nullptr;
 
     [ringIDLabel setStringValue:@""];
-    auto ringList = AccountModel::instance().getAccountsByProtocol(Account::Protocol::RING);
-    for (int i = 0 ; i < ringList.size() && !registered ; ++i) {
-        auto account = ringList.value(i);
-        if (account->isEnabled()) {
-            if(!enabled) {
-                enabled = finalChoice = account;
-            }
-            if (account->registrationState() == Account::RegistrationState::READY) {
-                registered = enabled = finalChoice = account;
-            }
-        } else {
-            if (!finalChoice) {
-                finalChoice = account;
+    finalChoice = AccountModel::instance().userChosenAccount();
+
+    if(finalChoice == nil) {
+        auto ringList = AccountModel::instance().getAccountsByProtocol(Account::Protocol::RING);
+        for (int i = 0 ; i < ringList.size() && !registered ; ++i) {
+            auto account = ringList.value(i);
+            if (account->isEnabled()) {
+                if(!enabled) {
+                    enabled = finalChoice = account;
+                }
+                if (account->registrationState() == Account::RegistrationState::READY) {
+                    registered = enabled = finalChoice = account;
+                }
+            } else {
+                if (!finalChoice) {
+                    finalChoice = account;
+                }
             }
         }
     }
-
     auto name = finalChoice->registeredName();
     NSString* uriToDisplay = nullptr;
     if (!name.isNull() && !name.isEmpty()) {
@@ -319,10 +327,13 @@
         [self migrateRingAccount:acc];
     } else {
         // Fresh run, we need to make sure RingID appears
-        [self updateRingID];
         [shareButton sendActionOn:NSLeftMouseDownMask];
 
         [self connect];
+        // display accounts to select
+        NSToolbar *toolbar = self.window.toolbar;
+        toolbar.delegate = self;
+        [toolbar insertItemWithItemIdentifier:kChangeAccountToolBarItemIdentifier atIndex:1];
     }
 }
 
@@ -336,4 +347,15 @@
     [self checkAccountsToMigrate];
 }
 
+#pragma mark - NSToolbarDelegate
+- (nullable NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag{
+    if(!(itemIdentifier == kChangeAccountToolBarItemIdentifier)) {
+        return nil;
+    }
+    NSToolbarItem *toolbarItem = [[NSToolbarItem alloc] initWithItemIdentifier:kChangeAccountToolBarItemIdentifier];
+    CGRect frame = chooseAccountVC.view.frame;
+    toolbarItem.view = chooseAccountVC.view;
+    return toolbarItem;
+}
+
 @end