2024-07-20 23:18:42 +08:00
|
|
|
// SPDX-FileCopyrightText: 2024 Gary Wang <git@blumia.net>
|
2022-06-19 16:17:38 +08:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2021-02-09 14:19:09 +08:00
|
|
|
#include "playlistmanager.h"
|
|
|
|
|
|
|
|
#include <QCollator>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QUrl>
|
|
|
|
|
2024-07-20 23:18:42 +08:00
|
|
|
PlaylistModel::PlaylistModel(QObject *parent)
|
|
|
|
: QAbstractListModel(parent)
|
2021-02-09 14:19:09 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-07-20 23:18:42 +08:00
|
|
|
PlaylistModel::~PlaylistModel()
|
2021-02-09 14:19:09 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-07-20 23:18:42 +08:00
|
|
|
void PlaylistModel::setPlaylist(const QList<QUrl> &urls)
|
2021-02-09 14:19:09 +08:00
|
|
|
{
|
2024-07-20 23:18:42 +08:00
|
|
|
beginResetModel();
|
|
|
|
m_playlist = urls;
|
|
|
|
endResetModel();
|
2021-02-09 14:19:09 +08:00
|
|
|
}
|
|
|
|
|
2024-07-20 23:18:42 +08:00
|
|
|
QModelIndex PlaylistModel::loadPlaylist(const QList<QUrl> & urls)
|
2021-02-09 14:19:09 +08:00
|
|
|
{
|
2024-07-20 23:18:42 +08:00
|
|
|
if (urls.isEmpty()) return QModelIndex();
|
|
|
|
if (urls.count() == 1) {
|
|
|
|
return loadPlaylist(urls.constFirst());
|
|
|
|
} else {
|
|
|
|
setPlaylist(urls);
|
2024-07-28 16:14:14 +08:00
|
|
|
return index(0);
|
2024-07-20 23:18:42 +08:00
|
|
|
}
|
2021-02-09 14:19:09 +08:00
|
|
|
}
|
|
|
|
|
2024-07-20 23:18:42 +08:00
|
|
|
QModelIndex PlaylistModel::loadPlaylist(const QUrl &url)
|
2021-02-26 00:57:12 +08:00
|
|
|
{
|
2024-07-20 23:18:42 +08:00
|
|
|
QFileInfo info(url.toLocalFile());
|
|
|
|
QDir dir(info.path());
|
|
|
|
QString && currentFileName = info.fileName();
|
|
|
|
|
|
|
|
if (dir.path() == m_currentDir) {
|
2024-07-28 16:14:14 +08:00
|
|
|
int idx = indexOf(url);
|
|
|
|
return idx == -1 ? appendToPlaylist(url) : index(idx);
|
2024-07-20 23:18:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList entryList = dir.entryList(
|
|
|
|
m_autoLoadSuffixes,
|
|
|
|
QDir::Files | QDir::NoSymLinks, QDir::NoSort);
|
|
|
|
|
|
|
|
QCollator collator;
|
|
|
|
collator.setNumericMode(true);
|
|
|
|
|
|
|
|
std::sort(entryList.begin(), entryList.end(), collator);
|
|
|
|
|
|
|
|
QList<QUrl> playlist;
|
|
|
|
|
2024-07-28 16:14:14 +08:00
|
|
|
int idx = -1;
|
2024-07-20 23:18:42 +08:00
|
|
|
for (int i = 0; i < entryList.count(); i++) {
|
|
|
|
const QString & fileName = entryList.at(i);
|
|
|
|
const QString & oneEntry = dir.absoluteFilePath(fileName);
|
|
|
|
const QUrl & url = QUrl::fromLocalFile(oneEntry);
|
|
|
|
playlist.append(url);
|
|
|
|
if (fileName == currentFileName) {
|
2024-07-28 16:14:14 +08:00
|
|
|
idx = i;
|
2024-07-20 23:18:42 +08:00
|
|
|
}
|
|
|
|
}
|
2024-07-28 16:14:14 +08:00
|
|
|
if (idx == -1) {
|
|
|
|
idx = playlist.count();
|
2024-07-20 23:18:42 +08:00
|
|
|
playlist.append(url);
|
|
|
|
}
|
|
|
|
m_currentDir = dir.path();
|
|
|
|
|
|
|
|
setPlaylist(playlist);
|
|
|
|
|
2024-07-28 16:14:14 +08:00
|
|
|
return index(idx);
|
2021-02-26 00:57:12 +08:00
|
|
|
}
|
|
|
|
|
2024-07-20 23:18:42 +08:00
|
|
|
QModelIndex PlaylistModel::appendToPlaylist(const QUrl &url)
|
2021-02-26 00:57:12 +08:00
|
|
|
{
|
2024-07-20 23:18:42 +08:00
|
|
|
const int lastIndex = rowCount();
|
|
|
|
beginInsertRows(QModelIndex(), lastIndex, lastIndex);
|
|
|
|
m_playlist.append(url);
|
|
|
|
endInsertRows();
|
2024-07-28 16:14:14 +08:00
|
|
|
return index(lastIndex);
|
2021-02-26 00:57:12 +08:00
|
|
|
}
|
|
|
|
|
2024-07-20 23:18:42 +08:00
|
|
|
bool PlaylistModel::removeAt(int index)
|
2021-02-09 14:19:09 +08:00
|
|
|
{
|
2024-07-20 23:18:42 +08:00
|
|
|
if (index < 0 || index >= rowCount()) return false;
|
|
|
|
beginRemoveRows(QModelIndex(), index, index);
|
|
|
|
m_playlist.removeAt(index);
|
|
|
|
endRemoveRows();
|
|
|
|
return true;
|
2021-02-09 14:19:09 +08:00
|
|
|
}
|
|
|
|
|
2024-07-20 23:18:42 +08:00
|
|
|
int PlaylistModel::indexOf(const QUrl &url) const
|
2021-02-09 14:19:09 +08:00
|
|
|
{
|
2024-07-20 23:18:42 +08:00
|
|
|
return m_playlist.indexOf(url);
|
2021-02-09 14:19:09 +08:00
|
|
|
}
|
|
|
|
|
2024-07-21 00:52:32 +08:00
|
|
|
QUrl PlaylistModel::urlByIndex(int index) const
|
|
|
|
{
|
|
|
|
return m_playlist.value(index);
|
|
|
|
}
|
|
|
|
|
2024-07-20 23:18:42 +08:00
|
|
|
QStringList PlaylistModel::autoLoadFilterSuffixes() const
|
2021-02-09 14:19:09 +08:00
|
|
|
{
|
2024-07-20 23:18:42 +08:00
|
|
|
return m_autoLoadSuffixes;
|
|
|
|
}
|
2021-02-09 14:19:09 +08:00
|
|
|
|
2024-07-28 16:14:14 +08:00
|
|
|
QHash<int, QByteArray> PlaylistModel::roleNames() const
|
2024-07-20 23:18:42 +08:00
|
|
|
{
|
2024-07-28 16:14:14 +08:00
|
|
|
QHash<int, QByteArray> result = QAbstractListModel::roleNames();
|
|
|
|
result.insert(UrlRole, "url");
|
|
|
|
return result;
|
2024-07-20 23:18:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int PlaylistModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
return m_playlist.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant PlaylistModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid()) return QVariant();
|
|
|
|
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
return m_playlist.at(index.row()).fileName();
|
|
|
|
case UrlRole:
|
|
|
|
return m_playlist.at(index.row());
|
2021-02-09 14:19:09 +08:00
|
|
|
}
|
|
|
|
|
2024-07-20 23:18:42 +08:00
|
|
|
return QVariant();
|
2021-02-09 14:19:09 +08:00
|
|
|
}
|
|
|
|
|
2024-07-20 23:18:42 +08:00
|
|
|
PlaylistManager::PlaylistManager(QObject *parent)
|
|
|
|
: QObject(parent)
|
2021-02-09 14:19:09 +08:00
|
|
|
{
|
2024-07-20 23:18:42 +08:00
|
|
|
connect(&m_model, &PlaylistModel::rowsRemoved, this,
|
|
|
|
[this](const QModelIndex &, int, int) {
|
|
|
|
if (m_model.rowCount() <= m_currentIndex) {
|
|
|
|
setProperty("currentIndex", m_currentIndex - 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
auto onRowCountChanged = [this](){
|
|
|
|
emit totalCountChanged(m_model.rowCount());
|
|
|
|
};
|
|
|
|
|
|
|
|
connect(&m_model, &PlaylistModel::rowsInserted, this, onRowCountChanged);
|
|
|
|
connect(&m_model, &PlaylistModel::rowsRemoved, this, onRowCountChanged);
|
|
|
|
connect(&m_model, &PlaylistModel::modelReset, this, onRowCountChanged);
|
2021-02-09 14:19:09 +08:00
|
|
|
}
|
|
|
|
|
2024-07-20 23:18:42 +08:00
|
|
|
PlaylistManager::~PlaylistManager()
|
2021-02-09 14:19:09 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-07-21 00:52:32 +08:00
|
|
|
PlaylistModel *PlaylistManager::model()
|
2024-04-15 01:16:00 +08:00
|
|
|
{
|
2024-07-20 23:18:42 +08:00
|
|
|
return &m_model;
|
|
|
|
}
|
2024-04-15 01:16:00 +08:00
|
|
|
|
2024-07-20 23:18:42 +08:00
|
|
|
void PlaylistManager::setPlaylist(const QList<QUrl> &urls)
|
|
|
|
{
|
|
|
|
m_model.setPlaylist(urls);
|
2024-04-15 01:16:00 +08:00
|
|
|
}
|
|
|
|
|
2024-07-20 23:18:42 +08:00
|
|
|
QModelIndex PlaylistManager::loadPlaylist(const QList<QUrl> &urls)
|
2021-02-09 14:19:09 +08:00
|
|
|
{
|
2024-07-20 23:18:42 +08:00
|
|
|
QModelIndex idx = m_model.loadPlaylist(urls);
|
|
|
|
setProperty("currentIndex", idx.row());
|
|
|
|
return idx;
|
2021-02-09 14:19:09 +08:00
|
|
|
}
|
|
|
|
|
2024-07-21 00:52:32 +08:00
|
|
|
QModelIndex PlaylistManager::loadPlaylist(const QUrl &url)
|
|
|
|
{
|
|
|
|
QModelIndex idx = m_model.loadPlaylist(url);
|
|
|
|
setProperty("currentIndex", idx.row());
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2024-07-20 23:18:42 +08:00
|
|
|
int PlaylistManager::totalCount() const
|
2022-03-11 16:21:56 +08:00
|
|
|
{
|
2024-07-20 23:18:42 +08:00
|
|
|
return m_model.rowCount();
|
2022-03-11 16:21:56 +08:00
|
|
|
}
|
|
|
|
|
2024-07-20 23:18:42 +08:00
|
|
|
QModelIndex PlaylistManager::previousIndex() const
|
2021-02-09 14:19:09 +08:00
|
|
|
{
|
2024-07-20 23:18:42 +08:00
|
|
|
int count = totalCount();
|
|
|
|
if (count == 0) return QModelIndex();
|
2021-02-09 14:19:09 +08:00
|
|
|
|
2024-07-28 16:14:14 +08:00
|
|
|
return m_model.index(m_currentIndex - 1 < 0 ? count - 1 : m_currentIndex - 1);
|
2021-02-09 14:19:09 +08:00
|
|
|
}
|
|
|
|
|
2024-07-20 23:18:42 +08:00
|
|
|
QModelIndex PlaylistManager::nextIndex() const
|
2021-02-09 14:19:09 +08:00
|
|
|
{
|
2024-07-20 23:18:42 +08:00
|
|
|
int count = totalCount();
|
|
|
|
if (count == 0) return QModelIndex();
|
2021-02-09 14:19:09 +08:00
|
|
|
|
2024-07-28 16:14:14 +08:00
|
|
|
return m_model.index(m_currentIndex + 1 == count ? 0 : m_currentIndex + 1);
|
2021-02-09 14:19:09 +08:00
|
|
|
}
|
|
|
|
|
2024-07-20 23:18:42 +08:00
|
|
|
QModelIndex PlaylistManager::curIndex() const
|
2021-02-09 14:19:09 +08:00
|
|
|
{
|
2024-07-28 16:14:14 +08:00
|
|
|
return m_model.index(m_currentIndex);
|
2024-07-20 23:18:42 +08:00
|
|
|
}
|
2021-02-09 14:19:09 +08:00
|
|
|
|
2024-07-20 23:18:42 +08:00
|
|
|
void PlaylistManager::setCurrentIndex(const QModelIndex &index)
|
|
|
|
{
|
|
|
|
if (index.isValid() && index.row() >= 0 && index.row() < totalCount()) {
|
|
|
|
setProperty("currentIndex", index.row());
|
|
|
|
}
|
2021-02-09 14:19:09 +08:00
|
|
|
}
|
|
|
|
|
2024-07-20 23:18:42 +08:00
|
|
|
QUrl PlaylistManager::urlByIndex(const QModelIndex &index)
|
2021-02-09 14:19:09 +08:00
|
|
|
{
|
2024-07-21 00:52:32 +08:00
|
|
|
return m_model.urlByIndex(index.row());
|
2024-07-20 23:18:42 +08:00
|
|
|
}
|
2021-02-09 14:19:09 +08:00
|
|
|
|
2024-07-20 23:18:42 +08:00
|
|
|
QString PlaylistManager::localFileByIndex(const QModelIndex &index)
|
|
|
|
{
|
|
|
|
return urlByIndex(index).toLocalFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PlaylistManager::removeAt(const QModelIndex &index)
|
|
|
|
{
|
|
|
|
return m_model.removeAt(index.row());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlaylistManager::setAutoLoadFilterSuffixes(const QStringList &nameFilters)
|
|
|
|
{
|
|
|
|
m_model.setProperty("autoLoadFilterSuffixes", nameFilters);
|
2021-02-09 14:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QList<QUrl> PlaylistManager::convertToUrlList(const QStringList &files)
|
|
|
|
{
|
|
|
|
QList<QUrl> urlList;
|
2023-11-25 02:18:37 +08:00
|
|
|
for (const QString & str : std::as_const(files)) {
|
2021-02-09 14:19:09 +08:00
|
|
|
QUrl url = QUrl::fromLocalFile(str);
|
|
|
|
if (url.isValid()) {
|
|
|
|
urlList.append(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return urlList;
|
|
|
|
}
|