Kgapi

locationcreatejob.cpp
1/*
2 * This file is part of LibKGAPI library
3 *
4 * SPDX-FileCopyrightText: 2013 Daniel Vrátil <dvratil@redhat.com>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9#include "locationcreatejob.h"
10#include "account.h"
11#include "debug.h"
12#include "latitudeservice.h"
13#include "location.h"
14#include "utils.h"
15
16#include <QNetworkReply>
17#include <QNetworkRequest>
18
19using namespace KGAPI2;
20
21class Q_DECL_HIDDEN LocationCreateJob::Private
22{
23public:
24 Private();
25
27 bool isCurrent = true;
28};
29
30LocationCreateJob::Private::Private()
31{
32}
33
36 , d(new Private)
37{
38 d->location = location;
39 d->isCurrent = isCurrent;
40}
41
46
48{
49 QUrl url;
50 if (d->isCurrent) {
52 } else {
54 }
55
56 QNetworkRequest request(url);
57 request.setRawHeader("GData-Version", LatitudeService::APIVersion().toLatin1());
58
59 const QByteArray rawData = LatitudeService::locationToJSON(d->location);
60
61 QStringList headers;
62 const auto rawHeaderList = request.rawHeaderList();
63 headers.reserve(rawHeaderList.size());
64 for (const QByteArray &str : std::as_const(rawHeaderList)) {
65 headers << QLatin1StringView(str) + QLatin1StringView(": ") + QLatin1StringView(request.rawHeader(str));
66 }
67
68 enqueueRequest(request, rawData, QStringLiteral("application/json"));
69}
70
72{
73 const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
74 ContentType ct = Utils::stringToContentType(contentType);
76 if (ct == KGAPI2::JSON) {
78 } else {
80 setErrorString(tr("Invalid response content type"));
82 }
83
84 return items;
85}
86
87#include "moc_locationcreatejob.cpp"
Abstract superclass for all jobs that create new objects on the server.
Definition createjob.h:26
virtual ObjectsList items() const
Definition createjob.cpp:40
void setErrorString(const QString &errorString)
Set job error description to errorString.
Definition job.cpp:401
AccountPtr account() const
Returns account used to authenticate requests.
Definition job.cpp:436
virtual void emitFinished()
Emits Job::finished() signal.
Definition job.cpp:493
void setError(KGAPI2::Error error)
Set job error to error.
Definition job.cpp:386
virtual void enqueueRequest(const QNetworkRequest &request, const QByteArray &data=QByteArray(), const QString &contentType=QString())
Enqueues request in dispatcher queue.
Definition job.cpp:513
A job to stores one or more Locations in Google Latitude service.
LocationCreateJob(const LocationPtr &location, bool isCurrent, const AccountPtr &account, QObject *parent=nullptr)
Constructs a job that will store given location in user's Google Latitude account.
void start() override
KGAPI2::Job::start implementation.
ObjectsList handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) override
KGAPI2::CreateJob::handleReplyWithItems implementation.
~LocationCreateJob() override
Destructor.
Base class for all objects.
Definition object.h:31
QString APIVersion()
Supported version of API.
QByteArray locationToJSON(const LocationPtr &location)
Serializes a Location object to JSON.
QUrl insertCurrentLocationUrl()
Returns URL for uploading user's current location.
LocationPtr JSONToLocation(const QByteArray &jsonData)
Parses JSON data into a Location object.
QUrl insertLocationUrl()
Returns URL for uploading a location.
A job to fetch a single map tile described by a StaticMapUrl.
Definition blog.h:16
@ InvalidResponse
LibKGAPI error - Google returned invalid response.
Definition types.h:183
ContentType
Definition types.h:210
QVariant location(const QVariant &res)
void reserve(qsizetype size)
QVariant header(QNetworkRequest::KnownHeaders header) const const
QByteArray rawHeader(const QByteArray &headerName) const const
QList< QByteArray > rawHeaderList() const const
void setRawHeader(const QByteArray &headerName, const QByteArray &headerValue)
QObject * parent() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QSharedPointer< X > dynamicCast() const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:59:23 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.