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();
32 if (!session_bus.isConnected())
35 return session_bus.connect(QString("org.lightmediascanner"), QString("/org/lightmediascanner/Scanner1"), "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(lmsUpdate(QString,QVariantMap,QStringList)));
38 #if defined(HAVE_LIGHTMEDIASCANNER)
39 void DbusService::lmsUpdate(const QString&, const QVariantMap& map, const QStringList&)
41 QVariantList mediaFiles;
44 if (!map.contains("IsScanning") && !map.contains("WriteLocked"))
47 if (map["IsScanning"].toBool() || map["WriteLocked"].toBool())
50 mediaFiles = LightMediaScanner::processLightMediaScanner();
52 if (!mediaFiles.isEmpty())
53 emit processPlaylistUpdate(mediaFiles);
55 emit processPlaylistHide();
58 void DbusService::lmsUpdate(const QString&, const QVariantMap&, const QStringList&)
67 void DbusService::setBluezPath(const QString& path)
69 this->bluezPath = path;
72 QString DbusService::getBluezPath() const
74 return this->bluezPath;
77 bool DbusService::enableBluetooth()
79 QDBusConnection system_bus = QDBusConnection::systemBus();
80 QString interface = "org.freedesktop.DBus.ObjectManager";
83 if (!system_bus.isConnected())
86 ret = system_bus.connect(QString("org.bluez"), QString("/"), interface, "InterfacesAdded", this, SLOT(newBluetoothDevice(QDBusObjectPath,QVariantMap)));
91 ret = system_bus.connect(QString("org.bluez"), QString("/"), interface, "InterfacesRemoved", this, SLOT(removeBluetoothDevice(QDBusObjectPath,QStringList)));
94 * Unregister InterfacesAdded on error condition
97 system_bus.disconnect(QString("org.bluez"), QString("/"), interface, "InterfacesAdded", this, SLOT(newBluetoothDevice(QString,QVariantMap)));
102 bool DbusService::checkIfPlayer(const QString& path) const
104 QRegExp regex("^.*/player\\d$");
105 if (regex.exactMatch(path))
111 void DbusService::newBluetoothDevice(const QDBusObjectPath& item, const QVariantMap&)
113 QString path = item.path();
114 if (!checkIfPlayer(path))
117 if (!getBluezPath().isEmpty()) {
118 qWarning() << "Another Bluetooth Player already connected";
122 emit processPlaylistHide();
124 QDBusConnection system_bus = QDBusConnection::systemBus();
125 system_bus.connect(QString("org.bluez"), path, "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(processBluetoothEvent(QString,QVariantMap,QStringList)));
130 void DbusService::removeBluetoothDevice(const QDBusObjectPath& item, const QStringList&)
132 QString path = item.path();
133 if (!checkIfPlayer(path))
136 if (getBluezPath().isEmpty()) {
137 qWarning() << "No Bluetooth Player connected";
141 QDBusConnection system_bus = QDBusConnection::systemBus();
142 system_bus.disconnect(QString("org.bluez"), path, "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(processBluetoothEvent(QString,QVariantMap,QStringList)));
144 setBluezPath(QString());
145 emit processPlaylistShow();
148 void DbusService::processBluetoothEvent(const QString&, const QVariantMap& map, const QStringList&)
150 if (map.contains("Track")) {
152 map["Track"].value<QDBusArgument>() >> track;
153 emit displayBluetoothMetadata(track["Artist"].toString(), track["Title"].toString(), track["Duration"].toInt());
156 if (map.contains("Position"))
157 emit updatePosition(map["Position"].toInt());
159 if (map.contains("Status"))
160 emit updatePlayerStatus(map["Status"].toString());
163 void DbusService::processQMLEvent(const QString& state)
165 QDBusInterface interface("org.bluez", getBluezPath(), "org.bluez.MediaPlayer1", QDBusConnection::systemBus());
166 interface.call(state);
169 long DbusService::getCurrentPosition()
171 QDBusInterface interface("org.bluez", getBluezPath(), "org.bluez.MediaPlayer1", QDBusConnection::systemBus());
172 return interface.property("Position").toInt();