File indexing completed on 2024-12-08 12:15:35

0001 /*
0002  * BluezQt - Asynchronous Bluez wrapper library
0003  *
0004  * SPDX-FileCopyrightText: 2015 David Rosca <nowrep@gmail.com>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007  */
0008 
0009 #include "mediaplayer_p.h"
0010 #include "macros.h"
0011 #include "utils.h"
0012 
0013 namespace BluezQt
0014 {
0015 static MediaPlayer::Equalizer stringToEqualizer(const QString &equalizer)
0016 {
0017     if (equalizer == QLatin1String("on")) {
0018         return MediaPlayer::EqualizerOn;
0019     }
0020     return MediaPlayer::EqualizerOff;
0021 }
0022 
0023 static MediaPlayer::Repeat stringToRepeat(const QString &repeat)
0024 {
0025     if (repeat == QLatin1String("singletrack")) {
0026         return MediaPlayer::RepeatSingleTrack;
0027     } else if (repeat == QLatin1String("alltracks")) {
0028         return MediaPlayer::RepeatAllTracks;
0029     } else if (repeat == QLatin1String("group")) {
0030         return MediaPlayer::RepeatGroup;
0031     }
0032     return MediaPlayer::RepeatOff;
0033 }
0034 
0035 static MediaPlayer::Shuffle stringToShuffle(const QString &shuffle)
0036 {
0037     if (shuffle == QLatin1String("alltracks")) {
0038         return MediaPlayer::ShuffleAllTracks;
0039     } else if (shuffle == QLatin1String("group")) {
0040         return MediaPlayer::ShuffleGroup;
0041     }
0042     return MediaPlayer::ShuffleOff;
0043 }
0044 
0045 static MediaPlayer::Status stringToStatus(const QString &status)
0046 {
0047     if (status == QLatin1String("playing")) {
0048         return MediaPlayer::Playing;
0049     } else if (status == QLatin1String("stopped")) {
0050         return MediaPlayer::Stopped;
0051     } else if (status == QLatin1String("paused")) {
0052         return MediaPlayer::Paused;
0053     } else if (status == QLatin1String("forward-seek")) {
0054         return MediaPlayer::ForwardSeek;
0055     } else if (status == QLatin1String("reverse-seek")) {
0056         return MediaPlayer::ReverseSeek;
0057     }
0058     return MediaPlayer::Error;
0059 }
0060 
0061 MediaPlayerPrivate::MediaPlayerPrivate(const QString &path, const QVariantMap &properties)
0062     : QObject()
0063     , m_dbusProperties(nullptr)
0064     , m_path(path)
0065     , m_equalizer(MediaPlayer::EqualizerOff)
0066     , m_repeat(MediaPlayer::RepeatOff)
0067     , m_shuffle(MediaPlayer::ShuffleOff)
0068     , m_status(MediaPlayer::Error)
0069     , m_position(0)
0070 {
0071     m_bluezMediaPlayer = new BluezMediaPlayer(Strings::orgBluez(), path, DBusConnection::orgBluez(), this);
0072 
0073     init(properties);
0074 }
0075 
0076 void MediaPlayerPrivate::init(const QVariantMap &properties)
0077 {
0078     m_dbusProperties = new DBusProperties(Strings::orgBluez(), m_bluezMediaPlayer->path(), DBusConnection::orgBluez(), this);
0079 
0080     // Init properties
0081     m_name = properties.value(QStringLiteral("Name")).toString();
0082     m_equalizer = stringToEqualizer(properties.value(QStringLiteral("Equalizer")).toString());
0083     m_repeat = stringToRepeat(properties.value(QStringLiteral("Repeat")).toString());
0084     m_shuffle = stringToShuffle(properties.value(QStringLiteral("Shuffle")).toString());
0085     m_status = stringToStatus(properties.value(QStringLiteral("Status")).toString());
0086     m_track = variantToTrack(properties.value(QStringLiteral("Track")));
0087     m_position = properties.value(QStringLiteral("Position")).toUInt();
0088 }
0089 
0090 QDBusPendingReply<> MediaPlayerPrivate::setDBusProperty(const QString &name, const QVariant &value)
0091 {
0092     return m_dbusProperties->Set(Strings::orgBluezMediaPlayer1(), name, QDBusVariant(value));
0093 }
0094 
0095 void MediaPlayerPrivate::propertiesChanged(const QString &interface, const QVariantMap &changed, const QStringList &invalidated)
0096 {
0097     if (interface != Strings::orgBluezMediaPlayer1()) {
0098         return;
0099     }
0100 
0101     QVariantMap::const_iterator i;
0102     for (i = changed.constBegin(); i != changed.constEnd(); ++i) {
0103         const QVariant &value = i.value();
0104         const QString &property = i.key();
0105 
0106         if (property == QLatin1String("Name")) {
0107             PROPERTY_CHANGED(m_name, toString, nameChanged);
0108         } else if (property == QLatin1String("Equalizer")) {
0109             PROPERTY_CHANGED2(m_equalizer, stringToEqualizer(value.toString()), equalizerChanged);
0110         } else if (property == QLatin1String("Repeat")) {
0111             PROPERTY_CHANGED2(m_repeat, stringToRepeat(value.toString()), repeatChanged);
0112         } else if (property == QLatin1String("Shuffle")) {
0113             PROPERTY_CHANGED2(m_shuffle, stringToShuffle(value.toString()), shuffleChanged);
0114         } else if (property == QLatin1String("Status")) {
0115             PROPERTY_CHANGED2(m_status, stringToStatus(value.toString()), statusChanged);
0116         } else if (property == QLatin1String("Position")) {
0117             PROPERTY_CHANGED(m_position, toUInt, positionChanged);
0118         } else if (property == QLatin1String("Track")) {
0119             m_track = variantToTrack(value);
0120             Q_EMIT q.lock()->trackChanged(m_track);
0121         }
0122     }
0123 
0124     for (const QString &property : invalidated) {
0125         if (property == QLatin1String("Name")) {
0126             PROPERTY_INVALIDATED(m_name, QString(), nameChanged);
0127         } else if (property == QLatin1String("Equalizer")) {
0128             PROPERTY_INVALIDATED(m_equalizer, MediaPlayer::EqualizerOff, equalizerChanged);
0129         } else if (property == QLatin1String("Repeat")) {
0130             PROPERTY_INVALIDATED(m_repeat, MediaPlayer::RepeatOff, repeatChanged);
0131         } else if (property == QLatin1String("Shuffle")) {
0132             PROPERTY_INVALIDATED(m_shuffle, MediaPlayer::ShuffleOff, shuffleChanged);
0133         } else if (property == QLatin1String("Status")) {
0134             PROPERTY_INVALIDATED(m_status, MediaPlayer::Error, statusChanged);
0135         } else if (property == QLatin1String("Position")) {
0136             PROPERTY_INVALIDATED(m_position, 0, positionChanged);
0137         } else if (property == QLatin1String("Track")) {
0138             m_track = variantToTrack(QVariant());
0139             Q_EMIT q.lock()->trackChanged(m_track);
0140         }
0141     }
0142 }
0143 
0144 MediaPlayerTrack MediaPlayerPrivate::variantToTrack(const QVariant &variant) const
0145 {
0146     const QVariantMap &properties = qdbus_cast<QVariantMap>(variant);
0147     return MediaPlayerTrack(properties);
0148 }
0149 
0150 } // namespace BluezQt
0151 
0152 #include "moc_mediaplayer_p.cpp"