Commit 3c062ad5 authored by Bharadwaj Raju's avatar Bharadwaj Raju 📚 Committed by Nate Graham
Browse files

Refactor and fix opening playlist files

Handle opening files through ElisaApplication::openFiles only, which
handles audio files and playlist files.

In the process, a lot of newly-unnecessary or duplicated code was removed.

BUG: 441544
FIXED-IN: 22.04
parent b2a43a35
Pipeline #103922 passed with stage
in 3 minutes and 51 seconds
......@@ -43,46 +43,6 @@ private Q_SLOTS:
qRegisterMetaType<ElisaUtils::PlayListEnqueueMode>("ElisaUtils::PlayListEnqueueMode");
qRegisterMetaType<ElisaUtils::PlayListEnqueueTriggerPlay>("ElisaUtils::PlayListEnqueueTriggerPlay");
}
void setArgumentsTest()
{
ElisaApplication myApp;
QSignalSpy argumentsChangedSpy(&myApp, &ElisaApplication::argumentsChanged);
QSignalSpy enqueueSpy(&myApp, &ElisaApplication::enqueue);
auto myTestFile = QFileInfo(QStringLiteral(LOCAL_FILE_TESTS_SAMPLE_FILES_PATH) + QStringLiteral("/music/test.ogg"));
auto myDirectory = QDir::current();
auto relativePath = myDirectory.relativeFilePath(myTestFile.canonicalFilePath());
myApp.setArguments({{{}, {}, QUrl::fromLocalFile(relativePath)}});
QCOMPARE(argumentsChangedSpy.count(), 1);
QCOMPARE(enqueueSpy.count(), 1);
QCOMPARE(myApp.arguments().count(), 1);
QCOMPARE(std::get<2>(myApp.arguments().at(0)), QUrl::fromLocalFile(myTestFile.canonicalFilePath()));
}
void activateRequestedTest()
{
ElisaApplication myApp;
QSignalSpy argumentsChangedSpy(&myApp, &ElisaApplication::argumentsChanged);
QSignalSpy enqueueSpy(&myApp, &ElisaApplication::enqueue);
auto myTestFile = QFileInfo(QStringLiteral(LOCAL_FILE_TESTS_SAMPLE_FILES_PATH) + QStringLiteral("/music/test.ogg"));
auto myDirectory = QDir::current();
auto relativePath = myDirectory.relativeFilePath(myTestFile.canonicalFilePath());
myApp.activateRequested({QString(), relativePath}, QDir::currentPath());
QCOMPARE(argumentsChangedSpy.count(), 0);
QCOMPARE(enqueueSpy.count(), 1);
QCOMPARE(enqueueSpy.at(0).count(), 3);
QCOMPARE(std::get<2>(enqueueSpy.at(0).at(0).value<DataTypes::EntryDataList>().at(0)), QUrl::fromLocalFile(myTestFile.canonicalFilePath()));
}
};
QTEST_MAIN(ElisaApplicationTests)
......
......@@ -383,8 +383,6 @@ ecm_generate_qmltypes(org.kde.elisa 1.0
if (Qt5Quick_FOUND AND Qt5Widgets_FOUND)
set(elisa_SOURCES
main.cpp
elisaarguments.cpp
elisaarguments.h
windows/WindowsTheme.qml
windows/PlatformIntegration.qml
......
......@@ -76,8 +76,6 @@ public:
KActionCollection mCollection;
#endif
DataTypes::EntryDataList mArguments;
std::unique_ptr<MusicListenersManager> mMusicManager;
std::unique_ptr<MediaPlayList> mMediaPlayList;
......@@ -224,38 +222,30 @@ void ElisaApplication::setupActions(const QString &actionName)
#endif
}
void ElisaApplication::setArguments(const DataTypes::EntryDataList &newArguments)
void ElisaApplication::openFiles(const QList<QUrl> &files)
{
if (d->mArguments == newArguments) {
return;
}
d->mArguments = checkFileListAndMakeAbsolute(newArguments, QDir::currentPath());
Q_EMIT argumentsChanged();
if (!d->mArguments.isEmpty()) {
Q_EMIT enqueue(d->mArguments,
ElisaUtils::PlayListEnqueueMode::AppendPlayList,
ElisaUtils::PlayListEnqueueTriggerPlay::TriggerPlay);
}
openFiles(files, QDir::currentPath());
}
void ElisaApplication::openFiles(const QList<QUrl> &files)
void ElisaApplication::openFiles(const QList<QUrl> &files, const QString &workingDirectory)
{
auto newArguments = DataTypes::EntryDataList{};
auto audioFiles = DataTypes::EntryDataList{};
const QMimeDatabase mimeDB;
for (const auto &file : files) {
const QMimeType mime = mimeDB.mimeTypeForUrl(file);
if (mime.inherits(QStringLiteral("audio/x-mpegurl"))) {
d->mMediaPlayListProxyModel->loadPlayList(file);
} else if (mime.name().startsWith(QStringLiteral("audio/"))) {
newArguments.push_back(DataTypes::EntryData{{{DataTypes::ElementTypeRole, ElisaUtils::FileName},
audioFiles.push_back(DataTypes::EntryData{{{DataTypes::ElementTypeRole, ElisaUtils::FileName},
{DataTypes::ResourceRole, file}}, {}, {}});
}
}
setArguments(newArguments);
// so that adding the same file more than once works
setArguments({});
audioFiles = checkFileListAndMakeAbsolute(audioFiles, workingDirectory);
if (!audioFiles.isEmpty()) {
Q_EMIT enqueue(audioFiles,
ElisaUtils::PlayListEnqueueMode::AppendPlayList,
ElisaUtils::PlayListEnqueueTriggerPlay::TriggerPlay);
}
}
void ElisaApplication::activateActionRequested(const QString &actionName, const QVariant &parameter)
......@@ -267,22 +257,16 @@ void ElisaApplication::activateActionRequested(const QString &actionName, const
void ElisaApplication::activateRequested(const QStringList &arguments, const QString &workingDirectory)
{
if (arguments.size() > 1) {
auto realArguments = DataTypes::EntryDataList{};
QList<QUrl> urls;
bool isFirst = true;
for (const auto &oneArgument : arguments) {
if (isFirst) {
isFirst = false;
continue;
}
realArguments.push_back({{{DataTypes::ElementTypeRole, ElisaUtils::Track},
{DataTypes::ResourceRole, QUrl::fromUserInput(oneArgument)}}, {}, {}});
urls.push_back(QUrl::fromUserInput(oneArgument));
}
Q_EMIT enqueue(checkFileListAndMakeAbsolute(realArguments, workingDirectory),
ElisaUtils::PlayListEnqueueMode::AppendPlayList,
ElisaUtils::PlayListEnqueueTriggerPlay::TriggerPlay);
openFiles(urls, workingDirectory);
}
}
......@@ -520,12 +504,6 @@ void ElisaApplication::initializePlayer()
d->mManageHeaderBar->setIsValidRole(MediaPlayList::IsValidRole);
QObject::connect(d->mMediaPlayListProxyModel.get(), &MediaPlayListProxyModel::currentTrackChanged, d->mManageHeaderBar.get(), &ManageHeaderBar::setCurrentTrack);
QObject::connect(d->mMediaPlayListProxyModel.get(), &MediaPlayListProxyModel::currentTrackDataChanged, d->mManageHeaderBar.get(), &ManageHeaderBar::updateCurrentTrackData);
if (!d->mArguments.isEmpty()) {
Q_EMIT enqueue(d->mArguments,
ElisaUtils::PlayListEnqueueMode::AppendPlayList,
ElisaUtils::PlayListEnqueueTriggerPlay::TriggerPlay);
}
}
QAction * ElisaApplication::action(const QString& name)
......@@ -582,11 +560,6 @@ void ElisaApplication::showInFolder(QUrl filePath)
#endif
}
const DataTypes::EntryDataList &ElisaApplication::arguments() const
{
return d->mArguments;
}
MusicListenersManager *ElisaApplication::musicManager() const
{
return d->mMusicManager.get();
......
......@@ -39,11 +39,6 @@ class ELISALIB_EXPORT ElisaApplication : public QObject
Q_OBJECT
Q_PROPERTY(DataTypes::EntryDataList arguments
READ arguments
WRITE setArguments
NOTIFY argumentsChanged)
Q_PROPERTY(ElisaUtils::PlayListEntryType embeddedView
READ embeddedView
NOTIFY embeddedViewChanged)
......@@ -117,8 +112,6 @@ public:
bool eventFilter(QObject *object, QEvent *event) override;
[[nodiscard]] const DataTypes::EntryDataList &arguments() const;
[[nodiscard]] MusicListenersManager *musicManager() const;
[[nodiscard]] MediaPlayList *mediaPlayList() const;
......@@ -147,8 +140,6 @@ public:
Q_SIGNALS:
void argumentsChanged();
void musicManagerChanged();
void mediaPlayListChanged();
......@@ -195,15 +186,14 @@ public Q_SLOTS:
void configureElisa();
void setArguments(const DataTypes::EntryDataList &newArguments);
void openFiles(const QList<QUrl> &files);
void openFiles(const QList<QUrl> &files, const QString &workingDirectory);
void activateActionRequested(const QString &actionName, const QVariant &parameter);
void activateRequested(const QStringList &arguments, const QString &workingDirectory);
void openRequested(const QList< QUrl > &uris);
void openRequested(const QList<QUrl> &uris);
void initialize();
......
/*
SPDX-FileCopyrightText: 2020 (c) Matthieu Gallien <matthieu_gallien@yahoo.fr>
SPDX-License-Identifier: LGPL-3.0-or-later
*/
#include "elisaarguments.h"
ElisaArguments::ElisaArguments(QObject *parent)
: QObject(parent)
{
}
#include "moc_elisaarguments.cpp"
/*
SPDX-FileCopyrightText: 2020 (c) Matthieu Gallien <matthieu_gallien@yahoo.fr>
SPDX-License-Identifier: LGPL-3.0-or-later
*/
#ifndef ELISAARGUMENTS_H
#define ELISAARGUMENTS_H
#include "datatypes.h"
#include <QObject>
class ElisaArguments : public QObject
{
Q_OBJECT
Q_PROPERTY(DataTypes::EntryDataList arguments READ arguments NOTIFY argumentsChanged)
public:
ElisaArguments(QObject *parent = nullptr);
[[nodiscard]] DataTypes::EntryDataList arguments() const
{
return mArguments;
}
Q_SIGNALS:
void argumentsChanged();
public Q_SLOTS:
void setArguments(DataTypes::EntryDataList arguments)
{
if (mArguments == arguments) {
return;
}
mArguments = std::move(arguments);
Q_EMIT argumentsChanged();
}
private:
DataTypes::EntryDataList mArguments;
};
#endif // ELISAARGUMENTS_H
......@@ -7,7 +7,6 @@
#include "config-upnp-qt.h"
#include "elisa-version.h"
#include "elisaarguments.h"
#include "elisaapplication.h"
#include "elisa_settings.h"
......@@ -149,26 +148,12 @@ int main(int argc, char *argv[])
engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
auto arguments = DataTypes::EntryDataList{};
auto realArgumentsList = parser.positionalArguments();
for (const auto &oneArgument : realArgumentsList) {
arguments.push_back(DataTypes::EntryData{{{DataTypes::ElementTypeRole, ElisaUtils::FileName},
{DataTypes::ResourceRole, QUrl::fromUserInput(oneArgument)}}, {}, {}});
QList<QUrl> urls;
for (const auto &oneArgument : parser.positionalArguments()) {
urls.push_back(QUrl::fromUserInput(oneArgument));
}
int typeId = qmlRegisterSingletonType<ElisaArguments>("org.kde.elisa.host", 1, 0, "ElisaArguments", [](QQmlEngine *qmlEngine, QJSEngine *scriptEngine) -> QObject* {
Q_UNUSED(qmlEngine)
Q_UNUSED(scriptEngine)
auto *result = new ElisaArguments;
return result;
});
auto *argumentsSingleton = engine.singletonInstance<ElisaArguments*>(typeId);
argumentsSingleton->setArguments(arguments);
engine.rootContext()->setContextProperty(QStringLiteral("elisaStartupArguments"), QVariant::fromValue(urls));
engine.load(QUrl(QStringLiteral("qrc:/qml/ElisaMainWindow.qml")));
......
......@@ -10,7 +10,6 @@ import QtQuick.Layouts 1.1
import QtQuick.Window 2.2
import org.kde.kirigami 2.5 as Kirigami
import org.kde.elisa 1.0
import org.kde.elisa.host 1.0
import Qt.labs.settings 1.0
import Qt.labs.platform 1.1
......@@ -433,7 +432,7 @@ Kirigami.ApplicationWindow {
mprisloader.active = true
ElisaApplication.arguments = ElisaArguments.arguments
ElisaApplication.openFiles(elisaStartupArguments)
// use global drawer on mobile
if (Kirigami.Settings.isMobile) {
......
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