KContacts

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

KDE's Doxygen guidelines are available online.