[OpenDocString] kdeconnect-kde (cpp)
mprisremoteplugin.cpp
MprisRemotePlugin::MprisRemotePlugin(QObject *parent, const QVariantList &args)
    : KdeConnectPlugin(parent, args)
    , m_currentPlayer()
    , m_players()
{
}
Constructs a plugin object and its players.
MprisRemotePlugin::~MprisRemotePlugin()
{
}
This implements the functionality of the MprisRemotePlugin class.
bool MprisRemotePlugin::receivePacket(const NetworkPacket &np)
{
    if (np.type() != PACKET_TYPE_MPRIS)
        return false;

    if (np.has(QStringLiteral("player"))) {
        const QString player = np.get(QStringLiteral("player"));
        if (!m_players.contains(player)) {
            m_players[player] = new MprisRemotePlayer(player, this);
        }
        m_players[player]->parseNetworkPacket(np);
    }

    if (np.has(QStringLiteral("playerList"))) {
        QStringList players = np.get(QStringLiteral("playerList"));

        // Remove players not available any more
        for (auto iter = m_players.begin(); iter != m_players.end();) {
            if (!players.contains(iter.key())) {
                iter.value()->deleteLater();
                iter = m_players.erase(iter);
            } else {
                ++iter;
            }
        }

        // Add new players
        for (const QString &player : players) {
            if (!m_players.contains(player)) {
                m_players[player] = new MprisRemotePlayer(player, this);
                requestPlayerStatus(player);
            }
        }

        if (m_players.empty()) {
            m_currentPlayer = QString();
        } else if (!m_players.contains(m_currentPlayer)) {
            m_currentPlayer = m_players.firstKey();
        }
    }
    Q_EMIT propertiesChanged();

    return true;
}
It receives a NetworkPacket object np and processes it. The method first checks if the packet type is MPRIS, if it exists, it retrieves the player information, and adds it to the internal list of players. Then it requests the status of each player from the server. If the list of players is empty, it adds the first player in the list to the list of players. Then it emits the propertiesChanged signal. If the list is empty, it returns true.
long MprisRemotePlugin::position() const
{
    auto player = m_players.value(m_currentPlayer);
    return player ? player->position() : 0;
}
Returns the position of the current player.
QString MprisRemotePlugin::dbusPath() const
{
    return QStringLiteral("/modules/kdeconnect/devices/") + device()->id() + QStringLiteral("/mprisremote");
}
Returns the dbus path as a QString.
void MprisRemotePlugin::requestPlayerStatus(const QString &player)
{
    NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST,
                     {{QStringLiteral("player"), player}, {QStringLiteral("requestNowPlaying"), true}, {QStringLiteral("requestVolume"), true}});
    sendPacket(np);
}
This requests that the player be played and requested the volume of the current device.
void MprisRemotePlugin::requestPlayerList()
{
    NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, {{QStringLiteral("requestPlayerList"), true}});
    sendPacket(np);
}
This requests the list of players from the slave.
void MprisRemotePlugin::sendAction(const QString &action)
{
    NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, {{QStringLiteral("player"), m_currentPlayer}, {QStringLiteral("action"), action}});
    sendPacket(np);
}
Sends an action to the current player.
void MprisRemotePlugin::seek(int offset) const
{
    NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, {{QStringLiteral("player"), m_currentPlayer}, {QStringLiteral("Seek"), offset}});
    sendPacket(np);
}
This sends a seek request to the current player and seek to the given offset.
void MprisRemotePlugin::setVolume(int volume)
{
    NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, {{QStringLiteral("player"), m_currentPlayer}, {QStringLiteral("setVolume"), volume}});
    sendPacket(np);
}
Sets the volume value of the current player in the mpries network package.
void MprisRemotePlugin::setPosition(int position)
{
    NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, {{QStringLiteral("player"), m_currentPlayer}, {QStringLiteral("SetPosition"), position}});
    sendPacket(np);

    m_players[m_currentPlayer]->setPosition(position);
}
Sets the position of the current player in the internal list of players.
void MprisRemotePlugin::setPlayer(const QString &player)
{
    if (m_currentPlayer != player) {
        m_currentPlayer = player;
        requestPlayerStatus(player);
        Q_EMIT propertiesChanged();
    }
}
Sets the player object within in the internal list of players. First it requests the status of the player and emits the propertiesChanged signal. Finally, it emits the propertiesChanged signal.
bool MprisRemotePlugin::isPlaying() const
{
    auto player = m_players.value(m_currentPlayer);
    return player ? player->playing() : false;
}
This implements checking if the current player is playing. It first gets the player object of the internal list of players, and if it is it is playing. It returns true if the player is playing.
int MprisRemotePlugin::length() const
{
    auto player = m_players.value(m_currentPlayer);
    return player ? player->length() : 0;
}
Returns the length of the currently selected player.
int MprisRemotePlugin::volume() const
{
    auto player = m_players.value(m_currentPlayer);
    return player ? player->volume() : 0;
}
Returns the volume of the currently selected player.
QString MprisRemotePlugin::player() const
{
    if (m_currentPlayer.isEmpty())
        return QString();
    return m_currentPlayer;
}
Returns the current player object. If the current player is empty, it returns a QString.
QStringList MprisRemotePlugin::playerList() const
{
    return m_players.keys();
}
Returns the list of all players.
QString MprisRemotePlugin::nowPlaying() const
{
    auto player = m_players.value(m_currentPlayer);
    return player ? player->nowPlaying() : QString();
}
Returns the current playing state as a QString.
QString MprisRemotePlugin::title() const
{
    auto player = m_players.value(m_currentPlayer);
    return player ? player->title() : QString();
}
Returns the title of the currently selected player.
QString MprisRemotePlugin::album() const
{
    auto player = m_players.value(m_currentPlayer);
    return player ? player->album() : QString();
}
Returns the album of the currently selected player.
QString MprisRemotePlugin::artist() const
{
    auto player = m_players.value(m_currentPlayer);
    return player ? player->artist() : QString();
}
Returns the artist of the currently selected player.
bool MprisRemotePlugin::canSeek() const
{
    auto player = m_players.value(m_currentPlayer);
    return player ? player->canSeek() : false;
}
This implements checking if the current player can seek. It first looks for a player in the internal list of players, then checks if it can seek. If it can seek, it returns true.