refactor: use extensible design for fetching playlist from single file

This commit is contained in:
2026-07-25 12:06:11 +08:00
parent 4ed491c78a
commit f55f8c731d
9 changed files with 146 additions and 76 deletions
+7 -43
View File
@@ -3,7 +3,7 @@
// SPDX-License-Identifier: MIT
#include "playlistmanager.h"
#include "winplaylistpatch.h"
#include "playlistscout.h"
#include <QCollator>
#include <QDir>
@@ -72,50 +72,14 @@ bool PlaylistManager::loadSingleFilePlaylist(const QUrl& url) {
if (!checkUrl(url))
return false;
QList<QUrl> playlist;
std::optional<qsizetype> idx = std::nullopt;
#ifdef Q_OS_WIN
// In Windows, Qt approach can not aware Windows Explorer custom sort order.
// So we need to try using Win32 functions for fetching it at first
if (!WinPlaylistPatch::loadSingleFilePlaylist(url, m_allowedSuffixes, playlist, idx)) {
#endif // Q_OS_WIN
// If Windows approach is failed or we are not in Windows,
// use Qt way to process it.
QFileInfo info(url.toLocalFile());
QDir dir(info.path());
QString currentFileName = info.fileName();
QStringList entryList = dir.entryList(
m_allowedSuffixes, QDir::Files | QDir::NoSymLinks, QDir::NoSort);
QCollator collator;
collator.setNumericMode(true);
std::sort(entryList.begin(), entryList.end(), collator);
playlist.clear();
idx = std::nullopt;
for (qsizetype i = 0; i < entryList.count(); i++) {
const QString &fileName = entryList.at(i);
const QString &oneEntry = dir.absoluteFilePath(fileName);
const QUrl &url = QUrl::fromLocalFile(oneEntry);
if (!checkUrl(url)) {
return false;
}
playlist.append(url);
if (fileName == currentFileName) {
idx = i;
}
}
#ifdef Q_OS_WIN
// Scout playlist by given file
auto scoutResult = PlaylistScout::scout(url, m_allowedSuffixes);
if (!scoutResult.has_value()) {
return false;
}
#endif // Q_OS_WIN
setPlaylist(std::move(playlist), idx);
// Setup playlist with index
setPlaylist(std::move(scoutResult->playlist), scoutResult->indexHint);
return true;
}