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

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 "tomboycollectionsdownloadjob.h"
0008 #include "debug.h"
0009 #include <Akonadi/CachePolicy>
0010 #include <Akonadi/NoteUtils>
0011 #include <QJsonDocument>
0012 #include <QJsonObject>
0013 #include <QJsonValue>
0014 
0015 TomboyCollectionsDownloadJob::TomboyCollectionsDownloadJob(const QString &collectionName, QNetworkAccessManager *manager, int refreshInterval, QObject *parent)
0016     : TomboyJobBase(manager, parent)
0017     , mCollectionName(collectionName)
0018     , mRefreshInterval(refreshInterval)
0019 {
0020 }
0021 
0022 Akonadi::Collection::List TomboyCollectionsDownloadJob::collections() const
0023 {
0024     return mResultCollections;
0025 }
0026 
0027 void TomboyCollectionsDownloadJob::start()
0028 {
0029     // Get user information
0030     QNetworkRequest request = QNetworkRequest(QUrl(mContentURL));
0031     mReply = mRequestor->get(request, QList<O0RequestParameter>());
0032 
0033     connect(mReply, &QNetworkReply::finished, this, &TomboyCollectionsDownloadJob::onRequestFinished);
0034     qCDebug(TOMBOYNOTESRESOURCE_LOG) << "TomboyCollectionsDownloadJob: Start network request";
0035 }
0036 
0037 void TomboyCollectionsDownloadJob::onRequestFinished()
0038 {
0039     qCDebug(TOMBOYNOTESRESOURCE_LOG) << "TomboyCollectionsDownloadJob: Network request finished";
0040     checkReplyError();
0041     if (error() != TomboyJobError::NoError) {
0042         setErrorText(mReply->errorString());
0043         emitResult();
0044         return;
0045     }
0046 
0047     // Parse received data as JSON
0048     const QJsonDocument document = QJsonDocument::fromJson(mReply->readAll(), nullptr);
0049 
0050     const QJsonObject jo = document.object();
0051     qCDebug(TOMBOYNOTESRESOURCE_LOG) << "TomboyCollectionsDownloadJob: " << jo;
0052     const QJsonValue collectionRevision = jo[QLatin1StringView("latest-sync-revision")];
0053     qCDebug(TOMBOYNOTESRESOURCE_LOG) << "TomboyCollectionsDownloadJob: " << collectionRevision;
0054 
0055     Akonadi::Collection c;
0056     c.setParentCollection(Akonadi::Collection::root());
0057     c.setRemoteId(mContentURL);
0058     c.setName(mCollectionName);
0059     c.setRemoteRevision(QString::number(collectionRevision.toInt()));
0060     qCDebug(TOMBOYNOTESRESOURCE_LOG) << "TomboyCollectionsDownloadJob: Sync revision " << collectionRevision.toString();
0061 
0062     Akonadi::CachePolicy cachePolicy;
0063     cachePolicy.setInheritFromParent(false);
0064     cachePolicy.setSyncOnDemand(false);
0065     cachePolicy.setCacheTimeout(-1);
0066     cachePolicy.setIntervalCheckTime(mRefreshInterval);
0067     cachePolicy.setLocalParts(QStringList() << QStringLiteral("ALL"));
0068     c.setCachePolicy(cachePolicy);
0069 
0070     c.setContentMimeTypes({Akonadi::NoteUtils::noteMimeType()});
0071 
0072     mResultCollections << c;
0073 
0074     emitResult();
0075 }
0076 
0077 #include "moc_tomboycollectionsdownloadjob.cpp"