summaryrefslogtreecommitdiffstats
path: root/app/dbus.cpp
blob: 4c307a7ae1df9212903ee3108400dc5edd8720bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright (C) 2017 Konsulko Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dbus.h"


DbusService::DbusService(QObject *parent) : QObject(parent)
{
}

/*
 * Light Media Scanner
 */

bool DbusService::enableLMS()
{
    QDBusConnection session_bus = QDBusConnection::sessionBus();

    if (!session_bus.isConnected())
        return false;

    return session_bus.connect(QString("org.lightmediascanner"), QString("/org/lightmediascanner/Scanner1"), "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(lmsUpdate(QString,QVariantMap,QStringList)));
}

#if defined(HAVE_LIGHTMEDIASCANNER)
void DbusService::lmsUpdate(const QString&, const QVariantMap& map, const QStringList&)
{
    QVariantList mediaFiles;
    QString music;

    if (!map.contains("IsScanning") && !map.contains("WriteLocked"))
        return;

    if (map["IsScanning"].toBool() || map["WriteLocked"].toBool())
        return;

    mediaFiles = LightMediaScanner::processLightMediaScanner();

    if (!mediaFiles.isEmpty())
        emit processPlaylistUpdate(mediaFiles);
    else
        emit processPlaylistHide();
}
#else
void DbusService::lmsUpdate(const QString&, const QVariantMap&, const QStringList&)
{
}
#endif

/*
 * Bluetooth
 */

void DbusService::setBluezPath(const QString& path)
{
    this->bluezPath = path;
}

QString DbusService::getBluezPath() const
{
    return this->bluezPath;
}

bool DbusService::enableBluetooth()
{
    QDBusConnection system_bus = QDBusConnection::systemBus();
    QString interface = "org.freedesktop.DBus.ObjectManager";
    bool ret;

    if (!system_bus.isConnected())
        return false;

    ret = system_bus.connect(QString("org.bluez"), QString("/"), interface, "InterfacesAdded", this, SLOT(newBluetoothDevice(QDBusObjectPath,QVariantMap)));

    if (!ret)
        return false;

    ret = system_bus.connect(QString("org.bluez"), QString("/"), interface, "InterfacesRemoved", this, SLOT(removeBluetoothDevice(QDBusObjectPath,QStringList)));

    /*
     * Unregister InterfacesAdded on error condition
     */
    if (!ret)
        system_bus.disconnect(QString("org.bluez"), QString("/"), interface, "InterfacesAdded", this, SLOT(newBluetoothDevice(QString,QVariantMap)));

    return ret;
}

bool DbusService::checkIfPlayer(const QString& path) const
{
    QRegExp regex("^.*/player\\d$");
    if (regex.exactMatch(path))
        return true;

    return false;
}

void DbusService::newBluetoothDevice(const QDBusObjectPath& item, const QVariantMap&)
{
    QString path = item.path();
    if (!checkIfPlayer(path))
        return;

    if (!getBluezPath().isEmpty()) {
        qWarning() << "Another Bluetooth Player already connected";
        return;
    }

    emit processPlaylistHide();

    QDBusConnection system_bus = QDBusConnection::systemBus();
    system_bus.connect(QString("org.bluez"), path, "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(processBluetoothEvent(QString,QVariantMap,QStringList)));

    setBluezPath(path);
}

void DbusService::removeBluetoothDevice(const QDBusObjectPath& item, const QStringList&)
{
    QString path = item.path();
    if (!checkIfPlayer(path))
        return;

    if (getBluezPath().isEmpty()) {
        qWarning() << "No Bluetooth Player connected";
        return;
    }

    QDBusConnection system_bus = QDBusConnection::systemBus();
    system_bus.disconnect(QString("org.bluez"), path, "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(processBluetoothEvent(QString,QVariantMap,QStringList)));

    setBluezPath(QString());
    emit processPlaylistShow();
}

void DbusService::processBluetoothEvent(const QString&, const QVariantMap& map, const QStringList&)
{
    if (map.contains("Track")) {
        QVariantMap track;
        map["Track"].value<QDBusArgument>() >> track;
        emit displayBluetoothMetadata(track["Artist"].toString(), track["Title"].toString(), track["Duration"].toInt());
    }

    if (map.contains("Position"))
        emit updatePosition(map["Position"].toInt());

    if (map.contains("Status"))
        emit updatePlayerStatus(map["Status"].toString());
}

void DbusService::processQMLEvent(const QString& state)
{
    QDBusInterface interface("org.bluez", getBluezPath(), "org.bluez.MediaPlayer1", QDBusConnection::systemBus());
    interface.call(state);
}

long DbusService::getCurrentPosition()
{
    QDBusInterface interface("org.bluez", getBluezPath(), "org.bluez.MediaPlayer1", QDBusConnection::systemBus());
    return interface.property("Position").toInt();
}