Commit 509670bb authored by Stepan Salenikovich's avatar Stepan Salenikovich Committed by Nicolas Jager

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: default avatarNicolas Jäger <nicolas.jager@savoirfairelinux.com>
parent 22667e42
......@@ -49,5 +49,10 @@
<summary>Entering a number in the search entry places a new call.</summary>
<description>Entering a number in the search entry places a new call. If false, then this will instead open a chat view.</description>
</key>
<key name="selected-account" type="s">
<default>""</default>
<summary>The user selected account.</summary>
<description>The account id of the user selected account; if empty there is no valid account selected.</description>
</key>
</schema>
</schemalist>
......@@ -54,6 +54,7 @@
#include <smartinfohub.h>
#include <media/textrecording.h>
#include <media/recordingmodel.h>
#include <availableaccountmodel.h>
// Ring client
#include "ring_client_options.h"
......@@ -503,6 +504,64 @@ chat_notifications_toggled(RingClient *self)
}
}
static void
selected_account_changed(RingClient *self)
{
auto priv = RING_CLIENT_GET_PRIVATE(self);
QByteArray account_id;
const auto idx = AvailableAccountModel::instance().selectionModel()->currentIndex();
if (idx.isValid()) {
account_id = idx.data(static_cast<int>(Account::Role::Id)).toByteArray();
if (account_id.isEmpty())
g_warning("selected account id is empty; possibly newly created account");
}
g_settings_set_string(priv->settings, "selected-account", account_id.constData());
}
static void
restore_selected_account(RingClient *self)
{
auto priv = RING_CLIENT_GET_PRIVATE(self);
gchar *saved_account_id = g_settings_get_string(priv->settings, "selected-account");
QModelIndex saved_idx;
// try to find this account
if (strlen(saved_account_id) > 0) {
if (auto account = AccountModel::instance().getById(saved_account_id)) {
saved_idx = AvailableAccountModel::instance().mapFromSource(account->index());
if (!saved_idx.isValid())
g_warning("could not select saved selected-account; it is possibly not enabled");
} else {
g_warning("could not find saved selected-account; it has possibly been deleted");
}
}
g_free(saved_account_id);
/* if no account selected; lets pick in the order of priority:
* 1. the first available Ring account
* 2. the first available SIP account
* 5. none (can't pick not enabled accounts)
*/
if (!saved_idx.isValid()) {
if (auto account = AvailableAccountModel::instance().currentDefaultAccount(URI::SchemeType::RING)) {
saved_idx = AvailableAccountModel::instance().mapFromSource(account->index());
}
}
if (!saved_idx.isValid()) {
if (auto account = AvailableAccountModel::instance().currentDefaultAccount(URI::SchemeType::SIP)) {
saved_idx = AvailableAccountModel::instance().mapFromSource(account->index());
}
}
AvailableAccountModel::instance().selectionModel()->setCurrentIndex(saved_idx, QItemSelectionModel::ClearAndSelect);
}
static void
ring_client_startup(GApplication *app)
{
......@@ -649,6 +708,13 @@ ring_client_startup(GApplication *app)
nm_client_new_async(priv->cancellable, (GAsyncReadyCallback)nm_client_cb, client);
#endif
/* keep track of the selected account */
QObject::connect(AvailableAccountModel::instance().selectionModel(),
&QItemSelectionModel::currentChanged,
[app] () { selected_account_changed(RING_CLIENT(app)); }
);
restore_selected_account(RING_CLIENT(app));
G_APPLICATION_CLASS(ring_client_parent_class)->startup(app);
}
......
......@@ -775,7 +775,9 @@ on_account_creation_completed(RingMainWindow *win)
/* 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 @@ handle_account_migrations(RingMainWindow *win)
{
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 @@ ring_main_window_init(RingMainWindow *win)
(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);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment