Commit b6fea1ef authored by Matthieu Gallien's avatar Matthieu Gallien 🎵
Browse files

remove global variable elisa by using a qml singleton type

parent 526efc1b
......@@ -357,6 +357,8 @@ add_dependencies(elisaqmlplugin copy copy2)
if (Qt5Quick_FOUND AND Qt5Widgets_FOUND)
set(elisa_SOURCES
main.cpp
elisaarguments.cpp
elisaarguments.h
windows/WindowsTheme.qml
windows/PlatformIntegration.qml
......
......@@ -97,6 +97,8 @@ LocalBalooFileListing::LocalBalooFileListing(QObject *parent)
LocalBalooFileListing::~LocalBalooFileListing()
{
d->mDbusAdaptor->setParent(nullptr);
d->mDbusAdaptor->deleteLater();
}
void LocalBalooFileListing::applicationAboutToQuit()
......
......@@ -34,7 +34,8 @@
#include <KCoreAddons/KAboutData>
#include <QQmlApplicationEngine>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QGuiApplication>
#include <QDesktopServices>
#include <QDialog>
......@@ -90,7 +91,7 @@ public:
std::unique_ptr<ManageHeaderBar> mManageHeaderBar;
QQmlApplicationEngine *mEngine = nullptr;
QQmlEngine *mEngine = nullptr;
QFileSystemWatcher mConfigFileWatcher;
......@@ -273,7 +274,10 @@ void ElisaApplication::configureElisa()
return;
}
d->mEngine->load(QUrl(QStringLiteral("qrc:/qml/ElisaConfigurationDialog.qml")));
QQmlComponent configurationDialogComponent(d->mEngine,
QUrl(QStringLiteral("qrc:/qml/ElisaConfigurationDialog.qml")));
configurationDialogComponent.create();
}
void ElisaApplication::goBack() {}
......@@ -340,7 +344,7 @@ void ElisaApplication::initialize()
Q_EMIT initializationDone();
}
void ElisaApplication::setQmlEngine(QQmlApplicationEngine *engine)
void ElisaApplication::setQmlEngine(QQmlEngine *engine)
{
d->mEngine = engine;
}
......
......@@ -28,7 +28,7 @@ class AudioWrapper;
class ManageAudioPlayer;
class ManageMediaPlayerControl;
class ManageHeaderBar;
class QQmlApplicationEngine;
class QQmlEngine;
class ElisaApplicationPrivate;
class QSessionManager;
......@@ -167,7 +167,7 @@ public Q_SLOTS:
public:
void setQmlEngine(QQmlApplicationEngine *engine);
void setQmlEngine(QQmlEngine *engine);
private Q_SLOTS:
......
/*
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);
DataTypes::EntryDataList arguments() const
{
return mArguments;
}
Q_SIGNALS:
void argumentsChanged();
public Q_SLOTS:
void setArguments(DataTypes::EntryDataList arguments)
{
if (mArguments == arguments) {
return;
}
mArguments = arguments;
Q_EMIT argumentsChanged();
}
private:
DataTypes::EntryDataList mArguments;
};
#endif // ELISAARGUMENTS_H
......@@ -6,6 +6,8 @@
#include "elisaqmlplugin.h"
#include "config-upnp-qt.h"
#if defined UPNPQT_FOUND && UPNPQT_FOUND
#include "upnp/upnpcontrolconnectionmanager.h"
#include "upnp/upnpcontrolmediaserver.h"
......@@ -22,6 +24,10 @@
#include "upnpbasictypes.h"
#endif
#if defined KF5DBusAddons_FOUND && KF5DBusAddons_FOUND
#include <KDBusService>
#endif
#include "elisautils.h"
#include "elisaapplication.h"
#include "progressindicator.h"
......@@ -183,6 +189,26 @@ void ElisaQmlTestPlugin::registerTypes(const char *uri)
return new ElisaConfigurationDialog;
});
qmlRegisterUncreatableType<ElisaApplication>(uri, 1, 0, "ElisaApplication", QStringLiteral("only one and done in c++"));
qmlRegisterSingletonType<ElisaApplication>(uri, 1, 0, "ElisaApplication",
[](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject* {
Q_UNUSED(scriptEngine)
auto newApplication = std::make_unique<ElisaApplication>();
#if defined KF5DBusAddons_FOUND && KF5DBusAddons_FOUND
auto *elisaService = new KDBusService(KDBusService::Unique, newApplication.get());
#endif
#if defined KF5DBusAddons_FOUND && KF5DBusAddons_FOUND
QObject::connect(elisaService, &KDBusService::activateActionRequested, newApplication.get(), &ElisaApplication::activateActionRequested);
QObject::connect(elisaService, &KDBusService::activateRequested, newApplication.get(), &ElisaApplication::activateRequested);
QObject::connect(elisaService, &KDBusService::openRequested, newApplication.get(), &ElisaApplication::openRequested);
#endif
newApplication->setQmlEngine(engine);
return newApplication.release();
});
qmlRegisterUncreatableMetaObject(ElisaUtils::staticMetaObject, uri, 1, 0, "ElisaUtils", QStringLiteral("Namespace ElisaUtils"));
}
......@@ -7,6 +7,7 @@
#include "config-upnp-qt.h"
#include "elisa-version.h"
#include "elisaarguments.h"
#include "elisaapplication.h"
#include "elisa_settings.h"
......@@ -28,10 +29,6 @@
#include <KCrash>
#endif
#if defined KF5DBusAddons_FOUND && KF5DBusAddons_FOUND
#include <KDBusService>
#endif
#include <QIcon>
......@@ -40,6 +37,7 @@
#include <QStandardPaths>
#include <QQmlApplicationEngine>
#include <QJSEngine>
#include <QQmlFileSelector>
#include <QQuickStyle>
#include <QQmlContext>
......@@ -142,20 +140,6 @@ int main(int argc, char *argv[])
engine.rootContext()->setContextObject(new KLocalizedContext(&engine));
#if defined KF5DBusAddons_FOUND && KF5DBusAddons_FOUND
KDBusService elisaService(KDBusService::Unique);
#endif
std::unique_ptr<ElisaApplication> myApp = std::make_unique<ElisaApplication>();
myApp->setQmlEngine(&engine);
#if defined KF5DBusAddons_FOUND && KF5DBusAddons_FOUND
QObject::connect(&elisaService, &KDBusService::activateActionRequested, myApp.get(), &ElisaApplication::activateActionRequested);
QObject::connect(&elisaService, &KDBusService::activateRequested, myApp.get(), &ElisaApplication::activateRequested);
QObject::connect(&elisaService, &KDBusService::openRequested, myApp.get(), &ElisaApplication::openRequested);
#endif
auto arguments = DataTypes::EntryDataList{};
auto realArgumentsList = parser.positionalArguments();
......@@ -163,9 +147,18 @@ int main(int argc, char *argv[])
arguments.push_back(DataTypes::EntryData{{}, {}, QUrl(oneArgument)});
}
myApp->setArguments(arguments);
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);
engine.rootContext()->setContextProperty(QStringLiteral("elisa"), myApp.release());
argumentsSingleton->setArguments(arguments);
engine.load(QUrl(QStringLiteral("qrc:/qml/ElisaMainWindow.qml")));
......
......@@ -13,19 +13,19 @@ Menu {
id: applicationMenu
title: i18nc("open application menu", "Application Menu")
property var helpAction: elisa.action("help_contents")
property var quitApplication: elisa.action("file_quit")
property var reportBugAction: elisa.action("help_report_bug")
property var aboutAppAction: elisa.action("help_about_app")
property var configureShortcutsAction: elisa.action("options_configure_keybinding")
property var configureAction: elisa.action("options_configure")
property var helpAction: ElisaApplication.action("help_contents")
property var quitApplication: ElisaApplication.action("file_quit")
property var reportBugAction: ElisaApplication.action("help_report_bug")
property var aboutAppAction: ElisaApplication.action("help_about_app")
property var configureShortcutsAction: ElisaApplication.action("options_configure_keybinding")
property var configureAction: ElisaApplication.action("options_configure")
Action {
text: i18nc("Refresh Music Collection application menu entry", "Refresh Music Collection")
icon.name: "view-refresh"
onTriggered: {
applicationMenu.close()
elisa.musicManager.resetMusicData()
ElisaApplication.musicManager.resetMusicData()
}
}
......@@ -35,7 +35,7 @@ Menu {
Action {
text: configureAction.text
shortcut: configureAction.shortcut
icon.name: elisa.iconName(configureAction.icon)
icon.name: ElisaApplication.iconName(configureAction.icon)
onTriggered: {
applicationMenu.close()
configureAction.trigger()
......@@ -45,7 +45,7 @@ Menu {
Action {
text: configureShortcutsAction.text
shortcut: configureShortcutsAction.shortcut
icon.name: elisa.iconName(configureShortcutsAction.icon)
icon.name: ElisaApplication.iconName(configureShortcutsAction.icon)
onTriggered: {
applicationMenu.close()
configureShortcutsAction.trigger()
......@@ -59,7 +59,7 @@ Menu {
Action {
text: reportBugAction.text
shortcut: reportBugAction.shortcut
icon.name: elisa.iconName(reportBugAction.icon)
icon.name: ElisaApplication.iconName(reportBugAction.icon)
onTriggered: {
applicationMenu.close()
reportBugAction.trigger()
......@@ -73,7 +73,7 @@ Menu {
Action {
text: helpAction.text
shortcut: helpAction.shortcut
icon.name: elisa.iconName(helpAction.icon)
icon.name: ElisaApplication.iconName(helpAction.icon)
onTriggered: {
applicationMenu.close()
helpAction.trigger()
......@@ -83,7 +83,7 @@ Menu {
Action {
text: aboutAppAction.text
shortcut: aboutAppAction.shortcut
icon.name: elisa.iconName(aboutAppAction.icon)
icon.name: ElisaApplication.iconName(aboutAppAction.icon)
onTriggered: {
applicationMenu.close()
aboutAppAction.trigger()
......@@ -97,7 +97,7 @@ Menu {
Action {
text: quitApplication.text
shortcut: quitApplication.shortcut
icon.name: elisa.iconName(quitApplication.icon)
icon.name: ElisaApplication.iconName(quitApplication.icon)
onTriggered: quitApplication.trigger()
}
}
......@@ -254,8 +254,8 @@ RowLayout {
Layout.fillHeight: true
onStartPlayback: elisa.audioControl.ensurePlay()
onPausePlayback: elisa.audioControl.playPause()
onStartPlayback: ElisaApplication.audioControl.ensurePlay()
onPausePlayback: ElisaApplication.audioControl.playPause()
}
states: [
......@@ -320,13 +320,13 @@ RowLayout {
ContextView {
StackView.onActivated: viewManager.viewIsLoaded()
databaseId: elisa.manageHeaderBar.databaseId
trackType: elisa.manageHeaderBar.trackType
title: elisa.manageHeaderBar.title
artistName: elisa.manageHeaderBar.artist
albumName: elisa.manageHeaderBar.album
albumArtUrl: elisa.manageHeaderBar.image
fileUrl: elisa.manageHeaderBar.fileUrl
databaseId: ElisaApplication.manageHeaderBar.databaseId
trackType: ElisaApplication.manageHeaderBar.trackType
title: ElisaApplication.manageHeaderBar.title
artistName: ElisaApplication.manageHeaderBar.artist
albumName: ElisaApplication.manageHeaderBar.album
albumArtUrl: ElisaApplication.manageHeaderBar.image
fileUrl: ElisaApplication.manageHeaderBar.fileUrl
}
}
}
......@@ -28,7 +28,7 @@ FocusScope {
TrackContextMetaDataModel {
id: metaDataModel
manager: elisa.musicManager
manager: ElisaApplication.musicManager
}
ColumnLayout {
......@@ -244,7 +244,7 @@ FocusScope {
}
Connections {
target: elisa
target: ElisaApplication
onMusicManagerChanged: {
metaDataModel.initializeByIdAndUrl(trackType, databaseId, fileUrl)
......@@ -252,7 +252,7 @@ FocusScope {
}
Component.onCompleted: {
if (elisa.musicManager) {
if (ElisaApplication.musicManager) {
metaDataModel.initializeByIdAndUrl(trackType, databaseId, fileUrl)
}
}
......
......@@ -43,7 +43,7 @@ FocusScope {
return
}
if (!elisa.musicManager) {
if (!ElisaApplication.musicManager) {
return
}
......@@ -53,10 +53,10 @@ FocusScope {
proxyModel.sourceModel = realModel
proxyModel.dataType = modelType
proxyModel.playList = elisa.mediaPlayListProxyModel
proxyModel.playList = ElisaApplication.mediaPlayListProxyModel
gridView.contentModel = proxyModel
realModel.initializeByData(elisa.musicManager, elisa.musicManager.viewDatabase,
realModel.initializeByData(ElisaApplication.musicManager, ElisaApplication.musicManager.viewDatabase,
modelType, filterType, filter)
modelIsInitialized = true
......@@ -103,7 +103,7 @@ FocusScope {
}
Connections {
target: elisa
target: ElisaApplication
onMusicManagerChanged: initializeModel()
}
......
......@@ -70,7 +70,7 @@ FocusScope {
return
}
if (!elisa.musicManager) {
if (!ElisaApplication.musicManager) {
return
}
......@@ -80,14 +80,14 @@ FocusScope {
proxyModel.sourceModel = realModel
proxyModel.dataType = modelType
proxyModel.playList = elisa.mediaPlayListProxyModel
proxyModel.playList = ElisaApplication.mediaPlayListProxyModel
listView.contentModel = proxyModel
if (!displaySingleAlbum) {
proxyModel.sortModel(sortAscending)
}
realModel.initializeByData(elisa.musicManager, elisa.musicManager.viewDatabase,
realModel.initializeByData(ElisaApplication.musicManager, ElisaApplication.musicManager.viewDatabase,
modelType, filterType, filter)
}
......@@ -126,11 +126,11 @@ FocusScope {
isAlternateColor: (index % 2) === 1
detailedView: false
onEnqueue: elisa.mediaPlayListProxyModel.enqueue(model.fullData, model.display,
onEnqueue: ElisaApplication.mediaPlayListProxyModel.enqueue(model.fullData, model.display,
ElisaUtils.AppendPlayList,
ElisaUtils.DoNotTriggerPlay)
onReplaceAndPlay: elisa.mediaPlayListProxyModel.enqueue(model.fullData, model.display,
onReplaceAndPlay: ElisaApplication.mediaPlayListProxyModel.enqueue(model.fullData, model.display,
ElisaUtils.ReplacePlayList,
ElisaUtils.TriggerPlay)
......@@ -174,11 +174,11 @@ FocusScope {
isSelected: listView.currentIndex === index
isAlternateColor: (index % 2) === 1
onEnqueue: elisa.mediaPlayListProxyModel.enqueue(model.fullData, model.display,
onEnqueue: ElisaApplication.mediaPlayListProxyModel.enqueue(model.fullData, model.display,
ElisaUtils.AppendPlayList,
ElisaUtils.DoNotTriggerPlay)
onReplaceAndPlay: elisa.mediaPlayListProxyModel.enqueue(model.fullData, model.display,
onReplaceAndPlay: ElisaApplication.mediaPlayListProxyModel.enqueue(model.fullData, model.display,
ElisaUtils.ReplacePlayList,
ElisaUtils.TriggerPlay)
......@@ -232,7 +232,7 @@ FocusScope {
}
Connections {
target: elisa
target: ElisaApplication
onMusicManagerChanged: initializeModel()
}
......
......@@ -10,6 +10,7 @@ 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
......@@ -34,11 +35,11 @@ ApplicationWindow {
Accessible.role: Accessible.Application
Accessible.name: title
property var goBackAction: elisa.action("go_back")
property var seekAction: elisa.action("Seek")
property var scrubAction: elisa.action("Scrub")
property var playPauseAction: elisa.action("Play-Pause")
property var findAction: elisa.action("edit_find")
property var goBackAction: ElisaApplication.action("go_back")
property var seekAction: ElisaApplication.action("Seek")
property var scrubAction: ElisaApplication.action("Scrub")
property var playPauseAction: ElisaApplication.action("Play-Pause")
property var findAction: ElisaApplication.action("edit_find")
Action {
shortcut: goBackAction.shortcut
......@@ -47,17 +48,17 @@ ApplicationWindow {
Action {
shortcut: seekAction.shortcut
onTriggered: elisa.audioControl.seek(headerBar.playerControl.position + 10000)
onTriggered: ElisaApplication.audioControl.seek(headerBar.playerControl.position + 10000)
}
Action {
shortcut: scrubAction.shortcut
onTriggered: elisa.audioControl.seek(headerBar.playerControl.position - 10000)
onTriggered: ElisaApplication.audioControl.seek(headerBar.playerControl.position - 10000)
}
Action {
shortcut: playPauseAction.shortcut
onTriggered: elisa.audioControl.playPause()
onTriggered: ElisaApplication.audioControl.playPause()
}
Action {
......@@ -120,8 +121,8 @@ ApplicationWindow {
persistentSettings.width = mainWindow.width;
persistentSettings.height = mainWindow.height;
persistentSettings.playListState = elisa.mediaPlayListProxyModel.persistentState;
persistentSettings.audioPlayerState = elisa.audioControl.persistentState
persistentSettings.playListState = ElisaApplication.mediaPlayListProxyModel.persistentState;
persistentSettings.audioPlayerState = ElisaApplication.audioControl.persistentState
persistentSettings.playControlItemVolume = headerBar.playerControl.volume
persistentSettings.playControlItemMuted = headerBar.playerControl.muted
......@@ -139,13 +140,13 @@ ApplicationWindow {
sourceComponent: PlatformIntegration {
id: platformInterface
playListModel: elisa.mediaPlayListProxyModel
audioPlayerManager: elisa.audioControl
player: elisa.audioPlayer
headerBarManager: elisa.manageHeaderBar
manageMediaPlayerControl: elisa.playerControl
showProgressOnTaskBar: elisa.showProgressOnTaskBar
showSystemTrayIcon: elisa.showSystemTrayIcon
playListModel: ElisaApplication.mediaPlayListProxyModel
audioPlayerManager: ElisaApplication.audioControl
player: ElisaApplication.audioPlayer
headerBarManager: ElisaApplication.manageHeaderBar
manageMediaPlayerControl: ElisaApplication.playerControl
showProgressOnTaskBar: ElisaApplication.showProgressOnTaskBar
showSystemTrayIcon: ElisaApplication.showSystemTrayIcon
elisaMainWindow: mainWindow
onRaisePlayer: {
......@@ -158,9 +159,9 @@ ApplicationWindow {
}
Connections {
target: elisa.audioPlayer
onVolumeChanged: headerBar.playerControl.volume = elisa.audioPlayer.volume
onMutedChanged: headerBar.playerControl.muted = elisa.audioPlayer.muted
target: ElisaApplication.audioPlayer
onVolumeChanged: headerBar.playerControl.volume = ElisaApplication.audioPlayer.volume
onMutedChanged: headerBar.playerControl.muted = ElisaApplication.audioPlayer.muted
}
Rectangle {
......@@ -180,36 +181,36 @@ ApplicationWindow {
Layout.maximumHeight: mainWindow.height * 0.2 + elisaTheme.mediaPlayerControlHeight
Layout.fillWidth: true
tracksCount: elisa.mediaPlayListProxyModel.remainingTracks
album: (elisa.manageHeaderBar.album !== undefined ? elisa.manageHeaderBar.album : '')
title: elisa.manageHeaderBar.title
artist: (elisa.manageHeaderBar.artist !== undefined ? elisa.manageHeaderBar.artist : '')
albumArtist: (elisa.manageHeaderBar.albumArtist !== undefined ? elisa.manageHeaderBar.albumArtist : '')
image: elisa.manageHeaderBar.image
albumID: elisa.manageHeaderBar.albumId
tracksCount: ElisaApplication.mediaPlayListProxyModel.remainingTracks
album: (ElisaApplication.manageHeaderBar.album !== undefined ? ElisaApplication.manageHeaderBar.album : '')
title: ElisaApplication.manageHeaderBar.title
artist: (ElisaApplication.manageHeaderBar.artist !== undefined ? ElisaApplication.manageHeaderBar.artist : '')
albumArtist: (ElisaApplication.manageHeaderBar.albumArtist !== undefined ? ElisaApplication.manageHeaderBar.albumArtist : '')
image: ElisaApplication.manageHeaderBar.image
albumID: ElisaApplication.manageHeaderBar.albumId
ratingVisible: false
playerControl.duration: elisa.audioPlayer.duration
playerControl.seekable: elisa.audioPlayer.seekable
playerControl.duration: ElisaApplication.audioPlayer.duration
playerControl.seekable: ElisaApplication.audioPlayer.seekable
playerControl.volume: persistentSettings.playControlItemVolume
playerControl.muted: persistentSettings.playControlItemMuted
playerControl.position: elisa.audioPlayer.position
playerControl.skipBackwardEnabled: elisa.playerControl.skipBackwardControlEnabled
playerControl.skipForwardEnabled: elisa.playerControl.skipForwardControlEnabled
playerControl.playEnabled: elisa.playerControl.playControlEnabled
playerControl.isPlaying: elisa.playerControl.musicPlaying
playerControl.position: ElisaApplication.audioPlayer.position
playerControl.skipBackwardEnabled: ElisaApplication.playerControl.skipBackwardControlEnabled
playerControl.skipForwardEnabled: ElisaApplication.playerControl.skipForwardControlEnabled
playerControl.playEnabled: ElisaApplication.playerControl.playControlEnabled