2021-07-31 13:06:17 +00:00
|
|
|
/*
|
|
|
|
* Sailfin: a Jellyfin client written using Qt
|
|
|
|
* Copyright (C) 2021 Chris Josten and the Sailfin Contributors.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
#include "JellyfinQt/model/playlist.h"
|
|
|
|
|
|
|
|
#include "JellyfinQt/model/shuffle.h"
|
|
|
|
|
|
|
|
namespace Jellyfin {
|
|
|
|
namespace Model {
|
|
|
|
|
|
|
|
Playlist::Playlist(QObject *parent)
|
|
|
|
: QObject(parent),
|
|
|
|
m_shuffler(new NoShuffle(this)){}
|
|
|
|
|
|
|
|
void Playlist::clearList() {
|
2021-08-21 20:01:13 +00:00
|
|
|
emit beforeListCleared();
|
2021-07-31 13:06:17 +00:00
|
|
|
m_currentItem.clear();
|
|
|
|
m_nextItem.clear();
|
|
|
|
m_list.clear();
|
|
|
|
emit listCleared();
|
|
|
|
}
|
|
|
|
|
2021-08-30 23:29:51 +00:00
|
|
|
bool Playlist::hasPrevious() {
|
|
|
|
return m_shuffler->hasPrevious();
|
|
|
|
}
|
|
|
|
|
2021-07-31 13:06:17 +00:00
|
|
|
void Playlist::previous() {
|
|
|
|
m_shuffler->previous();
|
|
|
|
int curItem = m_shuffler->currentItem();
|
|
|
|
if (curItem >= 0) {
|
|
|
|
m_currentItem = m_list[curItem];
|
|
|
|
} else {
|
|
|
|
m_currentItem.clear();
|
|
|
|
}
|
|
|
|
int nextItem = m_shuffler->nextItem();
|
2021-09-09 20:16:39 +00:00
|
|
|
if (nextItem >= 0) {
|
2021-07-31 13:06:17 +00:00
|
|
|
m_nextItem = m_list[m_shuffler->nextItem()];
|
|
|
|
} else {
|
|
|
|
m_nextItem.clear();
|
|
|
|
}
|
|
|
|
m_nextItemFromQueue = !m_queue.isEmpty();
|
|
|
|
m_currentItemFromQueue = false;
|
2021-08-21 20:01:13 +00:00
|
|
|
emit currentItemChanged();
|
2021-07-31 13:06:17 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 23:29:51 +00:00
|
|
|
bool Playlist::hasNext() {
|
|
|
|
return m_shuffler->hasNext();
|
|
|
|
}
|
|
|
|
|
2021-07-31 13:06:17 +00:00
|
|
|
void Playlist::next() {
|
|
|
|
// Determine the new current item
|
|
|
|
if (!m_queue.isEmpty()) {
|
|
|
|
m_currentItem = m_queue.first();
|
2021-08-21 20:01:13 +00:00
|
|
|
emit beforeItemsRemovedFromQueue(0, 1);
|
2021-07-31 13:06:17 +00:00
|
|
|
m_queue.removeFirst();
|
2021-08-21 20:01:13 +00:00
|
|
|
emit itemsRemovedFromQueue();
|
2021-07-31 13:06:17 +00:00
|
|
|
m_currentItemFromQueue = true;
|
|
|
|
} else if (!m_list.isEmpty()) {
|
|
|
|
// The queue is empty
|
|
|
|
m_currentItemFromQueue = false;
|
|
|
|
m_shuffler->next();
|
|
|
|
int next = m_shuffler->currentItem();
|
|
|
|
if (next >= 0) {
|
|
|
|
m_currentItem = m_list[next];
|
|
|
|
} else {
|
|
|
|
m_currentItem.clear();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m_currentItem.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the new next item
|
|
|
|
if (!m_queue.isEmpty()) {
|
|
|
|
m_nextItem = m_queue.first();
|
|
|
|
m_queue.removeFirst();
|
|
|
|
m_nextItemFromQueue = true;
|
|
|
|
} else if (!m_list.isEmpty()) {
|
|
|
|
// The queue is empty
|
|
|
|
m_nextItemFromQueue = false;
|
|
|
|
int next = m_shuffler->nextItem();
|
|
|
|
if (next >= 0) {
|
|
|
|
m_nextItem = m_list[next];
|
|
|
|
} else {
|
|
|
|
m_nextItem.clear();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m_nextItem.clear();
|
|
|
|
}
|
2021-08-21 20:01:13 +00:00
|
|
|
emit currentItemChanged();
|
2021-07-31 13:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QSharedPointer<const Item> Playlist::listAt(int index) const {
|
2021-08-21 20:01:13 +00:00
|
|
|
if (m_shuffler->canShuffleInAdvance()) {
|
|
|
|
return m_list.at(m_shuffler->itemAt(index));
|
|
|
|
} else {
|
|
|
|
return m_list.at(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QSharedPointer<const Item> Playlist::queueAt(int index) const {
|
|
|
|
return m_queue.at(index);
|
2021-07-31 13:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QSharedPointer<Item> Playlist::currentItem() {
|
|
|
|
return m_currentItem;
|
|
|
|
}
|
|
|
|
|
2021-08-21 20:01:13 +00:00
|
|
|
int Playlist::currentItemIndexInList() const {
|
|
|
|
if (m_currentItemFromQueue) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return m_shuffler->currentItem();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-31 13:06:17 +00:00
|
|
|
QSharedPointer<Item> Playlist::nextItem() {
|
|
|
|
return m_nextItem;
|
|
|
|
}
|
|
|
|
|
2021-08-11 21:35:33 +00:00
|
|
|
void Playlist::appendToList(ViewModel::ItemModel &model) {
|
2021-07-31 13:06:17 +00:00
|
|
|
int start = m_list.size();
|
|
|
|
int count = model.size();
|
|
|
|
m_list.reserve(count);
|
2021-08-21 20:01:13 +00:00
|
|
|
emit beforeItemsAddedToList(start, count);
|
2021-07-31 13:06:17 +00:00
|
|
|
for (int i = 0; i < count; i++) {
|
2021-08-11 21:35:33 +00:00
|
|
|
m_list.append(QSharedPointer<Model::Item>(model.at(i)));
|
2021-07-31 13:06:17 +00:00
|
|
|
}
|
2021-08-21 20:01:13 +00:00
|
|
|
emit itemsAddedToList();
|
2021-07-31 13:06:17 +00:00
|
|
|
reshuffle();
|
|
|
|
}
|
|
|
|
|
2021-09-09 03:57:41 +00:00
|
|
|
void Playlist::appendToList(QSharedPointer<Model::Item> item) {
|
|
|
|
int start = m_list.size();
|
|
|
|
emit beforeItemsAddedToList(start, 1);
|
|
|
|
m_list.append(item);
|
|
|
|
emit itemsAddedToList();
|
|
|
|
reshuffle();
|
|
|
|
}
|
|
|
|
|
2021-07-31 13:06:17 +00:00
|
|
|
void Playlist::reshuffle() {
|
|
|
|
if (m_shuffler->canShuffleInAdvance()) {
|
|
|
|
m_shuffler->shuffleInAdvance();
|
|
|
|
} else {
|
|
|
|
m_shuffler->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_nextItemFromQueue) {
|
|
|
|
int nextItemIdx = m_shuffler->nextItem();
|
|
|
|
if (nextItemIdx >= 0) {
|
|
|
|
m_nextItem = m_list[m_shuffler->nextItem()];
|
|
|
|
} else {
|
|
|
|
m_nextItem.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
emit listReshuffled();
|
2021-08-21 20:01:13 +00:00
|
|
|
emit currentItemChanged();
|
2021-07-31 13:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Playlist::play(int index) {
|
|
|
|
m_shuffler->setIndex(index);
|
|
|
|
if (!m_nextItemFromQueue) {
|
|
|
|
int nextItemIdx = m_shuffler->nextItem();
|
|
|
|
if (nextItemIdx >= 0) {
|
|
|
|
m_nextItem = m_list[m_shuffler->nextItem()];
|
|
|
|
} else {
|
|
|
|
m_nextItem.clear();
|
|
|
|
}
|
|
|
|
}
|
2021-08-21 20:33:23 +00:00
|
|
|
m_currentItem = m_list[m_shuffler->currentItem()];
|
2021-08-21 20:01:13 +00:00
|
|
|
emit currentItemChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Playlist::playingFromQueue() const {
|
|
|
|
return m_currentItemFromQueue;
|
2021-07-31 13:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // NS Model
|
|
|
|
} // NS Jellyfin
|