From 01d763eed12e1f632713a20104129560f5aad984 Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 6 Mar 2019 12:21:42 +0400 Subject: [PATCH] Use DocumentData::getDuration for all types. --- Telegram/SourceFiles/apiwrap.cpp | 2 +- Telegram/SourceFiles/data/data_document.cpp | 11 +++++++++-- Telegram/SourceFiles/data/data_document.h | 2 +- .../history/media/history_media_common.cpp | 11 ++++++----- .../SourceFiles/history/media/history_media_gif.cpp | 4 ++-- .../history/media/history_media_video.cpp | 2 +- .../inline_bots/inline_bot_layout_internal.cpp | 8 ++------ .../media/streaming/media_streaming_player.cpp | 8 +------- .../media/view/media_view_overlay_widget.cpp | 12 +----------- Telegram/SourceFiles/overview/overview_layout.cpp | 6 ++---- Telegram/SourceFiles/storage/serialize_document.cpp | 2 +- 11 files changed, 27 insertions(+), 41 deletions(-) diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index 6c63bbe8b..fbd6fa3fa 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -108,7 +108,7 @@ MTPVector ComposeSendingDocumentAttributes( const auto dimensions = document->dimensions; auto attributes = QVector(1, filenameAttribute); if (dimensions.width() > 0 && dimensions.height() > 0) { - const auto duration = document->duration(); + const auto duration = document->getDuration(); if (duration >= 0) { auto flags = MTPDdocumentAttributeVideo::Flags(0); if (document->isVideoMessage()) { diff --git a/Telegram/SourceFiles/data/data_document.cpp b/Telegram/SourceFiles/data/data_document.cpp index 246b91f7b..08a842b0f 100644 --- a/Telegram/SourceFiles/data/data_document.cpp +++ b/Telegram/SourceFiles/data/data_document.cpp @@ -1397,8 +1397,15 @@ bool DocumentData::isVideoFile() const { return (type == VideoDocument); } -int32 DocumentData::duration() const { - return (isAnimation() || isVideoFile()) ? _duration : -1; +TimeId DocumentData::getDuration() const { + if (const auto song = this->song()) { + return std::max(song->duration, 0); + } else if (const auto voice = this->voice()) { + return std::max(voice->duration, 0); + } else if (isAnimation() || isVideoFile()) { + return std::max(_duration, 0); + } + return -1; } bool DocumentData::isImage() const { diff --git a/Telegram/SourceFiles/data/data_document.h b/Telegram/SourceFiles/data/data_document.h index fe6093047..8e4417566 100644 --- a/Telegram/SourceFiles/data/data_document.h +++ b/Telegram/SourceFiles/data/data_document.h @@ -163,7 +163,7 @@ public: [[nodiscard]] bool isGifv() const; [[nodiscard]] bool isTheme() const; [[nodiscard]] bool isSharedMediaMusic() const; - [[nodiscard]] int32 duration() const; + [[nodiscard]] TimeId getDuration() const; [[nodiscard]] bool isImage() const; void recountIsImage(); [[nodiscard]] bool supportsStreaming() const; diff --git a/Telegram/SourceFiles/history/media/history_media_common.cpp b/Telegram/SourceFiles/history/media/history_media_common.cpp index f069ae277..67d1562eb 100644 --- a/Telegram/SourceFiles/history/media/history_media_common.cpp +++ b/Telegram/SourceFiles/history/media/history_media_common.cpp @@ -21,14 +21,15 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL int documentMaxStatusWidth(DocumentData *document) { auto result = st::normalFont->width(formatDownloadText(document->size, document->size)); + const auto duration = document->getDuration(); if (const auto song = document->song()) { - accumulate_max(result, st::normalFont->width(formatPlayedText(song->duration, song->duration))); - accumulate_max(result, st::normalFont->width(formatDurationAndSizeText(song->duration, document->size))); + accumulate_max(result, st::normalFont->width(formatPlayedText(duration, duration))); + accumulate_max(result, st::normalFont->width(formatDurationAndSizeText(duration, document->size))); } else if (const auto voice = document->voice()) { - accumulate_max(result, st::normalFont->width(formatPlayedText(voice->duration, voice->duration))); - accumulate_max(result, st::normalFont->width(formatDurationAndSizeText(voice->duration, document->size))); + accumulate_max(result, st::normalFont->width(formatPlayedText(duration, duration))); + accumulate_max(result, st::normalFont->width(formatDurationAndSizeText(duration, document->size))); } else if (document->isVideoFile()) { - accumulate_max(result, st::normalFont->width(formatDurationAndSizeText(document->duration(), document->size))); + accumulate_max(result, st::normalFont->width(formatDurationAndSizeText(duration, document->size))); } else { accumulate_max(result, st::normalFont->width(formatSizeText(document->size))); } diff --git a/Telegram/SourceFiles/history/media/history_media_gif.cpp b/Telegram/SourceFiles/history/media/history_media_gif.cpp index 358748a9f..c56a406b3 100644 --- a/Telegram/SourceFiles/history/media/history_media_gif.cpp +++ b/Telegram/SourceFiles/history/media/history_media_gif.cpp @@ -729,7 +729,7 @@ void HistoryGif::setStatusSize(int newSize) const { if (newSize < 0) { _statusText = formatDurationText(-newSize - 1); } else { - _statusText = formatDurationText(_data->duration()); + _statusText = formatDurationText(_data->getDuration()); } } else { HistoryFileMedia::setStatusSize(newSize, _data->size, -2, 0); @@ -749,7 +749,7 @@ void HistoryGif::updateStatusText() const { } else if (_data->loaded()) { statusSize = FileStatusSizeLoaded; if (const auto video = activeRoundPlayer()) { - statusSize = -1 - _data->duration(); + statusSize = -1 - _data->getDuration(); const auto type = AudioMsgId::Type::Voice; const auto state = Media::Player::instance()->getState(type); diff --git a/Telegram/SourceFiles/history/media/history_media_video.cpp b/Telegram/SourceFiles/history/media/history_media_video.cpp index 9f311b839..fb5ded111 100644 --- a/Telegram/SourceFiles/history/media/history_media_video.cpp +++ b/Telegram/SourceFiles/history/media/history_media_video.cpp @@ -518,7 +518,7 @@ void HistoryVideo::validateGroupedCache( } void HistoryVideo::setStatusSize(int newSize) const { - HistoryFileMedia::setStatusSize(newSize, _data->size, _data->duration(), 0); + HistoryFileMedia::setStatusSize(newSize, _data->size, _data->getDuration(), 0); } TextWithEntities HistoryVideo::selectedText(TextSelection selection) const { diff --git a/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp b/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp index fa96e0f5e..9b84ff0b1 100644 --- a/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp +++ b/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp @@ -73,12 +73,8 @@ int FileBase::content_height() const { int FileBase::content_duration() const { if (const auto document = getShownDocument()) { - if (document->duration() > 0) { - return document->duration(); - } else if (const auto song = document->song()) { - if (song->duration) { - return song->duration; - } + if (document->getDuration() > 0) { + return document->getDuration(); } } return getResultDuration(); diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_player.cpp b/Telegram/SourceFiles/media/streaming/media_streaming_player.cpp index 327ed88d6..5b40b1e78 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_player.cpp +++ b/Telegram/SourceFiles/media/streaming/media_streaming_player.cpp @@ -670,13 +670,7 @@ Media::Player::TrackState Player::prepareLegacyState() const { result.length = _totalDuration; if (result.length == kTimeUnknown) { const auto document = _options.audioId.audio(); - const auto duration = !document - ? crl::time(0) - : document->song() - ? document->song()->duration - : document->voice() - ? document->voice()->duration - : document->duration(); + const auto duration = document ? document->getDuration() : 0; if (duration > 0) { result.length = duration * crl::time(1000); } else { diff --git a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp index 1831cdf0c..d9b38158d 100644 --- a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp +++ b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp @@ -2292,17 +2292,7 @@ void OverlayWidget::updatePlaybackState() { if (videoIsGifv()) { return; } - auto state = _streamed->player.prepareLegacyState(); - if (state.length == kTimeUnknown) { - const auto duration = _doc->song() - ? _doc->song()->duration - : _doc->duration(); - if (duration > 0) { - state.length = std::max( - duration * crl::time(1000), - crl::time(state.position)); - } - } + const auto state = _streamed->player.prepareLegacyState(); if (state.position != kTimeUnknown && state.length != kTimeUnknown) { _streamed->controls.updatePlayback(state); } diff --git a/Telegram/SourceFiles/overview/overview_layout.cpp b/Telegram/SourceFiles/overview/overview_layout.cpp index 561760590..4bd1a8561 100644 --- a/Telegram/SourceFiles/overview/overview_layout.cpp +++ b/Telegram/SourceFiles/overview/overview_layout.cpp @@ -400,7 +400,7 @@ Video::Video( not_null video) : RadialProgressItem(parent) , _data(video) -, _duration(formatDurationText(_data->duration())) { +, _duration(formatDurationText(_data->getDuration())) { setDocumentLinks(_data); _data->loadThumbnail(parent->fullId()); } @@ -837,9 +837,7 @@ void Voice::updateName() { } int Voice::duration() const { - return _data->voice() - ? _data->voice()->duration - : std::max(_data->duration(), 0); + return std::max(_data->getDuration(), 0); } bool Voice::updateStatusText() { diff --git a/Telegram/SourceFiles/storage/serialize_document.cpp b/Telegram/SourceFiles/storage/serialize_document.cpp index f442299f7..8b0626919 100644 --- a/Telegram/SourceFiles/storage/serialize_document.cpp +++ b/Telegram/SourceFiles/storage/serialize_document.cpp @@ -48,7 +48,7 @@ void Document::writeToStream(QDataStream &stream, DocumentData *document) { } writeStorageImageLocation(stream, document->sticker()->loc); } else { - stream << qint32(document->duration()); + stream << qint32(document->getDuration()); if (const auto thumb = document->thumbnail()) { writeStorageImageLocation(stream, thumb->location()); } else {