contactcreatejob.cpp 5.96 KB
Newer Older
1
2
3
4
5
/*
 * This file is part of LibKGAPI library
 *
 * Copyright (C) 2013  Daniel Vrátil <dvratil@redhat.com>
 *
6
7
8
9
10
11
12
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3, or any
 * later version accepted by the membership of KDE e.V. (or its
 * successor approved by the membership of KDE e.V.), which shall
 * act as a proxy defined in Section 6 of version 3 of the license.
13
 *
14
 * This library is distributed in the hope that it will be useful,
15
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with this library.  If not, see <https://www.gnu.org/licenses/>.
21
22
23
24
25
26
 */

#include "contactcreatejob.h"
#include "contact.h"
#include "contactsservice.h"
#include "utils.h"
Daniel Vrátil's avatar
Daniel Vrátil committed
27
#include "../debug.h"
28
#include "account.h"
29
#include "private/queuehelper_p.h"
30

Laurent Montel's avatar
Laurent Montel committed
31
32
33
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QBuffer>
Laurent Montel's avatar
Laurent Montel committed
34
#include <QImage>
35
36
37

using namespace KGAPI2;

38
class Q_DECL_HIDDEN ContactCreateJob::Private
39
40
41
42
{
  public:
    Private(ContactCreateJob *parent);
    void processNextContact();
43
    void setPhoto(const KContacts::Picture &photo, const QString &uid);
44

45
    QueueHelper<ContactPtr> contacts;
46
47
    ContactPtr lastContact;
    QPair<QByteArray, QString> pendingPhoto;
48
49
50
51
52
53
54
55
56
57
58
  private:
    ContactCreateJob * const q;
};

ContactCreateJob::Private::Private(ContactCreateJob *parent):
    q(parent)
{
}

void ContactCreateJob::Private::processNextContact()
{
59
    if (contacts.atEnd()) {
60
61
62
        if (pendingPhoto.first.isEmpty()) {
            q->emitFinished();
        }
63
64
65
        return;
    }

66
    const ContactPtr contact = contacts.current();
67
    const QUrl url = ContactsService::createContactUrl(q->account()->accountName());
68
    QNetworkRequest request(url);
69
70
71
72
73
74
75
76
77
78
79
    request.setRawHeader("GData-Version", ContactsService::APIVersion().toLatin1());

    QByteArray rawData = ContactsService::contactToXML(contact);
    rawData.prepend("<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\" "
                     "xmlns:gd=\"http://schemas.google.com/g/2005\" "
                     "xmlns:gContact=\"http://schemas.google.com/contact/2008\">"
                    "<atom:category scheme=\"http://schemas.google.com/g/2005#kind\" "
                     "term=\"http://schemas.google.com/contact/2008#contact\"/>");
    rawData.append("</atom:entry>");

    QStringList headers;
80
81
    auto rawHeaderList = request.rawHeaderList();
    headers.reserve(rawHeaderList.size());
Laurent Montel's avatar
Laurent Montel committed
82
    for (const QByteArray &str : qAsConst(rawHeaderList)) {
83
84
85
        headers << QLatin1String(str) + QLatin1String(": ") + QLatin1String(request.rawHeader(str));
    }

86
    q->enqueueRequest(request, rawData, QStringLiteral("application/atom+xml"));
87
}
88

89
90
91
92
93
94
95
96
void ContactCreateJob::Private::setPhoto(const KContacts::Picture &photo, const QString &uid)
{
    const QUrl url = ContactsService::photoUrl(q->account()->accountName(), uid);
    QNetworkRequest photoRequest(url);
    photoRequest.setHeader(QNetworkRequest::ContentTypeHeader, QLatin1String("image/*"));
    pendingPhoto.first = photo.rawData();
    pendingPhoto.second = photo.type();
    q->enqueueRequest(photoRequest, pendingPhoto.first, QStringLiteral("modifyImage"));
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
}

ContactCreateJob::ContactCreateJob(const ContactsList& contacts, const AccountPtr& account, QObject* parent):
    CreateJob(account, parent),
    d(new Private(this))
{
    d->contacts = contacts;
}

ContactCreateJob::ContactCreateJob(const ContactPtr& contact, const AccountPtr& account, QObject* parent):
    CreateJob(account, parent),
    d(new Private(this))
{
    d->contacts << contact;
}

ContactCreateJob::~ContactCreateJob()
{
    delete d;
}

void ContactCreateJob::start()
{
    d->processNextContact();
}

void ContactCreateJob::dispatchRequest(QNetworkAccessManager *accessManager, const QNetworkRequest &request, const QByteArray &data, const QString &contentType)
{
    QNetworkRequest r = request;

    if (contentType == QLatin1String("modifyImage")) {
        accessManager->put(r, data);
    } else {
        r.setHeader(QNetworkRequest::ContentTypeHeader, contentType);
        accessManager->post(r, data);
    }
}

ObjectsList ContactCreateJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray& rawData)
{
    ObjectsList items;
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    if (!reply->url().path().contains(QLatin1String("/photos/media/"))) {
        const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
        ContentType ct = Utils::stringToContentType(contentType);
        if (ct == KGAPI2::JSON) {
            d->lastContact = ContactsService::JSONToContact(rawData);
            items << d->lastContact.dynamicCast<Object>();
        } else if (ct == KGAPI2::XML) {
            d->lastContact = ContactsService::XMLToContact(rawData);
            items << d->lastContact.dynamicCast<Object>();
        } else {
            setError(KGAPI2::InvalidResponse);
            setErrorString(tr("Invalid response content type"));
            emitFinished();
            return items;
        }
153
154
155
156
157
158
159
160
        if (!d->contacts.current()->photo().isEmpty()) {
            // current contact does not have uid populated
            d->setPhoto(d->contacts.current()->photo(), d->lastContact->uid());
            d->contacts.currentProcessed();
        } else {
            d->contacts.currentProcessed();
            d->processNextContact();
        }
161
    } else {
162
163
164
165
166
167
168
        if (d->lastContact) {
            KContacts::Picture picture;
            picture.setRawData(d->pendingPhoto.first, d->pendingPhoto.second);
            d->lastContact->setPhoto(picture);
            d->pendingPhoto.first.clear();
            d->pendingPhoto.second.clear();
        }
169
170
        // Enqueue next item or finish
        d->processNextContact();
171
172
173
174
    }
    return items;
}

175