selected account: save and restore

This commit save user chosen account in NSUserDefaults and restore
it on launch. If account is not saved then try to find registered
RING account, if it is not available search for SIP account.

Change-Id: I15294e163b4a69247532bf46dd2c115a7c7c4add
Reviewed-by: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
diff --git a/src/ChooseAccountVC.mm b/src/ChooseAccountVC.mm
index 7ca77c7..691dc50 100644
--- a/src/ChooseAccountVC.mm
+++ b/src/ChooseAccountVC.mm
@@ -37,6 +37,7 @@
 
 //RING
 #import "views/AccountMenuItemView.h"
+#import "AccountSelectionManager.h"
 
 @interface ChooseAccountVC () <NSMenuDelegate>
 
@@ -53,12 +54,14 @@
 NSMenu* accountsMenu;
 NSMenuItem* selectedMenuItem;
 QMetaObject::Connection accountUpdate;
+AccountSelectionManager* accountManager;
 
 - (void)awakeFromNib
 {
     [profileImage setWantsLayer: YES];
     profileImage.layer.cornerRadius = profileImage.frame.size.width / 2;
     profileImage.layer.masksToBounds = YES;
+    accountManager = [[AccountSelectionManager alloc] init];
 
     if (auto pro = ProfileModel::instance().selectedProfile()) {
         auto photo = GlobalInstances::pixmapManipulator().contactPhoto(pro->person(), {140,140});
@@ -78,6 +81,10 @@
     QObject::connect(AvailableAccountModel::instance().selectionModel(),
                      &QItemSelectionModel::currentChanged,
                      [self](const QModelIndex& idx){
+                         if(!idx.isValid()) {
+                             return;
+                         }
+                         [accountManager saveAccountWithIndex:idx];
                          [self update];
                      });
     QObject::connect(&AvailableAccountModel::instance(),
@@ -194,7 +201,11 @@
 - (IBAction)itemChanged:(id)sender {
     NSInteger row = [(NSPopUpButton *)sender indexOfSelectedItem] / 2;
     QModelIndex index = AvailableAccountModel::instance().selectionModel()->model()->index(row, 0);
+    if(!index.isValid()) {
+        return;
+    }
     AvailableAccountModel::instance().selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect);
+    [accountManager saveAccountWithIndex:index];
 }
 
 #pragma mark - NSMenuDelegate