Commit 1f400cf3 authored by Matthieu Gallien's avatar Matthieu Gallien

fix enqueue from Files browser to be more reliable

parent 8527f46b
......@@ -7,19 +7,16 @@
#include "filebrowserproxymodel.h"
#include "elisautils.h"
#include "filebrowsermodel.h"
#include "mediaplaylistproxymodel.h"
#include "elisautils.h"
#include "models/modelLogging.h"
#include <KIO/ListJob>
#include <KIO/UDSEntry>
#include <KCoreAddons/KJob>
#include <stack>
FileBrowserProxyModel::FileBrowserProxyModel(QObject *parent) : KDirSortFilterProxyModel(parent)
FileBrowserProxyModel::FileBrowserProxyModel(QObject *parent)
: KDirSortFilterProxyModel(parent)
{
setFilterCaseSensitivity(Qt::CaseInsensitive);
setSortFoldersFirst(true);
......@@ -50,18 +47,62 @@ void FileBrowserProxyModel::setFilterText(const QString &filterText)
Q_EMIT filterTextChanged(mFilterText);
}
void FileBrowserProxyModel::listRecursiveResult(KJob *)
{
if (mPendingEntries.empty()) {
mEnqueueInProgress = false;
Q_EMIT entriesToEnqueue(mAllData, mEnqueueMode, mTriggerPlay);
return;
}
recursiveEnqueue();
}
void FileBrowserProxyModel::listRecursiveNewEntries(KIO::Job *job, const KIO::UDSEntryList &list)
{
Q_UNUSED(job)
for (const auto &oneEntry : list) {
if (oneEntry.isDir()) {
continue;
}
auto returnedPath = oneEntry.stringValue(KIO::UDSEntry::UDS_NAME);
auto fullPath = QStringLiteral("%0/%1").arg(mCuurentUrl.toString(), returnedPath);
auto fullPathUrl = QUrl { fullPath };
auto mimeType = mMimeDatabase.mimeTypeForUrl(fullPathUrl);
if (!mimeType.name().startsWith(QLatin1String("audio/"))) {
continue;
}
mAllData.push_back(DataTypes::EntryData { { { DataTypes::ElementTypeRole, ElisaUtils::FileName } }, fullPath, fullPathUrl });
}
}
void FileBrowserProxyModel::genericEnqueueToPlayList(QModelIndex rootIndex,
ElisaUtils::PlayListEnqueueMode enqueueMode,
ElisaUtils::PlayListEnqueueTriggerPlay triggerPlay)
{
bool firstTime = true;
if (mEnqueueInProgress) {
// display error
return;
}
mPendingEntries = {};
mAllData.clear();
for (int rowIndex = 0, maxRowCount = rowCount(); rowIndex < maxRowCount; ++rowIndex) {
auto currentIndex = index(rowIndex, 0, rootIndex);
const auto rootUrl = data(currentIndex, DataTypes::FilePathRole).toUrl();
recursiveEnqueue(rootUrl, enqueueMode, triggerPlay, firstTime);
mPendingEntries.emplace(currentIndex.data(DataTypes::FilePathRole).toUrl(),
currentIndex.data(DataTypes::ElementTypeRole).value<ElisaUtils::PlayListEntryType>() == ElisaUtils::Container);
}
mEnqueueInProgress = true;
mEnqueueMode = enqueueMode;
mTriggerPlay = triggerPlay;
recursiveEnqueue();
}
void FileBrowserProxyModel::enqueueToPlayList(QModelIndex rootIndex)
......@@ -78,9 +119,21 @@ void FileBrowserProxyModel::enqueue(const DataTypes::MusicDataType &newEntry,
{
Q_UNUSED(newEntryTitle)
bool firstTime = true;
if (mEnqueueInProgress) {
// display error
return;
}
mPendingEntries = {};
mAllData.clear();
recursiveEnqueue(newEntry[DataTypes::FilePathRole].toUrl(), enqueueMode, triggerPlay, firstTime);
mPendingEntries.emplace(newEntry[DataTypes::FilePathRole].toUrl(),
newEntry.elementType() == ElisaUtils::Container);
mEnqueueInProgress = true;
mEnqueueMode = enqueueMode;
mTriggerPlay = triggerPlay;
recursiveEnqueue();
}
void FileBrowserProxyModel::replaceAndPlayOfPlayList(QModelIndex rootIndex)
......@@ -94,7 +147,7 @@ void FileBrowserProxyModel::disconnectPlayList()
{
if (mPlayList) {
disconnect(this, &FileBrowserProxyModel::entriesToEnqueue,
mPlayList, static_cast<void(MediaPlayListProxyModel::*)(const DataTypes::EntryDataList&, ElisaUtils::PlayListEnqueueMode, ElisaUtils::PlayListEnqueueTriggerPlay)>(&MediaPlayListProxyModel::enqueue));
mPlayList, static_cast<void (MediaPlayListProxyModel::*)(const DataTypes::EntryDataList &, ElisaUtils::PlayListEnqueueMode, ElisaUtils::PlayListEnqueueTriggerPlay)>(&MediaPlayListProxyModel::enqueue));
}
}
......@@ -102,63 +155,42 @@ void FileBrowserProxyModel::connectPlayList()
{
if (mPlayList) {
connect(this, &FileBrowserProxyModel::entriesToEnqueue,
mPlayList, static_cast<void(MediaPlayListProxyModel::*)(const DataTypes::EntryDataList&, ElisaUtils::PlayListEnqueueMode, ElisaUtils::PlayListEnqueueTriggerPlay)>(&MediaPlayListProxyModel::enqueue));
mPlayList, static_cast<void (MediaPlayListProxyModel::*)(const DataTypes::EntryDataList &, ElisaUtils::PlayListEnqueueMode, ElisaUtils::PlayListEnqueueTriggerPlay)>(&MediaPlayListProxyModel::enqueue));
}
}
void FileBrowserProxyModel::recursiveEnqueue(const QUrl &rootUrl,
ElisaUtils::PlayListEnqueueMode enqueueMode,
ElisaUtils::PlayListEnqueueTriggerPlay triggerPlay,
bool &firstTime)
void FileBrowserProxyModel::recursiveEnqueue()
{
auto *job = KIO::listRecursive(rootUrl, {KIO::HideProgressInfo});
connect(job, &KJob::result,
this, [](KJob*) {
});
auto [rootUrl, isDirectory] = mPendingEntries.front();
mPendingEntries.pop();
if (isDirectory) {
mCuurentUrl = rootUrl;
mCurrentJob = KIO::listRecursive(rootUrl, { KIO::HideProgressInfo });
connect(job, &KIO::ListJob::entries,
this, [rootUrl, this, &enqueueMode, &triggerPlay, &firstTime](KIO::Job *job, const KIO::UDSEntryList &list) {
Q_UNUSED(job)
connect(mCurrentJob, &KJob::result, this, &FileBrowserProxyModel::listRecursiveResult);
auto allData = DataTypes::EntryDataList{};
connect(dynamic_cast<KIO::ListJob*>(mCurrentJob), &KIO::ListJob::entries,
this, &FileBrowserProxyModel::listRecursiveNewEntries);
} else {
if (mPlayList) {
mAllData.push_back(DataTypes::EntryData { { { DataTypes::ElementTypeRole, ElisaUtils::FileName } }, rootUrl.toString(), rootUrl });
for (const auto &oneEntry : list) {
if (oneEntry.isDir()) {
continue;
if (mPendingEntries.empty()) {
mEnqueueInProgress = false;
Q_EMIT entriesToEnqueue(mAllData, mEnqueueMode, mTriggerPlay);
return;
}
auto returnedPath = oneEntry.stringValue(KIO::UDSEntry::UDS_NAME);
auto fullPath = QStringLiteral("%0/%1").arg(rootUrl.toString(), returnedPath);
auto fullPathUrl = QUrl{fullPath};
auto mimeType = mMimeDatabase.mimeTypeForUrl(fullPathUrl);
if (!mimeType.name().startsWith(QLatin1String("audio/"))) {
continue;
}
allData.push_back(DataTypes::EntryData{{{DataTypes::ElementTypeRole, ElisaUtils::FileName}}, fullPath, fullPathUrl});
}
if (!firstTime) {
enqueueMode = ElisaUtils::PlayListEnqueueMode::AppendPlayList;
triggerPlay = ElisaUtils::PlayListEnqueueTriggerPlay::DoNotTriggerPlay;
recursiveEnqueue();
}
if (!allData.isEmpty()) {
Q_EMIT entriesToEnqueue(allData, enqueueMode, triggerPlay);
firstTime = false;
}
});
}
}
void FileBrowserProxyModel::setSourceModel(QAbstractItemModel *sourceModel)
{
KDirSortFilterProxyModel::setSourceModel(sourceModel);
auto fileBrowserModel = dynamic_cast<FileBrowserModel*>(sourceModel);
auto fileBrowserModel = dynamic_cast<FileBrowserModel *>(sourceModel);
if (!fileBrowserModel) {
return;
......
......@@ -14,10 +14,14 @@
#include "elisautils.h"
#include <KDirSortFilterProxyModel>
#include <KCoreAddons/KJob>
#include <KIO/ListJob>
#include <KIO/UDSEntry>
#include <QMimeDatabase>
#include <QRegularExpression>
#include <queue>
#include <memory>
class MediaPlayListProxyModel;
......@@ -93,6 +97,12 @@ Q_SIGNALS:
protected:
private Q_SLOTS:
void listRecursiveResult(KJob *job);
void listRecursiveNewEntries(KIO::Job *job, const KIO::UDSEntryList &list);
private:
void genericEnqueueToPlayList(QModelIndex rootIndex,
......@@ -103,9 +113,7 @@ private:
void connectPlayList();
void recursiveEnqueue(const QUrl &rootUrl,
ElisaUtils::PlayListEnqueueMode enqueueMode,
ElisaUtils::PlayListEnqueueTriggerPlay triggerPlay, bool &firstTime);
void recursiveEnqueue();
QString mFilterText;
......@@ -117,6 +125,20 @@ private:
QMimeDatabase mMimeDatabase;
bool mEnqueueInProgress = false;
std::queue<std::tuple<QUrl, bool>> mPendingEntries;
DataTypes::EntryDataList mAllData;
ElisaUtils::PlayListEnqueueMode mEnqueueMode;
ElisaUtils::PlayListEnqueueTriggerPlay mTriggerPlay;
QUrl mCuurentUrl;
KIO::Job *mCurrentJob = nullptr;
};
#endif // FILEBROWSERPROXYMODEL_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment