|
| 1 | +/* |
| 2 | + * Copyright (C) 2019 Open Mobile Platform LLС. <[email protected]> |
| 3 | + * |
| 4 | + * You may use this file under the terms of the BSD license as follows: |
| 5 | + * |
| 6 | + * "Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are |
| 8 | + * met: |
| 9 | + * * Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * * Redistributions in binary form must reproduce the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer in |
| 13 | + * the documentation and/or other materials provided with the |
| 14 | + * distribution. |
| 15 | + * * Neither the name of Nemo Mobile nor the names of its contributors |
| 16 | + * may be used to endorse or promote products derived from this |
| 17 | + * software without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 30 | + */ |
| 31 | + |
| 32 | + |
| 33 | +#include "nfcsettings.h" |
| 34 | + |
| 35 | +#include <QDBusConnection> |
| 36 | +#include <QDBusConnectionInterface> |
| 37 | +#include <QDebug> |
| 38 | + |
| 39 | +NfcSettings::NfcSettings(QObject *parent) |
| 40 | + : QObject(parent) |
| 41 | + , m_valid(false) |
| 42 | + , m_enabled(false) |
| 43 | + , m_available(false) |
| 44 | +{ |
| 45 | + m_interface = new QDBusInterface("org.sailfishos.nfc.settings", |
| 46 | + "/", |
| 47 | + "org.sailfishos.nfc.Settings", |
| 48 | + QDBusConnection::systemBus(), |
| 49 | + this); |
| 50 | + if (QDBusConnection::systemBus().interface()->isServiceRegistered("org.sailfishos.nfc.settings")) { |
| 51 | + m_available = true; |
| 52 | + emit availableChanged(); |
| 53 | + |
| 54 | + QDBusPendingCall pcall = m_interface->asyncCall(QLatin1String("GetEnabled")); |
| 55 | + QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this); |
| 56 | + |
| 57 | + connect(watcher, &QDBusPendingCallWatcher::finished, this, &NfcSettings::getEnableStateFinished); |
| 58 | + QDBusConnection::systemBus().connect("org.sailfishos.nfc.settings", "/", "org.sailfishos.nfc.Settings", "EnabledChanged", this, SLOT(updateEnabledState(bool))); |
| 59 | + |
| 60 | + } else { |
| 61 | + qWarning() << "NFC interface not available"; |
| 62 | + qWarning() << m_interface->lastError(); |
| 63 | + } |
| 64 | + |
| 65 | +} |
| 66 | + |
| 67 | +NfcSettings::~NfcSettings() |
| 68 | +{ |
| 69 | +} |
| 70 | + |
| 71 | +bool NfcSettings::valid() const |
| 72 | +{ |
| 73 | + return m_valid; |
| 74 | +} |
| 75 | + |
| 76 | +bool NfcSettings::available() const |
| 77 | +{ |
| 78 | + return m_available; |
| 79 | +} |
| 80 | + |
| 81 | +bool NfcSettings::enabled() const |
| 82 | +{ |
| 83 | + return m_enabled; |
| 84 | +} |
| 85 | + |
| 86 | +void NfcSettings::setEnabled(bool enabled) |
| 87 | +{ |
| 88 | + m_interface->asyncCall("SetEnabled", enabled); |
| 89 | +} |
| 90 | + |
| 91 | +void NfcSettings::getEnableStateFinished(QDBusPendingCallWatcher *call) |
| 92 | +{ |
| 93 | + QDBusPendingReply<bool> reply = *call; |
| 94 | + if (reply.isError()) { |
| 95 | + qWarning() << "Get dbus error:" << reply.error(); |
| 96 | + } else { |
| 97 | + updateEnabledState(reply.value()); |
| 98 | + m_valid = true; |
| 99 | + emit validChanged(); |
| 100 | + } |
| 101 | + call->deleteLater(); |
| 102 | +} |
| 103 | + |
| 104 | +void NfcSettings::updateEnabledState(bool enabled) |
| 105 | +{ |
| 106 | + if (enabled != m_enabled) { |
| 107 | + m_enabled = enabled; |
| 108 | + emit enabledChanged(); |
| 109 | + } |
| 110 | +} |
0 commit comments