source: SMSSender/src/business/BCContactManager.cpp @ 71:415b45c71663

3.0
Last change on this file since 71:415b45c71663 was 71:415b45c71663, checked in by Sämy Zehnder <saemy.zehnder@…>, 12 years ago
  • Reorganized all the libraries. libinterfaces and libgateways are removed now and libdatatypes contains now all these files.
File size: 3.0 KB
Line 
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
12BCContactManager* BCContactManager::instance_=0;
13BCContactManager* BCContactManager::instance(){
14    return instance_ ? instance_ : (instance_ = new BCContactManager);
15}
16
17BCContactManager::BCContactManager(){
18    readContactsFromStorage();
19}
20
21
22void 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
36void 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
49void 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
61IContact* BCContactManager::getContact(int contactId) const {
62    return contactList_.value(contactId);
63}
64
65QSet<IContact*> BCContactManager::getContactList() const {
66    return contactList_.values().toSet();
67}
68
69void BCContactManager::saveContact(IContact* contact){
70    PersistenceFactory::instance()->getContactManager()->saveContact(contact);
71    addContactToList(contact);
72}
73
74void 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
85IContact* 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
95void BCContactManager::contactIdChanged(int oldId, int newId) {
96    IContact* contact = contactList_.take(oldId);
97    if (contact != NULL)
98        contactList_.insert(newId, contact);
99}
100void BCContactManager::contactDataChanged() {
101    IContact* contact= static_cast<SContactEventMapper*>(sender())->contact();
102    Q_ASSERT(contact);
103    emit contactUpdated(contact);
104}
Note: See TracBrowser for help on using the repository browser.