kutils: add support for setting the player ID to media classes

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2021-02-26 09:55:47 +02:00
parent d8693b19b6
commit 9d120c519d
3 changed files with 61 additions and 27 deletions

View file

@ -8,5 +8,5 @@ X-KDE-Library=kaudioplayer
X-KDE-DBus-ModuleName=kaudioplayer
X-KDE-Kded-autoload=false
X-KDE-Kded-load-on-demand=true
X-KDE-MediaPlayer=true
X-KDE-MediaPlayer=kded_kaudioplayer
OnlyShowIn=KDE;

View file

@ -20,6 +20,7 @@
#include "klocale.h"
#include "ksettings.h"
#include "kmediaplayer.h"
#include <QApplication>
#if defined(HAVE_MPV)
@ -36,11 +37,20 @@ static bool s_fullscreen = false;
pre-processor definitions are used to share the code as much as possible making modification
easier.
*/
#define COMMON_STATE_LOAD \
d->m_settings->sync(); \
const QString globalaudio = d->m_settings->value("global/audiooutput", "auto").toString(); \
const int globalvolume = d->m_settings->value("global/volume", 90).toInt(); \
const bool globalmute = d->m_settings->value("global/mute", false).toBool(); \
setAudioOutput(d->m_settings->value(d->m_playerid + "/audiooutput", globalaudio).toString()); \
setVolume(d->m_settings->value(d->m_playerid + "/volume", globalvolume).toInt()); \
setMute(d->m_settings->value(d->m_playerid + "/mute", globalmute).toBool());
#define COMMON_STATE_SAVE \
if (d->m_handle && d->m_settings && d->m_settings->isWritable()) { \
d->m_settings->setValue(d->m_appname + "/audiooutput", audiooutput()); \
d->m_settings->setValue(d->m_appname + "/volume", int(volume())); \
d->m_settings->setValue(d->m_appname + "/mute", mute()); \
d->m_settings->setValue(d->m_playerid + "/audiooutput", audiooutput()); \
d->m_settings->setValue(d->m_playerid + "/volume", int(volume())); \
d->m_settings->setValue(d->m_playerid + "/mute", mute()); \
d->m_settings->sync(); \
} else { \
kWarning() << i18n("Could not save state"); \
@ -189,7 +199,7 @@ public:
#if defined(HAVE_MPV)
mpv_handle *m_handle;
#endif
QString m_appname;
QString m_playerid;
KSettings *m_settings;
// the handle pointer is not NULL-ed once mpv_terminate_destroy() has been
// called, doing it manually is a race because _processHandleEvents() is
@ -198,7 +208,7 @@ public:
};
KAbstractPlayerPrivate::KAbstractPlayerPrivate()
: m_appname(QApplication::applicationName()),
: m_playerid(QApplication::applicationName()),
m_settings(new KSettings("kmediaplayer", KSettings::FullConfig)),
m_stopprocessing(false)
{
@ -418,7 +428,7 @@ static void wakeup_audio(void *ctx)
}
KAudioPlayer::KAudioPlayer(QObject *parent)
: QObject(parent), d(new KAbstractPlayerPrivate)
: QObject(parent), d(new KAbstractPlayerPrivate())
{
#if defined(HAVE_MPV)
if (d->m_handle) {
@ -428,12 +438,7 @@ KAudioPlayer::KAudioPlayer(QObject *parent)
setOption("vid", "no");
setOption("video", "no");
const QString globalaudio = d->m_settings->value("global/audiooutput", "auto").toString();
const int globalvolume = d->m_settings->value("global/volume", 90).toInt();
const bool globalmute = d->m_settings->value("global/mute", false).toBool();
setAudioOutput(d->m_settings->value(d->m_appname + "/audiooutput", globalaudio).toString());
setVolume(d->m_settings->value(d->m_appname + "/volume", globalvolume).toInt());
setMute(d->m_settings->value(d->m_appname + "/mute", globalmute).toBool());
COMMON_STATE_LOAD
}
#else
kWarning() << i18n("KAudioPlayer is a stub");
@ -485,6 +490,14 @@ void KAudioPlayer::_processHandleEvents()
#endif
}
void KAudioPlayer::setPlayerID(const QString &id)
{
d->m_playerid = id;
#if defined(HAVE_MPV)
COMMON_STATE_LOAD
#endif
}
bool KAudioPlayer::isMimeSupported(const QString &mime) const
{
#if defined(HAVE_MPV)
@ -522,12 +535,7 @@ KMediaPlayer::KMediaPlayer(QWidget *parent)
kWarning() << i18n("Could not get widget ID");
}
const QString globalaudio = d->m_settings->value("global/audiooutput", "auto").toString();
const int globalvolume = d->m_settings->value("global/volume", 90).toInt();
const bool globalmute = d->m_settings->value("global/mute", false).toBool();
setAudioOutput(d->m_settings->value(d->m_appname + "/audiooutput", globalaudio).toString());
setVolume(d->m_settings->value(d->m_appname + "/volume", globalvolume).toInt());
setMute(d->m_settings->value(d->m_appname + "/mute", globalmute).toBool());
COMMON_STATE_LOAD
}
#else
kWarning() << i18n("KMediaPlayer is a stub");
@ -579,6 +587,14 @@ void KMediaPlayer::_processHandleEvents()
#endif
}
void KMediaPlayer::setPlayerID(const QString &id)
{
d->m_playerid = id;
#if defined(HAVE_MPV)
COMMON_STATE_LOAD
#endif
}
bool KMediaPlayer::isMimeSupported(const QString &mime) const
{
#if defined(HAVE_MPV)

View file

@ -21,6 +21,7 @@
#include "kmimetype.h"
#include "kmediaplayer_export.h"
#include <QWidget>
#include <QEvent>
#include <QSettings>
@ -59,6 +60,13 @@ public:
virtual void setOption(const QString &name, const QVariant &value) const = 0;
//@}
/*!
@brief Sets the player ID to @p id and reloads the saved state
@note The ID, which is @p QApplication::applicationName() by default, must be one of the
strings in the "X-KDE-MediaPlayer" entry of the .desktop file
@param id player ID that identifies feature
*/
virtual void setPlayerID(const QString &id) = 0;
/*!
@brief Start playing from @p path
@param path path to load, it can start with "file://", "dvd://", "http://" and other
@ -216,10 +224,14 @@ public:
/*!
The @p KAudioPlayer class provides an object that can be used to playback from various media
sources including Hard-Drives, Internet streams, CD, DVD, Blue-Ray, file-descriptor, raw data,
you name it. It supports per-application state too, this includes audio output device, volume
and mute state currently. That feature requires a special entry in the application .desktop
file - "X-KDE-MediaPlayer=true" - which indicates that it uses the class and makes it appear
in the K Control Module (KCM) for multimedia.
you name it.
It supports per-application state too, this includes audio output device, volume and mute state
currently. That feature requires a special entry in the application .desktop file -
"X-KDE-MediaPlayer=true" - which indicates that it uses the class and makes it appear in the
K Control Module (KCM) for multimedia. If the player is not used in application but in @p KPart
or plugin which may create multiple instances for different purposes you may want to set its ID
via @p setPlayerID().
For an extended version of this class check out @p KMediaPlayer and @p KMediaWidget.
@ -237,6 +249,7 @@ public:
QVariant option(const QString &name) const;
void setOption(const QString &name, const QVariant& value) const;
void setPlayerID(const QString &id);
bool isMimeSupported(const QString &mime) const;
Q_SIGNALS:
@ -273,10 +286,14 @@ private:
/*!
The @p KMediaPlayer class provides an object that can be used to playback from various media
sources including Hard-Drives, Internet streams, CD, DVD, Blue-Ray, file-descriptor, raw data,
you name it. It supports per-application state too, this includes audio output device, volume
and mute state currently. That feature requires a special entry in the application .desktop
file - "X-KDE-MediaPlayer=true" - which indicates that it uses the class and makes it appear
in the K Control Module (KCM) for multimedia.
you name it.
It supports per-application state too, this includes audio output device, volume and mute state
currently. That feature requires a special entry in the application .desktop file -
"X-KDE-MediaPlayer=true" - which indicates that it uses the class and makes it appear in the
K Control Module (KCM) for multimedia. If the player is not used in application but in @p KPart
or plugin which may create multiple instances for different purposes you may want to set its ID
via @p setPlayerID().
For an extended version of this class check out @p KMediaWidget.
@ -296,6 +313,7 @@ public:
QVariant option(const QString &name) const;
void setOption(const QString &name, const QVariant &value) const;
void setPlayerID(const QString &id);
bool isMimeSupported(const QString &mime) const;
Q_SIGNALS: