2018-10-11 15:54:57 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2018-10-23 09:44:42 +00:00
|
|
|
#include "ui/image/image.h"
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-23 09:44:42 +00:00
|
|
|
namespace Images {
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
class ImageSource : public Source {
|
|
|
|
public:
|
|
|
|
ImageSource(QImage &&data, const QByteArray &format);
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
void load(
|
2018-10-11 15:54:57 +00:00
|
|
|
Data::FileOrigin origin,
|
2018-10-12 16:41:51 +00:00
|
|
|
bool loadFirst,
|
|
|
|
bool prior) override;
|
|
|
|
void loadEvenCancelled(
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
bool loadFirst,
|
|
|
|
bool prior) override;
|
|
|
|
QImage takeLoaded() override;
|
2018-10-23 12:57:43 +00:00
|
|
|
void unload() override;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
void automaticLoad(
|
2018-10-11 15:54:57 +00:00
|
|
|
Data::FileOrigin origin,
|
2018-10-12 16:41:51 +00:00
|
|
|
const HistoryItem *item) override;
|
|
|
|
void automaticLoadSettingsChanged() override;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
bool loading() override;
|
|
|
|
bool displayLoading() override;
|
|
|
|
void cancel() override;
|
|
|
|
float64 progress() override;
|
|
|
|
int loadOffset() override;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
const StorageImageLocation &location() override;
|
|
|
|
void refreshFileReference(const QByteArray &data) override;
|
|
|
|
std::optional<Storage::Cache::Key> cacheKey() override;
|
|
|
|
void setDelayedStorageLocation(
|
|
|
|
const StorageImageLocation &location) override;
|
|
|
|
void performDelayedLoad(Data::FileOrigin origin) override;
|
|
|
|
bool isDelayedStorageImage() const override;
|
|
|
|
void setImageBytes(const QByteArray &bytes) override;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
int width() override;
|
|
|
|
int height() override;
|
2018-10-25 09:24:45 +00:00
|
|
|
int bytesSize() override;
|
2018-10-12 16:41:51 +00:00
|
|
|
void setInformation(int size, int width, int height) override;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
QByteArray bytesForCache() override;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
private:
|
|
|
|
QImage _data;
|
|
|
|
QByteArray _format;
|
2018-10-23 13:04:42 +00:00
|
|
|
QByteArray _bytes;
|
2018-10-30 05:44:48 +00:00
|
|
|
int _width = 0;
|
|
|
|
int _height = 0;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
};
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
class LocalFileSource : public Source {
|
|
|
|
public:
|
2018-11-12 09:16:54 +00:00
|
|
|
explicit LocalFileSource(
|
2018-10-12 16:41:51 +00:00
|
|
|
const QString &path,
|
2018-11-12 09:16:54 +00:00
|
|
|
const QByteArray &content = QByteArray(),
|
|
|
|
const QByteArray &format = QByteArray(),
|
2018-10-12 16:41:51 +00:00
|
|
|
QImage &&data = QImage());
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
void load(
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
bool loadFirst,
|
|
|
|
bool prior) override;
|
|
|
|
void loadEvenCancelled(
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
bool loadFirst,
|
|
|
|
bool prior) override;
|
|
|
|
QImage takeLoaded() override;
|
2018-10-23 12:57:43 +00:00
|
|
|
void unload() override;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
void automaticLoad(
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
const HistoryItem *item) override;
|
|
|
|
void automaticLoadSettingsChanged() override;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
bool loading() override;
|
|
|
|
bool displayLoading() override;
|
|
|
|
void cancel() override;
|
|
|
|
float64 progress() override;
|
|
|
|
int loadOffset() override;
|
|
|
|
|
|
|
|
const StorageImageLocation &location() override;
|
|
|
|
void refreshFileReference(const QByteArray &data) override;
|
|
|
|
std::optional<Storage::Cache::Key> cacheKey() override;
|
|
|
|
void setDelayedStorageLocation(
|
|
|
|
const StorageImageLocation &location) override;
|
|
|
|
void performDelayedLoad(Data::FileOrigin origin) override;
|
|
|
|
bool isDelayedStorageImage() const override;
|
|
|
|
void setImageBytes(const QByteArray &bytes) override;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
int width() override;
|
|
|
|
int height() override;
|
2018-10-25 09:24:45 +00:00
|
|
|
int bytesSize() override;
|
2018-10-12 16:41:51 +00:00
|
|
|
void setInformation(int size, int width, int height) override;
|
|
|
|
|
|
|
|
QByteArray bytesForCache() override;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
|
|
|
private:
|
2018-10-12 16:41:51 +00:00
|
|
|
void ensureDimensionsKnown();
|
|
|
|
|
|
|
|
QString _path;
|
|
|
|
QByteArray _bytes;
|
|
|
|
QByteArray _format;
|
|
|
|
QImage _data;
|
|
|
|
int _width = 0;
|
|
|
|
int _height = 0;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
class RemoteSource : public Source {
|
2018-10-11 15:54:57 +00:00
|
|
|
public:
|
2018-10-12 16:41:51 +00:00
|
|
|
void load(
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
bool loadFirst,
|
|
|
|
bool prior) override;
|
|
|
|
void loadEvenCancelled(
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
bool loadFirst,
|
|
|
|
bool prior) override;
|
|
|
|
QImage takeLoaded() override;
|
2018-10-23 12:57:43 +00:00
|
|
|
void unload() override;
|
2018-10-12 16:41:51 +00:00
|
|
|
|
2018-10-11 15:54:57 +00:00
|
|
|
void automaticLoad(
|
|
|
|
Data::FileOrigin origin,
|
2018-10-12 16:41:51 +00:00
|
|
|
const HistoryItem *item) override;
|
2018-10-11 15:54:57 +00:00
|
|
|
void automaticLoadSettingsChanged() override;
|
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
bool loading() override;
|
|
|
|
bool displayLoading() override;
|
2018-10-11 15:54:57 +00:00
|
|
|
void cancel() override;
|
2018-10-12 16:41:51 +00:00
|
|
|
float64 progress() override;
|
|
|
|
int loadOffset() override;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
const StorageImageLocation &location() override;
|
|
|
|
void refreshFileReference(const QByteArray &data) override;
|
|
|
|
void setDelayedStorageLocation(
|
|
|
|
const StorageImageLocation &location) override;
|
|
|
|
void performDelayedLoad(Data::FileOrigin origin) override;
|
|
|
|
bool isDelayedStorageImage() const override;
|
|
|
|
void setImageBytes(const QByteArray &bytes) override;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
QByteArray bytesForCache() override;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
~RemoteSource();
|
2018-10-11 15:54:57 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// If after loading the image we need to shrink it to fit into a
|
|
|
|
// specific size, you can return this size here.
|
2018-10-12 16:41:51 +00:00
|
|
|
virtual QSize shrinkBox() const = 0;
|
2018-10-11 15:54:57 +00:00
|
|
|
virtual FileLoader *createLoader(
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
LoadFromCloudSetting fromCloud,
|
|
|
|
bool autoLoading) = 0;
|
|
|
|
|
|
|
|
void loadLocal();
|
|
|
|
|
|
|
|
private:
|
2018-10-12 16:41:51 +00:00
|
|
|
bool loaderValid() const;
|
2018-11-30 13:27:33 +00:00
|
|
|
bool cancelled() const;
|
2018-11-23 14:39:14 +00:00
|
|
|
void destroyLoader(FileLoader *newValue = nullptr);
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
FileLoader *_loader = nullptr;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
class StorageSource : public RemoteSource {
|
2018-10-11 15:54:57 +00:00
|
|
|
public:
|
2018-10-12 16:41:51 +00:00
|
|
|
StorageSource(
|
|
|
|
const StorageImageLocation &location,
|
|
|
|
int size);
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
const StorageImageLocation &location() override;
|
|
|
|
std::optional<Storage::Cache::Key> cacheKey() override;
|
|
|
|
|
|
|
|
void refreshFileReference(const QByteArray &data) override;
|
|
|
|
|
|
|
|
int width() override;
|
|
|
|
int height() override;
|
2018-10-25 09:24:45 +00:00
|
|
|
int bytesSize() override;
|
2018-10-12 16:41:51 +00:00
|
|
|
void setInformation(int size, int width, int height) override;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
|
|
|
protected:
|
2018-10-12 16:41:51 +00:00
|
|
|
QSize shrinkBox() const override;
|
2018-10-11 15:54:57 +00:00
|
|
|
FileLoader *createLoader(
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
LoadFromCloudSetting fromCloud,
|
|
|
|
bool autoLoading) override;
|
|
|
|
|
|
|
|
StorageImageLocation _location;
|
2018-10-12 16:41:51 +00:00
|
|
|
int _size = 0;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
class WebCachedSource : public RemoteSource {
|
2018-10-11 15:54:57 +00:00
|
|
|
public:
|
2018-10-12 16:41:51 +00:00
|
|
|
WebCachedSource(const WebFileLocation &location, QSize box, int size = 0);
|
|
|
|
WebCachedSource(
|
2018-10-11 15:54:57 +00:00
|
|
|
const WebFileLocation &location,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
int size = 0);
|
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
std::optional<Storage::Cache::Key> cacheKey() override;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
int width() override;
|
|
|
|
int height() override;
|
2018-10-25 09:24:45 +00:00
|
|
|
int bytesSize() override;
|
2018-10-11 15:54:57 +00:00
|
|
|
void setInformation(int size, int width, int height) override;
|
2018-10-12 16:41:51 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
QSize shrinkBox() const override;
|
2018-10-11 15:54:57 +00:00
|
|
|
FileLoader *createLoader(
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
LoadFromCloudSetting fromCloud,
|
|
|
|
bool autoLoading) override;
|
|
|
|
|
|
|
|
WebFileLocation _location;
|
|
|
|
QSize _box;
|
|
|
|
int _width = 0;
|
|
|
|
int _height = 0;
|
|
|
|
int _size = 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
class GeoPointSource : public RemoteSource {
|
2018-10-11 15:54:57 +00:00
|
|
|
public:
|
2018-10-12 16:41:51 +00:00
|
|
|
GeoPointSource(const GeoPointLocation &location);
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
std::optional<Storage::Cache::Key> cacheKey() override;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
int width() override;
|
|
|
|
int height() override;
|
2018-10-25 09:24:45 +00:00
|
|
|
int bytesSize() override;
|
2018-10-11 15:54:57 +00:00
|
|
|
void setInformation(int size, int width, int height) override;
|
2018-10-12 16:41:51 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
QSize shrinkBox() const override;
|
2018-10-11 15:54:57 +00:00
|
|
|
FileLoader *createLoader(
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
LoadFromCloudSetting fromCloud,
|
|
|
|
bool autoLoading) override;
|
|
|
|
|
|
|
|
GeoPointLocation _location;
|
|
|
|
int _size = 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
class DelayedStorageSource : public StorageSource {
|
2018-10-11 15:54:57 +00:00
|
|
|
public:
|
2018-10-12 16:41:51 +00:00
|
|
|
DelayedStorageSource();
|
|
|
|
DelayedStorageSource(int width, int height);
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
void load(
|
2018-10-11 15:54:57 +00:00
|
|
|
Data::FileOrigin origin,
|
2018-10-12 16:41:51 +00:00
|
|
|
bool loadFirst,
|
|
|
|
bool prior) override;
|
|
|
|
void loadEvenCancelled(
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
bool loadFirst,
|
|
|
|
bool prior) override;
|
|
|
|
|
|
|
|
void setDelayedStorageLocation(
|
|
|
|
const StorageImageLocation &location) override;
|
|
|
|
bool isDelayedStorageImage() const override;
|
|
|
|
void performDelayedLoad(Data::FileOrigin origin) override;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
|
|
|
void automaticLoad(
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
const HistoryItem *item) override; // auto load photo
|
|
|
|
void automaticLoadSettingsChanged() override;
|
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
bool loading() override {
|
2019-03-22 14:19:43 +00:00
|
|
|
return _location.valid()
|
|
|
|
? StorageSource::loading()
|
|
|
|
: _loadRequested;
|
2018-10-11 15:54:57 +00:00
|
|
|
}
|
2018-10-12 16:41:51 +00:00
|
|
|
bool displayLoading() override;
|
2018-10-11 15:54:57 +00:00
|
|
|
void cancel() override;
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
2018-10-12 16:41:51 +00:00
|
|
|
bool _loadRequested = false;
|
|
|
|
bool _loadCancelled = false;
|
|
|
|
bool _loadFromCloud = false;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
class WebUrlSource : public RemoteSource {
|
2018-10-11 15:54:57 +00:00
|
|
|
public:
|
|
|
|
// If !box.isEmpty() then resize the image to fit in this box.
|
2018-10-12 16:41:51 +00:00
|
|
|
explicit WebUrlSource(const QString &url, QSize box = QSize());
|
|
|
|
WebUrlSource(const QString &url, int width, int height);
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
std::optional<Storage::Cache::Key> cacheKey() override;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
2018-10-12 16:41:51 +00:00
|
|
|
int width() override;
|
|
|
|
int height() override;
|
2018-10-25 09:24:45 +00:00
|
|
|
int bytesSize() override;
|
2018-10-12 16:41:51 +00:00
|
|
|
void setInformation(int size, int width, int height) override;
|
2018-10-11 15:54:57 +00:00
|
|
|
|
|
|
|
protected:
|
2018-10-12 16:41:51 +00:00
|
|
|
QSize shrinkBox() const override;
|
2018-10-11 15:54:57 +00:00
|
|
|
FileLoader *createLoader(
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
LoadFromCloudSetting fromCloud,
|
|
|
|
bool autoLoading) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
QString _url;
|
|
|
|
QSize _box;
|
2018-10-12 16:41:51 +00:00
|
|
|
int _size = 0;
|
|
|
|
int _width = 0;
|
|
|
|
int _height = 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Images
|