~mateo-salta/uvolman/uvolman

« back to all changes in this revision

Viewing changes to backend/UVolMan/DBus/PulseRestoreEntry.cpp

  • Committer: Devid Antonio Filoni
  • Date: 2016-07-29 19:40:57 UTC
  • Revision ID: git-v1:cdfff2c72a8eb5247b10da7d60874d8a3f600168
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of uvolman.dfiloni
 
3
 *
 
4
 * Copyright (C) 2016 Devid Antonio Filoni https://launchpad.net/~d.filoni
 
5
 *
 
6
 * This program is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
#include "PulseRestoreEntry.h"
 
21
#include "DBusRestoreEntryVolume.h"
 
22
 
 
23
#include <QDBusInterface>
 
24
#include <QDBusReply>
 
25
#include <QMetaType>
 
26
#include <QDBusMetaType>
 
27
#include <QDebug>
 
28
 
 
29
#define PA_VOLUME_NORM 65536
 
30
Q_DECLARE_METATYPE(DBusRestoreEntryVolume)
 
31
 
 
32
PulseRestoreEntry::PulseRestoreEntry(QObject *parent, const QString &peerConnName, const QString &entryPath)
 
33
    : QObject(parent),
 
34
      conn(peerConnName)
 
35
{
 
36
    qDBusRegisterMetaType<DBusRestoreEntryVolume>();
 
37
 
 
38
    path = entryPath;
 
39
 
 
40
    name = getProperty("Name").toString();
 
41
    device = getProperty("Device").toString();
 
42
 
 
43
    QList<uint> tmpList;
 
44
    double tmp;
 
45
    const QDBusArgument &volumeArg = getProperty("Volume").value<QDBusArgument>();
 
46
    volumeArg.beginArray();
 
47
    while (!volumeArg.atEnd())
 
48
        volumeArg >> tmpList;
 
49
    volumeArg.endArray();
 
50
    tmp = tmpList.last();
 
51
    volume = tmp/PA_VOLUME_NORM;
 
52
 
 
53
    isMute = getProperty("Mute").toBool();
 
54
 
 
55
    bool connected;
 
56
    connected = conn.connect("org.PulseAudio1",
 
57
                            path,
 
58
                            "org.PulseAudio.Ext.StreamRestore1.RestoreEntry",
 
59
                            "VolumeUpdated",
 
60
                            this,
 
61
                            SLOT(VolumeUpdated(QDBusMessage)));
 
62
   if (!connected)
 
63
       qWarning() << "[FAILED] PulseStreamRestore[message=signalConnFailed; signal=VolumeUpdated; entry=" << path << "]: " << conn.lastError();
 
64
}
 
65
 
 
66
 
 
67
QString PulseRestoreEntry::getName()
 
68
{
 
69
    return name;
 
70
}
 
71
 
 
72
double PulseRestoreEntry::getVolume()
 
73
{
 
74
    return volume;
 
75
}
 
76
 
 
77
bool PulseRestoreEntry::setVolume(const double &newVolume)
 
78
{
 
79
    if (newVolume != volume)
 
80
    {
 
81
        if (setProperty("Volume", QVariant::fromValue(newVolume)))
 
82
        {
 
83
            volume = newVolume;
 
84
            Q_EMIT volumeChanged(QVariant::fromValue(volume));
 
85
        }
 
86
    }
 
87
    return volume == newVolume;
 
88
}
 
89
 
 
90
QVariant PulseRestoreEntry::getProperty(const QString &property)
 
91
{
 
92
 
 
93
    QDBusInterface interface (
 
94
                "org.PulseAudio1",
 
95
                path,
 
96
                "org.freedesktop.DBus.Properties",
 
97
                conn,
 
98
                this);
 
99
 
 
100
    if (interface.isValid()) {
 
101
        QDBusReply<QDBusVariant> reply = interface.call(
 
102
                    "Get",
 
103
                    "org.PulseAudio.Ext.StreamRestore1.RestoreEntry",
 
104
                    property);
 
105
        if (reply.isValid()) {
 
106
            return reply.value().variant();
 
107
        }
 
108
        else
 
109
            qWarning() << "[FAILED] PulseRestoreEntry[path=" << path << "; property=" << property << "; action=GET]: " << reply.error();
 
110
    }
 
111
    return QVariant();
 
112
}
 
113
 
 
114
bool PulseRestoreEntry::setProperty(const QString &property, const QVariant &value)
 
115
{
 
116
    QDBusInterface interface (
 
117
                "org.PulseAudio1",
 
118
                path,
 
119
                "org.freedesktop.DBus.Properties",
 
120
                conn,
 
121
                this);
 
122
 
 
123
    if (interface.isValid())
 
124
    {
 
125
        QVariant newValue;
 
126
        if (property == "Volume")
 
127
        {
 
128
            uint tmp = (value.toDouble()*PA_VOLUME_NORM);
 
129
            DBusRestoreEntryVolume volume = DBusRestoreEntryVolume(((uint) 0), tmp);
 
130
            QDBusArgument argument;
 
131
            argument.beginArray(qMetaTypeId<DBusRestoreEntryVolume>() );
 
132
            argument << volume;
 
133
            argument.endStructure();
 
134
 
 
135
            newValue = QVariant::fromValue(QDBusVariant(QVariant::fromValue(argument)));
 
136
        }
 
137
        else
 
138
            newValue = QVariant::fromValue(QDBusVariant(value));
 
139
 
 
140
        QDBusMessage message = interface.call("Set",
 
141
                                      "org.PulseAudio.Ext.StreamRestore1.RestoreEntry",
 
142
                                      property,
 
143
                                      QVariant::fromValue(newValue));
 
144
        if (message.type() == QDBusMessage::ErrorMessage)
 
145
            qWarning() << "[FAILED] PulseRestoreEntry[path=" << path << "; property=" << property << "; value=" << value << "; action=SET]: " << message.errorMessage();
 
146
 
 
147
        return message.type() == QDBusMessage::ReplyMessage;
 
148
    }
 
149
    else
 
150
        return false;
 
151
}
 
152
 
 
153
void PulseRestoreEntry::VolumeUpdated(const QDBusMessage &message)
 
154
{
 
155
    qWarning() << "[PulseRestoreEntry#TODO] IMPLEMENT PARSING OF MESSAGE: " << message;
 
156
    /* volume = ...
 
157
    Q_EMIT volumeChanged(QVariant::fromValue(volume)); */
 
158
}