4c307a7ae1df9212903ee3108400dc5edd8720bf
[apps/mediaplayer.git] / app / dbus.cpp
1 /*
2  * Copyright (C) 2017 Konsulko Group
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "dbus.h"
18
19
20 DbusService::DbusService(QObject *parent) : QObject(parent)
21 {
22 }
23
24 /*
25  * Light Media Scanner
26  */
27
28 bool DbusService::enableLMS()
29 {
30     QDBusConnection session_bus = QDBusConnection::sessionBus();
31
32     if (!session_bus.isConnected())
33         return false;
34
35     return session_bus.connect(QString("org.lightmediascanner"), QString("/org/lightmediascanner/Scanner1"), "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(lmsUpdate(QString,QVariantMap,QStringList)));
36 }
37
38 #if defined(HAVE_LIGHTMEDIASCANNER)
39 void DbusService::lmsUpdate(const QString&, const QVariantMap& map, const QStringList&)
40 {
41     QVariantList mediaFiles;
42     QString music;
43
44     if (!map.contains("IsScanning") && !map.contains("WriteLocked"))
45         return;
46
47     if (map["IsScanning"].toBool() || map["WriteLocked"].toBool())
48         return;
49
50     mediaFiles = LightMediaScanner::processLightMediaScanner();
51
52     if (!mediaFiles.isEmpty())
53         emit processPlaylistUpdate(mediaFiles);
54     else
55         emit processPlaylistHide();
56 }
57 #else
58 void DbusService::lmsUpdate(const QString&, const QVariantMap&, const QStringList&)
59 {
60 }
61 #endif
62
63 /*
64  * Bluetooth
65  */
66
67 void DbusService::setBluezPath(const QString& path)
68 {
69     this->bluezPath = path;
70 }
71
72 QString DbusService::getBluezPath() const
73 {
74     return this->bluezPath;
75 }
76
77 bool DbusService::enableBluetooth()
78 {
79     QDBusConnection system_bus = QDBusConnection::systemBus();
80     QString interface = "org.freedesktop.DBus.ObjectManager";
81     bool ret;
82
83     if (!system_bus.isConnected())
84         return false;
85
86     ret = system_bus.connect(QString("org.bluez"), QString("/"), interface, "InterfacesAdded", this, SLOT(newBluetoothDevice(QDBusObjectPath,QVariantMap)));
87
88     if (!ret)
89         return false;
90
91     ret = system_bus.connect(QString("org.bluez"), QString("/"), interface, "InterfacesRemoved", this, SLOT(removeBluetoothDevice(QDBusObjectPath,QStringList)));
92
93     /*
94      * Unregister InterfacesAdded on error condition
95      */
96     if (!ret)
97         system_bus.disconnect(QString("org.bluez"), QString("/"), interface, "InterfacesAdded", this, SLOT(newBluetoothDevice(QString,QVariantMap)));
98
99     return ret;
100 }
101
102 bool DbusService::checkIfPlayer(const QString& path) const
103 {
104     QRegExp regex("^.*/player\\d$");
105     if (regex.exactMatch(path))
106         return true;
107
108     return false;
109 }
110
111 void DbusService::newBluetoothDevice(const QDBusObjectPath& item, const QVariantMap&)
112 {
113     QString path = item.path();
114     if (!checkIfPlayer(path))
115         return;
116
117     if (!getBluezPath().isEmpty()) {
118         qWarning() << "Another Bluetooth Player already connected";
119         return;
120     }
121
122     emit processPlaylistHide();
123
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)));
126
127     setBluezPath(path);
128 }
129
130 void DbusService::removeBluetoothDevice(const QDBusObjectPath& item, const QStringList&)
131 {
132     QString path = item.path();
133     if (!checkIfPlayer(path))
134         return;
135
136     if (getBluezPath().isEmpty()) {
137         qWarning() << "No Bluetooth Player connected";
138         return;
139     }
140
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)));
143
144     setBluezPath(QString());
145     emit processPlaylistShow();
146 }
147
148 void DbusService::processBluetoothEvent(const QString&, const QVariantMap& map, const QStringList&)
149 {
150     if (map.contains("Track")) {
151         QVariantMap track;
152         map["Track"].value<QDBusArgument>() >> track;
153         emit displayBluetoothMetadata(track["Artist"].toString(), track["Title"].toString(), track["Duration"].toInt());
154     }
155
156     if (map.contains("Position"))
157         emit updatePosition(map["Position"].toInt());
158
159     if (map.contains("Status"))
160         emit updatePlayerStatus(map["Status"].toString());
161 }
162
163 void DbusService::processQMLEvent(const QString& state)
164 {
165     QDBusInterface interface("org.bluez", getBluezPath(), "org.bluez.MediaPlayer1", QDBusConnection::systemBus());
166     interface.call(state);
167 }
168
169 long DbusService::getCurrentPosition()
170 {
171     QDBusInterface interface("org.bluez", getBluezPath(), "org.bluez.MediaPlayer1", QDBusConnection::systemBus());
172     return interface.property("Position").toInt();
173 }