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 "core/observer.h"
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
namespace MTP {
|
|
|
|
void clearLoaderPriorities();
|
|
|
|
}
|
|
|
|
|
2015-09-29 15:29:21 +00:00
|
|
|
enum LocationType {
|
|
|
|
UnknownFileLocation = 0,
|
2016-07-08 13:56:53 +00:00
|
|
|
// 1, 2, etc are used as "version" value in mediaKey() method.
|
|
|
|
|
2015-09-29 15:29:21 +00:00
|
|
|
DocumentFileLocation = 0x4e45abe9, // mtpc_inputDocumentFileLocation
|
|
|
|
AudioFileLocation = 0x74dc404d, // mtpc_inputAudioFileLocation
|
|
|
|
VideoFileLocation = 0x3d0364ec, // mtpc_inputVideoFileLocation
|
|
|
|
};
|
|
|
|
|
2015-09-29 18:44:31 +00:00
|
|
|
enum StorageFileType {
|
|
|
|
StorageFileUnknown = 0xaa963b05, // mtpc_storage_fileUnknown
|
|
|
|
StorageFileJpeg = 0x7efe0e, // mtpc_storage_fileJpeg
|
|
|
|
StorageFileGif = 0xcae1aadf, // mtpc_storage_fileGif
|
|
|
|
StorageFilePng = 0xa4f63c0, // mtpc_storage_filePng
|
|
|
|
StorageFilePdf = 0xae1e508d, // mtpc_storage_filePdf
|
|
|
|
StorageFileMp3 = 0x528a0677, // mtpc_storage_fileMp3
|
|
|
|
StorageFileMov = 0x4b09ebbc, // mtpc_storage_fileMov
|
|
|
|
StorageFilePartial = 0x40bc6f52, // mtpc_storage_filePartial
|
|
|
|
StorageFileMp4 = 0xb3cea0e4, // mtpc_storage_fileMp4
|
|
|
|
StorageFileWebp = 0x1081464c, // mtpc_storage_fileWebp
|
|
|
|
};
|
|
|
|
inline StorageFileType mtpToStorageType(mtpTypeId type) {
|
|
|
|
switch (type) {
|
|
|
|
case mtpc_storage_fileJpeg: return StorageFileJpeg;
|
|
|
|
case mtpc_storage_fileGif: return StorageFileGif;
|
|
|
|
case mtpc_storage_filePng: return StorageFilePng;
|
|
|
|
case mtpc_storage_filePdf: return StorageFilePdf;
|
|
|
|
case mtpc_storage_fileMp3: return StorageFileMp3;
|
|
|
|
case mtpc_storage_fileMov: return StorageFileMov;
|
|
|
|
case mtpc_storage_filePartial: return StorageFilePartial;
|
|
|
|
case mtpc_storage_fileMp4: return StorageFileMp4;
|
|
|
|
case mtpc_storage_fileWebp: return StorageFileWebp;
|
|
|
|
case mtpc_storage_fileUnknown:
|
|
|
|
default: return StorageFileUnknown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inline mtpTypeId mtpFromStorageType(StorageFileType type) {
|
|
|
|
switch (type) {
|
2016-04-06 11:07:05 +00:00
|
|
|
case StorageFileJpeg: return mtpc_storage_fileJpeg;
|
2015-09-29 18:44:31 +00:00
|
|
|
case StorageFileGif: return mtpc_storage_fileGif;
|
|
|
|
case StorageFilePng: return mtpc_storage_filePng;
|
|
|
|
case StorageFilePdf: return mtpc_storage_filePdf;
|
|
|
|
case StorageFileMp3: return mtpc_storage_fileMp3;
|
|
|
|
case StorageFileMov: return mtpc_storage_fileMov;
|
|
|
|
case StorageFilePartial: return mtpc_storage_filePartial;
|
|
|
|
case StorageFileMp4: return mtpc_storage_fileMp4;
|
|
|
|
case StorageFileWebp: return mtpc_storage_fileWebp;
|
|
|
|
case StorageFileUnknown:
|
|
|
|
default: return mtpc_storage_fileUnknown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
struct StorageImageSaved {
|
|
|
|
StorageImageSaved() : type(StorageFileUnknown) {
|
|
|
|
}
|
|
|
|
StorageImageSaved(StorageFileType type, const QByteArray &data) : type(type), data(data) {
|
|
|
|
}
|
|
|
|
StorageFileType type;
|
|
|
|
QByteArray data;
|
|
|
|
};
|
|
|
|
|
2015-09-29 15:29:21 +00:00
|
|
|
enum LocalLoadStatus {
|
|
|
|
LocalNotTried,
|
|
|
|
LocalNotFound,
|
|
|
|
LocalLoading,
|
|
|
|
LocalLoaded,
|
|
|
|
LocalFailed,
|
|
|
|
};
|
|
|
|
|
2016-11-28 15:45:07 +00:00
|
|
|
using TaskId = void*; // no interface, just id
|
2015-09-29 18:44:31 +00:00
|
|
|
|
2015-12-24 19:26:28 +00:00
|
|
|
enum LoadFromCloudSetting {
|
|
|
|
LoadFromCloudOrLocal,
|
|
|
|
LoadFromLocalOnly,
|
|
|
|
};
|
|
|
|
enum LoadToCacheSetting {
|
|
|
|
LoadToFileOnly,
|
|
|
|
LoadToCacheAsWell,
|
|
|
|
};
|
|
|
|
|
2015-12-30 19:09:20 +00:00
|
|
|
class mtpFileLoader;
|
|
|
|
class webFileLoader;
|
|
|
|
|
|
|
|
struct FileLoaderQueue;
|
|
|
|
class FileLoader : public QObject {
|
2014-05-30 08:53:19 +00:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2015-12-31 05:34:43 +00:00
|
|
|
FileLoader(const QString &toFile, int32 size, LocationType locationType, LoadToCacheSetting, LoadFromCloudSetting fromCloud, bool autoLoading);
|
2015-09-29 18:44:31 +00:00
|
|
|
bool done() const {
|
2015-12-24 19:26:28 +00:00
|
|
|
return _complete;
|
2015-09-29 18:44:31 +00:00
|
|
|
}
|
|
|
|
mtpTypeId fileType() const {
|
2015-12-24 19:26:28 +00:00
|
|
|
return _type;
|
2015-09-29 18:44:31 +00:00
|
|
|
}
|
|
|
|
const QByteArray &bytes() const {
|
2015-12-24 19:26:28 +00:00
|
|
|
return _data;
|
2015-09-29 18:44:31 +00:00
|
|
|
}
|
2017-03-03 16:59:57 +00:00
|
|
|
virtual uint64 objId() const {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-04-10 14:53:01 +00:00
|
|
|
QByteArray imageFormat(const QSize &shrinkBox = QSize()) const;
|
|
|
|
QPixmap imagePixmap(const QSize &shrinkBox = QSize()) const;
|
2015-09-29 18:44:31 +00:00
|
|
|
QString fileName() const {
|
2015-12-24 19:26:28 +00:00
|
|
|
return _fname;
|
2015-09-29 18:44:31 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
float64 currentProgress() const;
|
2015-12-30 19:09:20 +00:00
|
|
|
virtual int32 currentOffset(bool includeSkipped = false) const = 0;
|
2014-05-30 08:53:19 +00:00
|
|
|
int32 fullSize() const;
|
|
|
|
|
2015-12-24 19:26:28 +00:00
|
|
|
bool setFileName(const QString &filename); // set filename for loaders to cache
|
|
|
|
void permitLoadFromCloud();
|
2014-09-04 07:33:44 +00:00
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
void pause();
|
|
|
|
void start(bool loadFirst = false, bool prior = true);
|
|
|
|
void cancel();
|
2015-12-23 19:23:14 +00:00
|
|
|
|
|
|
|
bool loading() const {
|
2015-12-24 19:26:28 +00:00
|
|
|
return _inQueue;
|
2015-12-23 19:23:14 +00:00
|
|
|
}
|
|
|
|
bool paused() const {
|
|
|
|
return _paused;
|
|
|
|
}
|
|
|
|
bool started() const {
|
2015-12-24 19:26:28 +00:00
|
|
|
return _inQueue || _paused;
|
2015-12-23 19:23:14 +00:00
|
|
|
}
|
|
|
|
bool loadingLocal() const {
|
|
|
|
return (_localStatus == LocalLoading);
|
|
|
|
}
|
2015-12-24 19:26:28 +00:00
|
|
|
bool autoLoading() const {
|
|
|
|
return _autoLoading;
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-12-30 19:09:20 +00:00
|
|
|
virtual void stop() {
|
|
|
|
}
|
|
|
|
virtual ~FileLoader();
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-09-29 18:44:31 +00:00
|
|
|
void localLoaded(const StorageImageSaved &result, const QByteArray &imageFormat = QByteArray(), const QPixmap &imagePixmap = QPixmap());
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
signals:
|
2015-12-30 19:09:20 +00:00
|
|
|
void progress(FileLoader *loader);
|
|
|
|
void failed(FileLoader *loader, bool started);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-12-30 19:09:20 +00:00
|
|
|
protected:
|
2016-04-10 14:53:01 +00:00
|
|
|
void readImage(const QSize &shrinkBox) const;
|
2015-12-30 19:09:20 +00:00
|
|
|
|
2016-10-12 19:34:25 +00:00
|
|
|
FileLoader *_prev = nullptr;
|
|
|
|
FileLoader *_next = nullptr;
|
|
|
|
int _priority = 0;
|
2015-12-30 19:09:20 +00:00
|
|
|
FileLoaderQueue *_queue;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-10-12 19:34:25 +00:00
|
|
|
bool _paused = false;
|
|
|
|
bool _autoLoading = false;
|
|
|
|
bool _inQueue = false;
|
|
|
|
bool _complete = false;
|
|
|
|
mutable LocalLoadStatus _localStatus = LocalNotTried;
|
2015-09-29 15:29:21 +00:00
|
|
|
|
2015-12-30 19:09:20 +00:00
|
|
|
virtual bool tryLoadLocal() = 0;
|
|
|
|
virtual void cancelRequests() = 0;
|
2014-10-30 16:23:44 +00:00
|
|
|
|
2015-12-23 19:23:14 +00:00
|
|
|
void startLoading(bool loadFirst, bool prior);
|
2014-05-30 08:53:19 +00:00
|
|
|
void removeFromQueue();
|
2015-12-24 19:26:28 +00:00
|
|
|
void cancel(bool failed);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-12-30 19:09:20 +00:00
|
|
|
void loadNext();
|
|
|
|
virtual bool loadPart() = 0;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-12-24 19:26:28 +00:00
|
|
|
QFile _file;
|
|
|
|
QString _fname;
|
2016-10-12 19:34:25 +00:00
|
|
|
bool _fileIsOpen = false;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-12-24 19:26:28 +00:00
|
|
|
LoadToCacheSetting _toCache;
|
|
|
|
LoadFromCloudSetting _fromCloud;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-12-24 19:26:28 +00:00
|
|
|
QByteArray _data;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-12-24 19:26:28 +00:00
|
|
|
int32 _size;
|
|
|
|
mtpTypeId _type;
|
2015-12-30 19:09:20 +00:00
|
|
|
LocationType _locationType;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-10-12 19:34:25 +00:00
|
|
|
TaskId _localTaskId = 0;
|
2015-09-29 18:44:31 +00:00
|
|
|
mutable QByteArray _imageFormat;
|
|
|
|
mutable QPixmap _imagePixmap;
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
};
|
2015-12-24 19:26:28 +00:00
|
|
|
|
2015-12-30 19:09:20 +00:00
|
|
|
class StorageImageLocation;
|
|
|
|
class mtpFileLoader : public FileLoader, public RPCSender {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
mtpFileLoader(const StorageImageLocation *location, int32 size, LoadFromCloudSetting fromCloud, bool autoLoading);
|
2016-07-08 13:56:53 +00:00
|
|
|
mtpFileLoader(int32 dc, const uint64 &id, const uint64 &access, int32 version, LocationType type, const QString &toFile, int32 size, LoadToCacheSetting toCache, LoadFromCloudSetting fromCloud, bool autoLoading);
|
2015-12-30 19:09:20 +00:00
|
|
|
|
2017-03-03 16:59:57 +00:00
|
|
|
int32 currentOffset(bool includeSkipped = false) const override;
|
2015-12-30 19:09:20 +00:00
|
|
|
|
2017-03-03 16:59:57 +00:00
|
|
|
uint64 objId() const override {
|
2015-12-30 19:09:20 +00:00
|
|
|
return _id;
|
|
|
|
}
|
|
|
|
|
2017-03-03 16:59:57 +00:00
|
|
|
void stop() override {
|
2016-02-28 12:36:23 +00:00
|
|
|
rpcClear();
|
2015-12-30 19:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~mtpFileLoader();
|
|
|
|
|
|
|
|
protected:
|
2017-03-03 16:59:57 +00:00
|
|
|
bool tryLoadLocal() override;
|
|
|
|
void cancelRequests() override;
|
2015-12-30 19:09:20 +00:00
|
|
|
|
|
|
|
typedef QMap<mtpRequestId, int32> Requests;
|
|
|
|
Requests _requests;
|
|
|
|
|
2017-03-03 16:59:57 +00:00
|
|
|
bool loadPart() override;
|
2015-12-30 19:09:20 +00:00
|
|
|
void partLoaded(int32 offset, const MTPupload_File &result, mtpRequestId req);
|
|
|
|
bool partFailed(const RPCError &error);
|
|
|
|
|
2016-07-08 13:56:53 +00:00
|
|
|
bool _lastComplete = false;
|
|
|
|
int32 _skippedBytes = 0;
|
|
|
|
int32 _nextRequestOffset = 0;
|
2015-12-30 19:09:20 +00:00
|
|
|
|
|
|
|
int32 _dc;
|
2016-07-08 13:56:53 +00:00
|
|
|
const StorageImageLocation *_location = nullptr;
|
2015-12-30 19:09:20 +00:00
|
|
|
|
2016-07-08 13:56:53 +00:00
|
|
|
uint64 _id = 0; // for other locations
|
|
|
|
uint64 _access = 0;
|
|
|
|
int32 _version = 0;
|
2015-12-31 15:27:21 +00:00
|
|
|
|
2015-12-30 19:09:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class webFileLoaderPrivate;
|
|
|
|
|
|
|
|
class webFileLoader : public FileLoader {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
webFileLoader(const QString &url, const QString &to, LoadFromCloudSetting fromCloud, bool autoLoading);
|
|
|
|
|
|
|
|
virtual int32 currentOffset(bool includeSkipped = false) const;
|
|
|
|
virtual webFileLoader *webLoader() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
virtual const webFileLoader *webLoader() const {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onProgress(qint64 already, qint64 size);
|
|
|
|
void onFinished(const QByteArray &data);
|
|
|
|
void onError();
|
|
|
|
|
|
|
|
virtual void stop() {
|
|
|
|
cancelRequests();
|
|
|
|
}
|
|
|
|
|
|
|
|
~webFileLoader();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual void cancelRequests();
|
|
|
|
virtual bool tryLoadLocal();
|
|
|
|
virtual bool loadPart();
|
|
|
|
|
|
|
|
QString _url;
|
|
|
|
|
|
|
|
bool _requestSent;
|
|
|
|
int32 _already;
|
|
|
|
|
|
|
|
friend class WebLoadManager;
|
|
|
|
webFileLoaderPrivate *_private;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
enum WebReplyProcessResult {
|
|
|
|
WebReplyProcessError,
|
|
|
|
WebReplyProcessProgress,
|
|
|
|
WebReplyProcessFinished,
|
|
|
|
};
|
|
|
|
|
|
|
|
class WebLoadManager : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
WebLoadManager(QThread *thread);
|
|
|
|
|
2016-03-20 08:16:35 +00:00
|
|
|
#ifndef TDESKTOP_DISABLE_NETWORK_PROXY
|
2015-12-30 19:09:20 +00:00
|
|
|
void setProxySettings(const QNetworkProxy &proxy);
|
2016-10-18 07:56:38 +00:00
|
|
|
#endif // !TDESKTOP_DISABLE_NETWORK_PROXY
|
2015-12-30 19:09:20 +00:00
|
|
|
|
|
|
|
void append(webFileLoader *loader, const QString &url);
|
|
|
|
void stop(webFileLoader *reader);
|
|
|
|
bool carries(webFileLoader *reader) const;
|
|
|
|
|
|
|
|
~WebLoadManager();
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void processDelayed();
|
|
|
|
void proxyApplyDelayed();
|
|
|
|
|
|
|
|
void progress(webFileLoader *loader, qint64 already, qint64 size);
|
|
|
|
void finished(webFileLoader *loader, QByteArray data);
|
|
|
|
void error(webFileLoader *loader);
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
void onFailed(QNetworkReply *reply);
|
|
|
|
void onFailed(QNetworkReply::NetworkError error);
|
|
|
|
void onProgress(qint64 already, qint64 size);
|
|
|
|
void onMeta();
|
|
|
|
|
|
|
|
void process();
|
|
|
|
void proxyApply();
|
|
|
|
void finish();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void clear();
|
|
|
|
void sendRequest(webFileLoaderPrivate *loader, const QString &redirect = QString());
|
|
|
|
bool handleReplyResult(webFileLoaderPrivate *loader, WebReplyProcessResult result);
|
|
|
|
|
2016-03-20 08:16:35 +00:00
|
|
|
#ifndef TDESKTOP_DISABLE_NETWORK_PROXY
|
2015-12-30 19:09:20 +00:00
|
|
|
QNetworkProxy _proxySettings;
|
2016-10-18 07:56:38 +00:00
|
|
|
#endif // !TDESKTOP_DISABLE_NETWORK_PROXY
|
2015-12-30 19:09:20 +00:00
|
|
|
QNetworkAccessManager _manager;
|
|
|
|
typedef QMap<webFileLoader*, webFileLoaderPrivate*> LoaderPointers;
|
|
|
|
LoaderPointers _loaderPointers;
|
|
|
|
mutable QMutex _loaderPointersMutex;
|
|
|
|
|
|
|
|
typedef OrderedSet<webFileLoaderPrivate*> Loaders;
|
|
|
|
Loaders _loaders;
|
|
|
|
|
|
|
|
typedef QMap<QNetworkReply*, webFileLoaderPrivate*> Replies;
|
|
|
|
Replies _replies;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class WebLoadMainManager : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
|
|
|
|
void progress(webFileLoader *loader, qint64 already, qint64 size);
|
|
|
|
void finished(webFileLoader *loader, QByteArray data);
|
|
|
|
void error(webFileLoader *loader);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
static FileLoader * const CancelledFileLoader = SharedMemoryLocation<FileLoader, 0>();
|
|
|
|
static mtpFileLoader * const CancelledMtpFileLoader = static_cast<mtpFileLoader*>(CancelledFileLoader);
|
|
|
|
static webFileLoader * const CancelledWebFileLoader = static_cast<webFileLoader*>(CancelledFileLoader);
|
|
|
|
static WebLoadManager * const FinishedWebLoadManager = SharedMemoryLocation<WebLoadManager, 0>();
|
|
|
|
|
|
|
|
void reinitWebLoadManager();
|
|
|
|
void stopWebLoadManager();
|
2016-05-25 17:59:21 +00:00
|
|
|
|
|
|
|
namespace FileDownload {
|
2016-09-26 12:09:59 +00:00
|
|
|
|
2016-09-26 13:57:08 +00:00
|
|
|
base::Observable<void> &ImageLoaded();
|
2016-05-25 17:59:21 +00:00
|
|
|
|
|
|
|
} // namespace FileDownload
|