File indexing completed on 2024-12-22 04:57:55

0001 /*
0002     SPDX-FileCopyrightText: 2016 Stefan Stäglich <sstaeglich@kdemail.net>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "tomboyitemdownloadjob.h"
0008 #include "debug.h"
0009 #include <KMime/Message>
0010 #include <QJsonDocument>
0011 #include <QJsonObject>
0012 
0013 TomboyItemDownloadJob::TomboyItemDownloadJob(const Akonadi::Item &item, QNetworkAccessManager *manager, QObject *parent)
0014     : TomboyJobBase(manager, parent)
0015     , mResultItem(item)
0016 {
0017 }
0018 
0019 Akonadi::Item TomboyItemDownloadJob::item() const
0020 {
0021     return mResultItem;
0022 }
0023 
0024 void TomboyItemDownloadJob::start()
0025 {
0026     // Get the specific note
0027     mContentURL.chop(1);
0028     QNetworkRequest request(QUrl(QString(mContentURL + QLatin1Char('/') + mResultItem.remoteId())));
0029     mReply = mRequestor->get(request, QList<O0RequestParameter>());
0030 
0031     connect(mReply, &QNetworkReply::finished, this, &TomboyItemDownloadJob::onRequestFinished);
0032     qCDebug(TOMBOYNOTESRESOURCE_LOG) << "TomboyItemDownloadJob: Start network request";
0033 }
0034 
0035 void TomboyItemDownloadJob::onRequestFinished()
0036 {
0037     checkReplyError();
0038     if (error() != TomboyJobError::NoError) {
0039         setErrorText(mReply->errorString());
0040         emitResult();
0041         return;
0042     }
0043     qCDebug(TOMBOYNOTESRESOURCE_LOG) << "TomboyItemDownloadJob: Network request finished. No error occurred";
0044 
0045     // Parse received data as JSON
0046     const QJsonDocument document = QJsonDocument::fromJson(mReply->readAll(), nullptr);
0047 
0048     const QJsonObject jsonNote = document.object();
0049 
0050     qCDebug(TOMBOYNOTESRESOURCE_LOG) << "TomboyItemDownloadJob: JSON note: " << jsonNote;
0051 
0052     mResultItem.setRemoteRevision(QString::number(jsonNote[QLatin1StringView("last-sync-revision")].toInt()));
0053     qCDebug(TOMBOYNOTESRESOURCE_LOG) << "TomboyItemDownloadJob: Sync revision " << mResultItem.remoteRevision();
0054 
0055     // Set timestamp
0056     const QString timeStampJson = jsonNote[QLatin1StringView("last-change-date")].toString();
0057     const QDateTime modificationTime = QDateTime::fromString(timeStampJson, Qt::ISODate);
0058     mResultItem.setModificationTime(modificationTime);
0059 
0060     // Set note title
0061     auto akonadiNote = KMime::Message::Ptr::create();
0062     akonadiNote->subject(true)->fromUnicodeString(jsonNote[QLatin1StringView("title")].toString(), "utf-8");
0063 
0064     // Set note content
0065     akonadiNote->contentType()->setMimeType("text/html");
0066     akonadiNote->contentType()->setCharset("utf-8");
0067     akonadiNote->contentTransferEncoding(true)->setEncoding(KMime::Headers::CEquPr);
0068     akonadiNote->mainBodyPart()->fromUnicodeString(jsonNote[QLatin1StringView("note-content")].toString());
0069 
0070     // Add title and content to Akonadi::Item
0071     akonadiNote->assemble();
0072     mResultItem.setPayload<KMime::Message::Ptr>(akonadiNote);
0073 
0074     emitResult();
0075 }
0076 
0077 #include "moc_tomboyitemdownloadjob.cpp"