Stepan Salenikovich | 75a3917 | 2015-07-10 13:21:08 -0400 | [diff] [blame] | 1 | /* |
Stepan Salenikovich | be87d2c | 2016-01-25 14:14:34 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
Stepan Salenikovich | 75a3917 | 2015-07-10 13:21:08 -0400 | [diff] [blame] | 3 | * 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 Salenikovich | 75a3917 | 2015-07-10 13:21:08 -0400 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "accounts.h" |
| 21 | |
| 22 | #include <accountmodel.h> |
| 23 | |
| 24 | /** |
| 25 | * returns TRUE if a RING account exists; FALSE otherwise |
| 26 | */ |
| 27 | gboolean |
| 28 | has_ring_account() |
| 29 | { |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 30 | return !AccountModel::instance().getAccountsByProtocol(Account::Protocol::RING).isEmpty(); |
Stepan Salenikovich | 75a3917 | 2015-07-10 13:21:08 -0400 | [diff] [blame] | 31 | } |
| 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 | */ |
| 38 | void |
| 39 | force_ring_display_name() |
| 40 | { |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 41 | auto ringaccounts = AccountModel::instance().getAccountsByProtocol(Account::Protocol::RING); |
Stepan Salenikovich | 75a3917 | 2015-07-10 13:21:08 -0400 | [diff] [blame] | 42 | 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 Salenikovich | 30e134e | 2015-09-24 21:20:25 -0400 | [diff] [blame] | 51 | |
| 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 | */ |
| 60 | Account* |
| 61 | get_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 Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 71 | int a_count = AccountModel::instance().rowCount(); |
Stepan Salenikovich | 30e134e | 2015-09-24 21:20:25 -0400 | [diff] [blame] | 72 | for (int i = 0; i < a_count && !registered_account; ++i) { |
Guillaume Roguez | 5d1514b | 2015-10-22 15:55:31 -0400 | [diff] [blame] | 73 | QModelIndex idx = AccountModel::instance().index(i, 0); |
| 74 | Account *account = AccountModel::instance().getAccountByModelIndex(idx); |
Stepan Salenikovich | 30e134e | 2015-09-24 21:20:25 -0400 | [diff] [blame] | 75 | 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 | } |