summaryrefslogtreecommitdiffstats
path: root/core/imagedownloader.h
blob: 28038325083d39db30907429161e49fb1356f35d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// SPDX-License-Identifier: GPL-2.0
#ifndef IMAGEDOWNLOADER_H
#define IMAGEDOWNLOADER_H

#include "metadata.h"
#include <QImage>
#include <QFuture>
#include <QNetworkReply>
#include <QThreadPool>

class ImageDownloader : public QObject {
	Q_OBJECT
public:
	static ImageDownloader *instance();
	ImageDownloader();
public slots:
	void load(QUrl url, QString filename);
signals:
	void loaded(QString filename);
	void failed(QString filename);
private:
	QNetworkAccessManager manager;
	void loadFromUrl(const QString &filename, const QUrl &);
	void saveImage(QNetworkReply *reply);
};

struct PictureEntry;
class Thumbnailer : public QObject {
	Q_OBJECT
public:
	static Thumbnailer *instance();

	// Schedule a thumbnail for fetching or calculation.
	// If synchronous is false, returns a placeholder thumbnail.
	// The actual thumbnail will be sent via a signal later.
	// If synchronous is true, try to fetch the actual thumbnail.
	// In this mode only precalculated thumbnails or thumbnails
	// from pictures are returned. Video extraction and remote
	// images are not supported.
	QImage fetchThumbnail(const QString &filename, bool synchronous);

	// Schedule multiple thumbnails for forced recalculation
	void calculateThumbnails(const QVector<QString> &filenames);

	// If we change dive, clear all unfinished thumbnail creations
	void clearWorkQueue();
	static int maxThumbnailSize();
	static int defaultThumbnailSize();
	static int thumbnailSize(double zoomLevel);
public slots:
	void imageDownloaded(QString filename);
	void imageDownloadFailed(QString filename);
	void frameExtracted(QString filename, QImage thumbnail, duration_t duration, duration_t offset);
	void frameExtractionFailed(QString filename, duration_t duration);
	void frameExtractionInvalid(QString filename, duration_t duration);
signals:
	void thumbnailChanged(QString filename, QImage thumbnail, duration_t duration);
private:
	struct Thumbnail {
		QImage img;
		mediatype_t type;
		duration_t duration;
	};

	Thumbnailer();
	Thumbnail fetchVideoThumbnail(const QString &filename, const QString &originalFilename, duration_t duration);
	Thumbnail extractVideoThumbnail(const QString &picture_filename, duration_t duration);
	Thumbnail addPictureThumbnailToCache(const QString &picture_filename, const QImage &thumbnail);
	Thumbnail addVideoThumbnailToCache(const QString &picture_filename, duration_t duration, const QImage &thumbnail, duration_t position);
	Thumbnail addUnknownThumbnailToCache(const QString &picture_filename);
	void recalculate(QString filename);
	void processItem(QString filename, bool tryDownload);
	Thumbnail getThumbnailFromCache(const QString &picture_filename);
	Thumbnail getPictureThumbnailFromStream(QDataStream &stream);
	Thumbnail getVideoThumbnailFromStream(QDataStream &stream, const QString &filename);
	Thumbnail fetchImage(const QString &filename, const QString &originalFilename, bool tryDownload);
	Thumbnail getHashedImage(const QString &filename, bool tryDownload);
	void markVideoThumbnail(QImage &img);

	mutable QMutex lock;
	QThreadPool pool;
	QImage failImage;		// Shown when image-fetching fails
	QImage dummyImage;		// Shown before thumbnail is fetched
	QImage videoImage;		// Place holder for videos
	QImage videoOverlayImage;	// Overlay for video thumbnails
	QImage unknownImage;		// Place holder for files where we couldn't determine the type

	QMap<QString,QFuture<void>> workingOn;
};

#endif // IMAGEDOWNLOADER_H