2 * Copyright (C) 2017 Konsulko Group
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 DbusService::DbusService(QObject *parent) : QObject(parent)
28 bool DbusService::enableLMS()
30 QDBusConnection session_bus = QDBusConnection::sessionBus();
31 QDBusConnection system_bus = QDBusConnection::systemBus();
34 if (!session_bus.isConnected())
37 if (!system_bus.isConnected())
40 ret = session_bus.connect(QString("org.lightmediascanner"), QString("/org/lightmediascanner/Scanner1"), "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(lmsUpdate(QString,QVariantMap,QStringList)));
44 /* Only subscribe to DeviceRemoved events, since we need lms scan to complete on insert */
45 return system_bus.connect(QString("org.freedesktop.UDisks"), QString("/org/freedesktop/UDisks"), "org.freedesktop.UDisks", "DeviceRemoved", this, SLOT(mediaRemoved(QDBusObjectPath)));
48 void DbusService::mediaRemoved(const QDBusObjectPath&)
53 #if defined(HAVE_LIGHTMEDIASCANNER)
54 void DbusService::lmsUpdate(const QString&, const QVariantMap& map, const QStringList&)
56 QVariantList mediaFiles;
59 if (!map.contains("IsScanning") && !map.contains("WriteLocked"))
62 if (map["IsScanning"].toBool() || map["WriteLocked"].toBool())
65 mediaFiles = LightMediaScanner::processLightMediaScanner();
67 if (!mediaFiles.isEmpty())
68 emit processPlaylistUpdate(mediaFiles);
70 emit processPlaylistHide();
73 void DbusService::lmsUpdate(const QString&, const QVariantMap&, const QStringList&)
82 void DbusService::setBluezPath(const QString& path)
84 this->bluezPath = path;
87 QString DbusService::getBluezPath() const
89 return this->bluezPath;
92 bool DbusService::enableBluetooth()
94 QDBusConnection system_bus = QDBusConnection::systemBus();
95 QString interface = "org.freedesktop.DBus.ObjectManager";
98 if (!system_bus.isConnected())
101 if (deviceConnected(system_bus))
102 initialBluetoothData(system_bus);
104 ret = system_bus.connect(QString("org.bluez"), QString("/"), interface, "InterfacesAdded", this, SLOT(newBluetoothDevice(QDBusObjectPath,QVariantMap)));
109 ret = system_bus.connect(QString("org.bluez"), QString("/"), interface, "InterfacesRemoved", this, SLOT(removeBluetoothDevice(QDBusObjectPath,QStringList)));
112 * Unregister InterfacesAdded on error condition
115 system_bus.disconnect(QString("org.bluez"), QString("/"), interface, "InterfacesAdded", this, SLOT(newBluetoothDevice(QString,QVariantMap)));
120 bool DbusService::checkIfPlayer(const QString& path) const
122 QRegExp regex("^.*/player\\d$");
123 if (regex.exactMatch(path))
129 bool DbusService::deviceConnected(const QDBusConnection& system_bus)
131 QDBusInterface interface("org.bluez", "/", "org.freedesktop.DBus.ObjectManager", system_bus);
132 QDBusMessage result = interface.call("GetManagedObjects");
133 const QDBusArgument argument = result.arguments().at(0).value<QDBusArgument>();
136 if (argument.currentType() != QDBusArgument::MapType)
141 while (!argument.atEnd()) {
144 argument.beginMapEntry();
146 argument.endMapEntry();
148 ret = checkIfPlayer(key);
151 newBluetoothDevice(QDBusObjectPath(key), QVariantMap());
161 void DbusService::initialBluetoothData(const QDBusConnection& system_bus)
163 QDBusInterface interface("org.bluez", getBluezPath(), "org.freedesktop.DBus.Properties", system_bus);
165 QDBusMessage result = interface.call("GetAll", "org.bluez.MediaPlayer1");
166 const QDBusArgument argument = result.arguments().at(0).value<QDBusArgument>();
167 QString status, artist, title;
168 int position = 0, duration = 0;
170 if (argument.currentType() != QDBusArgument::MapType)
175 while (!argument.atEnd()) {
179 argument.beginMapEntry();
180 argument >> key >> value;
182 if (key == "Position") {
183 position = value.toInt();
184 } else if (key == "Status") {
185 status = value.toString();
186 } else if (key == "Track") {
187 const QDBusArgument argument1 = qvariant_cast<QDBusArgument>(value);
191 argument1.beginMap();
193 while (!argument1.atEnd()) {
194 argument1.beginMapEntry();
195 argument1 >> key1 >> value1;
196 if (key1 == "Artist")
197 artist = value1.toString();
198 else if (key1 == "Title")
199 title = value1.toString();
200 else if (key1 == "Duration")
201 duration = value1.toInt();
202 argument1.endMapEntry();
206 argument.endMapEntry();
210 emit processPlaylistHide();
211 emit displayBluetoothMetadata(artist, title, duration);
212 emit updatePlayerStatus(status);
213 emit updatePosition(position);
216 void DbusService::newBluetoothDevice(const QDBusObjectPath& item, const QVariantMap&)
218 QString path = item.path();
219 if (!checkIfPlayer(path))
222 if (!getBluezPath().isEmpty()) {
223 qWarning() << "Another Bluetooth Player already connected";
227 emit processPlaylistHide();
229 QDBusConnection system_bus = QDBusConnection::systemBus();
230 system_bus.connect(QString("org.bluez"), path, "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(processBluetoothEvent(QString,QVariantMap,QStringList)));
235 void DbusService::removeBluetoothDevice(const QDBusObjectPath& item, const QStringList&)
237 QString path = item.path();
238 if (!checkIfPlayer(path))
241 if (getBluezPath().isEmpty()) {
242 qWarning() << "No Bluetooth Player connected";
246 QDBusConnection system_bus = QDBusConnection::systemBus();
247 system_bus.disconnect(QString("org.bluez"), path, "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(processBluetoothEvent(QString,QVariantMap,QStringList)));
249 setBluezPath(QString());
250 emit processPlaylistShow();
253 void DbusService::processBluetoothEvent(const QString&, const QVariantMap& map, const QStringList&)
255 if (map.contains("Track")) {
257 map["Track"].value<QDBusArgument>() >> track;
258 emit displayBluetoothMetadata(track["Artist"].toString(), track["Title"].toString(), track["Duration"].toInt());
261 if (map.contains("Position"))
262 emit updatePosition(map["Position"].toInt());
264 if (map.contains("Status"))
265 emit updatePlayerStatus(map["Status"].toString());
268 void DbusService::processQMLEvent(const QString& state)
270 QDBusInterface interface("org.bluez", getBluezPath(), "org.bluez.MediaPlayer1", QDBusConnection::systemBus());
271 interface.call(state);
274 long DbusService::getCurrentPosition()
276 QDBusInterface interface("org.bluez", getBluezPath(), "org.bluez.MediaPlayer1", QDBusConnection::systemBus());
277 return interface.property("Position").toInt();