• Skip to content
  • Skip to link menu
KDE API Documentation - metacontact.cpp Source File (KPeople)
  • KDE Home
  • Contact Us
 

KPeople

  • frameworks
  • kpeople
  • src
metacontact.cpp
1 /*
2  Copyright (C) 2013 David Edmundson <davidedmundson@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include "metacontact_p.h"
20 #include "global.h"
21 #include <QSharedData>
22 #include <QDebug>
23 #include <QSet>
24 
25 namespace KPeople
26 {
27 class MetaContactData : public QSharedData
28 {
29 public:
30  QString personUri;
31  QStringList contactUris;
32  AbstractContact::List contacts;
33  AbstractContact::Ptr personAddressee;
34 };
35 }
36 
37 // TODO: It feels like MetaContact and MetaContactProxy should be merged,
38 // still, not today.
39 
40 class MetaContactProxy : public KPeople::AbstractContact
41 {
42 public:
43  MetaContactProxy(const AbstractContact::List &contacts)
44  : m_contacts(contacts)
45  {}
46 
47  virtual QVariant customProperty(const QString &key) const
48  {
49  if (key.startsWith(QLatin1String("all-"))) {
50  QVariantList ret;
51  Q_FOREACH (const AbstractContact::Ptr &contact, m_contacts) {
52  QVariant val = contact->customProperty(key);
53  Q_ASSERT(val.canConvert<QVariantList>() || val.isNull());
54 
55  if (!val.isNull()) {
56  ret.append(val.toList());
57  }
58  }
59  return ret;
60  } else {
61  Q_FOREACH (const AbstractContact::Ptr &contact, m_contacts) {
62  QVariant val = contact->customProperty(key);
63  if (val.isValid()) {
64  return val;
65  }
66  }
67  return QVariant();
68  }
69  }
70 
71  AbstractContact::List m_contacts;
72 };
73 
74 using namespace KPeople;
75 
76 MetaContact::MetaContact():
77  d(new MetaContactData)
78 {
79 }
80 
81 MetaContact::MetaContact(const QString &personUri, const QMap<QString, AbstractContact::Ptr> &contacts):
82  d(new MetaContactData)
83 {
84  d->personUri = personUri;
85 
86  QMap<QString, AbstractContact::Ptr>::const_iterator it = contacts.constBegin();
87  while (it != contacts.constEnd()) {
88  insertContactInternal(it.key(), it.value());
89  it++;
90  }
91  reload();
92 }
93 
94 MetaContact::MetaContact(const QString &contactUri, const AbstractContact::Ptr &contact):
95  d(new MetaContactData)
96 {
97  d->personUri = contactUri;
98  insertContactInternal(contactUri, contact);
99  reload();
100 }
101 
102 MetaContact::MetaContact(const MetaContact &other)
103  : d(other.d)
104 {
105 
106 }
107 
108 MetaContact &MetaContact::operator=(const MetaContact &other)
109 {
110  if (this != &other) {
111  d = other.d;
112  }
113 
114  return *this;
115 }
116 
117 MetaContact::~MetaContact()
118 {
119 
120 }
121 
122 QString MetaContact::id() const
123 {
124  return d->personUri;
125 }
126 
127 bool MetaContact::isValid() const
128 {
129  return !d->contacts.isEmpty();
130 }
131 
132 QStringList MetaContact::contactUris() const
133 {
134  return d->contactUris;
135 }
136 
137 AbstractContact::Ptr MetaContact::contact(const QString &contactUri)
138 {
139  int index = d->contactUris.indexOf(contactUri);
140  if (index >= 0) {
141  return d->contacts[index];
142  } else {
143  return AbstractContact::Ptr();
144  }
145 }
146 
147 AbstractContact::List MetaContact::contacts() const
148 {
149  return d->contacts;
150 }
151 
152 const AbstractContact::Ptr &MetaContact::personAddressee() const
153 {
154  return d->personAddressee;
155 }
156 
157 int MetaContact::insertContact(const QString &contactUri, const AbstractContact::Ptr &contact)
158 {
159  int index = insertContactInternal(contactUri, contact);
160  if (index >= 0) {
161  reload();
162  } else {
163  qWarning() << "Inserting an already-present contact" << contactUri;
164  }
165  return index;
166 }
167 
168 int MetaContact::insertContactInternal(const QString &contactUri, const AbstractContact::Ptr &contact)
169 {
170  if (d->contactUris.contains(contactUri)) {
171  //if item is already listed, do nothing.
172  return -1;
173  } else {
174  //TODO if from the local address book - prepend to give higher priority.
175  int index = d->contacts.size();
176  d->contacts.append(contact);
177  d->contactUris.append(contactUri);
178  return index;
179  }
180 }
181 
182 int MetaContact::updateContact(const QString &contactUri, const AbstractContact::Ptr &contact)
183 {
184  const int index = d->contactUris.indexOf(contactUri);
185  Q_ASSERT(index < 0 || d->contacts[index] == contact);
186  if (index < 0) {
187  qWarning() << "contact not part of the metacontact";
188  }
189  return index;
190 }
191 
192 int MetaContact::removeContact(const QString &contactUri)
193 {
194  const int index = d->contactUris.indexOf(contactUri);
195  if (index >= 0) {
196  d->contacts.removeAt(index);
197  d->contactUris.removeAt(index);
198  reload();
199  }
200  return index;
201 }
202 
203 void MetaContact::reload()
204 {
205  //always favour the first item
206 
207  //Optimization, if only one contact re-use that one
208  d->personAddressee = (d->contacts.size() == 1) ? d->contacts.first() : AbstractContact::Ptr(new MetaContactProxy(d->contacts));
209  Q_ASSERT(d->personAddressee);
210 }
KPeople::AbstractContact
KPeople::AbstractContact is the class to provide the data from a given contact by the backends...
Definition: abstractcontact.h:38
KPeople
The KPeople namespace contains all the classes for Libkpeople.
KPeople::AbstractContact::customProperty
virtual QVariant customProperty(const QString &key) const =0
Generic method to access a random contact property.
This file is part of the KDE documentation.
Documentation copyright © 1996-2015 The KDE developers.
Generated on Fri Feb 13 2015 15:16:39 by doxygen 1.8.9.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KPeople

Skip menu "KPeople"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • File List

Class Picker

Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal