summaryrefslogtreecommitdiff
path: root/daemon/watchcommands.cpp
blob: b16efb50c68ed7e37bdef85ca4e3e77f1fd8074a (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
#include "watchcommands.h"

#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusReply>

using namespace watch;

WatchCommands::WatchCommands(WatchConnector *watch, QObject *parent) :
    QObject(parent), watch(watch)
{}

void WatchCommands::processMessage(uint endpoint, QByteArray data)
{
    logger()->debug() << __FUNCTION__ << endpoint << "/" << data.toHex() << data.length();
    switch (endpoint) {
    case WatchConnector::watchPHONE_VERSION:
        watch->sendPhoneVersion();
        break;
    case WatchConnector::watchPHONE_CONTROL:
        if (data.at(0) == WatchConnector::callHANGUP) {
            emit hangup();
        }
        break;
    case WatchConnector::watchMUSIC_CONTROL:
        musicControl(WatchConnector::MusicControl(data.at(0)));
        break;
    case WatchConnector::watchSYSTEM_MESSAGE:
        logger()->info() << "Got SYSTEM_MESSAGE" << WatchConnector::SystemMessage(data.at(0));
        // TODO: handle systemBLUETOOTH_START_DISCOVERABLE/systemBLUETOOTH_END_DISCOVERABLE
        break;

    default:
        logger()->info() << __FUNCTION__ << "endpoint" << endpoint << "not supported yet";
    }
}

void WatchCommands::onMprisMetadataChanged(QVariantMap metadata)
{
    QString track = metadata.value("xesam:title").toString();
    QString album = metadata.value("xesam:album").toString();
    QString artist = metadata.value("xesam:artist").toString();
    logger()->debug() << __FUNCTION__ << track << album << artist;
    watch->sendMusicNowPlaying(track, album, artist);
}

void WatchCommands::musicControl(WatchConnector::MusicControl operation)
{
    logger()->debug() << "Operation:" << operation;

    QString mpris = parent()->property("mpris").toString();
    if (mpris.isEmpty()) {
        logger()->debug() << "No mpris interface active";
        return;
    }

    QString method;

    switch(operation) {
    case WatchConnector::musicPLAY_PAUSE:
        method = "PlayPause";
        break;
    case WatchConnector::musicPAUSE:
        method = "Pause";
        break;
    case WatchConnector::musicPLAY:
        method = "Play";
        break;
    case WatchConnector::musicNEXT:
        method = "Next";
        break;
    case WatchConnector::musicPREVIOUS:
        method = "Previous";
        break;
    case WatchConnector::musicVOLUME_UP:
    case WatchConnector::musicVOLUME_DOWN: {
            QDBusReply<QDBusVariant> VolumeReply = QDBusConnection::sessionBus().call(
                        QDBusMessage::createMethodCall(mpris, "/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Properties", "Get")
                        << "org.mpris.MediaPlayer2.Player" << "Volume");
            if (VolumeReply.isValid()) {
                double volume = VolumeReply.value().variant().toDouble();
                if (operation == WatchConnector::musicVOLUME_UP) {
                    volume += 0.1;
                }
                else {
                    volume -= 0.1;
                }
                logger()->debug() << "Setting volume" << volume;
                QDBusError err = QDBusConnection::sessionBus().call(
                            QDBusMessage::createMethodCall(mpris, "/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Properties", "Set")
                            << "org.mpris.MediaPlayer2.Player" << "Volume" << QVariant::fromValue(QDBusVariant(volume)));
                if (err.isValid()) {
                    logger()->error() << err.message();
                }
            }
            else {
                logger()->error() << VolumeReply.error().message();
            }
        }
        return;
    case WatchConnector::musicGET_NOW_PLAYING:
        onMprisMetadataChanged(parent()->property("mprisMetadata").toMap());
        return;

    case WatchConnector::musicSEND_NOW_PLAYING:
        logger()->warn() << "Operation" << operation << "not supported";
        return;
    }

    if (method.isEmpty()) {
        logger()->error() << "Requested unsupported operation" << operation;
        return;
    }

    logger()->debug() << operation << "->" << method;

    QDBusError err = QDBusConnection::sessionBus().call(
                QDBusMessage::createMethodCall(mpris, "/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2.Player", method));
    if (err.isValid()) {
        logger()->error() << err.message();
    }
}