source: SMSSender/src/business/BCAccountManager.cpp @ 67:578192d6fe64

3.0
Last change on this file since 67:578192d6fe64 was 67:578192d6fe64, checked in by Sämy Zehnder <saemy.zehnder@…>, 12 years ago
  • added synchttp
  • backup before removing these diamond interfaces...
File size: 3.1 KB
Line 
1/*
2 * BCAccountManager.cpp
3 *
4 *  Created on: Aug 30, 2009
5 *      Author: saemy
6 */
7
8#include "BCAccountManager.h"
9
10#include "../persistence/PersistenceFactory.h"
11
12BCAccountManager* BCAccountManager::instance_=0;
13BCAccountManager* BCAccountManager::instance(){
14    return instance_ ? instance_ : (instance_ = new BCAccountManager);
15}
16
17void BCAccountManager::addAccountToList(IAccount* account) {
18    if ((account == NULL) || accountList_.contains(account->id()))
19        return;
20
21    accountList_.insert(account->id(), account);
22
23    connect(account->eventMapper(), SIGNAL(idChanged(int, int)),
24            this, SLOT(accountIdChanged(int, int)));
25    connect(account->eventMapper(), SIGNAL(dataChanged()),
26            this, SLOT(accountDataChanged()));
27
28    emit accountAdded(account);
29}
30
31void BCAccountManager::removeAccountFromList(IAccount* account) {
32    if ((account == NULL) || !accountList_.contains(account->id()))
33        return;
34
35    disconnect(account->eventMapper(), SIGNAL(idChanged(int, int)),
36               this, SLOT(accountIdChanged(int, int)));
37    disconnect(account->eventMapper(), SIGNAL(dataChanged()),
38               this, SLOT(accountDataChanged()));
39
40    accountList_.remove(account->id());
41    emit accountRemoved(account);
42}
43
44void BCAccountManager::readAccountsFromStorage() {
45    QSet<IAccount*> accountList = PersistenceFactory::instance()->getAccountManager()->getAccountList();
46
47    foreach(IAccount* account, accountList_.values().toSet().subtract(accountList)) {
48        removeAccountFromList(account);
49    }
50
51    foreach(IAccount* account, accountList.subtract(accountList_.values().toSet())) {
52        addAccountToList(account);
53    }
54}
55
56BCAccountLoadManager* BCAccountManager::accountLoadManager() {
57    return BCAccountLoadManager::instance();
58}
59
60
61IAccount* BCAccountManager::getAccount(int accountId){
62    return accountList_.value(accountId);
63}
64
65QSet<IAccount*> BCAccountManager::getAccountList(){
66    return accountList_.values().toSet();
67}
68
69QSet<IAccount*> BCAccountManager::getAccountListByLoadState(AccountLoadState state) {
70    QSet<IAccount*> res;
71    QSetIterator<IAccount*> i(getAccountList());
72    while (i.hasNext()) {
73        IAccount* a = i.next();
74        if (accountLoadManager()->getLoadStateOfAccount(a) == state) {
75            res.insert(a);
76        }
77    }
78    return res;
79}
80
81void BCAccountManager::saveAccount(IAccount* account){
82    PersistenceFactory::instance()->getAccountManager()->saveAccount(account);
83    addAccountToList(account);
84}
85
86void BCAccountManager::removeAccount(int accountId){
87    IAccount* account = getAccount(accountId);
88    if (account == NULL)
89        return;
90
91    PersistenceFactory::instance()->getAccountManager()->removeAccount(account);
92    removeAccountFromList(account);
93}
94
95
96void BCAccountManager::accountIdChanged(int oldId, int newId) {
97    IAccount* account = accountList_.take(oldId);
98    if (account != NULL)
99        accountList_.insert(newId, account);
100}
101void BCAccountManager::accountDataChanged() {
102    AccountEventMapper* eventMapper = qobject_cast<AccountEventMapper*>(sender());
103    IAccount* account = eventMapper->account();
104    Q_ASSERT(account);
105    emit accountUpdated(account);
106
107}
Note: See TracBrowser for help on using the repository browser.