1 | /* |
---|
2 | smssender - A frontend for fast and easy SMS sending over different gateways. |
---|
3 | Copyright (C) 2007-2012, gorrión. See http://smssender.gorrion.ch |
---|
4 | |
---|
5 | This program is free software: you can redistribute it and/or modify |
---|
6 | it under the terms of the GNU General Public License as published by |
---|
7 | the Free Software Foundation, either version 3 of the License, or |
---|
8 | (at your option) any later version. |
---|
9 | |
---|
10 | This program is distributed in the hope that it will be useful, |
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | GNU General Public License for more details. |
---|
14 | |
---|
15 | You should have received a copy of the GNU General Public License |
---|
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | */ |
---|
18 | #include "contactgroupmodel.h" |
---|
19 | |
---|
20 | #include <QDebug> |
---|
21 | #include <QIcon> |
---|
22 | |
---|
23 | namespace UI { |
---|
24 | |
---|
25 | ContactGroupListModel::ContactGroupListModel(QObject* parent, const ContactManager &contactManager, |
---|
26 | const GroupManager &groupManager) |
---|
27 | : QAbstractListModel(parent) |
---|
28 | , m_contactManager(contactManager) |
---|
29 | , m_groupManager(groupManager) |
---|
30 | { |
---|
31 | connect(&m_contactManager, SIGNAL(contactAdded(SContact*)), |
---|
32 | this, SLOT(onListAltered())); |
---|
33 | connect(&m_contactManager, SIGNAL(contactUpdated(SContact*)), |
---|
34 | this, SLOT(onDataUpdated(SContact*))); |
---|
35 | connect(&m_contactManager, SIGNAL(contactRemoved(SContact*)), |
---|
36 | this, SLOT(onListAltered())); |
---|
37 | |
---|
38 | connect(&m_groupManager, SIGNAL(groupAdded(SGroup*)), |
---|
39 | this, SLOT(onListAltered())); |
---|
40 | connect(&m_groupManager, SIGNAL(groupUpdated(SGroup*)), |
---|
41 | this, SLOT(onDataUpdated(SGroup*))); |
---|
42 | connect(&m_groupManager, SIGNAL(groupRemoved(SGroup*)), |
---|
43 | this, SLOT(onListAltered())); |
---|
44 | |
---|
45 | reloadList(); |
---|
46 | } |
---|
47 | |
---|
48 | void ContactGroupListModel::onListAltered() { |
---|
49 | reloadList(); |
---|
50 | } |
---|
51 | |
---|
52 | void ContactGroupListModel::onDataUpdated(SContact* contact) { |
---|
53 | dataUpdated(contact); |
---|
54 | } |
---|
55 | void ContactGroupListModel::onDataUpdated(SGroup *group) { |
---|
56 | dataUpdated(group); |
---|
57 | } |
---|
58 | void ContactGroupListModel::dataUpdated(const ContactGroupContainer& container) { |
---|
59 | int row = m_items.indexOf(container); |
---|
60 | |
---|
61 | QModelIndex idx1 = index(row, 0); |
---|
62 | QModelIndex idx2 = index(row, columnCount()); |
---|
63 | |
---|
64 | if (idx1.isValid() && idx2.isValid()) { // Contact/group in list |
---|
65 | emit dataChanged(idx1, idx2); |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | void ContactGroupListModel::reloadList() { |
---|
70 | m_items.clear(); |
---|
71 | foreach(SContact *contact, m_contactManager.contacts()) { |
---|
72 | m_items.append(contact); |
---|
73 | } |
---|
74 | foreach(SGroup *group, m_groupManager.groups()) { |
---|
75 | m_items.append(group); |
---|
76 | } |
---|
77 | |
---|
78 | reset(); |
---|
79 | } |
---|
80 | |
---|
81 | void ContactGroupListModel::addFilteredContact(const SContact& contact) { |
---|
82 | m_filteredItems.insert(&contact); |
---|
83 | m_items.removeAll(&contact); |
---|
84 | |
---|
85 | /* Filter groups, from whose all contacts are filtered */ |
---|
86 | foreach (const SGroup* group, groups()) { |
---|
87 | bool hasUnfilteredContacts = false; |
---|
88 | foreach (const SContact* contact, group->contacts()) { |
---|
89 | if (m_items.contains(contact)) { |
---|
90 | hasUnfilteredContacts = true; |
---|
91 | break; |
---|
92 | } |
---|
93 | } |
---|
94 | if (!hasUnfilteredContacts) { |
---|
95 | /* Add filtered Group */ |
---|
96 | m_filteredItems.insert(group); |
---|
97 | m_items.removeAll(group); |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | reset(); |
---|
102 | } |
---|
103 | void ContactGroupListModel::removeFilteredContact(const SContact& contact) { |
---|
104 | m_items.append(&contact); |
---|
105 | m_filteredItems.remove(&contact); |
---|
106 | |
---|
107 | /* Remove filter from groups, when all its contacts are unfiltered */ |
---|
108 | foreach (const SGroup* group, filteredGroups()) { |
---|
109 | bool hasFilteredContacts = false; |
---|
110 | foreach (const SContact* contact, group->contacts()) { |
---|
111 | if (m_filteredItems.contains(contact)) { |
---|
112 | hasFilteredContacts = true; |
---|
113 | break; |
---|
114 | } |
---|
115 | } |
---|
116 | if (!hasFilteredContacts) { |
---|
117 | /* Remove filtered group */ |
---|
118 | m_items.append(group); |
---|
119 | m_filteredItems.remove(group); |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | reset(); |
---|
124 | } |
---|
125 | |
---|
126 | ContactGroupContainer ContactGroupListModel::dataObject(const QModelIndex& index) const { |
---|
127 | if (!index.isValid() || (index.row() >= m_items.size())) { |
---|
128 | return ContactGroupContainer(); |
---|
129 | } else { |
---|
130 | return m_items.at(index.row()); |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | QVariant ContactGroupListModel::data(const QModelIndex& index, int role) const { |
---|
135 | if (!index.isValid() || index.row() < 0 || index.row() >= m_items.size()) |
---|
136 | return QVariant(); |
---|
137 | |
---|
138 | const ContactGroupContainer& container = m_items.at(index.row()); |
---|
139 | switch (role) { |
---|
140 | case Qt::DisplayRole: |
---|
141 | if (container.contact) { |
---|
142 | const SContact *contact = container.contact; |
---|
143 | QString contactStr = contact->name(); |
---|
144 | QString aliasStr = contact->aliases().join(", "); |
---|
145 | if (!aliasStr.isEmpty()) { |
---|
146 | contactStr += " (" + aliasStr + ")"; |
---|
147 | } |
---|
148 | return contactStr; |
---|
149 | } else { |
---|
150 | const SGroup *group = container.group; |
---|
151 | return "<" + group->name() + ">"; |
---|
152 | } |
---|
153 | break; |
---|
154 | |
---|
155 | case Qt::EditRole: |
---|
156 | if (container.contact) { |
---|
157 | return container.contact->name(); |
---|
158 | } else { |
---|
159 | return container.group->name(); |
---|
160 | } |
---|
161 | break; |
---|
162 | |
---|
163 | case Qt::DecorationRole: |
---|
164 | QImage img; |
---|
165 | if (container.contact) { |
---|
166 | img = container.contact->image(); |
---|
167 | } else { |
---|
168 | img = container.group->image(); |
---|
169 | } |
---|
170 | |
---|
171 | return QIcon(QPixmap::fromImage(img)); |
---|
172 | } |
---|
173 | |
---|
174 | return QVariant(); |
---|
175 | } |
---|
176 | |
---|
177 | QSet<const SGroup*> ContactGroupListModel::groups() const { |
---|
178 | QSet<const SGroup*> result; |
---|
179 | foreach (const ContactGroupContainer& c, m_items) { |
---|
180 | if (c.group) { |
---|
181 | result.insert(c.group); |
---|
182 | } |
---|
183 | } |
---|
184 | return result; |
---|
185 | } |
---|
186 | QSet<const SContact*> ContactGroupListModel::contacts() const { |
---|
187 | QSet<const SContact*> result; |
---|
188 | foreach (const ContactGroupContainer& c, m_items) { |
---|
189 | if (c.contact) { |
---|
190 | result.insert(c.contact); |
---|
191 | } |
---|
192 | } |
---|
193 | return result; |
---|
194 | } |
---|
195 | QSet<const SGroup*> ContactGroupListModel::filteredGroups() const { |
---|
196 | QSet<const SGroup*> result; |
---|
197 | foreach (const ContactGroupContainer& c, m_filteredItems) { |
---|
198 | if (c.group) { |
---|
199 | result.insert(c.group); |
---|
200 | } |
---|
201 | } |
---|
202 | return result; |
---|
203 | } |
---|
204 | QSet<const SContact*> ContactGroupListModel::filteredContacts() const { |
---|
205 | QSet<const SContact*> result; |
---|
206 | foreach (const ContactGroupContainer& c, m_filteredItems) { |
---|
207 | if (c.contact) { |
---|
208 | result.insert(c.contact); |
---|
209 | } |
---|
210 | } |
---|
211 | return result; |
---|
212 | } |
---|
213 | |
---|
214 | |
---|
215 | int ContactGroupListModel::columnCount(const QModelIndex& parent) const { |
---|
216 | return (parent.isValid()) ? 0 : 1; |
---|
217 | } |
---|
218 | |
---|
219 | int ContactGroupListModel::rowCount(const QModelIndex& parent) const { |
---|
220 | return (parent.isValid()) ? 0 : m_items.count(); |
---|
221 | } |
---|
222 | |
---|
223 | } // namespace UI |
---|