source: SMSSender/src/business/bcaccountmanager.cpp @ 275:fc4bfb4b9bc4

Last change on this file since 275:fc4bfb4b9bc4 was 275:fc4bfb4b9bc4, checked in by Sämy Zehnder <saemy.zehnder@…>, 7 years ago
  • Corrected and centralized sms-counting for all gateways.
  • Synchronizing sms length with the post and sunrise gateways now.
  • Fixed bug in contact import where only already existing contact names were considered in duplicate name checking.
  • Showing the account name instead of the gateway name in the account list if no free sms count is there.
File size: 4.4 KB
Line 
1/*
2 smssender - A frontend for fast and easy SMS sending over different gateways.
3 Copyright (C) 2007-2012, gorrión. See http://smssender.gorrion.ch
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, see <http://www.gnu.org/licenses/>.
17 */
18#include "bcaccountmanager.h"
19
20#include "persistence/persistencefactory.h"
21
22S_SINGLETON_IMPL(BCAccountManager)
23
24BCAccountManager::BCAccountManager()
25    : IAccountManager(NULL) // singleton
26{
27    connect(accountInitManager(), SIGNAL(accountInitStateChanged(IAccount*)),
28            this, SIGNAL(accountInitStateChanged(IAccount*)));
29}
30
31void BCAccountManager::addAccountToList(IAccount* account) {
32    Q_ASSERT(account);
33    if (accountList_.contains(account->id()))
34        return;
35
36    accountList_.insert(account->id(), account);
37
38    connect(account, SIGNAL(idChanged(IAccount*, int)),
39            this, SLOT(onAccountIdChanged(IAccount*, int)));
40    connect(account, SIGNAL(dataChanged(IAccount*, int)),
41            this, SLOT(onAccountDataChanged(IAccount*, int)));
42    connect(account, SIGNAL(loggedIn(IAccount*)), this, SIGNAL(accountLoginStateChanged(IAccount*)));
43    connect(account, SIGNAL(loggedOut(IAccount*)), this, SIGNAL(accountLoginStateChanged(IAccount*)));
44
45    emit accountAdded(account);
46
47    // Init the newly added account
48    account->resetInitialization();
49    if (account->isEnabled()) {
50        accountInitManager()->initAccount(account);
51    }
52}
53
54void BCAccountManager::removeAccountFromList(IAccount* account) {
55    Q_ASSERT(account);
56    if (!accountList_.contains(account->id()))
57        return;
58
59    disconnect(account, 0, this, 0);
60
61    accountList_.remove(account->id());
62    emit accountRemoved(account);
63}
64
65QHash<SDummyAccount*, const EException*> BCAccountManager::readAccountsFromStorage() {
66    QHash<SDummyAccount*, const EException*> exceptions;
67    QSet<IAccount*> accountList = PersistenceFactory::instance()->getAccountManager()->getAccountList(this, exceptions);
68
69    foreach(IAccount* account, accountList_.values().toSet().subtract(accountList)) {
70        removeAccountFromList(account);
71    }
72
73    foreach(IAccount* account, accountList.subtract(accountList_.values().toSet())) {
74        addAccountToList(account);
75    }
76
77    // Add erroneous accounts
78    foreach(SDummyAccount* dummy, exceptions.keys()) {
79        if (accountList_.values().contains(dummy)) {
80            continue;
81        }
82
83        addAccountToList(dummy);
84    }
85
86    return exceptions;
87}
88
89IAccountInitManager* BCAccountManager::accountInitManager() {
90    return BCAccountInitManager::instance();
91}
92
93
94IAccount* BCAccountManager::getAccount(int accountId){
95    return accountList_.value(accountId);
96}
97
98QSet<IAccount*> BCAccountManager::getAccountList(){
99    return accountList_.values().toSet();
100}
101
102void BCAccountManager::saveAccount(IAccount* account){
103    Q_ASSERT(account);
104    PersistenceFactory::instance()->getAccountManager()->saveAccount(account);
105    addAccountToList(account);
106}
107
108void BCAccountManager::removeAccount(int accountId){
109    IAccount* account = getAccount(accountId);
110    if (!account)
111        return;
112
113    PersistenceFactory::instance()->getAccountManager()->removeAccount(account);
114    removeAccountFromList(account);
115}
116
117
118void BCAccountManager::onAccountIdChanged(IAccount* account, int oldId) {
119    IAccount* acc = accountList_.take(oldId);
120    if (acc) {
121        Q_ASSERT(acc == account);
122        accountList_.insert(acc->id(), acc);
123    }
124}
125void BCAccountManager::onAccountDataChanged(IAccount* account, int property) {
126    emit accountDataChanged(account, property);
127
128    switch (property) {
129        case IAccount::Enabled:
130        case IAccount::Username:
131        case IAccount::Password: {
132            // If the account has been en-/disabled or the username/password has been changed, then just reset the initialization
133            account->resetInitialization();
134            accountInitManager()->initAccount(account);
135
136            break;
137        }
138    }
139
140}
Note: See TracBrowser for help on using the repository browser.