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 <guillaume.roguez@savoirfairelinux.com>
diff --git a/src/AccountSelectionManager.mm b/src/AccountSelectionManager.mm
index 335db25..f3e4a96 100644
--- a/src/AccountSelectionManager.mm
+++ b/src/AccountSelectionManager.mm
@@ -1,6 +1,8 @@
 /*
  *  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