tdesktop/Telegram/SourceFiles/storage/localimageloader.h

325 lines
7.3 KiB
C
Raw Normal View History

/*
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
2017-04-06 14:38:10 +00:00
#include "base/variant.h"
enum class CompressConfirm {
Auto,
Yes,
No,
None,
};
enum class SendMediaType {
Photo,
Audio,
File,
Secure,
};
struct SendMediaPrepare {
SendMediaPrepare(const QString &file, const PeerId &peer, SendMediaType type, MsgId replyTo) : id(rand_value<PhotoId>()), file(file), peer(peer), type(type), replyTo(replyTo) {
}
SendMediaPrepare(const QImage &img, const PeerId &peer, SendMediaType type, MsgId replyTo) : id(rand_value<PhotoId>()), img(img), peer(peer), type(type), replyTo(replyTo) {
}
SendMediaPrepare(const QByteArray &data, const PeerId &peer, SendMediaType type, MsgId replyTo) : id(rand_value<PhotoId>()), data(data), peer(peer), type(type), replyTo(replyTo) {
2015-05-29 18:52:43 +00:00
}
SendMediaPrepare(const QByteArray &data, int duration, const PeerId &peer, SendMediaType type, MsgId replyTo) : id(rand_value<PhotoId>()), data(data), peer(peer), type(type), duration(duration), replyTo(replyTo) {
}
PhotoId id;
QString file;
QImage img;
QByteArray data;
PeerId peer;
SendMediaType type;
int duration = 0;
MsgId replyTo;
};
using SendMediaPrepareList = QList<SendMediaPrepare>;
using UploadFileParts = QMap<int, QByteArray>;
struct SendMediaReady {
SendMediaReady() = default; // temp
2019-01-18 11:26:43 +00:00
SendMediaReady(
SendMediaType type,
const QString &file,
const QString &filename,
int32 filesize,
const QByteArray &data,
const uint64 &id,
const uint64 &thumbId,
const QString &thumbExt,
const PeerId &peer,
const MTPPhoto &photo,
const PreparedPhotoThumbs &photoThumbs,
const MTPDocument &document,
const QByteArray &jpeg,
MsgId replyTo);
MsgId replyTo;
SendMediaType type;
QString file, filename;
int32 filesize;
QByteArray data;
QString thumbExt;
uint64 id, thumbId; // id always file-id of media, thumbId is file-id of thumb ( == id for photos)
PeerId peer;
MTPPhoto photo;
MTPDocument document;
PreparedPhotoThumbs photoThumbs;
UploadFileParts parts;
QByteArray jpeg_md5;
2014-10-17 19:14:42 +00:00
2015-10-11 08:37:24 +00:00
QString caption;
};
2015-09-29 18:44:31 +00:00
SendMediaReady PreparePeerPhoto(PeerId peerId, QImage &&image);
using TaskId = void*; // no interface, just id
2015-09-29 18:44:31 +00:00
class Task {
public:
virtual void process() = 0; // is executed in a separate thread
virtual void finish() = 0; // is executed in the same as TaskQueue thread
virtual ~Task() = default;
2015-09-29 18:59:26 +00:00
2015-09-29 18:44:31 +00:00
TaskId id() const {
return static_cast<TaskId>(const_cast<Task*>(this));
2015-09-29 18:44:31 +00:00
}
};
2015-09-29 18:59:26 +00:00
class TaskQueueWorker;
2015-09-29 18:44:31 +00:00
class TaskQueue : public QObject {
Q_OBJECT
public:
2017-12-19 16:57:42 +00:00
explicit TaskQueue(TimeMs stopTimeoutMs = 0); // <= 0 - never stop worker
2015-09-29 18:44:31 +00:00
TaskId addTask(std::unique_ptr<Task> &&task);
void addTasks(std::vector<std::unique_ptr<Task>> &&tasks);
2015-09-29 18:44:31 +00:00
void cancelTask(TaskId id); // this task finish() won't be called
2015-09-29 18:44:31 +00:00
~TaskQueue();
signals:
void taskAdded();
public slots:
void onTaskProcessed();
void stop();
private:
friend class TaskQueueWorker;
void wakeThread();
std::deque<std::unique_ptr<Task>> _tasksToProcess;
std::deque<std::unique_ptr<Task>> _tasksToFinish;
TaskId _taskInProcessId = TaskId();
2015-09-29 18:44:31 +00:00
QMutex _tasksToProcessMutex, _tasksToFinishMutex;
2017-12-19 16:57:42 +00:00
QThread *_thread = nullptr;
TaskQueueWorker *_worker = nullptr;
QTimer *_stopTimer = nullptr;
2015-09-29 18:44:31 +00:00
};
class TaskQueueWorker : public QObject {
Q_OBJECT
public:
TaskQueueWorker(TaskQueue *queue) : _queue(queue) {
2015-09-29 18:44:31 +00:00
}
signals:
void taskProcessed();
public slots:
void onTaskAdded();
private:
TaskQueue *_queue;
bool _inTaskAdded = false;
2015-09-29 18:44:31 +00:00
};
2017-12-19 16:57:42 +00:00
struct SendingAlbum {
struct Item {
explicit Item(TaskId taskId) : taskId(taskId) {
}
2017-12-19 16:57:42 +00:00
TaskId taskId;
FullMsgId msgId;
2018-09-21 16:28:46 +00:00
std::optional<MTPInputSingleMedia> media;
2017-12-19 16:57:42 +00:00
};
SendingAlbum();
void fillMedia(
not_null<HistoryItem*> item,
const MTPInputMedia &media,
uint64 randomId);
void refreshMediaCaption(not_null<HistoryItem*> item);
void removeItem(not_null<HistoryItem*> item);
uint64 groupId = 0;
2017-12-19 16:57:42 +00:00
std::vector<Item> items;
bool silent = false;
2017-12-19 16:57:42 +00:00
};
struct FileLoadTo {
FileLoadTo(const PeerId &peer, bool silent, MsgId replyTo)
2016-02-25 16:19:54 +00:00
: peer(peer)
, silent(silent)
, replyTo(replyTo) {
}
PeerId peer;
bool silent;
MsgId replyTo;
};
struct FileLoadResult {
2017-12-19 16:57:42 +00:00
FileLoadResult(
TaskId taskId,
uint64 id,
const FileLoadTo &to,
const TextWithTags &caption,
2017-12-19 16:57:42 +00:00
std::shared_ptr<SendingAlbum> album);
TaskId taskId;
uint64 id;
FileLoadTo to;
2017-12-19 16:57:42 +00:00
std::shared_ptr<SendingAlbum> album;
SendMediaType type = SendMediaType::File;
QString filepath;
QByteArray content;
QString filename;
QString filemime;
int32 filesize = 0;
UploadFileParts fileparts;
QByteArray filemd5;
int32 partssize;
uint64 thumbId = 0; // id is always file-id of media, thumbId is file-id of thumb ( == id for photos)
QString thumbname;
UploadFileParts thumbparts;
QByteArray thumbmd5;
QImage thumb;
QImage goodThumbnail;
QByteArray goodThumbnailBytes;
MTPPhoto photo;
MTPDocument document;
PreparedPhotoThumbs photoThumbs;
TextWithTags caption;
2019-01-18 11:26:43 +00:00
void setFileData(const QByteArray &filedata);
void setThumbData(const QByteArray &thumbdata);
};
2017-12-19 16:57:42 +00:00
struct FileMediaInformation {
struct Image {
QImage data;
bool animated = false;
};
struct Song {
int duration = -1;
QString title;
QString performer;
QImage cover;
};
struct Video {
bool isGifv = false;
bool supportsStreaming = false;
int duration = -1;
QImage thumbnail;
};
2017-12-19 16:57:42 +00:00
QString filemime;
base::variant<Image, Song, Video> media;
};
class FileLoadTask final : public Task {
public:
static std::unique_ptr<FileMediaInformation> ReadMediaInformation(
const QString &filepath,
const QByteArray &content,
const QString &filemime);
static bool FillImageInformation(
QImage &&image,
bool animated,
std::unique_ptr<FileMediaInformation> &result);
2017-12-19 16:57:42 +00:00
FileLoadTask(
const QString &filepath,
const QByteArray &content,
std::unique_ptr<FileMediaInformation> information,
2017-12-19 16:57:42 +00:00
SendMediaType type,
const FileLoadTo &to,
const TextWithTags &caption,
2017-12-19 16:57:42 +00:00
std::shared_ptr<SendingAlbum> album = nullptr);
FileLoadTask(
const QByteArray &voice,
int32 duration,
const VoiceWaveform &waveform,
const FileLoadTo &to,
const TextWithTags &caption);
uint64 fileid() const {
return _id;
}
void process();
void finish();
private:
static bool CheckForSong(
const QString &filepath,
const QByteArray &content,
std::unique_ptr<FileMediaInformation> &result);
static bool CheckForVideo(
const QString &filepath,
const QByteArray &content,
std::unique_ptr<FileMediaInformation> &result);
static bool CheckForImage(
const QString &filepath,
const QByteArray &content,
std::unique_ptr<FileMediaInformation> &result);
template <typename Mimes, typename Extensions>
static bool CheckMimeOrExtensions(const QString &filepath, const QString &filemime, Mimes &mimes, Extensions &extensions);
2017-12-19 16:57:42 +00:00
std::unique_ptr<FileMediaInformation> readMediaInformation(const QString &filemime) const {
return ReadMediaInformation(_filepath, _content, filemime);
}
2017-12-19 16:57:42 +00:00
void removeFromAlbum();
uint64 _id;
FileLoadTo _to;
2017-12-19 16:57:42 +00:00
const std::shared_ptr<SendingAlbum> _album;
QString _filepath;
QByteArray _content;
2017-12-19 16:57:42 +00:00
std::unique_ptr<FileMediaInformation> _information;
int32 _duration = 0;
VoiceWaveform _waveform;
SendMediaType _type;
TextWithTags _caption;
2017-12-19 16:57:42 +00:00
std::shared_ptr<FileLoadResult> _result;
};