Move automaticLoad() to DocumentMedia.

This commit is contained in:
John Preston 2020-04-10 18:19:43 +04:00
parent 40f12a2584
commit 1e9c79ca85
11 changed files with 72 additions and 60 deletions

View File

@ -1235,7 +1235,7 @@ bool HasLottieThumbnail(
if (!info->animated) { if (!info->animated) {
return false; return false;
} }
document->automaticLoad(document->stickerSetOrigin(), nullptr); media->automaticLoad(document->stickerSetOrigin(), nullptr);
if (!media->loaded()) { if (!media->loaded()) {
return false; return false;
} }

View File

@ -233,7 +233,11 @@ QString FileNameForSave(
return result; return result;
} }
QString documentSaveFilename(const DocumentData *data, bool forceSavingAs = false, const QString already = QString(), const QDir &dir = QDir()) { QString DocumentFileNameForSave(
not_null<const DocumentData*> data,
bool forceSavingAs,
const QString &already,
const QDir &dir) {
auto alreadySavingFilename = data->loadingFilePath(); auto alreadySavingFilename = data->loadingFilePath();
if (!alreadySavingFilename.isEmpty()) { if (!alreadySavingFilename.isEmpty()) {
return alreadySavingFilename; return alreadySavingFilename;
@ -363,7 +367,7 @@ void DocumentSaveClickHandler::Save(
const auto filename = filepath.isEmpty() const auto filename = filepath.isEmpty()
? QString() ? QString()
: fileinfo.fileName(); : fileinfo.fileName();
savename = documentSaveFilename( savename = DocumentFileNameForSave(
data, data,
(mode == Mode::ToNewFile), (mode == Mode::ToNewFile),
filename, filename,
@ -736,43 +740,6 @@ void DocumentData::unload() {
_replyPreview = nullptr; _replyPreview = nullptr;
} }
void DocumentData::automaticLoad(
Data::FileOrigin origin,
const HistoryItem *item) {
const auto media = activeMediaView();
if (status != FileReady || !media || media->loaded() || cancelled()) {
return;
} else if (!item && type != StickerDocument && !isAnimation()) {
return;
}
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 = !Data::IsExecutableName(filename)
&& (item
? Data::AutoDownload::Should(
session().settings().autoDownload(),
item->history()->peer,
this)
: Data::AutoDownload::Should(
session().settings().autoDownload(),
this));
const auto loadFromCloud = shouldLoadFromCloud
? LoadFromCloudOrLocal
: LoadFromLocalOnly;
save(
origin,
filename,
loadFromCloud,
true);
}
void DocumentData::automaticLoadSettingsChanged() { void DocumentData::automaticLoadSettingsChanged() {
if (!cancelled() || status != FileReady) { if (!cancelled() || status != FileReady) {
return; return;
@ -1129,7 +1096,7 @@ bool DocumentData::saveFromDataChecked() {
if (bytes.isEmpty()) { if (bytes.isEmpty()) {
return false; return false;
} }
const auto path = documentSaveFilename(this); const auto path = DocumentFileNameForSave(this);
if (path.isEmpty()) { if (path.isEmpty()) {
return false; return false;
} }

View File

@ -95,9 +95,6 @@ public:
void setattributes( void setattributes(
const QVector<MTPDocumentAttribute> &attributes); const QVector<MTPDocumentAttribute> &attributes);
void automaticLoad(
Data::FileOrigin origin,
const HistoryItem *item);
void automaticLoadSettingsChanged(); void automaticLoadSettingsChanged();
[[nodiscard]] bool loading() const; [[nodiscard]] bool loading() const;
@ -430,6 +427,12 @@ QString FileNameForSave(
bool savingAs, bool savingAs,
const QDir &dir = QDir()); const QDir &dir = QDir());
QString DocumentFileNameForSave(
not_null<const DocumentData*> data,
bool forceSavingAs = false,
const QString &already = QString(),
const QDir &dir = QDir());
namespace Data { namespace Data {
QString FileExtension(const QString &filepath); QString FileExtension(const QString &filepath);

View File

@ -11,12 +11,16 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_session.h" #include "data/data_session.h"
#include "data/data_cloud_themes.h" #include "data/data_cloud_themes.h"
#include "data/data_file_origin.h" #include "data/data_file_origin.h"
#include "data/data_auto_download.h"
#include "media/clip/media_clip_reader.h" #include "media/clip/media_clip_reader.h"
#include "main/main_session.h" #include "main/main_session.h"
#include "lottie/lottie_animation.h" #include "lottie/lottie_animation.h"
#include "history/history_item.h"
#include "history/history.h"
#include "window/themes/window_theme_preview.h" #include "window/themes/window_theme_preview.h"
#include "storage/file_download.h" #include "storage/file_download.h"
#include "ui/image/image.h" #include "ui/image/image.h"
#include "facades.h"
#include "app.h" #include "app.h"
#include <QtCore/QBuffer> #include <QtCore/QBuffer>
@ -135,7 +139,7 @@ void DocumentMedia::checkStickerLarge() {
return; return;
} }
_owner->automaticLoad(_owner->stickerSetOrigin(), nullptr); automaticLoad(_owner->stickerSetOrigin(), nullptr);
if (data->animated || !loaded()) { if (data->animated || !loaded()) {
return; return;
} }
@ -158,6 +162,42 @@ void DocumentMedia::checkStickerLarge() {
} }
} }
void DocumentMedia::automaticLoad(
Data::FileOrigin origin,
const HistoryItem *item) {
if (_owner->status != FileReady || loaded() || _owner->cancelled()) {
return;
} else if (!item && !_owner->sticker() && !_owner->isAnimation()) {
return;
}
const auto toCache = _owner->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()
: DocumentFileNameForSave(_owner);
const auto shouldLoadFromCloud = !Data::IsExecutableName(filename)
&& (item
? Data::AutoDownload::Should(
_owner->session().settings().autoDownload(),
item->history()->peer,
_owner)
: Data::AutoDownload::Should(
_owner->session().settings().autoDownload(),
_owner));
const auto loadFromCloud = shouldLoadFromCloud
? LoadFromCloudOrLocal
: LoadFromLocalOnly;
_owner->save(
origin,
filename,
loadFromCloud,
true);
}
void DocumentMedia::setBytes(const QByteArray &bytes) { void DocumentMedia::setBytes(const QByteArray &bytes) {
if (!bytes.isEmpty()) { if (!bytes.isEmpty()) {
_bytes = bytes; _bytes = bytes;
@ -173,15 +213,15 @@ bool DocumentMedia::loaded(bool check) const {
} }
float64 DocumentMedia::progress() const { float64 DocumentMedia::progress() const {
return (owner()->uploading() || owner()->loading()) return (_owner->uploading() || _owner->loading())
? owner()->progress() ? _owner->progress()
: (loaded() ? 1. : 0.); : (loaded() ? 1. : 0.);
} }
bool DocumentMedia::canBePlayed() const { bool DocumentMedia::canBePlayed() const {
return !owner()->inappPlaybackFailed() return !_owner->inappPlaybackFailed()
&& owner()->useStreamingLoader() && _owner->useStreamingLoader()
&& (loaded() || owner()->canBeStreamed()); && (loaded() || _owner->canBeStreamed());
} }
void DocumentMedia::checkStickerSmall() { void DocumentMedia::checkStickerSmall() {
@ -189,7 +229,7 @@ void DocumentMedia::checkStickerSmall() {
if ((data && data->animated) || _owner->thumbnailEnoughForSticker()) { if ((data && data->animated) || _owner->thumbnailEnoughForSticker()) {
_owner->loadThumbnail(_owner->stickerSetOrigin()); _owner->loadThumbnail(_owner->stickerSetOrigin());
if (data && data->animated) { if (data && data->animated) {
_owner->automaticLoad(_owner->stickerSetOrigin(), nullptr); automaticLoad(_owner->stickerSetOrigin(), nullptr);
} }
} else { } else {
checkStickerLarge(); checkStickerLarge();

View File

@ -38,6 +38,8 @@ public:
[[nodiscard]] float64 progress() const; [[nodiscard]] float64 progress() const;
[[nodiscard]] bool canBePlayed() const; [[nodiscard]] bool canBePlayed() const;
void automaticLoad(Data::FileOrigin origin, const HistoryItem *item);
// For DocumentData. // For DocumentData.
static void CheckGoodThumbnail(not_null<DocumentData*> document); static void CheckGoodThumbnail(not_null<DocumentData*> document);

View File

@ -247,7 +247,7 @@ void Document::draw(Painter &p, const QRect &r, TextSelection selection, crl::ti
const auto cornerDownload = downloadInCorner(); const auto cornerDownload = downloadInCorner();
if (!_dataMedia->canBePlayed()) { if (!_dataMedia->canBePlayed()) {
_data->automaticLoad(_realParent->fullId(), _parent->data()); _dataMedia->automaticLoad(_realParent->fullId(), _parent->data());
} }
bool loaded = dataLoaded(), displayLoading = _data->displayLoading(); bool loaded = dataLoaded(), displayLoading = _data->displayLoading();
bool selected = (selection == FullSelection); bool selected = (selection == FullSelection);

View File

@ -110,7 +110,7 @@ void ThemeDocument::draw(Painter &p, const QRect &r, TextSelection selection, cr
ensureDataMediaCreated(); ensureDataMediaCreated();
_data->automaticLoad(_realParent->fullId(), _parent->data()); _dataMedia->automaticLoad(_realParent->fullId(), _parent->data());
auto selected = (selection == FullSelection); auto selected = (selection == FullSelection);
auto loaded = dataLoaded(); auto loaded = dataLoaded();
auto displayLoading = _data->displayLoading(); auto displayLoading = _data->displayLoading();

View File

@ -147,7 +147,7 @@ int Gif::resizeGetHeight(int width) {
void Gif::paint(Painter &p, const QRect &clip, const PaintContext *context) const { void Gif::paint(Painter &p, const QRect &clip, const PaintContext *context) const {
const auto document = getShownDocument(); const auto document = getShownDocument();
ensureDataMediaCreated(document); ensureDataMediaCreated(document);
document->automaticLoad(fileOrigin(), nullptr); _dataMedia->automaticLoad(fileOrigin(), nullptr);
bool loaded = _dataMedia->loaded(), loading = document->loading(), displayLoading = document->displayLoading(); bool loaded = _dataMedia->loaded(), loading = document->loading(), displayLoading = document->displayLoading();
if (loaded if (loaded
@ -1341,7 +1341,7 @@ void Game::paint(Painter &p, const QRect &clip, const PaintContext *context) con
} }
auto animatedThumb = document && document->isAnimation(); auto animatedThumb = document && document->isAnimation();
if (animatedThumb) { if (animatedThumb) {
document->automaticLoad(fileOrigin(), nullptr); _dataMedia->automaticLoad(fileOrigin(), nullptr);
bool loaded = _dataMedia->loaded(), loading = document->loading(), displayLoading = document->displayLoading(); bool loaded = _dataMedia->loaded(), loading = document->loading(), displayLoading = document->displayLoading();
if (loaded && !_gif && !_gif.isBad()) { if (loaded && !_gif && !_gif.isBad()) {

View File

@ -2000,13 +2000,13 @@ void OverlayWidget::displayDocument(
if (_docMedia->canBePlayed() if (_docMedia->canBePlayed()
&& initStreaming(continueStreaming)) { && initStreaming(continueStreaming)) {
} else if (_doc->isVideoFile()) { } else if (_doc->isVideoFile()) {
_doc->automaticLoad(fileOrigin(), item); _docMedia->automaticLoad(fileOrigin(), item);
initStreamingThumbnail(); initStreamingThumbnail();
} else if (_doc->isTheme()) { } else if (_doc->isTheme()) {
_doc->automaticLoad(fileOrigin(), item); _docMedia->automaticLoad(fileOrigin(), item);
initThemePreview(); initThemePreview();
} else { } else {
_doc->automaticLoad(fileOrigin(), item); _docMedia->automaticLoad(fileOrigin(), item);
auto &location = _doc->location(true); auto &location = _doc->location(true);
if (location.accessEnable()) { if (location.accessEnable()) {
const auto &path = location.name(); const auto &path = location.name();

View File

@ -972,7 +972,7 @@ void Document::paint(Painter &p, const QRect &clip, TextSelection selection, con
const auto cornerDownload = downloadInCorner(); const auto cornerDownload = downloadInCorner();
_data->automaticLoad(parent()->fullId(), parent()); _dataMedia->automaticLoad(parent()->fullId(), parent());
const auto loaded = dataLoaded(); const auto loaded = dataLoaded();
const auto displayLoading = _data->displayLoading(); const auto displayLoading = _data->displayLoading();

View File

@ -262,7 +262,7 @@ QPixmap MediaPreviewWidget::currentImage() const {
} }
} }
} else { } else {
_document->automaticLoad(_origin, nullptr); _documentMedia->automaticLoad(_origin, nullptr);
if (_documentMedia->loaded()) { if (_documentMedia->loaded()) {
if (!_gif && !_gif.isBad()) { if (!_gif && !_gif.isBad()) {
auto that = const_cast<MediaPreviewWidget*>(this); auto that = const_cast<MediaPreviewWidget*>(this);