File indexing completed on 2025-01-19 04:28:11

0001 /**
0002  * SPDX-FileCopyrightText: 2014 Sujith Haridasan <sujith.haridasan@kdemail.net>
0003  * SPDX-FileCopyrightText: 2014 Ashish Madeti <ashishmadeti@gmail.com>
0004  * SPDX-FileCopyrightText: 2016 Matthieu Gallien <matthieu_gallien@yahoo.fr>
0005  * SPDX-FileCopyrightText: 2022-2023 Bart De Vries <bart@mogwai.be>
0006  *
0007  * SPDX-License-Identifier: GPL-3.0-or-later
0008  */
0009 
0010 #include "mpris2.h"
0011 #include "mpris2logging.h"
0012 
0013 #include <QDebug>
0014 #if !defined Q_OS_ANDROID
0015 #include <QDBusConnection>
0016 #endif
0017 
0018 #if defined Q_OS_WIN
0019 #include <Windows.h>
0020 #else
0021 #include <unistd.h>
0022 #endif
0023 
0024 #include "kmediasession.h"
0025 #if !defined Q_OS_ANDROID
0026 #include "mediaplayer2.h"
0027 #include "mediaplayer2player.h"
0028 #endif
0029 
0030 Mpris2::Mpris2(QObject *parent)
0031     : QObject(parent)
0032     , m_audioPlayer(static_cast<KMediaSession *>(parent))
0033 {
0034     qCDebug(Mpris2Log) << "Mpris2::Mpris2()";
0035 
0036 #if !defined Q_OS_ANDROID
0037     connect(m_audioPlayer, &KMediaSession::playerNameChanged, this, [this]() {
0038         bool success = true;
0039         if (m_mp2) {
0040             success = unregisterDBusService(m_playerName);
0041         }
0042 
0043         if (success) {
0044             initDBusService(m_audioPlayer->playerName());
0045         }
0046     });
0047 
0048     initDBusService(m_audioPlayer->playerName());
0049 #endif
0050 }
0051 
0052 void Mpris2::initDBusService(const QString &playerName)
0053 {
0054     qCDebug(Mpris2Log) << "Mpris2::initDBusService(" << playerName << ")";
0055 #if !defined Q_OS_ANDROID
0056 
0057     QString tryPlayerName = playerName;
0058     QString mpris2Name(QStringLiteral("org.mpris.MediaPlayer2.") + tryPlayerName);
0059 
0060     bool success = QDBusConnection::sessionBus().registerService(mpris2Name);
0061 
0062     // If the above failed, it's likely because we're not the first instance
0063     // or the name is already taken. In that event the MPRIS2 spec wants the
0064     // following:
0065     if (!success) {
0066 #if defined Q_OS_WIN
0067         tryPlayerName = tryPlayerName + QLatin1String(".instance") + QString::number(GetCurrentProcessId());
0068 #else
0069         tryPlayerName = tryPlayerName + QLatin1String(".instance") + QString::number(getpid());
0070 #endif
0071         success = QDBusConnection::sessionBus().registerService(QStringLiteral("org.mpris.MediaPlayer2.") + tryPlayerName);
0072     }
0073 
0074     if (success) {
0075         m_playerName = tryPlayerName;
0076         if (!m_mp2) {
0077             m_mp2 = std::make_unique<MediaPlayer2>(m_audioPlayer, this);
0078             m_mp2p = std::make_unique<MediaPlayer2Player>(m_audioPlayer, m_ShowProgressOnTaskBar, this);
0079         }
0080 
0081         QDBusConnection::sessionBus().registerObject(QStringLiteral("/org/mpris/MediaPlayer2"), this, QDBusConnection::ExportAdaptors);
0082     }
0083 #endif
0084 }
0085 
0086 bool Mpris2::unregisterDBusService(const QString &playerName)
0087 {
0088     bool success = true;
0089 #if !defined Q_OS_ANDROID
0090     QString oldMpris2Name(QStringLiteral("org.mpris.MediaPlayer2.") + playerName);
0091     success = QDBusConnection::sessionBus().unregisterService(oldMpris2Name);
0092 
0093     if (success) {
0094         m_playerName = QLatin1String("");
0095     }
0096 #endif
0097     return success;
0098 }
0099 
0100 Mpris2::~Mpris2() = default;
0101 
0102 bool Mpris2::showProgressOnTaskBar() const
0103 {
0104     qCDebug(Mpris2Log) << "Mpris2::showProgressOnTaskBar()";
0105     return m_ShowProgressOnTaskBar;
0106 }
0107 
0108 void Mpris2::setShowProgressOnTaskBar(bool value)
0109 {
0110     qCDebug(Mpris2Log) << "Mpris2::setShowProgressOnTaskBar" << value << ")";
0111 #if !defined Q_OS_ANDROID
0112     m_mp2p->setShowProgressOnTaskBar(value);
0113 #endif
0114     m_ShowProgressOnTaskBar = value;
0115     Q_EMIT showProgressOnTaskBarChanged();
0116 }