File indexing completed on 2025-01-19 04:23:47

0001 /***************************************************************************
0002    SPDX-FileCopyrightText: 2014 (c) Sujith Haridasan <sujith.haridasan@kdemail.net>
0003    SPDX-FileCopyrightText: 2014 (c) Ashish Madeti <ashishmadeti@gmail.com>
0004    SPDX-FileCopyrightText: 2016 (c) Matthieu Gallien <matthieu_gallien@yahoo.fr>
0005 
0006    SPDX-License-Identifier: GPL-3.0-or-later
0007  ***************************************************************************/
0008 
0009 #include "mpris2.h"
0010 #include "../../services/local/player.h"
0011 #include "../../services/local/playlist.h"
0012 
0013 #if (defined Q_OS_LINUX || defined Q_OS_FREEBSD) && !defined Q_OS_ANDROID
0014 #include "mediaplayer2.h"
0015 #include "mediaplayer2player.h"
0016 #include <QDBusConnection>
0017 #endif
0018 
0019 #if defined Q_OS_WIN
0020 #include <Windows.h>
0021 #else
0022 #include <unistd.h>
0023 #endif
0024 
0025 Mpris2::Mpris2(QObject *parent)
0026     : QObject(parent)
0027 {
0028 }
0029 
0030 void Mpris2::initDBusService()
0031 {
0032 #if (defined Q_OS_LINUX || defined Q_OS_FREEBSD) && !defined Q_OS_ANDROID
0033 
0034     QString mspris2Name(QStringLiteral("org.mpris.MediaPlayer2.") + m_playerName);
0035 
0036     bool success = QDBusConnection::sessionBus().registerService(mspris2Name);
0037 
0038     // If the above failed, it's likely because we're not the first instance
0039     // or the name is already taken. In that event the MPRIS2 spec wants the
0040     // following:
0041     if (!success) {
0042 #if defined Q_OS_WIN
0043         success = QDBusConnection::sessionBus().registerService(mspris2Name + QLatin1String(".instance") + QString::number(GetCurrentProcessId()));
0044 #else
0045         success = QDBusConnection::sessionBus().registerService(mspris2Name + QLatin1String(".instance") + QString::number(getpid()));
0046 #endif
0047     }
0048 
0049     if (success) {
0050         m_mp2 = std::make_unique<MediaPlayer2>(this);
0051         m_mp2p = std::make_unique<MediaPlayer2Player>(m_playListModel, m_audioPlayer, mShowProgressOnTaskBar, this);
0052 
0053         QDBusConnection::sessionBus().registerObject(QStringLiteral("/org/mpris/MediaPlayer2"), this, QDBusConnection::ExportAdaptors);
0054 
0055         connect(m_mp2.get(), &MediaPlayer2::raisePlayer, this, &Mpris2::raisePlayer);
0056     }
0057 #endif
0058 }
0059 
0060 Mpris2::~Mpris2() = default;
0061 
0062 QString Mpris2::playerName() const
0063 {
0064     return m_playerName;
0065 }
0066 
0067 Playlist *Mpris2::playListModel() const
0068 {
0069     return m_playListModel;
0070 }
0071 
0072 Player *Mpris2::audioPlayer() const
0073 {
0074     return m_audioPlayer;
0075 }
0076 
0077 bool Mpris2::showProgressOnTaskBar() const
0078 {
0079     return mShowProgressOnTaskBar;
0080 }
0081 
0082 void Mpris2::setPlayerName(const QString &playerName)
0083 {
0084     if (m_playerName == playerName) {
0085         return;
0086     }
0087 
0088     m_playerName = playerName;
0089 
0090 #if (defined Q_OS_LINUX || defined Q_OS_FREEBSD) && !defined Q_OS_ANDROID
0091     if (m_playListModel && m_audioPlayer && m_audioPlayer && !m_playerName.isEmpty()) {
0092         if (!m_mp2) {
0093             initDBusService();
0094         }
0095     }
0096 #endif
0097 
0098     Q_EMIT playerNameChanged();
0099 }
0100 
0101 void Mpris2::setPlayListModel(Playlist *playListModel)
0102 {
0103     if (m_playListModel == playListModel) {
0104         return;
0105     }
0106 
0107     m_playListModel = playListModel;
0108 
0109 #if (defined Q_OS_LINUX || defined Q_OS_FREEBSD) && !defined Q_OS_ANDROID
0110 
0111     if (m_playListModel && m_audioPlayer && m_audioPlayer && !m_playerName.isEmpty()) {
0112         if (!m_mp2) {
0113             initDBusService();
0114         }
0115     }
0116 #endif
0117     Q_EMIT playListModelChanged();
0118 }
0119 
0120 void Mpris2::setAudioPlayer(Player *audioPlayer)
0121 {
0122     if (m_audioPlayer == audioPlayer)
0123         return;
0124 
0125     m_audioPlayer = audioPlayer;
0126 #if (defined Q_OS_LINUX || defined Q_OS_FREEBSD) && !defined Q_OS_ANDROID
0127 
0128     if (m_playListModel && m_audioPlayer && m_audioPlayer && !m_playerName.isEmpty()) {
0129         if (!m_mp2) {
0130             initDBusService();
0131         }
0132     }
0133 #endif
0134     Q_EMIT audioPlayerChanged();
0135 }
0136 
0137 void Mpris2::setShowProgressOnTaskBar(bool value)
0138 {
0139 #if (defined Q_OS_LINUX || defined Q_OS_FREEBSD) && !defined Q_OS_ANDROID
0140     m_mp2p->setShowProgressOnTaskBar(value);
0141     mShowProgressOnTaskBar = value;
0142     Q_EMIT showProgressOnTaskBarChanged();
0143 #else
0144    Q_UNUSED(value)
0145 #endif
0146 }
0147 
0148 #include "moc_mpris2.cpp"