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