2024-04-28 02:24:12 -04:00
|
|
|
/***************************************************************************
|
|
|
|
|
* Copyright (C) 2024 by Mike Conley - mike.conley@stickmud.com *
|
|
|
|
|
* *
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
|
* (at your option) any later version. *
|
|
|
|
|
* *
|
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
|
* *
|
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "TMediaPlaylist.h"
|
|
|
|
|
|
|
|
|
|
#include <QRandomGenerator>
|
|
|
|
|
|
|
|
|
|
TMediaPlaylist::TMediaPlaylist()
|
2025-04-17 02:22:26 +01:00
|
|
|
: mCurrentIndex(0)
|
|
|
|
|
, mPlaybackMode(Sequential)
|
|
|
|
|
{
|
|
|
|
|
}
|
2024-04-28 02:24:12 -04:00
|
|
|
|
2025-04-17 02:22:26 +01:00
|
|
|
TMediaPlaylist::~TMediaPlaylist() {}
|
2024-04-28 02:24:12 -04:00
|
|
|
|
2025-04-17 02:22:26 +01:00
|
|
|
void TMediaPlaylist::addMedia(const QUrl& url)
|
|
|
|
|
{
|
2024-04-28 02:24:12 -04:00
|
|
|
mMediaList.append(url);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-17 02:22:26 +01:00
|
|
|
void TMediaPlaylist::removeMedia(int startIndex, int endIndex)
|
|
|
|
|
{
|
2024-04-28 02:24:12 -04:00
|
|
|
if (endIndex >= startIndex && startIndex >= 0 && endIndex < mMediaList.size()) {
|
|
|
|
|
for (int i = endIndex; i >= startIndex; --i) {
|
|
|
|
|
mMediaList.removeAt(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-17 02:22:26 +01:00
|
|
|
void TMediaPlaylist::clear()
|
|
|
|
|
{
|
2024-04-28 02:24:12 -04:00
|
|
|
mMediaList.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-17 02:22:26 +01:00
|
|
|
int TMediaPlaylist::mediaCount() const
|
|
|
|
|
{
|
2024-04-28 02:24:12 -04:00
|
|
|
return mMediaList.count();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool TMediaPlaylist::isEmpty() const
|
|
|
|
|
{
|
|
|
|
|
return mMediaList.isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-17 02:22:26 +01:00
|
|
|
void TMediaPlaylist::setPlaybackMode(PlaybackMode mode)
|
|
|
|
|
{
|
2024-04-28 02:24:12 -04:00
|
|
|
mPlaybackMode = mode;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-17 02:22:26 +01:00
|
|
|
TMediaPlaylist::PlaybackMode TMediaPlaylist::playbackMode() const
|
|
|
|
|
{
|
2024-04-28 02:24:12 -04:00
|
|
|
return mPlaybackMode;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-17 02:22:26 +01:00
|
|
|
QUrl TMediaPlaylist::currentMedia() const
|
|
|
|
|
{
|
2024-04-28 02:24:12 -04:00
|
|
|
if (!mMediaList.isEmpty() && mCurrentIndex >= 0 && mCurrentIndex < mMediaList.size()) {
|
|
|
|
|
return mMediaList.at(mCurrentIndex);
|
|
|
|
|
}
|
|
|
|
|
return QUrl();
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-17 02:22:26 +01:00
|
|
|
bool TMediaPlaylist::setCurrentIndex(int index)
|
|
|
|
|
{
|
2024-04-28 02:24:12 -04:00
|
|
|
if (index >= 0 && index < mMediaList.size()) {
|
|
|
|
|
mCurrentIndex = index;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-17 02:22:26 +01:00
|
|
|
int TMediaPlaylist::currentIndex() const
|
|
|
|
|
{
|
2024-04-28 02:24:12 -04:00
|
|
|
return mCurrentIndex;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-17 02:22:26 +01:00
|
|
|
int TMediaPlaylist::nextIndex() const
|
|
|
|
|
{
|
2024-04-28 02:24:12 -04:00
|
|
|
if (mPlaybackMode == Loop && (mCurrentIndex + 1 == mMediaList.size())) {
|
|
|
|
|
return 0;
|
2026-07-18 18:39:57 +02:00
|
|
|
} else if (mPlaybackMode == Random) { // NOLINT(readability-else-after-return)
|
2024-04-28 02:24:12 -04:00
|
|
|
return QRandomGenerator::global()->bounded(mMediaList.size());
|
2026-07-18 18:39:57 +02:00
|
|
|
} else if (mCurrentIndex + 1 < mMediaList.size()) { // NOLINT(readability-else-after-return)
|
2024-04-28 02:24:12 -04:00
|
|
|
return mCurrentIndex + 1;
|
|
|
|
|
}
|
|
|
|
|
return -1; // No valid next index if out of range
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-17 02:22:26 +01:00
|
|
|
QUrl TMediaPlaylist::next()
|
|
|
|
|
{
|
2024-04-28 02:24:12 -04:00
|
|
|
if (mMediaList.isEmpty()) {
|
|
|
|
|
return QUrl();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mPlaybackMode == Loop && (mCurrentIndex + 1 == mMediaList.size())) {
|
|
|
|
|
mCurrentIndex = 0;
|
|
|
|
|
} else if (mPlaybackMode == Random) {
|
|
|
|
|
mCurrentIndex = QRandomGenerator::global()->bounded(mMediaList.size());
|
|
|
|
|
} else {
|
|
|
|
|
mCurrentIndex++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mCurrentIndex < mMediaList.size()) {
|
|
|
|
|
return mMediaList.at(mCurrentIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return QUrl();
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-17 02:22:26 +01:00
|
|
|
QUrl TMediaPlaylist::previous()
|
|
|
|
|
{
|
2024-04-28 02:24:12 -04:00
|
|
|
if (mPlaybackMode == Loop && mCurrentIndex == 0) {
|
|
|
|
|
mCurrentIndex = mMediaList.size() - 1;
|
|
|
|
|
} else {
|
|
|
|
|
mCurrentIndex--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mCurrentIndex >= 0) {
|
|
|
|
|
return mMediaList.at(mCurrentIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return QUrl();
|
|
|
|
|
}
|