File indexing completed on 2025-01-05 04:29:56

0001 /**
0002  * SPDX-FileCopyrightText: 2021-2022 Bart De Vries <bart@mogwai.be>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #pragma once
0008 
0009 #include <QNetworkAccessManager>
0010 #include <QNetworkReply>
0011 #include <QNetworkRequest>
0012 #include <QString>
0013 
0014 #include <Syndication/Syndication>
0015 #include <ThreadWeaver/Job>
0016 
0017 #include "enclosure.h"
0018 
0019 class UpdateFeedJob : public QObject, public ThreadWeaver::Job
0020 {
0021     Q_OBJECT
0022 
0023 public:
0024     explicit UpdateFeedJob(const QString &url, const QByteArray &data, QObject *parent = nullptr);
0025 
0026     void run(ThreadWeaver::JobPointer, ThreadWeaver::Thread *) override;
0027     void abort();
0028 
0029     struct EntryDetails {
0030         QString feed;
0031         QString id;
0032         QString title;
0033         QString content;
0034         int created;
0035         int updated;
0036         QString link;
0037         bool read;
0038         bool isNew;
0039         bool hasEnclosure;
0040         QString image;
0041     };
0042 
0043     struct AuthorDetails {
0044         QString feed;
0045         QString id;
0046         QString name;
0047         QString uri;
0048         QString email;
0049     };
0050 
0051     struct EnclosureDetails {
0052         QString feed;
0053         QString id;
0054         int duration;
0055         int size;
0056         QString title;
0057         QString type;
0058         QString url;
0059         int playPosition;
0060         Enclosure::Status downloaded;
0061     };
0062 
0063     struct ChapterDetails {
0064         QString feed;
0065         QString id;
0066         int start;
0067         QString title;
0068         QString link;
0069         QString image;
0070     };
0071 
0072 Q_SIGNALS:
0073     void feedDetailsUpdated(const QString &url,
0074                             const QString &name,
0075                             const QString &image,
0076                             const QString &link,
0077                             const QString &description,
0078                             const QDateTime &lastUpdated,
0079                             const QString &dirname);
0080     void feedUpdated(const QString &url);
0081     void entryAdded(const QString &feedurl, const QString &id);
0082     void entryUpdated(const QString &feedurl, const QString &id);
0083     void feedUpdateStatusChanged(const QString &url, bool status);
0084     void aborting();
0085     void finished();
0086 
0087 private:
0088     void processFeed(Syndication::FeedPtr feed);
0089     bool processEntry(Syndication::ItemPtr entry);
0090     bool processAuthor(const QString &entryId, const QString &authorName, const QString &authorUri, const QString &authorEmail);
0091     bool processEnclosure(Syndication::EnclosurePtr enclosure, const EntryDetails &newEntry, const EntryDetails &oldEntry);
0092     bool processChapter(const QString &entryId, const int &start, const QString &chapterTitle, const QString &link, const QString &image);
0093     void writeToDatabase();
0094 
0095     QString generateFeedDirname(const QString &name) const;
0096     bool m_abort = false;
0097 
0098     QString m_url;
0099     QString m_dirname;
0100     QByteArray m_data;
0101     QString m_oldHash, m_newHash;
0102 
0103     bool m_isNewFeed;
0104     bool m_markUnreadOnNewFeed;
0105     QVector<EntryDetails> m_entries, m_newEntries, m_updateEntries;
0106     QVector<AuthorDetails> m_authors, m_newAuthors, m_updateAuthors;
0107     QVector<EnclosureDetails> m_enclosures, m_newEnclosures, m_updateEnclosures;
0108     QVector<ChapterDetails> m_chapters, m_newChapters, m_updateChapters;
0109 };