1 | /* |
---|
2 | * ContactManager.cpp |
---|
3 | * |
---|
4 | * Created on: May 17, 2009 |
---|
5 | * Author: saemy |
---|
6 | */ |
---|
7 | |
---|
8 | #include "BCContactManager.h" |
---|
9 | |
---|
10 | #include "../persistence/PersistenceFactory.h" |
---|
11 | |
---|
12 | BCContactManager* BCContactManager::instance_=0; |
---|
13 | BCContactManager* BCContactManager::instance(){ |
---|
14 | return instance_ ? instance_ : (instance_ = new BCContactManager); |
---|
15 | } |
---|
16 | |
---|
17 | BCContactManager::BCContactManager(){ |
---|
18 | readContactsFromStorage(); |
---|
19 | } |
---|
20 | |
---|
21 | |
---|
22 | void BCContactManager::addContactToList(IContact* contact) { |
---|
23 | if ((contact == NULL) || contactList_.contains(contact->id())) |
---|
24 | return; |
---|
25 | |
---|
26 | contactList_.insert(contact->id(), contact); |
---|
27 | |
---|
28 | connect(contact->eventMapper(), SIGNAL(idChanged(int, int)), |
---|
29 | this, SLOT(contactIdChanged(int, int))); |
---|
30 | connect(contact->eventMapper(), SIGNAL(dataChanged()), |
---|
31 | this, SLOT(contactDataChanged())); |
---|
32 | |
---|
33 | emit contactAdded(contact); |
---|
34 | } |
---|
35 | |
---|
36 | void BCContactManager::removeContactFromList(IContact* contact) { |
---|
37 | if ((contact == NULL) || !contactList_.contains(contact->id())) |
---|
38 | return; |
---|
39 | |
---|
40 | disconnect(contact->eventMapper(), SIGNAL(idChanged(int, int)), |
---|
41 | this, SLOT(contactIdChanged(int, int))); |
---|
42 | disconnect(contact->eventMapper(), SIGNAL(dataChanged()), |
---|
43 | this, SLOT(contactDataChanged())); |
---|
44 | |
---|
45 | contactList_.remove(contact->id()); |
---|
46 | emit contactRemoved(contact); |
---|
47 | } |
---|
48 | |
---|
49 | void BCContactManager::readContactsFromStorage() { |
---|
50 | QSet<IContact*> contactList = PersistenceFactory::instance()->getContactManager()->getContactList(); |
---|
51 | |
---|
52 | foreach(IContact* contact, contactList_.values().toSet().subtract(contactList)) { |
---|
53 | removeContactFromList(contact); |
---|
54 | } |
---|
55 | |
---|
56 | foreach(IContact* contact, contactList.subtract(contactList_.values().toSet())) { |
---|
57 | addContactToList(contact); |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | IContact* BCContactManager::getContact(int contactId) const { |
---|
62 | return contactList_.value(contactId); |
---|
63 | } |
---|
64 | |
---|
65 | QSet<IContact*> BCContactManager::getContactList() const { |
---|
66 | return contactList_.values().toSet(); |
---|
67 | } |
---|
68 | |
---|
69 | void BCContactManager::saveContact(IContact* contact){ |
---|
70 | PersistenceFactory::instance()->getContactManager()->saveContact(contact); |
---|
71 | addContactToList(contact); |
---|
72 | } |
---|
73 | |
---|
74 | void BCContactManager::removeContact(int contactId){ |
---|
75 | IContact* contact = getContact(contactId); |
---|
76 | if (contact == NULL) |
---|
77 | return; |
---|
78 | |
---|
79 | PersistenceFactory::instance()->getContactManager()->removeContact(contact); |
---|
80 | removeContactFromList(contact); |
---|
81 | |
---|
82 | // TODO: Remove contact from contact groups -> (remove empty contact groups?) |
---|
83 | } |
---|
84 | |
---|
85 | IContact* BCContactManager::getContactByNumber(const SNumber number) const { |
---|
86 | foreach (IContact* contact, getContactList()) { |
---|
87 | if (number == contact->number()) { |
---|
88 | return contact; |
---|
89 | } |
---|
90 | } |
---|
91 | return NULL; |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | void BCContactManager::contactIdChanged(int oldId, int newId) { |
---|
96 | IContact* contact = contactList_.take(oldId); |
---|
97 | if (contact != NULL) |
---|
98 | contactList_.insert(newId, contact); |
---|
99 | } |
---|
100 | void BCContactManager::contactDataChanged() { |
---|
101 | IContact* contact= static_cast<ContactEventMapper*>(sender())->contact(); |
---|
102 | Q_ASSERT(contact); |
---|
103 | emit contactUpdated(contact); |
---|
104 | } |
---|