homescreen: Change from qmake to meson
[apps/homescreen.git] / homescreen / src / mastervolume.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 "mastervolume.h"
18 #include <QTimer>
19 #include <QtDebug>
20
21 MasterVolume::MasterVolume(QObject* parent) :
22         QObject(parent),
23         m_volume(50)
24 {
25         VehicleSignalsConfig vsConfig("homescreen");
26         m_vs = new VehicleSignals(vsConfig);
27
28         if (m_vs) {
29                 QObject::connect(m_vs, &VehicleSignals::connected, this, &MasterVolume::onConnected);
30                 QObject::connect(m_vs, &VehicleSignals::authorized, this, &MasterVolume::onAuthorized);
31                 QObject::connect(m_vs, &VehicleSignals::disconnected, this, &MasterVolume::onDisconnected);
32
33                 m_vs->connect();
34         }
35 }
36
37 qint32 MasterVolume::getVolume() const
38 {
39         return m_volume;
40 }
41
42 void MasterVolume::setVolume(qint32 volume)
43 {
44         if (m_volume == volume)
45                 return;
46
47         m_volume = volume;
48
49         if (!(m_vs && m_connected))
50                 return;
51
52         m_vs->set("Vehicle.Cabin.Infotainment.Media.Volume", QString::number(volume));
53 }
54
55 void MasterVolume::onConnected()
56 {
57         if (!m_vs)
58                 return;
59
60         m_vs->authorize();
61 }
62
63 void MasterVolume::onAuthorized()
64 {
65         if (!m_vs)
66                 return;
67
68         m_connected = true;
69
70         QObject::connect(m_vs, &VehicleSignals::getSuccessResponse, this, &MasterVolume::onGetSuccessResponse);
71         QObject::connect(m_vs, &VehicleSignals::signalNotification, this, &MasterVolume::onSignalNotification);
72
73         m_vs->subscribe("Vehicle.Cabin.Infotainment.Media.Volume");
74         m_vs->get("Vehicle.Cabin.Infotainment.Media.Volume");
75 }
76
77 void MasterVolume::onDisconnected()
78 {
79         QObject::disconnect(m_vs, &VehicleSignals::signalNotification, this, &MasterVolume::onGetSuccessResponse);
80         QObject::disconnect(m_vs, &VehicleSignals::signalNotification, this, &MasterVolume::onSignalNotification);
81
82         m_connected = false;
83 }
84
85 void MasterVolume::updateVolume(QString value)
86 {
87         bool ok;
88         qint32 volume = value.toInt(&ok);
89         if (ok) {
90                 volume = qBound(0, volume, 100);
91                 if (m_volume != volume) {
92                         m_volume = volume;
93                         emit VolumeChanged();
94                 }
95         }
96 }
97
98 void MasterVolume::onGetSuccessResponse(QString path, QString value, QString timestamp)
99 {
100         if (path == "Vehicle.Cabin.Infotainment.Media.Volume") {
101                 updateVolume(value);
102                 emit VolumeChanged();
103         }
104 }
105
106 void MasterVolume::onSignalNotification(QString path, QString value, QString timestamp)
107 {
108         if (path == "Vehicle.Cabin.Infotainment.Media.Volume")
109                 updateVolume(value);
110 }