1/*
2 * BluezQt - Asynchronous Bluez wrapper library
3 *
4 * SPDX-FileCopyrightText: 2015 David Rosca <nowrep@gmail.com>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9#include "mediaplayer_p.h"
10#include "macros.h"
11#include "utils.h"
12
13namespace BluezQt
14{
15static MediaPlayer::Equalizer stringToEqualizer(const QString &equalizer)
16{
17 if (equalizer == QLatin1String("on")) {
18 return MediaPlayer::EqualizerOn;
19 }
20 return MediaPlayer::EqualizerOff;
21}
22
23static MediaPlayer::Repeat stringToRepeat(const QString &repeat)
24{
25 if (repeat == QLatin1String("singletrack")) {
26 return MediaPlayer::RepeatSingleTrack;
27 } else if (repeat == QLatin1String("alltracks")) {
28 return MediaPlayer::RepeatAllTracks;
29 } else if (repeat == QLatin1String("group")) {
30 return MediaPlayer::RepeatGroup;
31 }
32 return MediaPlayer::RepeatOff;
33}
34
35static MediaPlayer::Shuffle stringToShuffle(const QString &shuffle)
36{
37 if (shuffle == QLatin1String("alltracks")) {
38 return MediaPlayer::ShuffleAllTracks;
39 } else if (shuffle == QLatin1String("group")) {
40 return MediaPlayer::ShuffleGroup;
41 }
42 return MediaPlayer::ShuffleOff;
43}
44
45static MediaPlayer::Status stringToStatus(const QString &status)
46{
47 if (status == QLatin1String("playing")) {
48 return MediaPlayer::Playing;
49 } else if (status == QLatin1String("stopped")) {
50 return MediaPlayer::Stopped;
51 } else if (status == QLatin1String("paused")) {
52 return MediaPlayer::Paused;
53 } else if (status == QLatin1String("forward-seek")) {
54 return MediaPlayer::ForwardSeek;
55 } else if (status == QLatin1String("reverse-seek")) {
56 return MediaPlayer::ReverseSeek;
57 }
58 return MediaPlayer::Error;
59}
60
61MediaPlayerPrivate::MediaPlayerPrivate(const QString &path, const QVariantMap &properties)
62 : QObject()
63 , m_dbusProperties(nullptr)
64 , m_path(path)
65 , m_equalizer(MediaPlayer::EqualizerOff)
66 , m_repeat(MediaPlayer::RepeatOff)
67 , m_shuffle(MediaPlayer::ShuffleOff)
68 , m_status(MediaPlayer::Error)
69 , m_position(0)
70{
71 m_bluezMediaPlayer = new BluezMediaPlayer(Strings::orgBluez(), path, DBusConnection::orgBluez(), this);
72
73 init(properties);
74}
75
76void MediaPlayerPrivate::init(const QVariantMap &properties)
77{
78 m_dbusProperties = new DBusProperties(Strings::orgBluez(), m_bluezMediaPlayer->path(), DBusConnection::orgBluez(), this);
79
80 // Init properties
81 m_name = properties.value(QStringLiteral("Name")).toString();
82 m_equalizer = stringToEqualizer(equalizer: properties.value(QStringLiteral("Equalizer")).toString());
83 m_repeat = stringToRepeat(repeat: properties.value(QStringLiteral("Repeat")).toString());
84 m_shuffle = stringToShuffle(shuffle: properties.value(QStringLiteral("Shuffle")).toString());
85 m_status = stringToStatus(status: properties.value(QStringLiteral("Status")).toString());
86 m_track = variantToTrack(variant: properties.value(QStringLiteral("Track")));
87 m_position = properties.value(QStringLiteral("Position")).toUInt();
88}
89
90QDBusPendingReply<> MediaPlayerPrivate::setDBusProperty(const QString &name, const QVariant &value)
91{
92 return m_dbusProperties->Set(interface_name: Strings::orgBluezMediaPlayer1(), property_name: name, value: QDBusVariant(value));
93}
94
95void MediaPlayerPrivate::propertiesChanged(const QString &interface, const QVariantMap &changed, const QStringList &invalidated)
96{
97 if (interface != Strings::orgBluezMediaPlayer1()) {
98 return;
99 }
100
101 QVariantMap::const_iterator i;
102 for (i = changed.constBegin(); i != changed.constEnd(); ++i) {
103 const QVariant &value = i.value();
104 const QString &property = i.key();
105
106 if (property == QLatin1String("Name")) {
107 PROPERTY_CHANGED(m_name, toString, nameChanged);
108 } else if (property == QLatin1String("Equalizer")) {
109 PROPERTY_CHANGED2(m_equalizer, stringToEqualizer(value.toString()), equalizerChanged);
110 } else if (property == QLatin1String("Repeat")) {
111 PROPERTY_CHANGED2(m_repeat, stringToRepeat(value.toString()), repeatChanged);
112 } else if (property == QLatin1String("Shuffle")) {
113 PROPERTY_CHANGED2(m_shuffle, stringToShuffle(value.toString()), shuffleChanged);
114 } else if (property == QLatin1String("Status")) {
115 PROPERTY_CHANGED2(m_status, stringToStatus(value.toString()), statusChanged);
116 } else if (property == QLatin1String("Position")) {
117 PROPERTY_CHANGED(m_position, toUInt, positionChanged);
118 } else if (property == QLatin1String("Track")) {
119 m_track = variantToTrack(variant: value);
120 Q_EMIT q.lock()->trackChanged(track: m_track);
121 }
122 }
123
124 for (const QString &property : invalidated) {
125 if (property == QLatin1String("Name")) {
126 PROPERTY_INVALIDATED(m_name, QString(), nameChanged);
127 } else if (property == QLatin1String("Equalizer")) {
128 PROPERTY_INVALIDATED(m_equalizer, MediaPlayer::EqualizerOff, equalizerChanged);
129 } else if (property == QLatin1String("Repeat")) {
130 PROPERTY_INVALIDATED(m_repeat, MediaPlayer::RepeatOff, repeatChanged);
131 } else if (property == QLatin1String("Shuffle")) {
132 PROPERTY_INVALIDATED(m_shuffle, MediaPlayer::ShuffleOff, shuffleChanged);
133 } else if (property == QLatin1String("Status")) {
134 PROPERTY_INVALIDATED(m_status, MediaPlayer::Error, statusChanged);
135 } else if (property == QLatin1String("Position")) {
136 PROPERTY_INVALIDATED(m_position, 0, positionChanged);
137 } else if (property == QLatin1String("Track")) {
138 m_track = variantToTrack(variant: QVariant());
139 Q_EMIT q.lock()->trackChanged(track: m_track);
140 }
141 }
142}
143
144MediaPlayerTrack MediaPlayerPrivate::variantToTrack(const QVariant &variant) const
145{
146 const QVariantMap &properties = qdbus_cast<QVariantMap>(v: variant);
147 return MediaPlayerTrack(properties);
148}
149
150} // namespace BluezQt
151
152#include "moc_mediaplayer_p.cpp"
153

source code of bluez-qt/src/mediaplayer_p.cpp