save user selected account

Use the GSettings to save the selected account and restore it on
launch. If no valid account is selected, then select the first
enabled RING account, then the first enabled SIP account, if any.

Change-Id: I3da2fecab49ee4c3deae47fe3c4ca106530e989c
Reviewed-by: Nicolas Jäger <nicolas.jager@savoirfairelinux.com>
diff --git a/src/ringmainwindow.cpp b/src/ringmainwindow.cpp
index cda0e50..7d222a2 100644
--- a/src/ringmainwindow.cpp
+++ b/src/ringmainwindow.cpp
@@ -775,7 +775,9 @@
     /* show the account selector */
     show_combobox_account_selector(win, TRUE);
 
-    /* init the selection for the account selector */
+    /* select the newly created account */
+    // TODO: the new account might not always be the first one; eg: if the user has an existing
+    //       SIP account
     gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combobox_account_selector), 0);
 }
 
@@ -1116,9 +1118,6 @@
     {
         gtk_widget_show(priv->ring_settings);
         show_combobox_account_selector(win, TRUE);
-
-        /* init the selection for the account selector */
-        gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combobox_account_selector), 0);
     }
 }
 
@@ -1380,10 +1379,12 @@
                                        (GtkCellLayoutDataFunc)print_account_and_state,
                                        nullptr, nullptr);
 
-    g_signal_connect(priv->combobox_account_selector, "changed", G_CALLBACK(selected_account_changed), win);
-
     /* init the selection for the account selector */
-    selected_account_changed(GTK_COMBO_BOX(priv->combobox_account_selector), win);
+    auto selected_idx = AvailableAccountModel::instance().selectionModel()->currentIndex();
+    if (selected_idx.isValid())
+        gtk_combo_box_set_active(GTK_COMBO_BOX(priv->combobox_account_selector), selected_idx.row());
+
+    g_signal_connect(priv->combobox_account_selector, "changed", G_CALLBACK(selected_account_changed), win);
 
     g_object_unref(account_model);
 }