From 3d54a263b8e31a4628d543a1b9d8da9a62bae63f Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 23 Oct 2020 15:22:38 +0300 Subject: [PATCH] Stop playing documents when items are deleted. --- Telegram/SourceFiles/data/data_session.cpp | 10 ++++++++++ Telegram/SourceFiles/data/data_session.h | 2 ++ Telegram/SourceFiles/history/history.cpp | 10 ++++++++++ Telegram/SourceFiles/mainwidget.cpp | 5 +++++ .../media/player/media_player_instance.cpp | 16 +++++++++++++--- .../media/player/media_player_instance.h | 1 + .../media/view/media_view_overlay_widget.cpp | 7 +++++++ Telegram/lib_spellcheck | 2 +- 8 files changed, 49 insertions(+), 4 deletions(-) diff --git a/Telegram/SourceFiles/data/data_session.cpp b/Telegram/SourceFiles/data/data_session.cpp index b748359af..3a186cc30 100644 --- a/Telegram/SourceFiles/data/data_session.cpp +++ b/Telegram/SourceFiles/data/data_session.cpp @@ -31,6 +31,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "storage/storage_account.h" #include "storage/storage_encrypted_file.h" #include "media/player/media_player_instance.h" // instance()->play() +#include "media/audio/media_audio.h" #include "boxes/abstract_box.h" #include "passport/passport_form_controller.h" #include "window/themes/window_theme.h" @@ -3333,6 +3334,15 @@ void Session::unregisterContactItem( } } +void Session::documentMessageRemoved(not_null document) { + if (_documentItems.find(document) != _documentItems.end()) { + return; + } + if (document->loading()) { + document->cancel(); + } +} + void Session::checkPlayingAnimations() { auto check = base::flat_set>(); for (const auto view : _heavyViewParts) { diff --git a/Telegram/SourceFiles/data/data_session.h b/Telegram/SourceFiles/data/data_session.h index d0337ca86..52807a6e8 100644 --- a/Telegram/SourceFiles/data/data_session.h +++ b/Telegram/SourceFiles/data/data_session.h @@ -547,6 +547,8 @@ public: UserId contactId, not_null item); + void documentMessageRemoved(not_null document); + void checkPlayingAnimations(); HistoryItem *findWebPageItem(not_null page) const; diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp index f640822d9..7c625f9e7 100644 --- a/Telegram/SourceFiles/history/history.cpp +++ b/Telegram/SourceFiles/history/history.cpp @@ -26,6 +26,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_channel.h" #include "data/data_chat.h" #include "data/data_user.h" +#include "data/data_document.h" #include "data/data_histories.h" #include "lang/lang_keys.h" #include "apiwrap.h" @@ -423,6 +424,11 @@ void History::destroyMessage(not_null item) { session().api().cancelLocalItem(item); } + const auto document = [&] { + const auto media = item->media(); + return media ? media->document() : nullptr; + }(); + owner().unregisterMessage(item); Core::App().notifications().clearFromItem(item); @@ -432,6 +438,10 @@ void History::destroyMessage(not_null item) { Assert(i != end(_messages)); _messages.erase(i); + + if (document) { + session().data().documentMessageRemoved(document); + } } not_null History::addNewItem( diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index c0855a983..2c3b51af3 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -349,6 +349,11 @@ MainWidget::MainWidget( if (!songState.id || IsStoppedOrStopping(songState.state)) { closeBothPlayers(); } + } else if (type == AudioMsgId::Type::Song) { + const auto songState = Media::Player::instance()->getState(AudioMsgId::Type::Song); + if (!songState.id) { + closeBothPlayers(); + } } }); diff --git a/Telegram/SourceFiles/media/player/media_player_instance.cpp b/Telegram/SourceFiles/media/player/media_player_instance.cpp index c96d8940f..673f81490 100644 --- a/Telegram/SourceFiles/media/player/media_player_instance.cpp +++ b/Telegram/SourceFiles/media/player/media_player_instance.cpp @@ -203,10 +203,14 @@ void Instance::setSession(not_null data, Main::Session *session) { ) | rpl::start_with_next([=] { setSession(data, nullptr); }, data->sessionLifetime); + session->data().itemRemoved( + ) | rpl::filter([=](not_null item) { + return (data->current.contextId() == item->fullId()); + }) | rpl::start_with_next([=] { + stopAndClear(data); + }, data->sessionLifetime); } else { - stop(data->type); - _tracksFinishedNotifier.notify(data->type); - *data = Data(data->type, data->overview); + stopAndClear(data); } } @@ -527,6 +531,12 @@ void Instance::stop(AudioMsgId::Type type) { } } +void Instance::stopAndClear(not_null data) { + stop(data->type); + _tracksFinishedNotifier.notify(data->type); + *data = Data(data->type, data->overview); +} + void Instance::playPause(AudioMsgId::Type type) { if (const auto data = getData(type)) { if (!data->streamed) { diff --git a/Telegram/SourceFiles/media/player/media_player_instance.h b/Telegram/SourceFiles/media/player/media_player_instance.h index 2f2997e8e..07cba2a5b 100644 --- a/Telegram/SourceFiles/media/player/media_player_instance.h +++ b/Telegram/SourceFiles/media/player/media_player_instance.h @@ -217,6 +217,7 @@ private: void playlistUpdated(not_null data); bool moveInPlaylist(not_null data, int delta, bool autonext); HistoryItem *itemByIndex(not_null data, int index); + void stopAndClear(not_null data); void handleStreamingUpdate( not_null data, diff --git a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp index fd2088e9b..a4ed8e449 100644 --- a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp +++ b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp @@ -3674,6 +3674,13 @@ void OverlayWidget::setSession(not_null session) { changingMsgId(change.item, change.oldId); }, _sessionLifetime); + session->data().itemRemoved( + ) | rpl::filter([=](not_null item) { + return (_document != nullptr) && (item->fullId() == _msgid); + }) | rpl::start_with_next([=] { + close(); + }, _sessionLifetime); + session->account().sessionChanges( ) | rpl::start_with_next([=] { clearSession(); diff --git a/Telegram/lib_spellcheck b/Telegram/lib_spellcheck index 58263c071..053e44a10 160000 --- a/Telegram/lib_spellcheck +++ b/Telegram/lib_spellcheck @@ -1 +1 @@ -Subproject commit 58263c0715ebfbf586c71e856a2ad962301bdc05 +Subproject commit 053e44a10107de4a0b7e9ad8826150c577db2a2b