19 #include "metacontact_p.h"
21 #include <QSharedData>
27 class MetaContactData :
public QSharedData
31 QStringList contactUris;
32 AbstractContact::List contacts;
33 AbstractContact::Ptr personAddressee;
43 MetaContactProxy(
const AbstractContact::List &contacts)
44 : m_contacts(contacts)
49 if (key.startsWith(QLatin1String(
"all-"))) {
51 Q_FOREACH (
const AbstractContact::Ptr &contact, m_contacts) {
52 QVariant val = contact->customProperty(key);
53 Q_ASSERT(val.canConvert<QVariantList>() || val.isNull());
56 ret.append(val.toList());
61 Q_FOREACH (
const AbstractContact::Ptr &contact, m_contacts) {
62 QVariant val = contact->customProperty(key);
71 AbstractContact::List m_contacts;
76 MetaContact::MetaContact():
77 d(new MetaContactData)
81 MetaContact::MetaContact(
const QString &personUri,
const QMap<QString, AbstractContact::Ptr> &contacts):
82 d(new MetaContactData)
84 d->personUri = personUri;
86 QMap<QString, AbstractContact::Ptr>::const_iterator it = contacts.constBegin();
87 while (it != contacts.constEnd()) {
88 insertContactInternal(it.key(), it.value());
94 MetaContact::MetaContact(
const QString &contactUri,
const AbstractContact::Ptr &contact):
95 d(new MetaContactData)
97 d->personUri = contactUri;
98 insertContactInternal(contactUri, contact);
102 MetaContact::MetaContact(
const MetaContact &other)
108 MetaContact &MetaContact::operator=(
const MetaContact &other)
110 if (
this != &other) {
117 MetaContact::~MetaContact()
122 QString MetaContact::id()
const
127 bool MetaContact::isValid()
const
129 return !d->contacts.isEmpty();
132 QStringList MetaContact::contactUris()
const
134 return d->contactUris;
137 AbstractContact::Ptr MetaContact::contact(
const QString &contactUri)
139 int index = d->contactUris.indexOf(contactUri);
141 return d->contacts[index];
143 return AbstractContact::Ptr();
147 AbstractContact::List MetaContact::contacts()
const
152 const AbstractContact::Ptr &MetaContact::personAddressee()
const
154 return d->personAddressee;
157 int MetaContact::insertContact(
const QString &contactUri,
const AbstractContact::Ptr &contact)
159 int index = insertContactInternal(contactUri, contact);
163 qWarning() <<
"Inserting an already-present contact" << contactUri;
168 int MetaContact::insertContactInternal(
const QString &contactUri,
const AbstractContact::Ptr &contact)
170 if (d->contactUris.contains(contactUri)) {
175 int index = d->contacts.size();
176 d->contacts.append(contact);
177 d->contactUris.append(contactUri);
182 int MetaContact::updateContact(
const QString &contactUri,
const AbstractContact::Ptr &contact)
184 const int index = d->contactUris.indexOf(contactUri);
185 Q_ASSERT(index < 0 || d->contacts[index] == contact);
187 qWarning() <<
"contact not part of the metacontact";
192 int MetaContact::removeContact(
const QString &contactUri)
194 const int index = d->contactUris.indexOf(contactUri);
196 d->contacts.removeAt(index);
197 d->contactUris.removeAt(index);
203 void MetaContact::reload()
208 d->personAddressee = (d->contacts.size() == 1) ? d->contacts.first() : AbstractContact::Ptr(
new MetaContactProxy(d->contacts));
209 Q_ASSERT(d->personAddressee);
The KPeople namespace contains all the classes for Libkpeople.