Changeset View
Changeset View
Standalone View
Standalone View
src/calendar/eventfetchjob.cpp
| Show All 30 Lines | |||||
| 31 | #include <QNetworkReply> | 31 | #include <QNetworkReply> | ||
| 32 | #include <QUrlQuery> | 32 | #include <QUrlQuery> | ||
| 33 | 33 | | |||
| 34 | using namespace KGAPI2; | 34 | using namespace KGAPI2; | ||
| 35 | 35 | | |||
| 36 | class Q_DECL_HIDDEN EventFetchJob::Private | 36 | class Q_DECL_HIDDEN EventFetchJob::Private | ||
| 37 | { | 37 | { | ||
| 38 | public: | 38 | public: | ||
| 39 | Private(EventFetchJob *parent); | | |||
| 40 | QNetworkRequest createRequest(const QUrl &url); | | |||
| 41 | | ||||
| 42 | QString calendarId; | 39 | QString calendarId; | ||
| 43 | QString eventId; | 40 | QString eventId; | ||
| 44 | QString filter; | 41 | QString filter; | ||
| 45 | bool fetchDeleted; | 42 | bool fetchDeleted = true; | ||
| 46 | quint64 updatedTimestamp; | 43 | quint64 updatedTimestamp = 0; | ||
| 47 | quint64 timeMin; | 44 | quint64 timeMin = 0; | ||
| 48 | quint64 timeMax; | 45 | quint64 timeMax = 0; | ||
| 49 | | ||||
| 50 | private: | | |||
| 51 | EventFetchJob * const q; | | |||
| 52 | }; | 46 | }; | ||
| 53 | 47 | | |||
| 54 | EventFetchJob::Private::Private(EventFetchJob* parent): | | |||
| 55 | fetchDeleted(true), | | |||
| 56 | updatedTimestamp(0), | | |||
| 57 | timeMin(0), | | |||
| 58 | timeMax(0), | | |||
| 59 | q(parent) | | |||
| 60 | { | | |||
| 61 | } | | |||
| 62 | | ||||
| 63 | QNetworkRequest EventFetchJob::Private::createRequest(const QUrl& url) | | |||
| 64 | { | | |||
| 65 | QNetworkRequest request(url); | | |||
| 66 | request.setRawHeader("GData-Version", CalendarService::APIVersion().toLatin1()); | | |||
| 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 | return request; | | |||
| 76 | } | | |||
| 77 | | ||||
| 78 | EventFetchJob::EventFetchJob(const QString& calendarId, const AccountPtr& account, QObject* parent): | 48 | EventFetchJob::EventFetchJob(const QString& calendarId, const AccountPtr& account, QObject* parent): | ||
| 79 | FetchJob(account, parent), | 49 | FetchJob(account, parent), | ||
| 80 | d(new Private(this)) | 50 | d(new Private) | ||
| 81 | { | 51 | { | ||
| 82 | d->calendarId = calendarId; | 52 | d->calendarId = calendarId; | ||
| 83 | } | 53 | } | ||
| 84 | 54 | | |||
| 85 | EventFetchJob::EventFetchJob(const QString& eventId, const QString& calendarId, const AccountPtr& account, QObject* parent): | 55 | EventFetchJob::EventFetchJob(const QString& eventId, const QString& calendarId, const AccountPtr& account, QObject* parent): | ||
| 86 | FetchJob(account, parent), | 56 | FetchJob(account, parent), | ||
| 87 | d(new Private(this)) | 57 | d(new Private) | ||
| 88 | { | 58 | { | ||
| 89 | d->calendarId = calendarId; | 59 | d->calendarId = calendarId; | ||
| 90 | d->eventId = eventId; | 60 | d->eventId = eventId; | ||
| 91 | } | 61 | } | ||
| 92 | 62 | | |||
| 93 | EventFetchJob::~EventFetchJob() | 63 | EventFetchJob::~EventFetchJob() = default; | ||
| 94 | { | | |||
| 95 | delete d; | | |||
| 96 | } | | |||
| 97 | 64 | | |||
| 98 | void EventFetchJob::setFetchDeleted(bool fetchDeleted) | 65 | void EventFetchJob::setFetchDeleted(bool fetchDeleted) | ||
| 99 | { | 66 | { | ||
| 100 | if (isRunning()) { | 67 | if (isRunning()) { | ||
| 101 | qCWarning(KGAPIDebug) << "Can't modify fetchDeleted property when job is running"; | 68 | qCWarning(KGAPIDebug) << "Can't modify fetchDeleted property when job is running"; | ||
| 102 | return; | 69 | return; | ||
| 103 | } | 70 | } | ||
| 104 | 71 | | |||
| ▲ Show 20 Lines • Show All 83 Lines • ▼ Show 20 Line(s) | 143 | if (d->eventId.isEmpty()) { | |||
| 188 | } | 155 | } | ||
| 189 | if (d->timeMax > 0) { | 156 | if (d->timeMax > 0) { | ||
| 190 | query.addQueryItem(QStringLiteral("timeMax"), Utils::ts2Str(d->timeMax)); | 157 | query.addQueryItem(QStringLiteral("timeMax"), Utils::ts2Str(d->timeMax)); | ||
| 191 | } | 158 | } | ||
| 192 | url.setQuery(query); | 159 | url.setQuery(query); | ||
| 193 | } else { | 160 | } else { | ||
| 194 | url = CalendarService::fetchEventUrl(d->calendarId, d->eventId); | 161 | url = CalendarService::fetchEventUrl(d->calendarId, d->eventId); | ||
| 195 | } | 162 | } | ||
| 196 | const QNetworkRequest request = d->createRequest(url); | 163 | const QNetworkRequest request = CalendarService::prepareRequest(url); | ||
| 197 | enqueueRequest(request); | 164 | enqueueRequest(request); | ||
| 198 | } | 165 | } | ||
| 199 | 166 | | |||
| 200 | ObjectsList EventFetchJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray& rawData) | 167 | ObjectsList EventFetchJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray& rawData) | ||
| 201 | { | 168 | { | ||
| 202 | FeedData feedData; | 169 | FeedData feedData; | ||
| 203 | feedData.requestUrl = reply->url(); | 170 | feedData.requestUrl = reply->url(); | ||
| 204 | ObjectsList items; | 171 | ObjectsList items; | ||
| 205 | const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString(); | 172 | const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString(); | ||
| 206 | ContentType ct = Utils::stringToContentType(contentType); | 173 | ContentType ct = Utils::stringToContentType(contentType); | ||
| 207 | if (ct == KGAPI2::JSON) { | 174 | if (ct == KGAPI2::JSON) { | ||
| 208 | if (d->eventId.isEmpty()) { | 175 | if (d->eventId.isEmpty()) { | ||
| 209 | items = CalendarService::parseEventJSONFeed(rawData, feedData); | 176 | items = CalendarService::parseEventJSONFeed(rawData, feedData); | ||
| 210 | } else { | 177 | } else { | ||
| 211 | items << CalendarService::JSONToEvent(rawData).dynamicCast<Object>(); | 178 | items << CalendarService::JSONToEvent(rawData).dynamicCast<Object>(); | ||
| 212 | } | 179 | } | ||
| 213 | } else { | 180 | } else { | ||
| 214 | setError(KGAPI2::InvalidResponse); | 181 | setError(KGAPI2::InvalidResponse); | ||
| 215 | setErrorString(tr("Invalid response content type")); | 182 | setErrorString(tr("Invalid response content type")); | ||
| 216 | emitFinished(); | 183 | emitFinished(); | ||
| 217 | return items; | 184 | return items; | ||
| 218 | } | 185 | } | ||
| 219 | 186 | | |||
| 220 | if (feedData.nextPageUrl.isValid()) { | 187 | if (feedData.nextPageUrl.isValid()) { | ||
| 221 | const QNetworkRequest request = d->createRequest(feedData.nextPageUrl); | 188 | const auto request = CalendarService::prepareRequest(feedData.nextPageUrl); | ||
| 222 | enqueueRequest(request); | 189 | enqueueRequest(request); | ||
| 223 | } | 190 | } | ||
| 224 | 191 | | |||
| 225 | return items; | 192 | return items; | ||
| 226 | } | 193 | } | ||
| 227 | 194 | | |||
| 228 | 195 | | |||