mirror of
https://github.com/vale981/tdesktop
synced 2025-03-06 02:01:40 -05:00
Add automatic loading of videos/files.
This commit is contained in:
parent
e3cc8652e4
commit
97b0288c7d
5 changed files with 76 additions and 51 deletions
|
@ -18,6 +18,7 @@ namespace AutoDownload {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
constexpr auto kDefaultMaxSize = 10 * 1024 * 1024;
|
constexpr auto kDefaultMaxSize = 10 * 1024 * 1024;
|
||||||
|
constexpr auto kNonCacheMaxSize = 2 * 1024 * 1024;
|
||||||
constexpr auto kVersion = char(1);
|
constexpr auto kVersion = char(1);
|
||||||
|
|
||||||
template <typename Enum>
|
template <typename Enum>
|
||||||
|
@ -38,6 +39,9 @@ void SetDefaultsForSource(Full &data, Source source) {
|
||||||
data.setBytesLimit(source, Type::VoiceMessage, kDefaultMaxSize);
|
data.setBytesLimit(source, Type::VoiceMessage, kDefaultMaxSize);
|
||||||
data.setBytesLimit(source, Type::VideoMessage, kDefaultMaxSize);
|
data.setBytesLimit(source, Type::VideoMessage, kDefaultMaxSize);
|
||||||
data.setBytesLimit(source, Type::GIF, kDefaultMaxSize);
|
data.setBytesLimit(source, Type::GIF, kDefaultMaxSize);
|
||||||
|
data.setBytesLimit(source, Type::File, kNonCacheMaxSize);
|
||||||
|
data.setBytesLimit(source, Type::Video, kNonCacheMaxSize);
|
||||||
|
data.setBytesLimit(source, Type::Music, kNonCacheMaxSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
const Full &Defaults() {
|
const Full &Defaults() {
|
||||||
|
|
|
@ -632,12 +632,20 @@ void DocumentData::unload() {
|
||||||
void DocumentData::automaticLoad(
|
void DocumentData::automaticLoad(
|
||||||
Data::FileOrigin origin,
|
Data::FileOrigin origin,
|
||||||
const HistoryItem *item) {
|
const HistoryItem *item) {
|
||||||
if (loaded() || status != FileReady) return;
|
if (status != FileReady || loaded() || cancelled()) {
|
||||||
|
return;
|
||||||
if (saveToCache() && _loader != CancelledMtpFileLoader) {
|
} else if (!item && type != StickerDocument && !isAnimation()) {
|
||||||
if (type == StickerDocument
|
return;
|
||||||
|| isAnimation()
|
}
|
||||||
|| (isVoiceMessage() && item)) {
|
const auto toCache = saveToCache();
|
||||||
|
if (!toCache && Global::AskDownloadPath()) {
|
||||||
|
// We need a filename, but we're supposed to ask user for it.
|
||||||
|
// No automatic download in this case.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const auto filename = toCache
|
||||||
|
? QString()
|
||||||
|
: documentSaveFilename(this);
|
||||||
const auto shouldLoadFromCloud = item
|
const auto shouldLoadFromCloud = item
|
||||||
? Data::AutoDownload::Should(
|
? Data::AutoDownload::Should(
|
||||||
Auth().settings().autoDownload(),
|
Auth().settings().autoDownload(),
|
||||||
|
@ -651,19 +659,15 @@ void DocumentData::automaticLoad(
|
||||||
: LoadFromLocalOnly;
|
: LoadFromLocalOnly;
|
||||||
save(
|
save(
|
||||||
origin,
|
origin,
|
||||||
QString(),
|
filename,
|
||||||
_actionOnLoad,
|
_actionOnLoad,
|
||||||
_actionOnLoadMsgId,
|
_actionOnLoadMsgId,
|
||||||
loadFromCloud,
|
loadFromCloud,
|
||||||
true);
|
true);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DocumentData::automaticLoadSettingsChanged() {
|
void DocumentData::automaticLoadSettingsChanged() {
|
||||||
if (_loader != CancelledMtpFileLoader
|
if (!cancelled() || status != FileReady || loaded()) {
|
||||||
|| status != FileReady
|
|
||||||
|| loaded()) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_loader = nullptr;
|
_loader = nullptr;
|
||||||
|
@ -790,7 +794,7 @@ bool DocumentData::loaded(FilePathResolveType type) const {
|
||||||
|
|
||||||
void DocumentData::destroyLoader(mtpFileLoader *newValue) const {
|
void DocumentData::destroyLoader(mtpFileLoader *newValue) const {
|
||||||
const auto loader = std::exchange(_loader, newValue);
|
const auto loader = std::exchange(_loader, newValue);
|
||||||
if (_loader == CancelledMtpFileLoader) {
|
if (cancelled()) {
|
||||||
loader->cancel();
|
loader->cancel();
|
||||||
}
|
}
|
||||||
loader->stop();
|
loader->stop();
|
||||||
|
@ -798,7 +802,7 @@ void DocumentData::destroyLoader(mtpFileLoader *newValue) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DocumentData::loading() const {
|
bool DocumentData::loading() const {
|
||||||
return _loader && _loader != CancelledMtpFileLoader;
|
return _loader && !cancelled();
|
||||||
}
|
}
|
||||||
|
|
||||||
QString DocumentData::loadingFilePath() const {
|
QString DocumentData::loadingFilePath() const {
|
||||||
|
@ -874,7 +878,7 @@ void DocumentData::save(
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_loader == CancelledMtpFileLoader) {
|
if (cancelled()) {
|
||||||
_loader = nullptr;
|
_loader = nullptr;
|
||||||
}
|
}
|
||||||
if (_loader) {
|
if (_loader) {
|
||||||
|
@ -887,7 +891,9 @@ void DocumentData::save(
|
||||||
_actionOnLoad = action;
|
_actionOnLoad = action;
|
||||||
_actionOnLoadMsgId = actionMsgId;
|
_actionOnLoadMsgId = actionMsgId;
|
||||||
if (_loader) {
|
if (_loader) {
|
||||||
if (fromCloud == LoadFromCloudOrLocal) _loader->permitLoadFromCloud();
|
if (fromCloud == LoadFromCloudOrLocal) {
|
||||||
|
_loader->permitLoadFromCloud();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
status = FileReady;
|
status = FileReady;
|
||||||
if (hasWebLocation()) {
|
if (hasWebLocation()) {
|
||||||
|
@ -922,6 +928,8 @@ void DocumentData::save(
|
||||||
|
|
||||||
_loader->connect(_loader, SIGNAL(progress(FileLoader*)), App::main(), SLOT(documentLoadProgress(FileLoader*)));
|
_loader->connect(_loader, SIGNAL(progress(FileLoader*)), App::main(), SLOT(documentLoadProgress(FileLoader*)));
|
||||||
_loader->connect(_loader, SIGNAL(failed(FileLoader*,bool)), App::main(), SLOT(documentLoadFailed(FileLoader*,bool)));
|
_loader->connect(_loader, SIGNAL(failed(FileLoader*,bool)), App::main(), SLOT(documentLoadFailed(FileLoader*,bool)));
|
||||||
|
}
|
||||||
|
if (loading()) {
|
||||||
_loader->start();
|
_loader->start();
|
||||||
}
|
}
|
||||||
_session->data().notifyDocumentLayoutChanged(this);
|
_session->data().notifyDocumentLayoutChanged(this);
|
||||||
|
@ -934,13 +942,15 @@ void DocumentData::cancel() {
|
||||||
|
|
||||||
destroyLoader(CancelledMtpFileLoader);
|
destroyLoader(CancelledMtpFileLoader);
|
||||||
_session->data().notifyDocumentLayoutChanged(this);
|
_session->data().notifyDocumentLayoutChanged(this);
|
||||||
if (auto main = App::main()) {
|
App::main()->documentLoadProgress(this);
|
||||||
main->documentLoadProgress(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
_actionOnLoad = ActionOnLoadNone;
|
_actionOnLoad = ActionOnLoadNone;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool DocumentData::cancelled() const {
|
||||||
|
return (_loader == CancelledMtpFileLoader);
|
||||||
|
}
|
||||||
|
|
||||||
VoiceWaveform documentWaveformDecode(const QByteArray &encoded5bit) {
|
VoiceWaveform documentWaveformDecode(const QByteArray &encoded5bit) {
|
||||||
auto bitsCount = static_cast<int>(encoded5bit.size() * 8);
|
auto bitsCount = static_cast<int>(encoded5bit.size() * 8);
|
||||||
auto valuesCount = bitsCount / 5;
|
auto valuesCount = bitsCount / 5;
|
||||||
|
|
|
@ -106,6 +106,7 @@ public:
|
||||||
LoadFromCloudSetting fromCloud = LoadFromCloudOrLocal,
|
LoadFromCloudSetting fromCloud = LoadFromCloudOrLocal,
|
||||||
bool autoLoading = false);
|
bool autoLoading = false);
|
||||||
void cancel();
|
void cancel();
|
||||||
|
bool cancelled() const;
|
||||||
float64 progress() const;
|
float64 progress() const;
|
||||||
int32 loadOffset() const;
|
int32 loadOffset() const;
|
||||||
bool uploading() const;
|
bool uploading() const;
|
||||||
|
|
|
@ -299,14 +299,14 @@ QImage RemoteSource::takeLoaded() {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RemoteSource::loaderValid() const {
|
bool RemoteSource::loaderValid() const {
|
||||||
return _loader && _loader != CancelledFileLoader;
|
return _loader && !cancelled();
|
||||||
}
|
}
|
||||||
|
|
||||||
void RemoteSource::destroyLoader(FileLoader *newValue) {
|
void RemoteSource::destroyLoader(FileLoader *newValue) {
|
||||||
Expects(loaderValid());
|
Expects(loaderValid());
|
||||||
|
|
||||||
const auto loader = std::exchange(_loader, newValue);
|
const auto loader = std::exchange(_loader, newValue);
|
||||||
if (_loader == CancelledFileLoader) {
|
if (cancelled()) {
|
||||||
loader->cancel();
|
loader->cancel();
|
||||||
}
|
}
|
||||||
loader->stop();
|
loader->stop();
|
||||||
|
@ -350,21 +350,26 @@ bool RemoteSource::loading() {
|
||||||
void RemoteSource::automaticLoad(
|
void RemoteSource::automaticLoad(
|
||||||
Data::FileOrigin origin,
|
Data::FileOrigin origin,
|
||||||
const HistoryItem *item) {
|
const HistoryItem *item) {
|
||||||
if (_loader != CancelledFileLoader && item) {
|
if (!item || cancelled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const auto loadFromCloud = Data::AutoDownload::Should(
|
const auto loadFromCloud = Data::AutoDownload::Should(
|
||||||
Auth().settings().autoDownload(),
|
Auth().settings().autoDownload(),
|
||||||
item->history()->peer,
|
item->history()->peer,
|
||||||
this);
|
this);
|
||||||
|
|
||||||
if (_loader) {
|
if (_loader) {
|
||||||
if (loadFromCloud) _loader->permitLoadFromCloud();
|
if (loadFromCloud) {
|
||||||
|
_loader->permitLoadFromCloud();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
_loader = createLoader(
|
_loader = createLoader(
|
||||||
origin,
|
origin,
|
||||||
loadFromCloud ? LoadFromCloudOrLocal : LoadFromLocalOnly,
|
loadFromCloud ? LoadFromCloudOrLocal : LoadFromLocalOnly,
|
||||||
true);
|
true);
|
||||||
if (_loader) _loader->start();
|
|
||||||
}
|
}
|
||||||
|
if (loaderValid()) {
|
||||||
|
_loader->start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -386,11 +391,15 @@ void RemoteSource::load(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool RemoteSource::cancelled() const {
|
||||||
|
return (_loader == CancelledFileLoader);
|
||||||
|
}
|
||||||
|
|
||||||
void RemoteSource::loadEvenCancelled(
|
void RemoteSource::loadEvenCancelled(
|
||||||
Data::FileOrigin origin,
|
Data::FileOrigin origin,
|
||||||
bool loadFirst,
|
bool loadFirst,
|
||||||
bool prior) {
|
bool prior) {
|
||||||
if (_loader == CancelledFileLoader) {
|
if (cancelled()) {
|
||||||
_loader = nullptr;
|
_loader = nullptr;
|
||||||
}
|
}
|
||||||
return load(origin, loadFirst, prior);
|
return load(origin, loadFirst, prior);
|
||||||
|
|
|
@ -169,6 +169,7 @@ protected:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool loaderValid() const;
|
bool loaderValid() const;
|
||||||
|
bool cancelled() const;
|
||||||
void destroyLoader(FileLoader *newValue = nullptr);
|
void destroyLoader(FileLoader *newValue = nullptr);
|
||||||
|
|
||||||
FileLoader *_loader = nullptr;
|
FileLoader *_loader = nullptr;
|
||||||
|
|
Loading…
Add table
Reference in a new issue