Kgapi

contactsgroupcreatejob.cpp
1 /*
2  * This file is part of LibKGAPI library
3  *
4  * SPDX-FileCopyrightText: 2013 Daniel Vrátil <[email protected]>
5  *
6  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7  */
8 
9 #include "contactsgroupcreatejob.h"
10 #include "contactsgroup.h"
11 #include "contactsservice.h"
12 #include "../debug.h"
13 #include "utils.h"
14 #include "account.h"
15 #include "private/queuehelper_p.h"
16 
17 #include <QNetworkRequest>
18 #include <QNetworkReply>
19 
20 
21 using namespace KGAPI2;
22 
23 class Q_DECL_HIDDEN ContactsGroupCreateJob::Private
24 {
25  public:
26  QueueHelper<ContactsGroupPtr> groups;
27 };
28 
30  CreateJob(account, parent),
31  d(new Private)
32 {
33  d->groups = groups;
34 }
35 
37  CreateJob(account, parent),
38  d(new Private)
39 {
40  d->groups << contactsGroup;
41 }
42 
44 {
45  delete d;
46 }
47 
49 {
50  if (d->groups.atEnd()) {
51  emitFinished();
52  return;
53  }
54 
55  const ContactsGroupPtr contact = d->groups.current();
56  const QUrl url = ContactsService::createGroupUrl(account()->accountName());
57  QNetworkRequest request(url);
58  request.setRawHeader("GData-Version", ContactsService::APIVersion().toLatin1());
59 
61  rawData.prepend("<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\" "
62  "xmlns:gd=\"http://schemas.google.com/g/2005\" "
63  "xmlns:gContact=\"http://schemas.google.com/contact/2008\">"
64  "<atom:category scheme=\"http://schemas.google.com/g/2005#kind\" "
65  "term=\"http://schemas.google.com/g/2008#group\"/>");
66  rawData.append("</atom:entry>");
67 
68  QStringList headers;
69  auto rawHeaderList = request.rawHeaderList();
70  headers.reserve(rawHeaderList.size());
71  for (const QByteArray &str : qAsConst(rawHeaderList)) {
72  headers << QLatin1String(str) + QLatin1String(": ") + QLatin1String(request.rawHeader(str));
73  }
74 
75  enqueueRequest(request, rawData, QStringLiteral("application/atom+xml"));
76 }
77 
79 {
80  const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
81  ContentType ct = Utils::stringToContentType(contentType);
83  if (ct == KGAPI2::JSON) {
84  items << ContactsService::JSONToContactsGroup(rawData);
85  d->groups.currentProcessed();
86  } else if (ct == KGAPI2::XML) {
87  items << ContactsService::XMLToContactsGroup(rawData);
88  d->groups.currentProcessed();
89  } else {
91  setErrorString(tr("Invalid response content type"));
92  emitFinished();
93  }
94 
95  // Enqueue next item or finish
96  start();
97 
98  return items;
99 }
100 
101 
ContactsGroupPtr XMLToContactsGroup(const QByteArray &xmlData)
Parses XML data into ContactsGroup object.
AccountPtr account() const
Returns account used to authenticate requests.
Definition: job.cpp:425
virtual ObjectsList items() const
Definition: createjob.cpp:40
QList< QByteArray > rawHeaderList() const const
Abstract superclass for all jobs that create new objects on the server.
Definition: createjob.h:25
void reserve(int alloc)
QByteArray rawHeader(const QByteArray &headerName) const const
QString APIVersion()
Supported API version.
void start() override
KGAPI2::Job::start implementation.
QString tr(const char *sourceText, const char *disambiguation, int n)
QUrl createGroupUrl(const QString &user)
Returns URL for creating a new contacts group.
ObjectsList handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) override
KGAPI2::Job::handleReply implementation.
virtual void enqueueRequest(const QNetworkRequest &request, const QByteArray &data=QByteArray(), const QString &contentType=QString())
Enqueues request in dispatcher queue.
Definition: job.cpp:497
QByteArray & prepend(char ch)
A job to fetch a single map tile described by a StaticMapUrl.
Definition: blog.h:16
~ContactsGroupCreateJob() override
Destructor.
ContactsGroupPtr JSONToContactsGroup(const QByteArray &jsonData)
Parses JSON data into ContactsGroup object.
ContactsGroupCreateJob(const ContactsGroupsList &groups, const AccountPtr &account, QObject *parent=nullptr)
Constructs a job that will create given groups in user&#39;s addressbook.
QByteArray & append(char ch)
LibKGAPI error - Google returned invalid response.
Definition: types.h:180
QByteArray contactsGroupToXML(const ContactsGroupPtr &group)
Serializes a ContactsGroup object into XML.
QVariant header(QNetworkRequest::KnownHeaders header) const const
virtual void emitFinished()
Emits Job::finished() signal.
Definition: job.cpp:479
void setErrorString(const QString &errorString)
Set job error description to errorString.
Definition: job.cpp:390
void setRawHeader(const QByteArray &headerName, const QByteArray &headerValue)
void setError(KGAPI2::Error error)
Set job error to error.
Definition: job.cpp:375
QObject * parent() const const
QString toString() const const
A job to create one or more new groups (folders) in Google Contacts addressbook.
ContentType
Definition: types.h:207
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat Sep 19 2020 23:09:42 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.