Lomiri
Loading...
Searching...
No Matches
displays.cpp
1/*
2 * Copyright (C) 2016 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors:
17 * Ken VanDine <ken.vandine@canonical.com>
18 *
19 */
20
21#include "displays.h"
22#include <org_aethercast.h>
23#include "dbus-shared.h"
24
25#include <QQmlEngine>
26
27Displays::Displays(QObject *parent):
28 Displays(QDBusConnection::systemBus(), parent)
29{
30}
31
32Displays::Displays(const QDBusConnection &dbus, QObject *parent):
33 QObject(parent),
34 m_dbus(dbus)
35{
36 m_manager.reset(new OrgAethercastManagerInterface(AETHERCAST_SERVICE, AETHERCAST_PATH, m_dbus));
37 m_manager->setTimeout(1000);
38
39 m_aethercastProperties.reset(new OrgFreedesktopDBusPropertiesInterface(AETHERCAST_SERVICE, AETHERCAST_PATH, m_dbus));
40 m_aethercastProperties->setTimeout(1000);
41
42 QObject::connect(m_aethercastProperties.data(), SIGNAL(PropertiesChanged(const QString&, const QVariantMap&, const QStringList&)),
43 this, SLOT(slotPropertiesChanged(const QString&, const QVariantMap&, const QStringList&)));
44
45 getAll();
46 connect(m_dbus.interface(), &QDBusConnectionInterface::serviceRegistered, this, [=](QString service) {
47 if (service != AETHERCAST_SERVICE)
48 return;
49
50 getAll();
51 });
52}
53
54void Displays::getAll()
55{
56 watchCall(m_aethercastProperties->GetAll(AETHERCAST_MANAGER_IFACE), [=](QDBusPendingCallWatcher *watcher) {
57 QDBusPendingReply<QVariantMap> reply = *watcher;
58
59 if (reply.isError()) {
60 qWarning() << "Failed to retrieve properties for manager";
61 watcher->deleteLater();
62 return;
63 }
64
65 auto properties = reply.argumentAt<0>();
66 setProperties(properties);
67 watcher->deleteLater();
68 });
69}
70
71void Displays::slotPropertiesChanged(const QString &interface, const QVariantMap &changedProperties,
72 const QStringList &invalidatedProperties)
73{
74 Q_UNUSED(invalidatedProperties);
75
76 if (interface != AETHERCAST_MANAGER_IFACE)
77 return;
78
79 setProperties(changedProperties);
80}
81
82void Displays::setProperties(const QMap<QString,QVariant> &properties)
83{
84 QMapIterator<QString,QVariant> it(properties);
85 while (it.hasNext()) {
86 it.next();
87 updateProperty(it.key(), it.value());
88 }
89}
90
91void Displays::setEnabled(bool enabled)
92{
93 if (!m_manager)
94 return;
95
96 m_manager->setEnabled(enabled);
97 Q_EMIT enabledChanged(enabled);
98}
99
100void Displays::handleConnectError(QDBusError error)
101{
102 if (error.name() == "org.aethercast.Error.None")
103 Q_EMIT(connectError(Error::None));
104 else if (error.name() == "org.aethercast.Error.Failed")
105 Q_EMIT(connectError(Error::Failed));
106 else if (error.name() == "org.aethercast.Error.Already")
107 Q_EMIT(connectError(Error::Already));
108 else if (error.name() == "org.aethercast.Error.ParamInvalid")
109 Q_EMIT(connectError(Error::ParamInvalid));
110 else if (error.name() == "org.aethercast.Error.InvalidState")
111 Q_EMIT(connectError(Error::InvalidState));
112 else if (error.name() == "org.aethercast.Error.NotConnected")
113 Q_EMIT(connectError(Error::NotConnected));
114 else if (error.name() == "org.aethercast.Error.NotReady")
115 Q_EMIT(connectError(Error::NotReady));
116 else
117 Q_EMIT(connectError(Error::Unknown));
118}
119
120void Displays::callFinishedSlot(QDBusPendingCallWatcher *call)
121{
122 QDBusPendingReply<void> reply = *call;
123 if (reply.isFinished() && reply.isError())
124 handleConnectError(reply.error());
125 call->deleteLater();
126}
127
128void Displays::updateProperty(const QString &key, const QVariant &value)
129{
130 if (key == "Scanning")
131 Q_EMIT(scanningChanged(value.toBool()));
132 if (key == "State")
133 Q_EMIT(stateChanged());
134 if (key == "Enabled")
135 Q_EMIT(enabledChanged(value.toBool()));
136}