From 1e9c79ca85637e6fb561759019eee5ebc04871a2 Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 10 Apr 2020 18:19:43 +0400 Subject: [PATCH] Move automaticLoad() to DocumentMedia. --- .../SourceFiles/chat_helpers/stickers.cpp | 2 +- Telegram/SourceFiles/data/data_document.cpp | 47 +++------------- Telegram/SourceFiles/data/data_document.h | 9 ++-- .../SourceFiles/data/data_document_media.cpp | 54 ++++++++++++++++--- .../SourceFiles/data/data_document_media.h | 2 + .../view/media/history_view_document.cpp | 2 +- .../media/history_view_theme_document.cpp | 2 +- .../inline_bot_layout_internal.cpp | 4 +- .../media/view/media_view_overlay_widget.cpp | 6 +-- .../SourceFiles/overview/overview_layout.cpp | 2 +- .../window/window_media_preview.cpp | 2 +- 11 files changed, 72 insertions(+), 60 deletions(-) diff --git a/Telegram/SourceFiles/chat_helpers/stickers.cpp b/Telegram/SourceFiles/chat_helpers/stickers.cpp index 7e95055d7..14ceae895 100644 --- a/Telegram/SourceFiles/chat_helpers/stickers.cpp +++ b/Telegram/SourceFiles/chat_helpers/stickers.cpp @@ -1235,7 +1235,7 @@ bool HasLottieThumbnail( if (!info->animated) { return false; } - document->automaticLoad(document->stickerSetOrigin(), nullptr); + media->automaticLoad(document->stickerSetOrigin(), nullptr); if (!media->loaded()) { return false; } diff --git a/Telegram/SourceFiles/data/data_document.cpp b/Telegram/SourceFiles/data/data_document.cpp index f6d2130e4..75677b966 100644 --- a/Telegram/SourceFiles/data/data_document.cpp +++ b/Telegram/SourceFiles/data/data_document.cpp @@ -233,7 +233,11 @@ QString FileNameForSave( return result; } -QString documentSaveFilename(const DocumentData *data, bool forceSavingAs = false, const QString already = QString(), const QDir &dir = QDir()) { +QString DocumentFileNameForSave( + not_null data, + bool forceSavingAs, + const QString &already, + const QDir &dir) { auto alreadySavingFilename = data->loadingFilePath(); if (!alreadySavingFilename.isEmpty()) { return alreadySavingFilename; @@ -363,7 +367,7 @@ void DocumentSaveClickHandler::Save( const auto filename = filepath.isEmpty() ? QString() : fileinfo.fileName(); - savename = documentSaveFilename( + savename = DocumentFileNameForSave( data, (mode == Mode::ToNewFile), filename, @@ -736,43 +740,6 @@ void DocumentData::unload() { _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() { if (!cancelled() || status != FileReady) { return; @@ -1129,7 +1096,7 @@ bool DocumentData::saveFromDataChecked() { if (bytes.isEmpty()) { return false; } - const auto path = documentSaveFilename(this); + const auto path = DocumentFileNameForSave(this); if (path.isEmpty()) { return false; } diff --git a/Telegram/SourceFiles/data/data_document.h b/Telegram/SourceFiles/data/data_document.h index 0b857a1d0..2550ce11d 100644 --- a/Telegram/SourceFiles/data/data_document.h +++ b/Telegram/SourceFiles/data/data_document.h @@ -95,9 +95,6 @@ public: void setattributes( const QVector &attributes); - void automaticLoad( - Data::FileOrigin origin, - const HistoryItem *item); void automaticLoadSettingsChanged(); [[nodiscard]] bool loading() const; @@ -430,6 +427,12 @@ QString FileNameForSave( bool savingAs, const QDir &dir = QDir()); +QString DocumentFileNameForSave( + not_null data, + bool forceSavingAs = false, + const QString &already = QString(), + const QDir &dir = QDir()); + namespace Data { QString FileExtension(const QString &filepath); diff --git a/Telegram/SourceFiles/data/data_document_media.cpp b/Telegram/SourceFiles/data/data_document_media.cpp index b0e9bfaef..bbb18e185 100644 --- a/Telegram/SourceFiles/data/data_document_media.cpp +++ b/Telegram/SourceFiles/data/data_document_media.cpp @@ -11,12 +11,16 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_session.h" #include "data/data_cloud_themes.h" #include "data/data_file_origin.h" +#include "data/data_auto_download.h" #include "media/clip/media_clip_reader.h" #include "main/main_session.h" #include "lottie/lottie_animation.h" +#include "history/history_item.h" +#include "history/history.h" #include "window/themes/window_theme_preview.h" #include "storage/file_download.h" #include "ui/image/image.h" +#include "facades.h" #include "app.h" #include @@ -135,7 +139,7 @@ void DocumentMedia::checkStickerLarge() { return; } - _owner->automaticLoad(_owner->stickerSetOrigin(), nullptr); + automaticLoad(_owner->stickerSetOrigin(), nullptr); if (data->animated || !loaded()) { 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) { if (!bytes.isEmpty()) { _bytes = bytes; @@ -173,15 +213,15 @@ bool DocumentMedia::loaded(bool check) const { } float64 DocumentMedia::progress() const { - return (owner()->uploading() || owner()->loading()) - ? owner()->progress() + return (_owner->uploading() || _owner->loading()) + ? _owner->progress() : (loaded() ? 1. : 0.); } bool DocumentMedia::canBePlayed() const { - return !owner()->inappPlaybackFailed() - && owner()->useStreamingLoader() - && (loaded() || owner()->canBeStreamed()); + return !_owner->inappPlaybackFailed() + && _owner->useStreamingLoader() + && (loaded() || _owner->canBeStreamed()); } void DocumentMedia::checkStickerSmall() { @@ -189,7 +229,7 @@ void DocumentMedia::checkStickerSmall() { if ((data && data->animated) || _owner->thumbnailEnoughForSticker()) { _owner->loadThumbnail(_owner->stickerSetOrigin()); if (data && data->animated) { - _owner->automaticLoad(_owner->stickerSetOrigin(), nullptr); + automaticLoad(_owner->stickerSetOrigin(), nullptr); } } else { checkStickerLarge(); diff --git a/Telegram/SourceFiles/data/data_document_media.h b/Telegram/SourceFiles/data/data_document_media.h index ed5dfd7c7..9d6a2fdc6 100644 --- a/Telegram/SourceFiles/data/data_document_media.h +++ b/Telegram/SourceFiles/data/data_document_media.h @@ -38,6 +38,8 @@ public: [[nodiscard]] float64 progress() const; [[nodiscard]] bool canBePlayed() const; + void automaticLoad(Data::FileOrigin origin, const HistoryItem *item); + // For DocumentData. static void CheckGoodThumbnail(not_null document); diff --git a/Telegram/SourceFiles/history/view/media/history_view_document.cpp b/Telegram/SourceFiles/history/view/media/history_view_document.cpp index 1bcfcca24..dabdc9542 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_document.cpp +++ b/Telegram/SourceFiles/history/view/media/history_view_document.cpp @@ -247,7 +247,7 @@ void Document::draw(Painter &p, const QRect &r, TextSelection selection, crl::ti const auto cornerDownload = downloadInCorner(); if (!_dataMedia->canBePlayed()) { - _data->automaticLoad(_realParent->fullId(), _parent->data()); + _dataMedia->automaticLoad(_realParent->fullId(), _parent->data()); } bool loaded = dataLoaded(), displayLoading = _data->displayLoading(); bool selected = (selection == FullSelection); diff --git a/Telegram/SourceFiles/history/view/media/history_view_theme_document.cpp b/Telegram/SourceFiles/history/view/media/history_view_theme_document.cpp index 5a88425ae..56cc6a679 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_theme_document.cpp +++ b/Telegram/SourceFiles/history/view/media/history_view_theme_document.cpp @@ -110,7 +110,7 @@ void ThemeDocument::draw(Painter &p, const QRect &r, TextSelection selection, cr ensureDataMediaCreated(); - _data->automaticLoad(_realParent->fullId(), _parent->data()); + _dataMedia->automaticLoad(_realParent->fullId(), _parent->data()); auto selected = (selection == FullSelection); auto loaded = dataLoaded(); auto displayLoading = _data->displayLoading(); diff --git a/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp b/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp index 60203a124..113cf1a9d 100644 --- a/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp +++ b/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp @@ -147,7 +147,7 @@ int Gif::resizeGetHeight(int width) { void Gif::paint(Painter &p, const QRect &clip, const PaintContext *context) const { const auto document = getShownDocument(); ensureDataMediaCreated(document); - document->automaticLoad(fileOrigin(), nullptr); + _dataMedia->automaticLoad(fileOrigin(), nullptr); bool loaded = _dataMedia->loaded(), loading = document->loading(), displayLoading = document->displayLoading(); if (loaded @@ -1341,7 +1341,7 @@ void Game::paint(Painter &p, const QRect &clip, const PaintContext *context) con } auto animatedThumb = document && document->isAnimation(); if (animatedThumb) { - document->automaticLoad(fileOrigin(), nullptr); + _dataMedia->automaticLoad(fileOrigin(), nullptr); bool loaded = _dataMedia->loaded(), loading = document->loading(), displayLoading = document->displayLoading(); if (loaded && !_gif && !_gif.isBad()) { diff --git a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp index d83ca5d68..9d97db4f6 100644 --- a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp +++ b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp @@ -2000,13 +2000,13 @@ void OverlayWidget::displayDocument( if (_docMedia->canBePlayed() && initStreaming(continueStreaming)) { } else if (_doc->isVideoFile()) { - _doc->automaticLoad(fileOrigin(), item); + _docMedia->automaticLoad(fileOrigin(), item); initStreamingThumbnail(); } else if (_doc->isTheme()) { - _doc->automaticLoad(fileOrigin(), item); + _docMedia->automaticLoad(fileOrigin(), item); initThemePreview(); } else { - _doc->automaticLoad(fileOrigin(), item); + _docMedia->automaticLoad(fileOrigin(), item); auto &location = _doc->location(true); if (location.accessEnable()) { const auto &path = location.name(); diff --git a/Telegram/SourceFiles/overview/overview_layout.cpp b/Telegram/SourceFiles/overview/overview_layout.cpp index 81c489a4b..85e47e3da 100644 --- a/Telegram/SourceFiles/overview/overview_layout.cpp +++ b/Telegram/SourceFiles/overview/overview_layout.cpp @@ -972,7 +972,7 @@ void Document::paint(Painter &p, const QRect &clip, TextSelection selection, con const auto cornerDownload = downloadInCorner(); - _data->automaticLoad(parent()->fullId(), parent()); + _dataMedia->automaticLoad(parent()->fullId(), parent()); const auto loaded = dataLoaded(); const auto displayLoading = _data->displayLoading(); diff --git a/Telegram/SourceFiles/window/window_media_preview.cpp b/Telegram/SourceFiles/window/window_media_preview.cpp index a70af5fde..ea2cb7064 100644 --- a/Telegram/SourceFiles/window/window_media_preview.cpp +++ b/Telegram/SourceFiles/window/window_media_preview.cpp @@ -262,7 +262,7 @@ QPixmap MediaPreviewWidget::currentImage() const { } } } else { - _document->automaticLoad(_origin, nullptr); + _documentMedia->automaticLoad(_origin, nullptr); if (_documentMedia->loaded()) { if (!_gif && !_gif.isBad()) { auto that = const_cast(this);