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
|
2015-10-03 13:16:42 +00:00
|
|
|
Copyright (c) 2014-2015 John Preston, https://desktop.telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
namespace MTP {
|
|
|
|
void clearLoaderPriorities();
|
|
|
|
}
|
|
|
|
|
2015-09-29 15:29:21 +00:00
|
|
|
enum LocationType {
|
|
|
|
UnknownFileLocation = 0,
|
|
|
|
DocumentFileLocation = 0x4e45abe9, // mtpc_inputDocumentFileLocation
|
|
|
|
AudioFileLocation = 0x74dc404d, // mtpc_inputAudioFileLocation
|
|
|
|
VideoFileLocation = 0x3d0364ec, // mtpc_inputVideoFileLocation
|
|
|
|
};
|
|
|
|
inline LocationType mtpToLocationType(mtpTypeId type) {
|
|
|
|
switch (type) {
|
|
|
|
case mtpc_inputDocumentFileLocation: return DocumentFileLocation;
|
|
|
|
case mtpc_inputAudioFileLocation: return AudioFileLocation;
|
|
|
|
case mtpc_inputVideoFileLocation: return VideoFileLocation;
|
|
|
|
default: return UnknownFileLocation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inline mtpTypeId mtpFromLocationType(LocationType type) {
|
|
|
|
switch (type) {
|
|
|
|
case DocumentFileLocation: return mtpc_inputDocumentFileLocation;
|
|
|
|
case AudioFileLocation: return mtpc_inputAudioFileLocation;
|
|
|
|
case VideoFileLocation: return mtpc_inputVideoFileLocation;
|
|
|
|
case UnknownFileLocation:
|
|
|
|
default: return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2015-09-29 18:44:31 +00:00
|
|
|
typedef void *TaskId; // no interface, just id
|
|
|
|
|
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
|
|
|
}
|
|
|
|
QByteArray imageFormat() const;
|
|
|
|
QPixmap imagePixmap() const;
|
|
|
|
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 mtpFileLoader *mtpLoader() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
virtual const mtpFileLoader *mtpLoader() const {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
virtual webFileLoader *webLoader() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
virtual const webFileLoader *webLoader() const {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
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:
|
|
|
|
|
|
|
|
FileLoader *_prev, *_next;
|
|
|
|
int32 _priority;
|
|
|
|
FileLoaderQueue *_queue;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-12-24 19:26:28 +00:00
|
|
|
bool _paused, _autoLoading, _inQueue, _complete;
|
2015-12-23 19:23:14 +00:00
|
|
|
mutable LocalLoadStatus _localStatus;
|
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;
|
|
|
|
bool _fileIsOpen;
|
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
|
|
|
|
2015-09-29 18:44:31 +00:00
|
|
|
TaskId _localTaskId;
|
|
|
|
mutable QByteArray _imageFormat;
|
|
|
|
mutable QPixmap _imagePixmap;
|
|
|
|
void readImage() const;
|
|
|
|
|
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);
|
|
|
|
mtpFileLoader(int32 dc, const uint64 &id, const uint64 &access, LocationType type, const QString &toFile, int32 size, LoadToCacheSetting toCache, LoadFromCloudSetting fromCloud, bool autoLoading);
|
|
|
|
|
|
|
|
virtual int32 currentOffset(bool includeSkipped = false) const;
|
|
|
|
|
|
|
|
uint64 objId() const {
|
|
|
|
return _id;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual mtpFileLoader *mtpLoader() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
virtual const mtpFileLoader *mtpLoader() const {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void stop() {
|
|
|
|
rpcInvalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
~mtpFileLoader();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual bool tryLoadLocal();
|
|
|
|
virtual void cancelRequests();
|
|
|
|
|
|
|
|
typedef QMap<mtpRequestId, int32> Requests;
|
|
|
|
Requests _requests;
|
|
|
|
|
|
|
|
virtual bool loadPart();
|
|
|
|
void partLoaded(int32 offset, const MTPupload_File &result, mtpRequestId req);
|
|
|
|
bool partFailed(const RPCError &error);
|
|
|
|
|
|
|
|
bool _lastComplete;
|
|
|
|
int32 _skippedBytes;
|
|
|
|
int32 _nextRequestOffset;
|
|
|
|
|
|
|
|
int32 _dc;
|
|
|
|
const StorageImageLocation *_location;
|
|
|
|
|
|
|
|
uint64 _id; // for other locations
|
|
|
|
uint64 _access;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
void setProxySettings(const QNetworkProxy &proxy);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
QNetworkProxy _proxySettings;
|
|
|
|
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();
|