[OpenDocString] kdeconnect-kde (cpp)
mprisremoteplayer.cpp
MprisRemotePlayer::MprisRemotePlayer(QString id, MprisRemotePlugin *plugin)
    : QObject(plugin)
    , id(id)
    , m_playing(false)
    , m_canPlay(true)
    , m_canPause(true)
    , m_canGoPrevious(true)
    , m_canGoNext(true)
    , m_nowPlaying()
    , m_volume(50)
    , m_length(0)
    , m_lastPosition(0)
    , m_lastPositionTime()
    , m_title()
    , m_artist()
    , m_album()
    , m_canSeek(false)
    , m_dbusConnectionName(QStringLiteral("mpris_") + QUuid::createUuid().toString(QUuid::Id128))
    , m_dbusConnection(QDBusConnection::connectToBus(QDBusConnection::SessionBus, m_dbusConnectionName))
{
    // Expose this player on the newly created connection. This allows multiple mpris services in the same Qt process
    new MprisRemotePlayerMediaPlayer2(this, plugin);
    new MprisRemotePlayerMediaPlayer2Player(this, plugin);

    m_dbusConnection.registerObject(QStringLiteral("/org/mpris/MediaPlayer2"), this);
    // Make sure our service name is unique. Reuse the connection name for this.
    m_dbusConnection.registerService(QStringLiteral("org.mpris.MediaPlayer2.kdeconnect.") + m_dbusConnectionName);
}
Constructs a new media player object and registers it with DBus.
MprisRemotePlayer::~MprisRemotePlayer()
{
    // Drop the DBus connection (it was only used for this class)
    QDBusConnection::disconnectFromBus(m_dbusConnectionName);
}
Drops the DBus connection when the class is not used.
void MprisRemotePlayer::parseNetworkPacket(const NetworkPacket &np)
{
    bool trackInfoHasChanged = false;

    // Track properties
    QString newNowPlaying = np.get(QStringLiteral("nowPlaying"), m_nowPlaying);
    QString newTitle = np.get(QStringLiteral("title"), m_title);
    QString newArtist = np.get(QStringLiteral("artist"), m_artist);
    QString newAlbum = np.get(QStringLiteral("album"), m_album);
    int newLength = np.get(QStringLiteral("length"), m_length);

    // Check if they changed
    if (newNowPlaying != m_nowPlaying || newTitle != m_title || newArtist != m_artist || newAlbum != m_album || newLength != m_length) {
        trackInfoHasChanged = true;
        Q_EMIT trackInfoChanged();
    }
    // Set the new values
    m_nowPlaying = newNowPlaying;
    m_title = newTitle;
    m_artist = newArtist;
    m_album = newAlbum;
    m_length = newLength;

    // Check volume changes
    int newVolume = np.get(QStringLiteral("volume"), m_volume);
    if (newVolume != m_volume) {
        Q_EMIT volumeChanged();
    }
    m_volume = newVolume;

    if (np.has(QStringLiteral("pos"))) {
        // Check position
        int newLastPosition = np.get(QStringLiteral("pos"), m_lastPosition);
        int positionDiff = qAbs(position() - newLastPosition);
        m_lastPosition = newLastPosition;
        m_lastPositionTime = QDateTime::currentMSecsSinceEpoch();

        // Only consider it seeking if the position changed more than 1 second, and the track has not changed
        if (qAbs(positionDiff) >= 1000 && !trackInfoHasChanged) {
            Q_EMIT positionChanged();
        }
    }

    // Check if we started/stopped playing
    bool newPlaying = np.get(QStringLiteral("isPlaying"), m_playing);
    if (newPlaying != m_playing) {
        Q_EMIT playingChanged();
    }
    m_playing = newPlaying;

    // Control properties
    bool newCanSeek = np.get(QStringLiteral("canSeek"), m_canSeek);
    bool newCanPlay = np.get(QStringLiteral("canPlay"), m_canPlay);
    bool newCanPause = np.get(QStringLiteral("canPause"), m_canPause);
    bool newCanGoPrevious = np.get(QStringLiteral("canGoPrevious"), m_canGoPrevious);
    bool newCanGoNext = np.get(QStringLiteral("canGoNext"), m_canGoNext);

    // Check if they changed
    if (newCanSeek != m_canSeek || newCanPlay != m_canPlay || newCanPause != m_canPause || newCanGoPrevious != m_canGoPrevious || newCanGoNext != m_canGoNext) {
        Q_EMIT controlsChanged();
    }
    // Set the new values
    m_canSeek = newCanSeek;
    m_canPlay = newCanPlay;
    m_canPause = newCanPause;
    m_canGoPrevious = newCanGoPrevious;
    m_canGoNext = newCanGoNext;
}
This parses a network packet and sets the internal values of the objects m_nowPlaying, m_title, m_artist, m_length, and m_volume fields. It checks if the track info has changed, as well as if the position has changed.
long MprisRemotePlayer::position() const
{
    if (m_playing) {
        return m_lastPosition + (QDateTime::currentMSecsSinceEpoch() - m_lastPositionTime);
    } else {
        return m_lastPosition;
    }
}
Returns the current position in the last iteration.
void MprisRemotePlayer::setPosition(long position)
{
    m_lastPosition = position;
    m_lastPositionTime = QDateTime::currentMSecsSinceEpoch();
}
Sets the position of the remote player in the current time.
int MprisRemotePlayer::volume() const
{
    return m_volume;
}
Returns the volume number in the internal list.
long int MprisRemotePlayer::length() const
{
    return m_length;
}
Returns the length in bytes of the internal buffer.
bool MprisRemotePlayer::playing() const
{
    return m_playing;
}
This implements checking if the remote player is playing.
QString MprisRemotePlayer::nowPlaying() const
{
    return m_nowPlaying;
}
Returns the currently playing QString.
QString MprisRemotePlayer::title() const
{
    return m_title;
}
Returns the title stored in the internal list.
QString MprisRemotePlayer::artist() const
{
    return m_artist;
}
Returns the artist stored in the m_artist member variable.
QString MprisRemotePlayer::album() const
{
    return m_album;
}
Returns the album string of the remote player.
bool MprisRemotePlayer::canSeek() const
{
    return m_canSeek;
}
This implements checking if the remote player can seek.
QString MprisRemotePlayer::identity() const
{
    return id;
}
Returns the identity QString of the current player.
bool MprisRemotePlayer::canPlay() const
{
    return m_canPlay;
}
This implements checking if the device is able to play.
bool MprisRemotePlayer::canPause() const
{
    return m_canPause;
}
This implements checking if the remote player can pause.
bool MprisRemotePlayer::canGoPrevious() const
{
    return m_canGoPrevious;
}
This implements checking if the remote player can go previous.
bool MprisRemotePlayer::canGoNext() const
{
    return m_canGoNext;
}
This implements checking if the remote player can go the next item.
QDBusConnection &MprisRemotePlayer::dbus()
{
    return m_dbusConnection;
}
Returns a copy of the dbus connection object.