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
|
2016-02-08 10:56:18 +00:00
|
|
|
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2016-11-28 15:45:07 +00:00
|
|
|
enum class CompressConfirm {
|
|
|
|
Auto,
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
None,
|
2014-05-30 08:53:19 +00:00
|
|
|
};
|
|
|
|
|
2016-11-28 15:45:07 +00:00
|
|
|
enum class SendMediaType {
|
|
|
|
Photo,
|
|
|
|
Audio,
|
|
|
|
File,
|
|
|
|
};
|
|
|
|
|
|
|
|
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) {
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-11-28 15:45:07 +00:00
|
|
|
SendMediaPrepare(const QImage &img, const PeerId &peer, SendMediaType type, MsgId replyTo) : id(rand_value<PhotoId>()), img(img), peer(peer), type(type), replyTo(replyTo) {
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-11-28 15:45:07 +00:00
|
|
|
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
|
|
|
}
|
2016-11-28 15:45:07 +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) {
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
PhotoId id;
|
|
|
|
QString file;
|
|
|
|
QImage img;
|
|
|
|
QByteArray data;
|
|
|
|
PeerId peer;
|
2016-11-28 15:45:07 +00:00
|
|
|
SendMediaType type;
|
|
|
|
int duration = 0;
|
2015-03-19 09:18:19 +00:00
|
|
|
MsgId replyTo;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-28 15:45:07 +00:00
|
|
|
};
|
|
|
|
using SendMediaPrepareList = QList<SendMediaPrepare>;
|
|
|
|
|
|
|
|
using UploadFileParts = QMap<int, QByteArray>;
|
|
|
|
struct SendMediaReady {
|
|
|
|
SendMediaReady() = default; // temp
|
|
|
|
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)
|
|
|
|
: replyTo(replyTo)
|
|
|
|
, type(type)
|
|
|
|
, file(file)
|
|
|
|
, filename(filename)
|
|
|
|
, filesize(filesize)
|
|
|
|
, data(data)
|
|
|
|
, thumbExt(thumbExt)
|
|
|
|
, id(id)
|
|
|
|
, thumbId(thumbId)
|
|
|
|
, peer(peer)
|
|
|
|
, photo(photo)
|
|
|
|
, document(document)
|
|
|
|
, photoThumbs(photoThumbs) {
|
2014-05-30 08:53:19 +00:00
|
|
|
if (!jpeg.isEmpty()) {
|
|
|
|
int32 size = jpeg.size();
|
|
|
|
for (int32 i = 0, part = 0; i < size; i += UploadPartSize, ++part) {
|
|
|
|
parts.insert(part, jpeg.mid(i, UploadPartSize));
|
|
|
|
}
|
|
|
|
jpeg_md5.resize(32);
|
|
|
|
hashMd5Hex(jpeg.constData(), jpeg.size(), jpeg_md5.data());
|
|
|
|
}
|
|
|
|
}
|
2015-03-19 09:18:19 +00:00
|
|
|
MsgId replyTo;
|
2016-11-28 15:45:07 +00:00
|
|
|
SendMediaType type;
|
2014-05-30 08:53:19 +00:00
|
|
|
QString file, filename;
|
|
|
|
int32 filesize;
|
|
|
|
QByteArray data;
|
2015-01-05 20:17:33 +00:00
|
|
|
QString thumbExt;
|
|
|
|
uint64 id, thumbId; // id always file-id of media, thumbId is file-id of thumb ( == id for photos)
|
2014-05-30 08:53:19 +00:00
|
|
|
PeerId peer;
|
|
|
|
|
|
|
|
MTPPhoto photo;
|
|
|
|
MTPDocument document;
|
|
|
|
PreparedPhotoThumbs photoThumbs;
|
2015-10-27 02:39:02 +00:00
|
|
|
UploadFileParts parts;
|
2014-05-30 08:53:19 +00:00
|
|
|
QByteArray jpeg_md5;
|
2014-10-17 19:14:42 +00:00
|
|
|
|
2015-10-11 08:37:24 +00:00
|
|
|
QString caption;
|
2015-10-27 02:39:02 +00:00
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
};
|
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
|
2016-11-28 15:45:07 +00:00
|
|
|
virtual ~Task() = default;
|
2015-09-29 18:59:26 +00:00
|
|
|
|
2015-09-29 18:44:31 +00:00
|
|
|
TaskId id() const {
|
2016-11-28 15:45:07 +00:00
|
|
|
return static_cast<TaskId>(const_cast<Task*>(this));
|
2015-09-29 18:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2016-11-28 15:45:07 +00:00
|
|
|
using TaskPtr = QSharedPointer<Task>;
|
|
|
|
using TasksList = QList<TaskPtr>;
|
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:
|
|
|
|
TaskQueue(QObject *parent, int32 stopTimeoutMs = 0); // <= 0 - never stop worker
|
|
|
|
|
|
|
|
TaskId addTask(TaskPtr task);
|
2015-10-28 02:41:13 +00:00
|
|
|
void addTasks(const TasksList &tasks);
|
2015-09-29 18:44:31 +00:00
|
|
|
void cancelTask(TaskId id); // this task finish() won't be called
|
2016-02-12 16:35:06 +00:00
|
|
|
|
2015-09-29 18:44:31 +00:00
|
|
|
~TaskQueue();
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void taskAdded();
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
void onTaskProcessed();
|
|
|
|
void stop();
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class TaskQueueWorker;
|
|
|
|
|
2015-10-28 02:41:13 +00:00
|
|
|
void wakeThread();
|
|
|
|
|
|
|
|
TasksList _tasksToProcess, _tasksToFinish;
|
2015-09-29 18:44:31 +00:00
|
|
|
QMutex _tasksToProcessMutex, _tasksToFinishMutex;
|
|
|
|
QThread *_thread;
|
|
|
|
TaskQueueWorker *_worker;
|
|
|
|
QTimer *_stopTimer;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class TaskQueueWorker : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2016-11-28 15:45:07 +00:00
|
|
|
TaskQueueWorker(TaskQueue *queue) : _queue(queue) {
|
2015-09-29 18:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void taskProcessed();
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
void onTaskAdded();
|
|
|
|
|
|
|
|
private:
|
|
|
|
TaskQueue *_queue;
|
2016-11-28 15:45:07 +00:00
|
|
|
bool _inTaskAdded = false;
|
2015-09-29 18:44:31 +00:00
|
|
|
|
|
|
|
};
|
2015-10-27 02:39:02 +00:00
|
|
|
|
|
|
|
struct FileLoadTo {
|
2016-05-27 16:47:46 +00:00
|
|
|
FileLoadTo(const PeerId &peer, bool silent, MsgId replyTo)
|
2016-02-25 16:19:54 +00:00
|
|
|
: peer(peer)
|
|
|
|
, silent(silent)
|
|
|
|
, replyTo(replyTo) {
|
2015-10-27 02:39:02 +00:00
|
|
|
}
|
|
|
|
PeerId peer;
|
2016-05-27 16:47:46 +00:00
|
|
|
bool silent;
|
2015-10-27 02:39:02 +00:00
|
|
|
MsgId replyTo;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FileLoadResult {
|
2016-11-28 15:45:07 +00:00
|
|
|
FileLoadResult(const uint64 &id, const FileLoadTo &to, const QString &caption)
|
|
|
|
: id(id)
|
2015-11-24 11:23:14 +00:00
|
|
|
, to(to)
|
2016-11-28 15:45:07 +00:00
|
|
|
, caption(caption) {
|
2015-10-27 02:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64 id;
|
|
|
|
FileLoadTo to;
|
2016-11-28 15:45:07 +00:00
|
|
|
SendMediaType type;
|
2015-10-27 02:39:02 +00:00
|
|
|
QString filepath;
|
|
|
|
QByteArray content;
|
|
|
|
|
|
|
|
QString filename;
|
2015-12-19 14:37:28 +00:00
|
|
|
QString filemime;
|
2016-11-28 15:45:07 +00:00
|
|
|
int32 filesize = 0;
|
2015-10-27 02:39:02 +00:00
|
|
|
UploadFileParts fileparts;
|
|
|
|
QByteArray filemd5;
|
2015-12-24 19:26:28 +00:00
|
|
|
int32 partssize;
|
2015-10-27 02:39:02 +00:00
|
|
|
|
2016-11-28 15:45:07 +00:00
|
|
|
uint64 thumbId = 0; // id is always file-id of media, thumbId is file-id of thumb ( == id for photos)
|
2015-10-27 02:39:02 +00:00
|
|
|
QString thumbname;
|
|
|
|
UploadFileParts thumbparts;
|
|
|
|
QByteArray thumbmd5;
|
|
|
|
QPixmap thumb;
|
|
|
|
|
|
|
|
MTPPhoto photo;
|
|
|
|
MTPDocument document;
|
|
|
|
|
|
|
|
PreparedPhotoThumbs photoThumbs;
|
2015-12-28 10:28:00 +00:00
|
|
|
QString caption;
|
2015-10-27 02:39:02 +00:00
|
|
|
|
|
|
|
void setFileData(const QByteArray &filedata) {
|
2015-12-24 19:26:28 +00:00
|
|
|
if (filedata.isEmpty()) {
|
|
|
|
partssize = 0;
|
|
|
|
} else {
|
|
|
|
partssize = filedata.size();
|
|
|
|
for (int32 i = 0, part = 0; i < partssize; i += UploadPartSize, ++part) {
|
2015-10-27 02:39:02 +00:00
|
|
|
fileparts.insert(part, filedata.mid(i, UploadPartSize));
|
|
|
|
}
|
|
|
|
filemd5.resize(32);
|
|
|
|
hashMd5Hex(filedata.constData(), filedata.size(), filemd5.data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void setThumbData(const QByteArray &thumbdata) {
|
|
|
|
if (!thumbdata.isEmpty()) {
|
|
|
|
int32 size = thumbdata.size();
|
|
|
|
for (int32 i = 0, part = 0; i < size; i += UploadPartSize, ++part) {
|
|
|
|
thumbparts.insert(part, thumbdata.mid(i, UploadPartSize));
|
|
|
|
}
|
|
|
|
thumbmd5.resize(32);
|
|
|
|
hashMd5Hex(thumbdata.constData(), thumbdata.size(), thumbmd5.data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
typedef QSharedPointer<FileLoadResult> FileLoadResultPtr;
|
|
|
|
|
|
|
|
class FileLoadTask : public Task {
|
|
|
|
public:
|
2016-11-28 15:45:07 +00:00
|
|
|
FileLoadTask(const QString &filepath, SendMediaType type, const FileLoadTo &to, const QString &caption);
|
|
|
|
FileLoadTask(const QByteArray &content, const QImage &image, SendMediaType type, const FileLoadTo &to, const QString &caption);
|
|
|
|
FileLoadTask(const QByteArray &voice, int32 duration, const VoiceWaveform &waveform, const FileLoadTo &to, const QString &caption);
|
2015-10-27 02:39:02 +00:00
|
|
|
|
|
|
|
uint64 fileid() const {
|
|
|
|
return _id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void process();
|
|
|
|
void finish();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
uint64 _id;
|
|
|
|
FileLoadTo _to;
|
|
|
|
QString _filepath;
|
|
|
|
QByteArray _content;
|
2016-11-28 15:45:07 +00:00
|
|
|
QImage _image;
|
2016-10-12 19:34:25 +00:00
|
|
|
int32 _duration = 0;
|
2016-02-12 16:35:06 +00:00
|
|
|
VoiceWaveform _waveform;
|
2016-11-28 15:45:07 +00:00
|
|
|
SendMediaType _type;
|
|
|
|
QString _caption;
|
2015-10-27 02:39:02 +00:00
|
|
|
|
|
|
|
FileLoadResultPtr _result;
|
|
|
|
|
|
|
|
};
|