2014-05-30 12:53:19 +04:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2014-12-01 13:47:38 +03:00
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
2014-05-30 12:53:19 +04: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 16:16:42 +03: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 12:53:19 +04:00
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2015-10-03 16:16:42 +03:00
|
|
|
Copyright (c) 2014-2015 John Preston, https://desktop.telegram.org
|
2014-05-30 12:53:19 +04:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QtGui/QPixmap>
|
|
|
|
|
2014-09-21 20:52:37 -07:00
|
|
|
QImage imageBlur(QImage img);
|
2015-05-20 22:50:05 +03:00
|
|
|
void imageRound(QImage &img);
|
2014-09-21 20:52:37 -07:00
|
|
|
|
2015-05-19 18:46:45 +03:00
|
|
|
struct StorageImageLocation {
|
|
|
|
StorageImageLocation() : width(0), height(0), dc(0), volume(0), local(0), secret(0) {
|
|
|
|
}
|
|
|
|
StorageImageLocation(int32 width, int32 height, int32 dc, const uint64 &volume, int32 local, const uint64 &secret) : width(width), height(height), dc(dc), volume(volume), local(local), secret(secret) {
|
|
|
|
}
|
|
|
|
StorageImageLocation(int32 width, int32 height, const MTPDfileLocation &location) : width(width), height(height), dc(location.vdc_id.v), volume(location.vvolume_id.v), local(location.vlocal_id.v), secret(location.vsecret.v) {
|
|
|
|
}
|
2015-08-07 15:11:50 +03:00
|
|
|
bool isNull() const {
|
|
|
|
return !dc;
|
|
|
|
}
|
2015-05-19 18:46:45 +03:00
|
|
|
int32 width, height;
|
|
|
|
int32 dc;
|
|
|
|
uint64 volume;
|
|
|
|
int32 local;
|
|
|
|
uint64 secret;
|
|
|
|
};
|
|
|
|
|
2015-08-07 15:11:50 +03:00
|
|
|
inline bool operator==(const StorageImageLocation &a, const StorageImageLocation &b) {
|
2015-10-30 19:35:44 -04:00
|
|
|
return (a.width == b.width) && (a.height == b.height) && (a.dc == b.dc) && (a.volume == b.volume) && (a.local == b.local) && (a.secret == b.secret);
|
2015-08-07 15:11:50 +03:00
|
|
|
}
|
|
|
|
inline bool operator!=(const StorageImageLocation &a, const StorageImageLocation &b) {
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
2015-12-19 17:37:28 +03:00
|
|
|
QPixmap imagePix(QImage img, int32 w, int32 h, bool smooth, bool blurred, bool rounded, int32 outerw, int32 outerh);
|
|
|
|
|
2014-05-30 12:53:19 +04:00
|
|
|
class Image {
|
|
|
|
public:
|
|
|
|
|
2014-06-14 23:32:11 +04:00
|
|
|
Image(QByteArray format = "PNG") : format(format), forgot(false) {
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
virtual bool loaded() const {
|
|
|
|
return true;
|
|
|
|
}
|
2014-09-30 07:11:09 -07:00
|
|
|
virtual bool loading() const {
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-30 12:53:19 +04:00
|
|
|
const QPixmap &pix(int32 w = 0, int32 h = 0) const;
|
2015-05-20 22:28:24 +03:00
|
|
|
const QPixmap &pixRounded(int32 w = 0, int32 h = 0) const;
|
2014-08-11 13:03:45 +04:00
|
|
|
const QPixmap &pixBlurred(int32 w = 0, int32 h = 0) const;
|
2015-01-05 23:17:33 +03:00
|
|
|
const QPixmap &pixColored(const style::color &add, int32 w = 0, int32 h = 0) const;
|
|
|
|
const QPixmap &pixBlurredColored(const style::color &add, int32 w = 0, int32 h = 0) const;
|
2015-12-13 18:21:20 +03:00
|
|
|
const QPixmap &pixSingle(int32 w, int32 h, int32 outerw, int32 outerh) const;
|
2015-05-20 22:28:24 +03:00
|
|
|
const QPixmap &pixBlurredSingle(int32 w, int32 h, int32 outerw, int32 outerh) const;
|
|
|
|
QPixmap pixNoCache(int32 w = 0, int32 h = 0, bool smooth = false, bool blurred = false, bool rounded = false, int32 outerw = -1, int32 outerh = -1) const;
|
2015-01-05 23:17:33 +03:00
|
|
|
QPixmap pixColoredNoCache(const style::color &add, int32 w = 0, int32 h = 0, bool smooth = false) const;
|
|
|
|
QPixmap pixBlurredColoredNoCache(const style::color &add, int32 w, int32 h = 0) const;
|
2014-05-30 12:53:19 +04:00
|
|
|
|
|
|
|
virtual int32 width() const = 0;
|
|
|
|
virtual int32 height() const = 0;
|
|
|
|
|
2014-06-14 23:32:11 +04:00
|
|
|
virtual void load(bool /*loadFirst*/ = false, bool /*prior*/ = true) {
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void checkload() const {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isNull() const;
|
|
|
|
|
|
|
|
void forget() const;
|
|
|
|
void restore() const;
|
|
|
|
|
2014-11-22 12:45:04 +03:00
|
|
|
QByteArray savedFormat() const {
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
QByteArray savedData() const {
|
|
|
|
return saved;
|
|
|
|
}
|
|
|
|
|
2014-05-30 12:53:19 +04:00
|
|
|
virtual ~Image() {
|
|
|
|
invalidateSizeCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual const QPixmap &pixData() const = 0;
|
|
|
|
virtual void doForget() const = 0;
|
|
|
|
virtual void doRestore() const = 0;
|
|
|
|
|
|
|
|
void invalidateSizeCache() const;
|
|
|
|
|
|
|
|
mutable QByteArray saved, format;
|
|
|
|
mutable bool forgot;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
typedef QMap<uint64, QPixmap> Sizes;
|
|
|
|
mutable Sizes _sizesCache;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class LocalImage : public Image {
|
|
|
|
public:
|
|
|
|
|
2014-12-23 02:11:37 +03:00
|
|
|
LocalImage(const QString &file, QByteArray format = QByteArray());
|
|
|
|
LocalImage(const QByteArray &filecontent, QByteArray format = QByteArray());
|
|
|
|
LocalImage(const QPixmap &pixmap, QByteArray format = QByteArray());
|
2015-10-01 17:05:05 +03:00
|
|
|
LocalImage(const QByteArray &filecontent, QByteArray format, const QPixmap &pixmap);
|
|
|
|
|
2014-05-30 12:53:19 +04:00
|
|
|
int32 width() const;
|
|
|
|
int32 height() const;
|
|
|
|
|
|
|
|
~LocalImage();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
const QPixmap &pixData() const;
|
|
|
|
void doForget() const {
|
|
|
|
data = QPixmap();
|
|
|
|
}
|
|
|
|
void doRestore() const {
|
|
|
|
QBuffer buffer(&saved);
|
|
|
|
QImageReader reader(&buffer, format);
|
|
|
|
data = QPixmap::fromImageReader(&reader, Qt::ColorOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
mutable QPixmap data;
|
|
|
|
};
|
|
|
|
|
2014-12-23 02:11:37 +03:00
|
|
|
LocalImage *getImage(const QString &file, QByteArray format);
|
|
|
|
LocalImage *getImage(const QByteArray &filecontent, QByteArray format);
|
2014-05-30 12:53:19 +04:00
|
|
|
LocalImage *getImage(const QPixmap &pixmap, QByteArray format);
|
2015-10-01 17:05:05 +03:00
|
|
|
LocalImage *getImage(const QByteArray &filecontent, QByteArray format, const QPixmap &pixmap);
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2014-11-22 12:45:04 +03: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 18:46:45 +03:00
|
|
|
inline StorageKey storageKey(int32 dc, const uint64 &volume, int32 local) {
|
2014-11-22 12:45:04 +03: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 22:50:48 +03:00
|
|
|
inline StorageKey storageKey(const StorageImageLocation &location) {
|
|
|
|
return storageKey(location.dc, location.volume, location.local);
|
|
|
|
}
|
|
|
|
|
2014-05-30 12:53:19 +04:00
|
|
|
class StorageImage : public Image {
|
|
|
|
public:
|
|
|
|
|
2015-05-19 18:46:45 +03:00
|
|
|
StorageImage(const StorageImageLocation &location, int32 size = 0);
|
|
|
|
StorageImage(const StorageImageLocation &location, QByteArray &bytes);
|
2014-05-30 12:53:19 +04:00
|
|
|
|
|
|
|
int32 width() const;
|
|
|
|
int32 height() const;
|
|
|
|
bool loaded() const;
|
2014-09-30 07:11:09 -07:00
|
|
|
bool loading() const {
|
|
|
|
return loader ? loader->loading() : false;
|
|
|
|
}
|
2014-12-23 02:11:37 +03:00
|
|
|
void setData(QByteArray &bytes, const QByteArray &format = QByteArray());
|
2014-05-30 12:53:19 +04:00
|
|
|
|
|
|
|
void load(bool loadFirst = false, bool prior = true) {
|
|
|
|
if (loader) {
|
|
|
|
loader->start(loadFirst, prior);
|
2014-11-22 12:45:04 +03:00
|
|
|
if (loader) check();
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void checkload() const {
|
|
|
|
if (loader) {
|
|
|
|
if (!loader->loading()) {
|
|
|
|
loader->start(true);
|
|
|
|
}
|
2014-11-22 12:45:04 +03:00
|
|
|
if (loader) check();
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~StorageImage();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
const QPixmap &pixData() const;
|
|
|
|
bool check() const;
|
|
|
|
void doForget() const {
|
|
|
|
data = QPixmap();
|
|
|
|
}
|
|
|
|
void doRestore() const {
|
|
|
|
QBuffer buffer(&saved);
|
|
|
|
QImageReader reader(&buffer, format);
|
|
|
|
data = QPixmap::fromImageReader(&reader, Qt::ColorOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
mutable QPixmap data;
|
|
|
|
mutable int32 w, h;
|
|
|
|
mutable mtpFileLoader *loader;
|
|
|
|
};
|
|
|
|
|
2015-05-19 18:46:45 +03:00
|
|
|
StorageImage *getImage(const StorageImageLocation &location, int32 size = 0);
|
|
|
|
StorageImage *getImage(const StorageImageLocation &location, const QByteArray &bytes);
|
2014-05-30 12:53:19 +04:00
|
|
|
Image *getImage(int32 width, int32 height, const MTPFileLocation &location);
|
|
|
|
|
|
|
|
class ImagePtr : public ManagedPtr<Image> {
|
|
|
|
public:
|
|
|
|
ImagePtr();
|
2014-12-23 02:11:37 +03:00
|
|
|
ImagePtr(const QString &file, QByteArray format = QByteArray()) : Parent(getImage(file, format)) {
|
|
|
|
}
|
|
|
|
ImagePtr(const QByteArray &filecontent, QByteArray format = QByteArray()) : Parent(getImage(filecontent, format)) {
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
2015-10-01 17:05:05 +03:00
|
|
|
ImagePtr(const QByteArray &filecontent, QByteArray format, const QPixmap &pixmap) : Parent(getImage(filecontent, format, pixmap)) {
|
|
|
|
}
|
2014-05-30 12:53:19 +04:00
|
|
|
ImagePtr(const QPixmap &pixmap, QByteArray format) : Parent(getImage(pixmap, format)) {
|
|
|
|
}
|
2015-05-19 18:46:45 +03:00
|
|
|
ImagePtr(const StorageImageLocation &location, int32 size = 0) : Parent(getImage(location, size)) {
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
2015-05-19 18:46:45 +03:00
|
|
|
ImagePtr(const StorageImageLocation &location, const QByteArray &bytes) : Parent(getImage(location, bytes)) {
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
ImagePtr(int32 width, int32 height, const MTPFileLocation &location, ImagePtr def = ImagePtr());
|
|
|
|
};
|
|
|
|
|
|
|
|
void clearStorageImages();
|
|
|
|
void clearAllImages();
|
|
|
|
int64 imageCacheSize();
|
2014-12-05 16:44:27 +03:00
|
|
|
|
2015-11-26 20:34:52 +03:00
|
|
|
class PsFileBookmark;
|
|
|
|
class ReadAccessEnabler {
|
|
|
|
public:
|
|
|
|
ReadAccessEnabler(const PsFileBookmark *bookmark);
|
|
|
|
ReadAccessEnabler(const QSharedPointer<PsFileBookmark> &bookmark);
|
|
|
|
bool failed() const {
|
|
|
|
return _failed;
|
2014-12-05 16:44:27 +03:00
|
|
|
}
|
2015-11-26 20:34:52 +03:00
|
|
|
~ReadAccessEnabler();
|
|
|
|
|
|
|
|
private:
|
|
|
|
const PsFileBookmark *_bookmark;
|
|
|
|
bool _failed;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class FileLocation {
|
|
|
|
public:
|
|
|
|
FileLocation(StorageFileType type, const QString &name);
|
2014-12-05 16:44:27 +03:00
|
|
|
FileLocation() : size(0) {
|
|
|
|
}
|
|
|
|
|
2015-11-26 20:34:52 +03: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 16:44:27 +03:00
|
|
|
}
|
2015-11-26 20:34:52 +03:00
|
|
|
|
|
|
|
bool accessEnable() const;
|
|
|
|
void accessDisable() const;
|
|
|
|
|
2015-05-19 18:46:45 +03:00
|
|
|
StorageFileType type;
|
2015-11-26 20:34:52 +03:00
|
|
|
QString fname;
|
2014-12-05 16:44:27 +03:00
|
|
|
QDateTime modified;
|
|
|
|
qint32 size;
|
2015-11-26 20:34:52 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
QSharedPointer<PsFileBookmark> _bookmark;
|
|
|
|
|
2014-12-05 16:44:27 +03:00
|
|
|
};
|
|
|
|
inline bool operator==(const FileLocation &a, const FileLocation &b) {
|
2015-11-26 20:34:52 +03:00
|
|
|
return a.type == b.type && a.name() == b.name() && a.modified == b.modified && a.size == b.size;
|
2014-12-05 16:44:27 +03:00
|
|
|
}
|
|
|
|
inline bool operator!=(const FileLocation &a, const FileLocation &b) {
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef QPair<uint64, uint64> MediaKey;
|
2015-05-19 18:46:45 +03:00
|
|
|
inline uint64 mediaMix32To64(int32 a, int32 b) {
|
2014-12-05 16:44:27 +03:00
|
|
|
return (uint64(*reinterpret_cast<uint32*>(&a)) << 32) | uint64(*reinterpret_cast<uint32*>(&b));
|
|
|
|
}
|
2015-05-19 18:46:45 +03:00
|
|
|
inline MediaKey mediaKey(LocationType type, int32 dc, const uint64 &id) {
|
2014-12-05 16:44:27 +03:00
|
|
|
return MediaKey(mediaMix32To64(type, dc), id);
|
|
|
|
}
|
|
|
|
inline StorageKey mediaKey(const MTPDfileLocation &location) {
|
|
|
|
return storageKey(location.vdc_id.v, location.vvolume_id.v, location.vlocal_id.v);
|
|
|
|
}
|