| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #ifndef FEEDDOWNLOADER_H |
| 4 | #define FEEDDOWNLOADER_H |
| 5 | |
| 6 | #include "core/message.h" |
| 7 | #include "exceptions/applicationexception.h" |
| 8 | #include "services/abstract/cacheforserviceroot.h" |
| 9 | #include "services/abstract/feed.h" |
| 10 | |
| 11 | #include <QFutureWatcher> |
| 12 | #include <QHash> |
| 13 | #include <QObject> |
| 14 | #include <QPair> |
| 15 | |
| 16 | class MessageFilter; |
| 17 | |
| 18 | // Represents results of batch feed updates. |
| 19 | class FeedDownloadResults { |
| 20 | public: |
| 21 | QHash<Feed*, QString> erroredFeeds() const; |
| 22 | QHash<Feed*, QList<Message>> updatedFeeds() const; |
| 23 | QString overview(int how_many_feeds) const; |
| 24 | |
| 25 | void appendUpdatedFeed(Feed* feed, const QList<Message>& updated_unread_msgs); |
| 26 | void appendErroredFeed(Feed* feed, const QString& error); |
| 27 | void clear(); |
| 28 | |
| 29 | private: |
| 30 | // QString represents title if the feed, int represents count of newly downloaded messages. |
| 31 | QHash<Feed*, QList<Message>> m_updatedFeeds; |
| 32 | QHash<Feed*, QString> m_erroredFeeds; |
| 33 | }; |
| 34 | |
| 35 | struct FeedUpdateRequest { |
| 36 | Feed* feed = nullptr; |
| 37 | ServiceRoot* account = nullptr; |
| 38 | QHash<ServiceRoot::BagOfMessages, QStringList> stated_messages; |
| 39 | QHash<QString, QStringList> tagged_messages; |
| 40 | }; |
| 41 | |
| 42 | struct FeedUpdateResult { |
| 43 | Feed* feed = nullptr; |
| 44 | }; |
| 45 | |
| 46 | // This class offers means to "update" feeds and "special" categories. |
| 47 | // NOTE: This class is used within separate thread. |
| 48 | class FeedDownloader : public QObject { |
| 49 | Q_OBJECT |
| 50 | |
| 51 | public: |
| 52 | explicit FeedDownloader(); |
| 53 | virtual ~FeedDownloader(); |
| 54 | |
| 55 | bool isUpdateRunning() const; |
| 56 | bool isCacheSynchronizationRunning() const; |
| 57 | |
| 58 | public slots: |
| 59 | void synchronizeAccountCaches(const QList<CacheForServiceRoot*>& caches, bool emit_signals); |
| 60 | void updateFeeds(const QList<Feed*>& feeds); |
| 61 | void stopRunningUpdate(); |
| 62 | |
| 63 | signals: |
| 64 | void cachesSynchronized(); |
| 65 | void updateStarted(); |
| 66 | void updateFinished(FeedDownloadResults updated_feeds); |
| 67 | void updateProgress(const Feed* feed, int current, int total); |
| 68 | |
| 69 | private: |
| 70 | void clearFeedOverload(Feed* feed); |
| 71 | bool checkIfFeedOverloaded(Feed* feed) const; |
| 72 | |
| 73 | void skipFeedUpdateWithError(ServiceRoot* acc, Feed* feed, const ApplicationException& ex); |
| 74 | void updateOneFeed(ServiceRoot* acc, |
| 75 | Feed* feed, |
| 76 | const QHash<ServiceRoot::BagOfMessages, QStringList>& stated_messages, |
| 77 | const QHash<QString, QStringList>& tagged_messages); |
| 78 | void finalizeUpdate(); |
| 79 | void removeDuplicateMessages(QList<Message>& messages); |
| 80 | void removeTooOldMessages(Feed* feed, QList<Message>& msgs); |
| 81 | |
| 82 | QList<Feed*> scrambleFeedsWithSameHost(const QList<Feed*>& feeds) const; |
| 83 | FeedUpdateResult updateThreadedFeed(const FeedUpdateRequest& fd); |
| 84 | |
| 85 | private: |
| 86 | bool m_isCacheSynchronizationRunning; |
| 87 | bool m_stopCacheSynchronization; |
| 88 | QMutex m_mutexDb; |
| 89 | QHash<ServiceRoot*, ApplicationException> m_erroredAccounts; |
| 90 | QList<FeedUpdateRequest> m_feeds = {}; |
| 91 | QFutureWatcher<FeedUpdateResult> m_watcherLookup; |
| 92 | FeedDownloadResults m_results; |
| 93 | QHash<QString, QDateTime> m_overloadedHosts; |
| 94 | }; |
| 95 | |
| 96 | #endif // FEEDDOWNLOADER_H |