blob: c2591e8c7a697fb4802512ce56d038b549b80c29 [file] [log] [blame]
Stepan Salenikovich75a39172015-07-10 13:21:08 -04001/*
Stepan Salenikovichbe87d2c2016-01-25 14:14:34 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Stepan Salenikovich75a39172015-07-10 13:21:08 -04003 * Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Stepan Salenikovich75a39172015-07-10 13:21:08 -040018 */
19
20#include "accounts.h"
21
22#include <accountmodel.h>
23
24/**
25 * returns TRUE if a RING account exists; FALSE otherwise
26 */
27gboolean
28has_ring_account()
29{
Guillaume Roguez5d1514b2015-10-22 15:55:31 -040030 return !AccountModel::instance().getAccountsByProtocol(Account::Protocol::RING).isEmpty();
Stepan Salenikovich75a39172015-07-10 13:21:08 -040031}
32
33/**
34 * itterates through all existing accounts and make sure all RING accounts have
35 * a display name set; if a display name is empty, it is set to the alias of the
36 * account
37 */
38void
39force_ring_display_name()
40{
Guillaume Roguez5d1514b2015-10-22 15:55:31 -040041 auto ringaccounts = AccountModel::instance().getAccountsByProtocol(Account::Protocol::RING);
Stepan Salenikovich75a39172015-07-10 13:21:08 -040042 for (int i = 0; i < ringaccounts.size(); ++i) {
43 auto account = ringaccounts.at(i);
44 if (account->displayName().isEmpty()) {
45 g_debug("updating RING account display name to: %s", account->alias().toUtf8().constData());
46 account->setDisplayName(account->alias());
47 account << Account::EditAction::SAVE;
48 }
49 }
50}
Stepan Salenikovich30e134e2015-09-24 21:20:25 -040051
52/**
53 * Finds and returns the first RING account, in order of priority:
54 * 1. registered
55 * 2. enabled
56 * 3. existing
57 *
58 * Returns a nullptr if no RING acconts exist
59 */
60Account*
61get_active_ring_account()
62{
63 /* get the users Ring account
64 * if multiple accounts exist, get the first one which is registered,
65 * if none, then the first one which is enabled,
66 * if none, then the first one in the list of ring accounts
67 */
68 Account *registered_account = nullptr;
69 Account *enabled_account = nullptr;
70 Account *ring_account = nullptr;
Guillaume Roguez5d1514b2015-10-22 15:55:31 -040071 int a_count = AccountModel::instance().rowCount();
Stepan Salenikovich30e134e2015-09-24 21:20:25 -040072 for (int i = 0; i < a_count && !registered_account; ++i) {
Guillaume Roguez5d1514b2015-10-22 15:55:31 -040073 QModelIndex idx = AccountModel::instance().index(i, 0);
74 Account *account = AccountModel::instance().getAccountByModelIndex(idx);
Stepan Salenikovich30e134e2015-09-24 21:20:25 -040075 if (account->protocol() == Account::Protocol::RING) {
76 /* got RING account, check if active */
77 if (account->isEnabled()) {
78 /* got enabled account, check if connected */
79 if (account->registrationState() == Account::RegistrationState::READY) {
80 /* got registered account, use this one */
81 registered_account = enabled_account = ring_account = account;
82 // g_debug("got registered account: %s", ring_account->alias().toUtf8().constData());
83 } else {
84 /* not registered, but enabled, use if its the first one */
85 if (!enabled_account) {
86 enabled_account = ring_account = account;
87 // g_debug("got enabled ring accout: %s", ring_account->alias().toUtf8().constData());
88 }
89 }
90 } else {
91 /* not enabled, but a Ring account, use if its the first one */
92 if (!ring_account) {
93 ring_account = account;
94 // g_debug("got ring account: %s", ring_account->alias().toUtf8().constData());
95 }
96 }
97 }
98 }
99
100 return ring_account;
101}