• Skip to content
  • Skip to link menu
Brand

API Documentation

  1. KDE API Reference
  2. The KDE Frameworks
  3. KContacts
  • KDE Home
  • Contact Us

Quick Links

Skip menu "KContacts"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • File List
  • Dependencies
  • Related Pages

Class Picker

About

Address book API for KDE

Maintainer
Laurent Montel
Supported platforms
Android, FreeBSD, iOS, Linux, MacOSX, Windows
Community
IRC: #kde-devel on Freenode
Mailing list: kde-frameworks-devel
Use with CMake
find_package(KF5Contacts)
target_link_libraries(yourapp KF5::Contacts)
Use with QMake
QT += KContacts 
Clone
git clone git://anongit.kde.org/kcontacts.git
Browse source
KContacts on cgit.kde.org

KContacts

  • frameworks
  • frameworks
  • kcontacts
  • src
contactgrouptool.cpp
1 /*
2  This file is part of the KContacts framework.
3  Copyright (c) 2008 Tobias Koenig <[email protected]>
4  Copyright (c) 2008 Kevin Krammer <[email protected]>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "contactgrouptool.h"
23 #include "contactgroup.h"
24 
25 #include <QIODevice>
26 #include <QString>
27 
28 #include <QXmlStreamReader>
29 #include <QXmlStreamWriter>
30 
31 using namespace KContacts;
32 
33 class XmlContactGroupWriter : public QXmlStreamWriter
34 {
35 public:
36  XmlContactGroupWriter();
37 
38  void write(const ContactGroup &group, QIODevice *device);
39  void write(const QVector<ContactGroup> &groupLis, QIODevice *device);
40 
41 private:
42  void writeGroup(const ContactGroup &group);
43  void writeContactReference(const ContactGroup::ContactReference &reference);
44  void writeContactGroupReference(const ContactGroup::ContactGroupReference &reference);
45  void writeData(const ContactGroup::Data &data);
46 };
47 
48 XmlContactGroupWriter::XmlContactGroupWriter()
49 {
50  setAutoFormatting(true);
51 }
52 
53 void XmlContactGroupWriter::write(const ContactGroup &group, QIODevice *device)
54 {
55  setDevice(device);
56 
57  writeStartDocument();
58 
59  writeGroup(group);
60 
61  writeEndDocument();
62 }
63 
64 void XmlContactGroupWriter::write(const QVector<ContactGroup> &groupList, QIODevice *device)
65 {
66  setDevice(device);
67 
68  writeStartDocument();
69 
70  writeStartElement(QStringLiteral("contactGroupList"));
71 
72  for (const ContactGroup &group : groupList) {
73  writeGroup(group);
74  }
75 
76  writeEndElement();
77 
78  writeEndDocument();
79 }
80 
81 void XmlContactGroupWriter::writeGroup(const ContactGroup &group)
82 {
83  writeStartElement(QStringLiteral("contactGroup"));
84  writeAttribute(QStringLiteral("uid"), group.id());
85  writeAttribute(QStringLiteral("name"), group.name());
86 
87  const uint contactCount(group.contactReferenceCount());
88  for (uint i = 0; i < contactCount; ++i) {
89  writeContactReference(group.contactReference(i));
90  }
91 
92  const uint contactGroupReference(group.contactGroupReferenceCount());
93  for (uint i = 0; i < contactGroupReference; ++i) {
94  writeContactGroupReference(group.contactGroupReference(i));
95  }
96 
97  const uint dataCount(group.dataCount());
98  for (uint i = 0; i < dataCount; ++i) {
99  writeData(group.data(i));
100  }
101 
102  writeEndElement();
103 }
104 
105 void XmlContactGroupWriter::writeContactReference(const ContactGroup::ContactReference &reference)
106 {
107  writeStartElement(QStringLiteral("contactReference"));
108  writeAttribute(QStringLiteral("uid"), reference.uid());
109  writeAttribute(QStringLiteral("gid"), reference.gid());
110  if (!reference.preferredEmail().isEmpty()) {
111  writeAttribute(QStringLiteral("preferredEmail"), reference.preferredEmail());
112  }
113 
114  // TODO: customs
115 
116  writeEndElement();
117 }
118 
119 void XmlContactGroupWriter::writeContactGroupReference(
120  const ContactGroup::ContactGroupReference &reference)
121 {
122  writeStartElement(QStringLiteral("contactGroupReference"));
123  writeAttribute(QStringLiteral("uid"), reference.uid());
124 
125  // TODO: customs
126 
127  writeEndElement();
128 }
129 
130 void XmlContactGroupWriter::writeData(const ContactGroup::Data &data)
131 {
132  writeStartElement(QStringLiteral("contactData"));
133  writeAttribute(QStringLiteral("name"), data.name());
134  writeAttribute(QStringLiteral("email"), data.email());
135 
136  // TODO: customs
137 
138  writeEndElement();
139 }
140 
141 class XmlContactGroupReader : public QXmlStreamReader
142 {
143 public:
144  XmlContactGroupReader();
145 
146  bool read(QIODevice *device, ContactGroup &group);
147  bool read(QIODevice *device, QVector<ContactGroup> &groupList);
148 
149 private:
150  bool readGroup(ContactGroup &group);
151  bool readContactReference(ContactGroup::ContactReference &reference);
152  bool readContactGroupReference(ContactGroup::ContactGroupReference &reference);
153  bool readData(ContactGroup::Data &data);
154 };
155 
156 XmlContactGroupReader::XmlContactGroupReader()
157 {
158 }
159 
160 bool XmlContactGroupReader::read(QIODevice *device, ContactGroup &group)
161 {
162  setDevice(device);
163 
164  while (!atEnd()) {
165  readNext();
166  if (isStartElement()) {
167  if (name() == QLatin1String("contactGroup")) {
168  return readGroup(group);
169  } else {
170  raiseError(QStringLiteral("The document does not describe a ContactGroup"));
171  }
172  }
173  }
174 
175  return error() == NoError;
176 }
177 
178 bool XmlContactGroupReader::read(QIODevice *device, QVector<ContactGroup> &groupList)
179 {
180  setDevice(device);
181 
182  int depth = 0;
183 
184  while (!atEnd()) {
185  readNext();
186  if (isStartElement()) {
187  ++depth;
188  if (depth == 1) {
189  if (name() == QLatin1String("contactGroupList")) {
190  continue;
191  } else {
192  raiseError(QStringLiteral("The document does not describe a list of ContactGroup"));
193  }
194  } else if (depth == 2) {
195  if (name() == QLatin1String("contactGroup")) {
196  ContactGroup group;
197  if (!readGroup(group)) {
198  return false;
199  }
200 
201  groupList.append(group);
202  } else {
203  raiseError(QStringLiteral("The document does not describe a list of ContactGroup"));
204  }
205  }
206  }
207 
208  if (isEndElement()) {
209  --depth;
210  }
211  }
212 
213  return error() == NoError;
214 }
215 
216 bool XmlContactGroupReader::readGroup(ContactGroup &group)
217 {
218  const QXmlStreamAttributes elementAttributes = attributes();
219  const QStringRef uid = elementAttributes.value(QLatin1String("uid"));
220  if (uid.isEmpty()) {
221  raiseError(QStringLiteral("ContactGroup is missing a uid"));
222  return false;
223  }
224 
225  const QStringRef groupName = elementAttributes.value(QLatin1String("name"));
226  if (groupName.isEmpty()) {
227  raiseError(QStringLiteral("ContactGroup is missing a name"));
228  return false;
229  }
230 
231  group.setId(uid.toString());
232  group.setName(groupName.toString());
233 
234  while (!atEnd()) {
235  readNext();
236  if (isStartElement()) {
237  if (name() == QLatin1String("contactData")) {
238  ContactGroup::Data data;
239  if (!readData(data)) {
240  return false;
241  }
242  group.append(data);
243  } else if (name() == QLatin1String("contactReference")) {
244  ContactGroup::ContactReference reference;
245  if (!readContactReference(reference)) {
246  return false;
247  }
248  group.append(reference);
249  } else if (name() == QLatin1String("contactGroupReference")) {
250  ContactGroup::ContactGroupReference reference;
251  if (!readContactGroupReference(reference)) {
252  return false;
253  }
254  group.append(reference);
255  } else {
256  raiseError(QStringLiteral("The document does not describe a ContactGroup"));
257  }
258  }
259 
260  if (isEndElement()) {
261  if (name() == QLatin1String("contactGroup")) {
262  return true;
263  }
264  }
265  }
266 
267  return false;
268 }
269 
270 bool XmlContactGroupReader::readData(ContactGroup::Data &data)
271 {
272  const QXmlStreamAttributes elementAttributes = attributes();
273  const QStringRef email = elementAttributes.value(QLatin1String("email"));
274  if (email.isEmpty()) {
275  raiseError(QStringLiteral("ContactData is missing an email address"));
276  return false;
277  }
278 
279  const QStringRef name = elementAttributes.value(QLatin1String("name"));
280 
281  data.setName(name.toString());
282  data.setEmail(email.toString());
283 
284  return true;
285 }
286 
287 bool XmlContactGroupReader::readContactReference(ContactGroup::ContactReference &reference)
288 {
289  const QXmlStreamAttributes elementAttributes = attributes();
290  const QStringRef uid = elementAttributes.value(QLatin1String("uid"));
291  const QStringRef gid = elementAttributes.value(QLatin1String("gid"));
292  if (uid.isEmpty() && gid.isEmpty()) {
293  raiseError(QStringLiteral("ContactReference is missing both uid and gid"));
294  return false;
295  }
296  const QStringRef preferredEmail = elementAttributes.value(QLatin1String("preferredEmail"));
297 
298  reference.setUid(uid.toString());
299  reference.setGid(gid.toString());
300  reference.setPreferredEmail(preferredEmail.toString());
301 
302  return true;
303 }
304 
305 bool XmlContactGroupReader::readContactGroupReference(
306  ContactGroup::ContactGroupReference &reference)
307 {
308  const QXmlStreamAttributes elementAttributes = attributes();
309  const QStringRef uid = elementAttributes.value(QLatin1String("uid"));
310  if (uid.isEmpty()) {
311  raiseError(QStringLiteral("ContactGroupReference is missing a uid"));
312  return false;
313  }
314 
315  reference.setUid(uid.toString());
316 
317  return true;
318 }
319 
320 bool ContactGroupTool::convertFromXml(QIODevice *device, ContactGroup &group, QString *errorMessage)
321 {
322  Q_UNUSED(errorMessage);
323 
324  XmlContactGroupReader reader;
325 
326  bool ok = reader.read(device, group);
327 
328  if (!ok && errorMessage != nullptr) {
329  *errorMessage = reader.errorString();
330  }
331 
332  return ok;
333 }
334 
335 bool ContactGroupTool::convertToXml(const ContactGroup &group, QIODevice *device, QString *errorMessage)
336 {
337  Q_UNUSED(errorMessage);
338 
339  XmlContactGroupWriter writer;
340  writer.write(group, device);
341 
342  return true;
343 }
344 
345 bool ContactGroupTool::convertFromXml(QIODevice *device, QVector<ContactGroup> &groupList, QString *errorMessage)
346 {
347  Q_UNUSED(errorMessage);
348 
349  XmlContactGroupReader reader;
350 
351  bool ok = reader.read(device, groupList);
352 
353  if (!ok && errorMessage != nullptr) {
354  *errorMessage = reader.errorString();
355  }
356 
357  return ok;
358 }
359 
360 bool ContactGroupTool::convertToXml(const QVector<ContactGroup> &groupList, QIODevice *device, QString *errorMessage)
361 {
362  Q_UNUSED(errorMessage);
363 
364  XmlContactGroupWriter writer;
365  writer.write(groupList, device);
366 
367  return true;
368 }
QIODevice
KContacts::ContactGroup::ContactGroupReference::uid
QString uid() const
Returns the contact group uid of the contact group reference.
Definition: contactgroup.cpp:179
QXmlStreamAttributes
KContacts::ContactGroup::ContactReference::setUid
void setUid(const QString &uid)
Sets the contact uid of the contact reference.
Definition: contactgroup.cpp:73
KContacts::ContactGroup::Data::setEmail
void setEmail(const QString &email)
Sets the email address of the contact data object.
Definition: contactgroup.cpp:267
KItinerary::LocationUtil::name
QString name(const QVariant &location)
KGAPI2::NoError
NoError
KContacts::ContactGroup::contactReferenceCount
int contactReferenceCount() const
Returns the number of contact references in this group.
Definition: contactgroup.cpp:379
KContacts::ContactGroupTool::convertToXml
KCONTACTS_EXPORT bool convertToXml(const ContactGroup &group, QIODevice *device, QString *errorMessage=nullptr)
Converts a contact group into XML data and writes them to a device.
Definition: contactgrouptool.cpp:335
KContacts::ContactGroup::id
QString id() const
Returns the unique id of the contact group.
Definition: contactgroup.cpp:369
QVector::append
void append(const T &value)
QXmlStreamWriter
KContacts::ContactGroup::Data::name
QString name() const
Returns the name of the contact data object.
Definition: contactgroup.cpp:262
QStringRef::toString
QString toString() const
KContacts::ContactGroup
This class represents a group of contacts.
Definition: contactgroup.h:45
QXmlStreamAttributes::value
QStringRef value(const QString &namespaceUri, const QString &name) const
KContacts::ContactGroup::ContactGroupReference
This class represents a contact group reference.
Definition: contactgroup.h:159
KContacts::ContactGroup::contactGroupReference
ContactGroupReference & contactGroupReference(int index)
Returns the contact group reference at the given index.
Definition: contactgroup.cpp:410
KContacts::ContactGroup::contactReference
ContactReference & contactReference(int index)
Returns the contact reference at the given index.
Definition: contactgroup.cpp:394
KContacts::ContactGroup::contactGroupReferenceCount
int contactGroupReferenceCount() const
Returns the number of group references in this group.
Definition: contactgroup.cpp:384
QStringRef
KContacts::ContactGroup::ContactReference::gid
QString gid() const
Returns the contact GID of the contact reference.
Definition: contactgroup.cpp:88
KContacts::ContactGroup::name
QString name() const
Returns the i18n&#39;d name of the contact group.
Definition: contactgroup.cpp:359
QString::isEmpty
bool isEmpty() const
KContacts::ContactGroup::ContactReference::setGid
void setGid(const QString &gid)
Sets the contact gid of the contact reference.
Definition: contactgroup.cpp:83
KContacts::ContactGroup::ContactReference::preferredEmail
QString preferredEmail() const
Returns the preferred email address, or an empty string if no preferred email address is set...
Definition: contactgroup.cpp:98
KMessageBox::error
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
QString
KContacts::ContactGroup::setName
void setName(const QString &name)
Sets the i18n&#39;d name of the contact group.
Definition: contactgroup.cpp:354
KContacts::ContactGroup::ContactReference
This class represents a contact reference.
Definition: contactgroup.h:52
KContacts::ContactGroupTool::convertFromXml
KCONTACTS_EXPORT bool convertFromXml(QIODevice *device, ContactGroup &group, QString *errorMessage=nullptr)
Converts XML data coming from a device into a contact group.
Definition: contactgrouptool.cpp:320
KContacts::ContactGroup::ContactReference::setPreferredEmail
void setPreferredEmail(const QString &email)
Sets the preferred email address.
Definition: contactgroup.cpp:93
KContacts::ContactGroup::Data::setName
void setName(const QString &name)
Sets the name of the contact data object.
Definition: contactgroup.cpp:257
KContacts::ContactGroup::ContactGroupReference::setUid
void setUid(const QString &uid)
Sets the contact group uid of the contact group reference.
Definition: contactgroup.cpp:174
QXmlStreamReader
KContacts::ContactGroup::Data::email
QString email() const
Returns the email address of the contact data object.
Definition: contactgroup.cpp:272
KContacts
Definition: address.h:32
QVector
QLatin1String
KContacts::ContactGroup::Data
This class represents a contact data object.
Definition: contactgroup.h:236
KContacts::ContactGroup::dataCount
int dataCount() const
Returns the number of contact data objects in this group.
Definition: contactgroup.cpp:389
QStringRef::isEmpty
bool isEmpty() const
KContacts::ContactGroup::append
void append(const ContactReference &reference)
Appends a new contact reference to the contact group.
Definition: contactgroup.cpp:441
KContacts::ContactGroup::data
Data & data(int index)
Returns the contact data object at the given index.
Definition: contactgroup.cpp:427
KContacts::ContactGroup::setId
void setId(const QString &id)
Sets the unique id of the contact group.
Definition: contactgroup.cpp:364
KContacts::ContactGroup::ContactReference::uid
QString uid() const
Returns the contact uid of the contact reference.
Definition: contactgroup.cpp:78
This file is part of the KDE documentation.
Documentation copyright © 1996-2019 The KDE developers.
Generated on Fri Dec 6 2019 03:47:40 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

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