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 | |
---|
12 | BCAccountManager* BCAccountManager::instance_=0; |
---|
13 | BCAccountManager* BCAccountManager::instance(){ |
---|
14 | return instance_ ? instance_ : (instance_ = new BCAccountManager); |
---|
15 | } |
---|
16 | |
---|
17 | void 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 | |
---|
31 | void 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 | |
---|
44 | void 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 | |
---|
56 | BCAccountLoadManager* BCAccountManager::accountLoadManager() { |
---|
57 | return BCAccountLoadManager::instance(); |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | IAccount* BCAccountManager::getAccount(int accountId){ |
---|
62 | return accountList_.value(accountId); |
---|
63 | } |
---|
64 | |
---|
65 | QSet<IAccount*> BCAccountManager::getAccountList(){ |
---|
66 | return QSet<IAccount*>::fromList(accountList_.values()); |
---|
67 | } |
---|
68 | |
---|
69 | QSet<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 | |
---|
81 | void BCAccountManager::saveAccount(IAccount* account){ |
---|
82 | PersistenceFactory::instance()->getAccountManager()->saveAccount(account); |
---|
83 | addAccountToList(account); |
---|
84 | } |
---|
85 | |
---|
86 | void 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 | |
---|
96 | void BCAccountManager::accountIdChanged(int oldId, int newId) { |
---|
97 | IAccount* account = accountList_.take(oldId); |
---|
98 | if (account != NULL) |
---|
99 | accountList_.insert(newId, account); |
---|
100 | } |
---|
101 | void BCAccountManager::accountDataChanged() { |
---|
102 | IAccount* account = static_cast<AccountEventMapper*>(sender())->account(); |
---|
103 | Q_ASSERT(account); |
---|
104 | emit accountUpdated(account); |
---|
105 | |
---|
106 | } |
---|