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 if (deviceConnected(system_bus))
87 initialBluetoothData(system_bus);
89 ret = system_bus.connect(QString("org.bluez"), QString("/"), interface, "InterfacesAdded", this, SLOT(newBluetoothDevice(QDBusObjectPath,QVariantMap)));
94 ret = system_bus.connect(QString("org.bluez"), QString("/"), interface, "InterfacesRemoved", this, SLOT(removeBluetoothDevice(QDBusObjectPath,QStringList)));
97 * Unregister InterfacesAdded on error condition
100 system_bus.disconnect(QString("org.bluez"), QString("/"), interface, "InterfacesAdded", this, SLOT(newBluetoothDevice(QString,QVariantMap)));
105 bool DbusService::checkIfPlayer(const QString& path) const
107 QRegExp regex("^.*/player\\d$");
108 if (regex.exactMatch(path))
114 bool DbusService::deviceConnected(const QDBusConnection& system_bus)
116 QDBusInterface interface("org.bluez", "/", "org.freedesktop.DBus.ObjectManager", system_bus);
117 QDBusMessage result = interface.call("GetManagedObjects");
118 const QDBusArgument argument = result.arguments().at(0).value<QDBusArgument>();
121 if (argument.currentType() != QDBusArgument::MapType)
126 while (!argument.atEnd()) {
129 argument.beginMapEntry();
131 argument.endMapEntry();
133 ret = checkIfPlayer(key);
136 newBluetoothDevice(QDBusObjectPath(key), QVariantMap());
146 void DbusService::initialBluetoothData(const QDBusConnection& system_bus)
148 QDBusInterface interface("org.bluez", getBluezPath(), "org.freedesktop.DBus.Properties", system_bus);
150 QDBusMessage result = interface.call("GetAll", "org.bluez.MediaPlayer1");
151 const QDBusArgument argument = result.arguments().at(0).value<QDBusArgument>();
152 QString status, artist, title;
153 int position = 0, duration = 0;
155 if (argument.currentType() != QDBusArgument::MapType)
160 while (!argument.atEnd()) {
164 argument.beginMapEntry();
165 argument >> key >> value;
167 if (key == "Position") {
168 position = value.toInt();
169 } else if (key == "Status") {
170 status = value.toString();
171 } else if (key == "Track") {
172 const QDBusArgument argument1 = qvariant_cast<QDBusArgument>(value);
176 argument1.beginMap();
178 while (!argument1.atEnd()) {
179 argument1.beginMapEntry();
180 argument1 >> key1 >> value1;
181 if (key1 == "Artist")
182 artist = value1.toString();
183 else if (key1 == "Title")
184 title = value1.toString();
185 else if (key1 == "Duration")
186 duration = value1.toInt();
187 argument1.endMapEntry();
191 argument.endMapEntry();
195 emit processPlaylistHide();
196 emit displayBluetoothMetadata(artist, title, duration);
197 emit updatePlayerStatus(status);
198 emit updatePosition(position);
201 void DbusService::newBluetoothDevice(const QDBusObjectPath& item, const QVariantMap&)
203 QString path = item.path();
204 if (!checkIfPlayer(path))
207 if (!getBluezPath().isEmpty()) {
208 qWarning() << "Another Bluetooth Player already connected";
212 emit processPlaylistHide();
214 QDBusConnection system_bus = QDBusConnection::systemBus();
215 system_bus.connect(QString("org.bluez"), path, "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(processBluetoothEvent(QString,QVariantMap,QStringList)));
220 void DbusService::removeBluetoothDevice(const QDBusObjectPath& item, const QStringList&)
222 QString path = item.path();
223 if (!checkIfPlayer(path))
226 if (getBluezPath().isEmpty()) {
227 qWarning() << "No Bluetooth Player connected";
231 QDBusConnection system_bus = QDBusConnection::systemBus();
232 system_bus.disconnect(QString("org.bluez"), path, "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(processBluetoothEvent(QString,QVariantMap,QStringList)));
234 setBluezPath(QString());
235 emit processPlaylistShow();
238 void DbusService::processBluetoothEvent(const QString&, const QVariantMap& map, const QStringList&)
240 if (map.contains("Track")) {
242 map["Track"].value<QDBusArgument>() >> track;
243 emit displayBluetoothMetadata(track["Artist"].toString(), track["Title"].toString(), track["Duration"].toInt());
246 if (map.contains("Position"))
247 emit updatePosition(map["Position"].toInt());
249 if (map.contains("Status"))
250 emit updatePlayerStatus(map["Status"].toString());
253 void DbusService::processQMLEvent(const QString& state)
255 QDBusInterface interface("org.bluez", getBluezPath(), "org.bluez.MediaPlayer1", QDBusConnection::systemBus());
256 interface.call(state);
259 long DbusService::getCurrentPosition()
261 QDBusInterface interface("org.bluez", getBluezPath(), "org.bluez.MediaPlayer1", QDBusConnection::systemBus());
262 return interface.property("Position").toInt();