1 | /* |
---|
2 | * BCGroup.cpp |
---|
3 | * |
---|
4 | * Created on: Jun 20, 2009 |
---|
5 | * Author: saemy |
---|
6 | */ |
---|
7 | |
---|
8 | #include "BCGroup.h" |
---|
9 | |
---|
10 | #include "BCBinary.h" |
---|
11 | |
---|
12 | BCGroup::BCGroup() |
---|
13 | : id_(-1) |
---|
14 | , name_("") |
---|
15 | , image_(QImage()) |
---|
16 | { |
---|
17 | eventMapper_ = new GroupEventMapper(this); |
---|
18 | } |
---|
19 | |
---|
20 | GroupEventMapper* BCGroup::eventMapper() const { |
---|
21 | return eventMapper_; |
---|
22 | } |
---|
23 | IGroupEvents* BCGroup::groupEvents() const { |
---|
24 | return static_cast<IGroupEvents*>(eventMapper()); |
---|
25 | } |
---|
26 | |
---|
27 | |
---|
28 | int BCGroup::id() const{ |
---|
29 | return id_; |
---|
30 | } |
---|
31 | QString BCGroup::name() const{ |
---|
32 | return name_; |
---|
33 | } |
---|
34 | QImage BCGroup::image() const{ |
---|
35 | return image_; |
---|
36 | } |
---|
37 | QSet<IContact*> BCGroup::contacts() const{ |
---|
38 | return contactList_.values().toSet(); |
---|
39 | } |
---|
40 | |
---|
41 | |
---|
42 | void BCGroup::setId(int id){ |
---|
43 | if (id_ == id) return; |
---|
44 | |
---|
45 | int oldId = id_; |
---|
46 | id_ = id; |
---|
47 | |
---|
48 | emit groupEvents()->idChanged(oldId, id); |
---|
49 | emit groupEvents()->dataChanged(); |
---|
50 | } |
---|
51 | void BCGroup::setName(const QString& name){ |
---|
52 | SET_IF_DIFFERENT(name_, name.trimmed()); |
---|
53 | emit groupEvents()->dataChanged(); |
---|
54 | } |
---|
55 | void BCGroup::setImage(const QImage& image){ |
---|
56 | SET_IF_DIFFERENT(image_, image); |
---|
57 | emit groupEvents()->dataChanged(); |
---|
58 | } |
---|
59 | void BCGroup::setContacts(const QSet<IContact*>& contacts){ |
---|
60 | foreach (IContact* contact, contactList_) { |
---|
61 | if (!contacts.contains(contact)) |
---|
62 | removeContactFromList(contact); |
---|
63 | } |
---|
64 | foreach (IContact* contact, contacts) { |
---|
65 | addContactToList(contact); |
---|
66 | } |
---|
67 | } |
---|
68 | void BCGroup::addContact(IContact* contact){ |
---|
69 | addContactToList(contact); |
---|
70 | } |
---|
71 | void BCGroup::removeContact(int contactId){ |
---|
72 | removeContact(contactList_[contactId]); |
---|
73 | } |
---|
74 | void BCGroup::removeContact(IContact* contact){ |
---|
75 | removeContactFromList(contact); |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | void BCGroup::addContactToList(IContact* contact) { |
---|
80 | if ((contact == NULL) || contactList_.contains(contact->id())) |
---|
81 | return; |
---|
82 | |
---|
83 | connect(contact->eventMapper(), SIGNAL(idChanged(int, int)), |
---|
84 | this, SLOT(contactIdChanged(int, int))); |
---|
85 | |
---|
86 | contactList_.insert(contact->id(), contact); |
---|
87 | emit groupEvents()->contactAdded(contact); |
---|
88 | } |
---|
89 | void BCGroup::removeContactFromList(IContact* contact) { |
---|
90 | if ((contact == NULL) || !contactList_.contains(contact->id())) |
---|
91 | return; |
---|
92 | |
---|
93 | disconnect(contact->eventMapper(), SIGNAL(idChanged(int, int)), |
---|
94 | this, SLOT(contactIdChanged(int, int))); |
---|
95 | |
---|
96 | contactList_.remove(contact->id()); |
---|
97 | emit groupEvents()->contactRemoved(contact); |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | void BCGroup::contactIdChanged(int oldId, int newId) { |
---|
102 | IContact* contact = contactList_.take(oldId); |
---|
103 | if (contact != NULL) |
---|
104 | contactList_.insert(newId, contact); |
---|
105 | } |
---|