Warn on quit if downloading files.

This commit is contained in:
John Preston 2022-03-09 15:30:22 +04:00
parent 6dd720b76e
commit 602e7a7164
5 changed files with 118 additions and 1 deletions

View file

@ -1953,6 +1953,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_box_leave" = "Leave";
"lng_upload_sure_stop" = "Are you sure you want to stop uploading your files?\n\nIf you do, you'll need to start over.";
"lng_download_sure_stop" = "Are you sure you want to stop downloading your files?\n\nIf you do, you'll need to start over.";
"lng_upload_show_file" = "Show file";
"lng_about_version" = "version {version}";

View file

@ -661,6 +661,10 @@ void Application::logoutWithChecks(Main::Account *account) {
_exportManager->stopWithConfirmation(retry);
} else if (account->session().uploadsInProgress()) {
account->session().uploadsStopWithConfirmation(retry);
} else if (_downloadManager->loadingInProgress(&account->session())) {
_downloadManager->loadingStopWithConfirmation(
retry,
&account->session());
} else {
logout(account);
}
@ -801,8 +805,18 @@ bool Application::uploadPreventsQuit() {
return false;
}
bool Application::downloadPreventsQuit() {
if (_downloadManager->loadingInProgress()) {
_downloadManager->loadingStopWithConfirmation([=] { Quit(); });
return true;
}
return false;
}
bool Application::preventsQuit(QuitReason reason) {
if (exportPreventsQuit() || uploadPreventsQuit()) {
if (exportPreventsQuit()
|| uploadPreventsQuit()
|| downloadPreventsQuit()) {
return true;
} else if (const auto window = activeWindow()) {
if (window->widget()->isActive()) {

View file

@ -255,6 +255,7 @@ public:
not_null<Main::Account*> account,
const TextWithEntities &explanation);
[[nodiscard]] bool uploadPreventsQuit();
[[nodiscard]] bool downloadPreventsQuit();
void checkLocalTime();
void lockByPasscode();
void unlockPasscode();

View file

@ -27,8 +27,12 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "core/mime_type.h"
#include "ui/controls/download_bar.h"
#include "ui/text/format_song_document_name.h"
#include "ui/layers/generic_box.h"
#include "storage/serialize_common.h"
#include "window/window_controller.h"
#include "window/window_session_controller.h"
#include "apiwrap.h"
#include "styles/style_layers.h"
namespace Data {
namespace {
@ -396,6 +400,93 @@ auto DownloadManager::loadingProgressValue() const
return _loadingProgress.value();
}
bool DownloadManager::loadingInProgress(Main::Session *onlyInSession) const {
return lookupLoadingItem(onlyInSession) != nullptr;
}
HistoryItem *DownloadManager::lookupLoadingItem(
Main::Session *onlyInSession) const {
constexpr auto find = [](const SessionData &data) {
constexpr auto proj = &DownloadingId::done;
const auto i = ranges::find(data.downloading, false, proj);
return (i != end(data.downloading)) ? i->object.item.get() : nullptr;
};
if (onlyInSession) {
const auto i = _sessions.find(onlyInSession);
return (i != end(_sessions)) ? find(i->second) : nullptr;
} else {
for (const auto &[session, data] : _sessions) {
if (const auto result = find(data)) {
return result;
}
}
}
return nullptr;
}
void DownloadManager::loadingStopWithConfirmation(
Fn<void()> callback,
Main::Session *onlyInSession) {
const auto window = Core::App().primaryWindow();
const auto item = lookupLoadingItem(onlyInSession);
if (!window || !item) {
return;
}
const auto weak = base::make_weak(&item->history()->session());
const auto id = item->fullId();
auto box = Box([=](not_null<Ui::GenericBox*> box) {
box->addRow(
object_ptr<Ui::FlatLabel>(
box.get(),
tr::lng_download_sure_stop(),
st::boxLabel),
st::boxPadding + QMargins(0, 0, 0, st::boxPadding.bottom()));
box->setStyle(st::defaultBox);
box->addButton(tr::lng_selected_upload_stop(), [=] {
box->closeBox();
if (!onlyInSession || weak.get()) {
loadingStop(onlyInSession);
}
if (callback) {
callback();
}
}, st::attentionBoxButton);
box->addButton(tr::lng_cancel(), [=] { box->closeBox(); });
box->addLeftButton(tr::lng_upload_show_file(), [=] {
box->closeBox();
if (const auto strong = weak.get()) {
if (const auto item = strong->data().message(id)) {
if (const auto window = strong->tryResolveWindow()) {
window->showPeerHistoryAtItem(item);
}
}
}
});
});
window->show(std::move(box));
window->activate();
}
void DownloadManager::loadingStop(Main::Session *onlyInSession) {
const auto stopInSession = [&](SessionData &data) {
while (!data.downloading.empty()) {
cancel(data, data.downloading.end() - 1);
}
};
if (onlyInSession) {
const auto i = _sessions.find(onlyInSession);
if (i != end(_sessions)) {
stopInSession(i->second);
}
} else {
for (auto &[session, data] : _sessions) {
stopInSession(data);
}
}
}
void DownloadManager::clearLoading() {
Expects(_loading.empty());

View file

@ -101,6 +101,12 @@ public:
[[nodiscard]] auto loadingProgressValue() const
-> rpl::producer<DownloadProgress>;
[[nodiscard]] bool loadingInProgress(
Main::Session *onlyInSession = nullptr) const;
void loadingStopWithConfirmation(
Fn<void()> callback,
Main::Session *onlyInSession = nullptr);
[[nodiscard]] auto loadedList()
-> ranges::any_view<const DownloadedId*, ranges::category::input>;
[[nodiscard]] auto loadedAdded() const
@ -156,6 +162,10 @@ private:
PhotoData *photo);
void generateEntry(not_null<Main::Session*> session, DownloadedId &id);
[[nodiscard]] HistoryItem *lookupLoadingItem(
Main::Session *onlyInSession) const;
void loadingStop(Main::Session *onlyInSession);
void writePostponed(not_null<Main::Session*> session);
[[nodiscard]] Fn<std::optional<QByteArray>()> serializator(
not_null<Main::Session*> session) const;