From fd138f65caa5c67bd7811576da69f4ed3f29ba65 Mon Sep 17 00:00:00 2001 From: Martchus Date: Thu, 17 Aug 2017 18:55:17 +0200 Subject: [PATCH] Add method to find available languages --- mediafileinfo.cpp | 35 ++++++++++++++++++++++++++++------- mediafileinfo.h | 2 ++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/mediafileinfo.cpp b/mediafileinfo.cpp index 3aebae2..4ea26ee 100644 --- a/mediafileinfo.cpp +++ b/mediafileinfo.cpp @@ -810,7 +810,7 @@ bool MediaFileInfo::hasTracksOfType(MediaType type) const if(m_singleTrack && m_singleTrack->mediaType() == type) { return true; } else if(m_container) { - for(size_t i = 0, count = m_container->trackCount(); i < count; ++i) { + for(size_t i = 0, count = m_container->trackCount(); i != count; ++i) { if(m_container->track(i)->mediaType() == type) { return true; } @@ -831,15 +831,36 @@ bool MediaFileInfo::hasTracksOfType(MediaType type) const */ ChronoUtilities::TimeSpan MediaFileInfo::duration() const { - TimeSpan res; if(m_container) { - res = m_container->duration(); - } else { - for(const AbstractTrack *track : tracks()) { - if(track->duration() > res) { - res = track->duration(); + return m_container->duration(); + } else if(m_singleTrack) { + return m_singleTrack->duration(); + } + return TimeSpan(); +} + +/*! + * \brief Determines the available languages for specified media type (by default MediaType::Audio). + * + * If \a type is MediaType::Unknown, all media types are considered. + * + * parseTracks() needs to be called before. Otherwise this + * method always returns an empty set. + * + * \sa parseTracks() + */ +unordered_set MediaFileInfo::availableLanguages(MediaType type) const +{ + unordered_set res; + if(m_container) { + for(size_t i = 0, count = m_container->trackCount(); i != count; ++i) { + const AbstractTrack *track = m_container->track(i); + if((type == MediaType::Unknown || track->mediaType() == type) && !track->language().empty() && track->language() != "und") { + res.emplace(track->language()); } } + } else if(m_singleTrack && (type == MediaType::Unknown || m_singleTrack->mediaType() == type) && !m_singleTrack->language().empty() && m_singleTrack->language() != "und") { + res.emplace(m_singleTrack->language()); } return res; } diff --git a/mediafileinfo.h b/mediafileinfo.h index 44328f4..20cbff9 100644 --- a/mediafileinfo.h +++ b/mediafileinfo.h @@ -7,6 +7,7 @@ #include "./abstractcontainer.h" #include +#include #include namespace Media { @@ -95,6 +96,7 @@ public: std::vector tracks() const; bool hasTracksOfType(Media::MediaType type) const; ChronoUtilities::TimeSpan duration() const; + std::unordered_set availableLanguages(Media::MediaType type = Media::MediaType::Audio) const; bool areTracksSupported() const; // ... the tags ParsingStatus tagsParsingStatus() const;