Kgapi

contactfetchphotojob.cpp
1 /*
2  * This file is part of LibKGAPI library
3  *
4  * Copyright (C) 2013 Daniel Vrátil <[email protected]>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) version 3, or any
10  * later version accepted by the membership of KDE e.V. (or its
11  * successor approved by the membership of KDE e.V.), which shall
12  * 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  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * 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 #include "contactfetchphotojob.h"
24 #include "account.h"
25 #include "contact.h"
26 #include "contactsservice.h"
27 #include "private/queuehelper_p.h"
28 
29 #include <QNetworkRequest>
30 #include <QNetworkReply>
31 #include <QImage>
32 
33 Q_DECLARE_METATYPE(KGAPI2::ContactPtr)
34 
35 using namespace KGAPI2;
36 
37 class Q_DECL_HIDDEN ContactFetchPhotoJob::Private
38 {
39  public:
40  Private(ContactFetchPhotoJob *parent);
41  void processNextContact();
42 
43  QueueHelper<ContactPtr> contacts;
44 
45  private:
46  ContactFetchPhotoJob * const q;
47 };
48 
49 ContactFetchPhotoJob::Private::Private(ContactFetchPhotoJob *parent):
50  q(parent)
51 {
52 }
53 
54 void ContactFetchPhotoJob::Private::processNextContact()
55 {
56  if (contacts.atEnd()) {
57  q->emitFinished();
58  return;
59  }
60 
61  const ContactPtr contact = contacts.current();
62  const QUrl url = ContactsService::photoUrl(q->account()->accountName(), contact->uid());
63  QNetworkRequest request(url);
64  request.setRawHeader("GData-Version", ContactsService::APIVersion().toLatin1());
65  q->enqueueRequest(request);
66 }
67 
68 
70  FetchJob(account, parent),
71  d(new Private(this))
72 {
73  d->contacts = contacts;
74 }
75 
77  FetchJob(account, parent),
78  d(new Private(this))
79 {
80  d->contacts << contact;
81 }
82 
84 {
85  delete d;
86 }
87 
89 {
90  d->processNextContact();
91 }
92 
94 {
95  if (reply->error() == QNetworkReply::ContentNotFoundError
96  || reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == NotFound) {
97  d->contacts.currentProcessed();
98  d->processNextContact();
99  // If the last photo failed, make sure we don't fail the whole job!
102  return;
103  }
104 
105  ContactPtr contact = d->contacts.current();
106  KContacts::Picture picture;
107  picture.setRawData(rawData, reply->header(QNetworkRequest::ContentTypeHeader).toString());
108  contact->setPhoto(picture);
109  Q_EMIT photoFetched(this, contact);
110 
111  d->contacts.currentProcessed();
112  d->processNextContact();
113 }
114 
115 
AccountPtr account() const
Returns account used to authenticate requests.
Definition: job.cpp:437
LibKGAPI error - no error.
Definition: types.h:189
void start() override
KGAPI2::Job::start implementation.
~ContactFetchPhotoJob() override
Destructor.
Requested object was not found on the remote side.
Definition: types.h:209
QString APIVersion()
Supported API version.
A job to fetch contact photos.
QUrl photoUrl(const QString &user, const QString &contactID)
Returns URL for modifying photo.
int toInt(bool *ok) const const
A job to fetch a single map tile described by a StaticMapUrl.
Definition: blog.h:30
ContactFetchPhotoJob(const ContactsList &contacts, const AccountPtr &account, QObject *parent=nullptr)
Constructs a job that will fetch photos for given contacts.
void photoFetched(KGAPI2::Job *job, const KGAPI2::ContactPtr &contact)
A signal that is emitted every time a photo is retrieved.
QVariant header(QNetworkRequest::KnownHeaders header) const const
void setErrorString(const QString &errorString)
Set job error description to errorString.
Definition: job.cpp:402
void setRawData(const QByteArray &rawData, const QString &type)
void handleReply(const QNetworkReply *reply, const QByteArray &rawData) override
KGAPI2::Job::handleReply implementation.
QVariant attribute(QNetworkRequest::Attribute code) const const
void setError(KGAPI2::Error error)
Set job error to error.
Definition: job.cpp:387
QNetworkReply::NetworkError error() const const
Abstract superclass for all jobs that fetch resources from Google.
Definition: fetchjob.h:38
QObject * parent() const const
QString toString() const const
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat Jun 6 2020 23:01:16 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.