Line data Source code
1 : #ifndef IMAGEGRABBER_H
2 : #define IMAGEGRABBER_H
3 :
4 : #include <QObject>
5 : #include <QFutureWatcher>
6 : #include <QImage>
7 : #include <QList>
8 : #include <QUrl>
9 : #include <QMap>
10 : #include <QByteArray>
11 :
12 : #include "../FangObject.h"
13 :
14 : class BatchDownloadCore;
15 : class QNetworkAccessManager;
16 :
17 : /*!
18 : \brief ImageData stores downloaded image data for offline viewing.
19 : */
20 : struct ImageData {
21 : QImage image; // Decoded image
22 : QByteArray rawData; // Original bytes (saved to disk cache)
23 : QString mimeType; // MIME type (jpeg vs png, etc.)
24 :
25 71 : inline bool isValid() const { return !image.isNull() && !rawData.isEmpty(); }
26 : };
27 :
28 : /*!
29 : \brief The ImageGrabber attempts to download all images from a list of URLs.
30 :
31 : Uses BatchDownloadCore internally to handle parallel downloads with
32 : redirect and timeout handling.
33 : */
34 : class ImageGrabber : public FangObject
35 : {
36 : Q_OBJECT
37 : public:
38 : explicit ImageGrabber(QObject *parent = nullptr, QNetworkAccessManager* networkManager = nullptr);
39 :
40 : signals:
41 : /*!
42 : \brief Called when all images are found.
43 : */
44 : void finished();
45 :
46 : public slots:
47 :
48 : /*!
49 : \brief Fetch a group of remote images.
50 : \param urls
51 : */
52 : void fetchUrls(const QList<QUrl> &urls);
53 :
54 : /*!
55 : \brief Fetch a remote image.
56 : \param url
57 : */
58 : void fetchUrl(const QUrl &url);
59 :
60 56 : inline QMap<QUrl, ImageData>* getResults() { return &results; }
61 :
62 : private slots:
63 : void onBatchFinished();
64 :
65 : private:
66 : BatchDownloadCore* batchDownloader;
67 : QMap<QUrl, ImageData> results;
68 : QFutureWatcher<void> processWatcher;
69 : };
70 :
71 : #endif // IMAGEGRABBER_H
|