// For license of this file, see /LICENSE.md. #ifndef FEEDDOWNLOADER_H #define FEEDDOWNLOADER_H #include "core/message.h" #include "exceptions/applicationexception.h" #include "services/abstract/cacheforserviceroot.h" #include "services/abstract/feed.h" #include #include #include #include class MessageFilter; // Represents results of batch feed updates. class FeedDownloadResults { public: QHash erroredFeeds() const; QHash> updatedFeeds() const; QString overview(int how_many_feeds) const; void appendUpdatedFeed(Feed* feed, const QList& updated_unread_msgs); void appendErroredFeed(Feed* feed, const QString& error); void clear(); private: // QString represents title if the feed, int represents count of newly downloaded messages. QHash> m_updatedFeeds; QHash m_erroredFeeds; }; struct FeedUpdateRequest { Feed* feed = nullptr; ServiceRoot* account = nullptr; QHash stated_messages; QHash tagged_messages; }; struct FeedUpdateResult { Feed* feed = nullptr; }; // This class offers means to "update" feeds and "special" categories. // NOTE: This class is used within separate thread. class FeedDownloader : public QObject { Q_OBJECT public: explicit FeedDownloader(); virtual ~FeedDownloader(); bool isUpdateRunning() const; bool isCacheSynchronizationRunning() const; public slots: void synchronizeAccountCaches(const QList& caches, bool emit_signals); void updateFeeds(const QList& feeds); void stopRunningUpdate(); signals: void cachesSynchronized(); void updateStarted(); void updateFinished(FeedDownloadResults updated_feeds); void updateProgress(const Feed* feed, int current, int total); private: void clearFeedOverload(Feed* feed); bool checkIfFeedOverloaded(Feed* feed) const; void skipFeedUpdateWithError(ServiceRoot* acc, Feed* feed, const ApplicationException& ex); void updateOneFeed(ServiceRoot* acc, Feed* feed, const QHash& stated_messages, const QHash& tagged_messages); void finalizeUpdate(); void removeDuplicateMessages(QList& messages); void removeTooOldMessages(Feed* feed, QList& msgs); QList scrambleFeedsWithSameHost(const QList& feeds) const; FeedUpdateResult updateThreadedFeed(const FeedUpdateRequest& fd); private: bool m_isCacheSynchronizationRunning; bool m_stopCacheSynchronization; QMutex m_mutexDb; QHash m_erroredAccounts; QList m_feeds = {}; QFutureWatcher m_watcherLookup; FeedDownloadResults m_results; QHash m_overloadedHosts; }; #endif // FEEDDOWNLOADER_H