utils: move RING account detection to utils
This code might be re-used elsewhere.
Issue: #80846
Change-Id: I5dd8036896ce94efca02514a06f3257ce20a7768
diff --git a/src/ringmainwindow.cpp b/src/ringmainwindow.cpp
index 5f114fb..c1f50ce 100644
--- a/src/ringmainwindow.cpp
+++ b/src/ringmainwindow.cpp
@@ -569,50 +569,6 @@
g_strdup_printf("<span fgcolor=\"gray\">%s</span>",
_("no Ring account")));
}
-
-}
-
-static void
-get_active_ring_account(RingMainWindow *win)
-{
- /* get the users Ring account
- * if multiple accounts exist, get the first one which is registered,
- * if none, then the first one which is enabled,
- * if none, then the first one in the list of ring accounts
- */
- Account *registered_account = NULL;
- Account *enabled_account = NULL;
- Account *ring_account = NULL;
- int a_count = AccountModel::instance()->rowCount();
- for (int i = 0; i < a_count && !registered_account; ++i) {
- QModelIndex idx = AccountModel::instance()->index(i, 0);
- Account *account = AccountModel::instance()->getAccountByModelIndex(idx);
- if (account->protocol() == Account::Protocol::RING) {
- /* got RING account, check if active */
- if (account->isEnabled()) {
- /* got enabled account, check if connected */
- if (account->registrationState() == Account::RegistrationState::READY) {
- /* got registered account, use this one */
- registered_account = enabled_account = ring_account = account;
- // g_debug("got registered account: %s", ring_account->alias().toUtf8().constData());
- } else {
- /* not registered, but enabled, use if its the first one */
- if (!enabled_account) {
- enabled_account = ring_account = account;
- // g_debug("got enabled ring accout: %s", ring_account->alias().toUtf8().constData());
- }
- }
- } else {
- /* not enabled, but a Ring account, use if its the first one */
- if (!ring_account) {
- ring_account = account;
- // g_debug("got ring account: %s", ring_account->alias().toUtf8().constData());
- }
- }
- }
- }
-
- show_ring_id(win, ring_account);
}
static void
@@ -1019,14 +975,14 @@
/* display ring id by first getting the active ring account */
gtk_widget_override_font(priv->label_ring_id, pango_font_description_from_string("monospace"));
- get_active_ring_account(win);
+ show_ring_id(win, get_active_ring_account());
QObject::connect(
AccountModel::instance(),
&AccountModel::dataChanged,
- [=] () {
+ [win] () {
/* check if the active ring account has changed,
* eg: if it was deleted */
- get_active_ring_account(win);
+ show_ring_id(win, get_active_ring_account());
}
);
diff --git a/src/utils/accounts.cpp b/src/utils/accounts.cpp
index ed5906e..fc8a626 100644
--- a/src/utils/accounts.cpp
+++ b/src/utils/accounts.cpp
@@ -59,3 +59,54 @@
}
}
}
+
+/**
+ * Finds and returns the first RING account, in order of priority:
+ * 1. registered
+ * 2. enabled
+ * 3. existing
+ *
+ * Returns a nullptr if no RING acconts exist
+ */
+Account*
+get_active_ring_account()
+{
+ /* get the users Ring account
+ * if multiple accounts exist, get the first one which is registered,
+ * if none, then the first one which is enabled,
+ * if none, then the first one in the list of ring accounts
+ */
+ Account *registered_account = nullptr;
+ Account *enabled_account = nullptr;
+ Account *ring_account = nullptr;
+ int a_count = AccountModel::instance()->rowCount();
+ for (int i = 0; i < a_count && !registered_account; ++i) {
+ QModelIndex idx = AccountModel::instance()->index(i, 0);
+ Account *account = AccountModel::instance()->getAccountByModelIndex(idx);
+ if (account->protocol() == Account::Protocol::RING) {
+ /* got RING account, check if active */
+ if (account->isEnabled()) {
+ /* got enabled account, check if connected */
+ if (account->registrationState() == Account::RegistrationState::READY) {
+ /* got registered account, use this one */
+ registered_account = enabled_account = ring_account = account;
+ // g_debug("got registered account: %s", ring_account->alias().toUtf8().constData());
+ } else {
+ /* not registered, but enabled, use if its the first one */
+ if (!enabled_account) {
+ enabled_account = ring_account = account;
+ // g_debug("got enabled ring accout: %s", ring_account->alias().toUtf8().constData());
+ }
+ }
+ } else {
+ /* not enabled, but a Ring account, use if its the first one */
+ if (!ring_account) {
+ ring_account = account;
+ // g_debug("got ring account: %s", ring_account->alias().toUtf8().constData());
+ }
+ }
+ }
+ }
+
+ return ring_account;
+}
diff --git a/src/utils/accounts.h b/src/utils/accounts.h
index fce3b4e..06fe1d2 100644
--- a/src/utils/accounts.h
+++ b/src/utils/accounts.h
@@ -33,6 +33,8 @@
#include <gtk/gtk.h>
+class Account;
+
/**
* returns TRUE if a RING account exists; FALSE otherwise
*/
@@ -40,11 +42,22 @@
has_ring_account();
/**
- * itterates through all existing accounts and make sure all RING accounts have
+ * iterates through all existing accounts and make sure all RING accounts have
* a display name set; if a display name is empty, it is set to the alias of the
* account
*/
void
force_ring_display_name();
+/**
+ * Finds and returns the first RING account, in order of priority:
+ * 1. registered
+ * 2. enabled
+ * 3. existing
+ *
+ * Returns a nullptr if no RING acconts exist
+ */
+Account*
+get_active_ring_account();
+
#endif /* _ACCOUNTS_H */