diff --git a/Telegram/SourceFiles/menu/menu_item_download_files.cpp b/Telegram/SourceFiles/menu/menu_item_download_files.cpp index 1735e8041..23f6c2525 100644 --- a/Telegram/SourceFiles/menu/menu_item_download_files.cpp +++ b/Telegram/SourceFiles/menu/menu_item_download_files.cpp @@ -35,7 +35,7 @@ namespace Menu { namespace { using Documents = std::vector, FullMsgId>>; -using Photos = std::vector>; +using Photos = std::vector, FullMsgId>>; [[nodiscard]] bool Added( HistoryItem *item, @@ -44,12 +44,8 @@ using Photos = std::vector>; if (item && !item->forbidsForward()) { if (const auto media = item->media()) { if (const auto photo = media->photo()) { - if (const auto view = photo->activeMediaView()) { - if (view->loaded()) { - photos.push_back(view); - return true; - } - } + photos.emplace_back(photo, item->fullId()); + return true; } else if (const auto document = media->document()) { documents.emplace_back(document, item->fullId()); return true; @@ -71,7 +67,7 @@ void AddAction( const auto icon = documents.empty() ? &st::menuIconSaveImage : &st::menuIconDownload; - const auto showToast = documents.empty(); + const auto shouldShowToast = documents.empty(); const auto weak = base::make_weak(controller); const auto saveImages = [=](const QString &folderPath) { @@ -91,38 +87,75 @@ void AddAction( if (path.isEmpty()) { return; } - QDir().mkpath(path); - const auto fullPath = [&](int i) { - return filedialogDefaultName( - u"photo_"_q + QString::number(i), - u".jpg"_q, - path); - }; - auto lastPath = QString(); - for (auto i = 0; i < photos.size(); i++) { - lastPath = fullPath(i + 1); - photos[i]->saveToFile(lastPath); + + const auto showToast = !shouldShowToast + ? Fn(nullptr) + : [=](const QString &lastPath) { + const auto filter = [lastPath](const auto ...) { + File::ShowInFolder(lastPath); + return false; + }; + controller->showToast({ + .text = (photos.size() > 1 + ? tr::lng_mediaview_saved_images_to + : tr::lng_mediaview_saved_to)( + tr::now, + lt_downloads, + Ui::Text::Link( + tr::lng_mediaview_downloads(tr::now), + "internal:show_saved_message"), + Ui::Text::WithEntities), + .st = &st::defaultToast, + .filter = filter, + }); + }; + + auto views = std::vector>(); + for (const auto &[photo, fullId] : photos) { + if (const auto view = photo->createMediaView()) { + view->wanted(Data::PhotoSize::Large, fullId); + views.push_back(view); + } } - if (showToast) { - const auto filter = [lastPath](const auto ...) { - File::ShowInFolder(lastPath); - return false; + const auto finalCheck = [=] { + for (const auto &[photo, _] : photos) { + if (photo->loading()) { + return false; + } + } + return true; + }; + + const auto saveToFiles = [=] { + const auto fullPath = [&](int i) { + return filedialogDefaultName( + u"photo_"_q + QString::number(i), + u".jpg"_q, + path); }; - controller->showToast({ - .text = (photos.size() > 1 - ? tr::lng_mediaview_saved_images_to - : tr::lng_mediaview_saved_to)( - tr::now, - lt_downloads, - Ui::Text::Link( - tr::lng_mediaview_downloads(tr::now), - "internal:show_saved_message"), - Ui::Text::WithEntities), - .st = &st::defaultToast, - .filter = filter, - }); + auto lastPath = QString(); + for (auto i = 0; i < views.size(); i++) { + lastPath = fullPath(i + 1); + views[i]->saveToFile(lastPath); + } + if (showToast) { + showToast(lastPath); + } + }; + + if (finalCheck()) { + saveToFiles(); + } else { + auto lifetime = std::make_shared(); + session->downloaderTaskFinished( + ) | rpl::start_with_next([=]() mutable { + if (finalCheck()) { + saveToFiles(); + base::take(lifetime)->destroy(); + } + }, *lifetime); } }; const auto saveDocuments = [=](const QString &folderPath) {