2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2014-12-01 10:47:38 +00:00
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2015-10-03 13:16:42 +00:00
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2017-01-11 18:31:31 +00:00
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2016-05-25 17:59:21 +00:00
|
|
|
#include "mtproto/file_download.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-07-07 12:35:14 +00:00
|
|
|
enum class ImageRoundRadius {
|
2016-07-19 10:54:43 +00:00
|
|
|
None,
|
2016-07-07 12:35:14 +00:00
|
|
|
Large,
|
|
|
|
Small,
|
|
|
|
};
|
2016-11-21 20:26:54 +00:00
|
|
|
enum class ImageRoundCorner {
|
|
|
|
None = 0x00,
|
|
|
|
TopLeft = 0x01,
|
|
|
|
TopRight = 0x02,
|
|
|
|
BottomLeft = 0x04,
|
|
|
|
BottomRight = 0x08,
|
|
|
|
All = 0x0f,
|
|
|
|
};
|
|
|
|
Q_DECLARE_FLAGS(ImageRoundCorners, ImageRoundCorner);
|
|
|
|
Q_DECLARE_OPERATORS_FOR_FLAGS(ImageRoundCorners);
|
2016-07-07 12:35:14 +00:00
|
|
|
|
2015-12-24 19:26:28 +00:00
|
|
|
inline uint32 packInt(int32 a) {
|
|
|
|
return (a < 0) ? uint32(int64(a) + 0x100000000LL) : uint32(a);
|
|
|
|
}
|
|
|
|
inline int32 unpackInt(uint32 a) {
|
|
|
|
return (a > 0x7FFFFFFFU) ? int32(int64(a) - 0x100000000LL) : int32(a);
|
|
|
|
}
|
|
|
|
inline uint64 packUIntUInt(uint32 a, uint32 b) {
|
|
|
|
return (uint64(a) << 32) | uint64(b);
|
|
|
|
}
|
|
|
|
inline uint64 packUIntInt(uint32 a, int32 b) {
|
|
|
|
return packUIntUInt(a, packInt(b));
|
|
|
|
}
|
|
|
|
inline uint64 packIntUInt(int32 a, uint32 b) {
|
|
|
|
return packUIntUInt(packInt(a), b);
|
|
|
|
}
|
|
|
|
inline uint64 packIntInt(int32 a, int32 b) {
|
|
|
|
return packUIntUInt(packInt(a), packInt(b));
|
|
|
|
}
|
|
|
|
inline uint32 unpackUIntFirst(uint64 v) {
|
|
|
|
return uint32(v >> 32);
|
|
|
|
}
|
|
|
|
inline int32 unpackIntFirst(uint64 v) {
|
|
|
|
return unpackInt(unpackUIntFirst(v));
|
|
|
|
}
|
|
|
|
inline uint32 unpackUIntSecond(uint64 v) {
|
|
|
|
return uint32(v & 0xFFFFFFFFULL);
|
|
|
|
}
|
|
|
|
inline int32 unpackIntSecond(uint64 v) {
|
|
|
|
return unpackInt(unpackUIntSecond(v));
|
|
|
|
}
|
|
|
|
|
|
|
|
class StorageImageLocation {
|
|
|
|
public:
|
|
|
|
StorageImageLocation() : _widthheight(0), _dclocal(0), _volume(0), _secret(0) {
|
2015-05-19 15:46:45 +00:00
|
|
|
}
|
2015-12-24 19:26:28 +00:00
|
|
|
StorageImageLocation(int32 width, int32 height, int32 dc, const uint64 &volume, int32 local, const uint64 &secret) : _widthheight(packIntInt(width, height)), _dclocal(packIntInt(dc, local)), _volume(volume), _secret(secret) {
|
2015-05-19 15:46:45 +00:00
|
|
|
}
|
2015-12-24 19:26:28 +00:00
|
|
|
StorageImageLocation(int32 width, int32 height, const MTPDfileLocation &location) : _widthheight(packIntInt(width, height)), _dclocal(packIntInt(location.vdc_id.v, location.vlocal_id.v)), _volume(location.vvolume_id.v), _secret(location.vsecret.v) {
|
2015-05-19 15:46:45 +00:00
|
|
|
}
|
2015-08-07 12:11:50 +00:00
|
|
|
bool isNull() const {
|
2015-12-24 19:26:28 +00:00
|
|
|
return !_dclocal;
|
|
|
|
}
|
|
|
|
int32 width() const {
|
|
|
|
return unpackIntFirst(_widthheight);
|
2015-08-07 12:11:50 +00:00
|
|
|
}
|
2015-12-24 19:26:28 +00:00
|
|
|
int32 height() const {
|
|
|
|
return unpackIntSecond(_widthheight);
|
|
|
|
}
|
|
|
|
void setSize(int32 width, int32 height) {
|
|
|
|
_widthheight = packIntInt(width, height);
|
|
|
|
}
|
|
|
|
int32 dc() const {
|
|
|
|
return unpackIntFirst(_dclocal);
|
|
|
|
}
|
|
|
|
uint64 volume() const {
|
|
|
|
return _volume;
|
|
|
|
}
|
|
|
|
int32 local() const {
|
|
|
|
return unpackIntSecond(_dclocal);
|
|
|
|
}
|
|
|
|
uint64 secret() const {
|
|
|
|
return _secret;
|
|
|
|
}
|
|
|
|
|
2015-12-27 21:37:48 +00:00
|
|
|
static StorageImageLocation Null;
|
|
|
|
|
2015-12-24 19:26:28 +00:00
|
|
|
private:
|
|
|
|
uint64 _widthheight;
|
|
|
|
uint64 _dclocal;
|
|
|
|
uint64 _volume;
|
|
|
|
uint64 _secret;
|
|
|
|
|
|
|
|
friend inline bool operator==(const StorageImageLocation &a, const StorageImageLocation &b) {
|
|
|
|
return (a._dclocal == b._dclocal) && (a._volume == b._volume) && (a._secret == b._secret);
|
|
|
|
}
|
|
|
|
|
2015-05-19 15:46:45 +00:00
|
|
|
};
|
|
|
|
|
2015-08-07 12:11:50 +00:00
|
|
|
inline bool operator!=(const StorageImageLocation &a, const StorageImageLocation &b) {
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
2016-11-28 15:45:07 +00:00
|
|
|
namespace Images {
|
|
|
|
|
|
|
|
QImage prepareBlur(QImage image);
|
|
|
|
void prepareRound(QImage &image, ImageRoundRadius radius, ImageRoundCorners corners = ImageRoundCorner::All);
|
2016-12-23 13:21:01 +00:00
|
|
|
void prepareRound(QImage &image, QImage **cornerMasks, ImageRoundCorners corners = ImageRoundCorner::All);
|
2016-11-28 15:45:07 +00:00
|
|
|
void prepareCircle(QImage &image);
|
2016-12-23 13:21:01 +00:00
|
|
|
QImage prepareColored(style::color add, QImage image);
|
2016-11-28 15:45:07 +00:00
|
|
|
QImage prepareOpaque(QImage image);
|
|
|
|
|
|
|
|
enum class Option {
|
|
|
|
None = 0x000,
|
|
|
|
Smooth = 0x001,
|
|
|
|
Blurred = 0x002,
|
|
|
|
Circled = 0x004,
|
|
|
|
RoundedLarge = 0x008,
|
|
|
|
RoundedSmall = 0x010,
|
|
|
|
RoundedTopLeft = 0x020,
|
|
|
|
RoundedTopRight = 0x040,
|
|
|
|
RoundedBottomLeft = 0x080,
|
2016-11-21 20:26:54 +00:00
|
|
|
RoundedBottomRight = 0x100,
|
2016-11-28 15:45:07 +00:00
|
|
|
Colored = 0x200,
|
2016-03-18 10:18:30 +00:00
|
|
|
};
|
2016-11-28 15:45:07 +00:00
|
|
|
Q_DECLARE_FLAGS(Options, Option);
|
|
|
|
Q_DECLARE_OPERATORS_FOR_FLAGS(Options);
|
|
|
|
|
|
|
|
QImage prepare(QImage img, int w, int h, Options options, int outerw, int outerh);
|
|
|
|
|
|
|
|
inline QPixmap pixmap(QImage img, int w, int h, Options options, int outerw, int outerh) {
|
|
|
|
return QPixmap::fromImage(prepare(img, w, h, options, outerw, outerh), Qt::ColorOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Images
|
2015-12-19 14:37:28 +00:00
|
|
|
|
2015-12-31 15:27:21 +00:00
|
|
|
class DelayedStorageImage;
|
|
|
|
|
2015-12-24 19:26:28 +00:00
|
|
|
class HistoryItem;
|
2014-05-30 08:53:19 +00:00
|
|
|
class Image {
|
|
|
|
public:
|
2015-12-25 13:09:14 +00:00
|
|
|
Image(const QString &file, QByteArray format = QByteArray());
|
|
|
|
Image(const QByteArray &filecontent, QByteArray format = QByteArray());
|
|
|
|
Image(const QPixmap &pixmap, QByteArray format = QByteArray());
|
|
|
|
Image(const QByteArray &filecontent, QByteArray format, const QPixmap &pixmap);
|
2015-12-24 19:26:28 +00:00
|
|
|
|
|
|
|
virtual void automaticLoad(const HistoryItem *item) { // auto load photo
|
|
|
|
}
|
2015-12-27 21:37:48 +00:00
|
|
|
virtual void automaticLoadSettingsChanged() {
|
|
|
|
}
|
2015-12-24 19:26:28 +00:00
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
virtual bool loaded() const {
|
|
|
|
return true;
|
|
|
|
}
|
2014-09-30 14:11:09 +00:00
|
|
|
virtual bool loading() const {
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-24 19:26:28 +00:00
|
|
|
virtual bool displayLoading() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
virtual void cancel() {
|
|
|
|
}
|
|
|
|
virtual float64 progress() const {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
virtual int32 loadOffset() const {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
const QPixmap &pix(int32 w = 0, int32 h = 0) const;
|
2016-11-21 20:26:54 +00:00
|
|
|
const QPixmap &pixRounded(int32 w = 0, int32 h = 0, ImageRoundRadius radius = ImageRoundRadius::None, ImageRoundCorners corners = ImageRoundCorner::All) const;
|
2014-08-11 09:03:45 +00:00
|
|
|
const QPixmap &pixBlurred(int32 w = 0, int32 h = 0) const;
|
2016-12-23 13:21:01 +00:00
|
|
|
const QPixmap &pixColored(style::color add, int32 w = 0, int32 h = 0) const;
|
|
|
|
const QPixmap &pixBlurredColored(style::color add, int32 w = 0, int32 h = 0) const;
|
2016-11-21 20:26:54 +00:00
|
|
|
const QPixmap &pixSingle(int32 w, int32 h, int32 outerw, int32 outerh, ImageRoundRadius radius, ImageRoundCorners corners = ImageRoundCorner::All) const;
|
|
|
|
const QPixmap &pixBlurredSingle(int32 w, int32 h, int32 outerw, int32 outerh, ImageRoundRadius radius, ImageRoundCorners corners = ImageRoundCorner::All) const;
|
2016-12-03 12:10:35 +00:00
|
|
|
const QPixmap &pixCircled(int32 w = 0, int32 h = 0) const;
|
|
|
|
const QPixmap &pixBlurredCircled(int32 w = 0, int32 h = 0) const;
|
2016-11-28 15:45:07 +00:00
|
|
|
QPixmap pixNoCache(int w = 0, int h = 0, Images::Options options = 0, int outerw = -1, int outerh = -1) const;
|
2016-12-23 13:21:01 +00:00
|
|
|
QPixmap pixColoredNoCache(style::color add, int32 w = 0, int32 h = 0, bool smooth = false) const;
|
|
|
|
QPixmap pixBlurredColoredNoCache(style::color add, int32 w, int32 h = 0) const;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-01-30 16:31:10 +00:00
|
|
|
int32 width() const {
|
|
|
|
return qMax(countWidth(), 1);
|
2015-12-25 13:09:14 +00:00
|
|
|
}
|
|
|
|
|
2016-01-30 16:31:10 +00:00
|
|
|
int32 height() const {
|
|
|
|
return qMax(countHeight(), 1);
|
2015-12-25 13:09:14 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-12-24 19:26:28 +00:00
|
|
|
virtual void load(bool loadFirst = false, bool prior = true) {
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2015-12-25 13:09:14 +00:00
|
|
|
|
2015-12-24 19:26:28 +00:00
|
|
|
virtual void loadEvenCancelled(bool loadFirst = false, bool prior = true) {
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 21:37:48 +00:00
|
|
|
virtual const StorageImageLocation &location() const {
|
|
|
|
return StorageImageLocation::Null;
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
bool isNull() const;
|
2015-12-31 15:27:21 +00:00
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
void forget() const;
|
|
|
|
|
2014-11-22 09:45:04 +00:00
|
|
|
QByteArray savedFormat() const {
|
2015-12-25 13:09:14 +00:00
|
|
|
return _format;
|
2014-11-22 09:45:04 +00:00
|
|
|
}
|
|
|
|
QByteArray savedData() const {
|
2015-12-25 13:09:14 +00:00
|
|
|
return _saved;
|
2014-11-22 09:45:04 +00:00
|
|
|
}
|
|
|
|
|
2015-12-31 15:27:21 +00:00
|
|
|
virtual DelayedStorageImage *toDelayedStorageImage() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
virtual const DelayedStorageImage *toDelayedStorageImage() const {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-25 13:09:14 +00:00
|
|
|
virtual ~Image();
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
protected:
|
2015-12-25 13:09:14 +00:00
|
|
|
Image(QByteArray format = "PNG") : _format(format), _forgot(false) {
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-12-25 13:09:14 +00:00
|
|
|
void restore() const;
|
2015-12-24 19:26:28 +00:00
|
|
|
virtual void checkload() const {
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
void invalidateSizeCache() const;
|
|
|
|
|
2016-01-30 16:31:10 +00:00
|
|
|
virtual int32 countWidth() const {
|
|
|
|
restore();
|
|
|
|
return _data.width();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int32 countHeight() const {
|
|
|
|
restore();
|
|
|
|
return _data.height();
|
|
|
|
}
|
|
|
|
|
2015-12-25 13:09:14 +00:00
|
|
|
mutable QByteArray _saved, _format;
|
|
|
|
mutable bool _forgot;
|
|
|
|
mutable QPixmap _data;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
private:
|
2016-11-22 08:36:02 +00:00
|
|
|
using Sizes = QMap<uint64, QPixmap>;
|
2014-05-30 08:53:19 +00:00
|
|
|
mutable Sizes _sizesCache;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-11-22 09:45:04 +00:00
|
|
|
typedef QPair<uint64, uint64> StorageKey;
|
|
|
|
inline uint64 storageMix32To64(int32 a, int32 b) {
|
|
|
|
return (uint64(*reinterpret_cast<uint32*>(&a)) << 32) | uint64(*reinterpret_cast<uint32*>(&b));
|
|
|
|
}
|
2015-05-19 15:46:45 +00:00
|
|
|
inline StorageKey storageKey(int32 dc, const uint64 &volume, int32 local) {
|
2014-11-22 09:45:04 +00:00
|
|
|
return StorageKey(storageMix32To64(dc, local), volume);
|
|
|
|
}
|
|
|
|
inline StorageKey storageKey(const MTPDfileLocation &location) {
|
|
|
|
return storageKey(location.vdc_id.v, location.vvolume_id.v, location.vlocal_id.v);
|
|
|
|
}
|
2015-08-11 19:50:48 +00:00
|
|
|
inline StorageKey storageKey(const StorageImageLocation &location) {
|
2015-12-24 19:26:28 +00:00
|
|
|
return storageKey(location.dc(), location.volume(), location.local());
|
2015-08-11 19:50:48 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 19:09:20 +00:00
|
|
|
class RemoteImage : public Image {
|
2014-05-30 08:53:19 +00:00
|
|
|
public:
|
|
|
|
|
2015-12-30 19:09:20 +00:00
|
|
|
RemoteImage() : _loader(0) {
|
|
|
|
}
|
2015-12-24 19:26:28 +00:00
|
|
|
|
|
|
|
void automaticLoad(const HistoryItem *item); // auto load photo
|
2015-12-27 21:37:48 +00:00
|
|
|
void automaticLoadSettingsChanged();
|
2015-12-24 19:26:28 +00:00
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
bool loaded() const;
|
2015-12-25 13:09:14 +00:00
|
|
|
bool loading() const {
|
|
|
|
return amLoading();
|
|
|
|
}
|
2015-12-24 19:26:28 +00:00
|
|
|
bool displayLoading() const;
|
|
|
|
void cancel();
|
|
|
|
float64 progress() const;
|
|
|
|
int32 loadOffset() const;
|
|
|
|
|
2014-12-22 23:11:37 +00:00
|
|
|
void setData(QByteArray &bytes, const QByteArray &format = QByteArray());
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-12-24 19:26:28 +00:00
|
|
|
void load(bool loadFirst = false, bool prior = true);
|
|
|
|
void loadEvenCancelled(bool loadFirst = false, bool prior = true);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-12-30 19:09:20 +00:00
|
|
|
~RemoteImage();
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
protected:
|
2016-04-10 14:53:01 +00:00
|
|
|
// If after loading the image we need to shrink it to fit into a
|
|
|
|
// specific size, you can return this size here.
|
|
|
|
virtual QSize shrinkBox() const {
|
|
|
|
return QSize();
|
|
|
|
}
|
|
|
|
virtual void setInformation(int32 size, int32 width, int32 height) = 0;
|
|
|
|
virtual FileLoader *createLoader(LoadFromCloudSetting fromCloud, bool autoLoading) = 0;
|
|
|
|
|
2015-12-25 13:09:14 +00:00
|
|
|
void checkload() const {
|
|
|
|
doCheckload();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2015-12-31 15:27:21 +00:00
|
|
|
void loadLocal();
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
private:
|
2015-12-30 19:09:20 +00:00
|
|
|
mutable FileLoader *_loader;
|
2015-12-24 19:26:28 +00:00
|
|
|
bool amLoading() const {
|
|
|
|
return _loader && _loader != CancelledFileLoader;
|
|
|
|
}
|
2015-12-25 13:09:14 +00:00
|
|
|
void doCheckload() const;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-12-30 19:09:20 +00:00
|
|
|
class StorageImage : public RemoteImage {
|
|
|
|
public:
|
|
|
|
|
|
|
|
StorageImage(const StorageImageLocation &location, int32 size = 0);
|
|
|
|
StorageImage(const StorageImageLocation &location, QByteArray &bytes);
|
2015-12-31 15:27:21 +00:00
|
|
|
|
2016-04-10 11:13:37 +00:00
|
|
|
const StorageImageLocation &location() const override {
|
2015-12-30 19:09:20 +00:00
|
|
|
return _location;
|
|
|
|
}
|
|
|
|
|
2015-12-31 15:27:21 +00:00
|
|
|
protected:
|
2016-04-10 14:53:01 +00:00
|
|
|
void setInformation(int32 size, int32 width, int32 height) override;
|
|
|
|
FileLoader *createLoader(LoadFromCloudSetting fromCloud, bool autoLoading) override;
|
|
|
|
|
2015-12-30 19:09:20 +00:00
|
|
|
StorageImageLocation _location;
|
|
|
|
int32 _size;
|
|
|
|
|
2016-04-10 11:13:37 +00:00
|
|
|
int32 countWidth() const override;
|
|
|
|
int32 countHeight() const override;
|
2016-01-30 16:31:10 +00:00
|
|
|
|
2015-12-30 19:09:20 +00:00
|
|
|
};
|
|
|
|
|
2015-12-31 15:27:21 +00:00
|
|
|
class DelayedStorageImage : public StorageImage {
|
|
|
|
public:
|
|
|
|
|
|
|
|
DelayedStorageImage();
|
|
|
|
DelayedStorageImage(int32 w, int32 h);
|
|
|
|
DelayedStorageImage(QByteArray &bytes);
|
|
|
|
|
|
|
|
void setStorageLocation(const StorageImageLocation location);
|
|
|
|
|
|
|
|
virtual DelayedStorageImage *toDelayedStorageImage() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
virtual const DelayedStorageImage *toDelayedStorageImage() const {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void automaticLoad(const HistoryItem *item); // auto load photo
|
|
|
|
void automaticLoadSettingsChanged();
|
|
|
|
|
|
|
|
bool loading() const {
|
|
|
|
return _location.isNull() ? _loadRequested : StorageImage::loading();
|
|
|
|
}
|
|
|
|
bool displayLoading() const;
|
|
|
|
void cancel();
|
|
|
|
|
|
|
|
void load(bool loadFirst = false, bool prior = true);
|
|
|
|
void loadEvenCancelled(bool loadFirst = false, bool prior = true);
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool _loadRequested, _loadCancelled, _loadFromCloud;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-12-30 19:09:20 +00:00
|
|
|
class WebImage : public RemoteImage {
|
|
|
|
public:
|
|
|
|
|
2016-04-10 11:13:37 +00:00
|
|
|
// If !box.isEmpty() then resize the image to fit in this box.
|
|
|
|
WebImage(const QString &url, QSize box = QSize());
|
2016-04-10 18:18:26 +00:00
|
|
|
WebImage(const QString &url, int width, int height);
|
|
|
|
|
|
|
|
void setSize(int width, int height);
|
2015-12-30 19:09:20 +00:00
|
|
|
|
2016-04-10 14:53:01 +00:00
|
|
|
protected:
|
|
|
|
|
|
|
|
QSize shrinkBox() const override {
|
|
|
|
return _box;
|
|
|
|
}
|
2016-04-10 11:13:37 +00:00
|
|
|
void setInformation(int32 size, int32 width, int32 height) override;
|
|
|
|
FileLoader *createLoader(LoadFromCloudSetting fromCloud, bool autoLoading) override;
|
2015-12-30 19:09:20 +00:00
|
|
|
|
2016-04-10 11:13:37 +00:00
|
|
|
int32 countWidth() const override;
|
|
|
|
int32 countHeight() const override;
|
2016-01-30 16:31:10 +00:00
|
|
|
|
2015-12-30 19:09:20 +00:00
|
|
|
private:
|
|
|
|
QString _url;
|
2016-04-10 11:13:37 +00:00
|
|
|
QSize _box;
|
2015-12-30 19:09:20 +00:00
|
|
|
int32 _size, _width, _height;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-04-10 11:13:37 +00:00
|
|
|
namespace internal {
|
|
|
|
Image *getImage(const QString &file, QByteArray format);
|
|
|
|
Image *getImage(const QString &url, QSize box);
|
2016-04-10 18:18:26 +00:00
|
|
|
Image *getImage(const QString &url, int width, int height);
|
2016-04-10 11:13:37 +00:00
|
|
|
Image *getImage(const QByteArray &filecontent, QByteArray format);
|
|
|
|
Image *getImage(const QPixmap &pixmap, QByteArray format);
|
|
|
|
Image *getImage(const QByteArray &filecontent, QByteArray format, const QPixmap &pixmap);
|
|
|
|
Image *getImage(int32 width, int32 height);
|
|
|
|
StorageImage *getImage(const StorageImageLocation &location, int32 size = 0);
|
|
|
|
StorageImage *getImage(const StorageImageLocation &location, const QByteArray &bytes);
|
|
|
|
} // namespace internal
|
2015-12-30 19:09:20 +00:00
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
class ImagePtr : public ManagedPtr<Image> {
|
|
|
|
public:
|
|
|
|
ImagePtr();
|
2016-04-10 11:13:37 +00:00
|
|
|
ImagePtr(const QString &file, QByteArray format = QByteArray()) : Parent(internal::getImage(file, format)) {
|
|
|
|
}
|
|
|
|
ImagePtr(const QString &url, QSize box) : Parent(internal::getImage(url, box)) {
|
2014-12-22 23:11:37 +00:00
|
|
|
}
|
2016-04-10 18:18:26 +00:00
|
|
|
ImagePtr(const QString &url, int width, int height) : Parent(internal::getImage(url, width, height)) {
|
|
|
|
}
|
2016-04-10 11:13:37 +00:00
|
|
|
ImagePtr(const QByteArray &filecontent, QByteArray format = QByteArray()) : Parent(internal::getImage(filecontent, format)) {
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-04-10 11:13:37 +00:00
|
|
|
ImagePtr(const QByteArray &filecontent, QByteArray format, const QPixmap &pixmap) : Parent(internal::getImage(filecontent, format, pixmap)) {
|
2015-10-01 14:05:05 +00:00
|
|
|
}
|
2016-04-10 11:13:37 +00:00
|
|
|
ImagePtr(const QPixmap &pixmap, QByteArray format) : Parent(internal::getImage(pixmap, format)) {
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-04-10 11:13:37 +00:00
|
|
|
ImagePtr(const StorageImageLocation &location, int32 size = 0) : Parent(internal::getImage(location, size)) {
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-04-10 11:13:37 +00:00
|
|
|
ImagePtr(const StorageImageLocation &location, const QByteArray &bytes) : Parent(internal::getImage(location, bytes)) {
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
ImagePtr(int32 width, int32 height, const MTPFileLocation &location, ImagePtr def = ImagePtr());
|
2016-04-10 11:13:37 +00:00
|
|
|
ImagePtr(int32 width, int32 height) : Parent(internal::getImage(width, height)) {
|
2015-12-31 15:27:21 +00:00
|
|
|
}
|
2016-12-29 09:03:51 +00:00
|
|
|
|
|
|
|
explicit operator bool() const {
|
|
|
|
return (_data != nullptr) && !_data->isNull();
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
};
|
|
|
|
|
2016-04-10 11:13:37 +00:00
|
|
|
inline QSize shrinkToKeepAspect(int32 width, int32 height, int32 towidth, int32 toheight) {
|
2015-12-31 15:27:21 +00:00
|
|
|
int32 w = qMax(width, 1), h = qMax(height, 1);
|
|
|
|
if (w * toheight > h * towidth) {
|
|
|
|
h = qRound(h * towidth / float64(w));
|
|
|
|
w = towidth;
|
|
|
|
} else {
|
|
|
|
w = qRound(w * toheight / float64(h));
|
|
|
|
h = toheight;
|
|
|
|
}
|
|
|
|
return QSize(qMax(w, 1), qMax(h, 1));
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
void clearStorageImages();
|
|
|
|
void clearAllImages();
|
|
|
|
int64 imageCacheSize();
|
2014-12-05 13:44:27 +00:00
|
|
|
|
2015-11-26 17:34:52 +00:00
|
|
|
class PsFileBookmark;
|
|
|
|
class ReadAccessEnabler {
|
|
|
|
public:
|
|
|
|
ReadAccessEnabler(const PsFileBookmark *bookmark);
|
|
|
|
ReadAccessEnabler(const QSharedPointer<PsFileBookmark> &bookmark);
|
|
|
|
bool failed() const {
|
|
|
|
return _failed;
|
2014-12-05 13:44:27 +00:00
|
|
|
}
|
2015-11-26 17:34:52 +00:00
|
|
|
~ReadAccessEnabler();
|
|
|
|
|
|
|
|
private:
|
|
|
|
const PsFileBookmark *_bookmark;
|
|
|
|
bool _failed;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class FileLocation {
|
|
|
|
public:
|
|
|
|
FileLocation(StorageFileType type, const QString &name);
|
2014-12-05 13:44:27 +00:00
|
|
|
FileLocation() : size(0) {
|
|
|
|
}
|
|
|
|
|
2015-11-26 17:34:52 +00:00
|
|
|
bool check() const;
|
|
|
|
const QString &name() const;
|
|
|
|
void setBookmark(const QByteArray &bookmark);
|
|
|
|
QByteArray bookmark() const;
|
|
|
|
bool isEmpty() const {
|
|
|
|
return name().isEmpty();
|
2014-12-05 13:44:27 +00:00
|
|
|
}
|
2015-11-26 17:34:52 +00:00
|
|
|
|
|
|
|
bool accessEnable() const;
|
|
|
|
void accessDisable() const;
|
|
|
|
|
2015-05-19 15:46:45 +00:00
|
|
|
StorageFileType type;
|
2015-11-26 17:34:52 +00:00
|
|
|
QString fname;
|
2014-12-05 13:44:27 +00:00
|
|
|
QDateTime modified;
|
|
|
|
qint32 size;
|
2015-11-26 17:34:52 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
QSharedPointer<PsFileBookmark> _bookmark;
|
|
|
|
|
2014-12-05 13:44:27 +00:00
|
|
|
};
|
|
|
|
inline bool operator==(const FileLocation &a, const FileLocation &b) {
|
2015-11-26 17:34:52 +00:00
|
|
|
return a.type == b.type && a.name() == b.name() && a.modified == b.modified && a.size == b.size;
|
2014-12-05 13:44:27 +00:00
|
|
|
}
|
|
|
|
inline bool operator!=(const FileLocation &a, const FileLocation &b) {
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef QPair<uint64, uint64> MediaKey;
|
2015-05-19 15:46:45 +00:00
|
|
|
inline uint64 mediaMix32To64(int32 a, int32 b) {
|
2014-12-05 13:44:27 +00:00
|
|
|
return (uint64(*reinterpret_cast<uint32*>(&a)) << 32) | uint64(*reinterpret_cast<uint32*>(&b));
|
|
|
|
}
|
2016-07-08 13:56:53 +00:00
|
|
|
// Old method, should not be used anymore.
|
|
|
|
//inline MediaKey mediaKey(LocationType type, int32 dc, const uint64 &id) {
|
|
|
|
// return MediaKey(mediaMix32To64(type, dc), id);
|
|
|
|
//}
|
|
|
|
// New method when version was introduced, type is not relevant anymore (all files are Documents).
|
|
|
|
inline MediaKey mediaKey(LocationType type, int32 dc, const uint64 &id, int32 version) {
|
|
|
|
return (version > 0) ? MediaKey(mediaMix32To64(version, dc), id) : MediaKey(mediaMix32To64(type, dc), id);
|
2014-12-05 13:44:27 +00:00
|
|
|
}
|
|
|
|
inline StorageKey mediaKey(const MTPDfileLocation &location) {
|
|
|
|
return storageKey(location.vdc_id.v, location.vvolume_id.v, location.vlocal_id.v);
|
|
|
|
}
|