2 * This file is part of ofono-qt
4 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
6 * Contact: Alexander Kanavin <alexander.kanavin@nokia.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * version 2.1 as published by the Free Software Foundation.
12 * This library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 #include <QtDBus/QtDBus>
25 #include <QtCore/QObject>
26 #include <QtCore/QDebug>
28 #include "ofonointerface.h"
30 #define GET_PROPERTIES_TIMEOUT 300000
31 #define SET_PROPERTY_TIMEOUT 300000
33 OfonoInterface::OfonoInterface(const QString& path, const QString& ifname, GetPropertySetting setting, QObject *parent)
34 : QObject(parent) , m_path(path), m_ifname(ifname), m_getpropsetting(setting)
36 QDBusConnection::systemBus().connect("org.ofono", path, ifname,
39 SLOT(onPropertyChanged(QString, QDBusVariant)));
40 if (setting == GetAllOnStartup && path != "/")
41 m_properties = getAllPropertiesSync();
44 OfonoInterface::~OfonoInterface()
48 void OfonoInterface::setPath(const QString& path)
50 QDBusConnection::systemBus().disconnect("org.ofono", m_path, m_ifname,
53 SLOT(onPropertyChanged(QString, QDBusVariant)));
55 QDBusConnection::systemBus().connect("org.ofono", m_path, m_ifname,
58 SLOT(onPropertyChanged(QString, QDBusVariant)));
60 if (m_getpropsetting == GetAllOnStartup)
61 m_properties = getAllPropertiesSync();
66 QVariantMap OfonoInterface::properties() const
71 void OfonoInterface::resetProperties()
73 m_properties = QVariantMap();
76 QVariantMap OfonoInterface::getAllPropertiesSync()
78 QDBusReply<QVariantMap> reply;
82 request = QDBusMessage::createMethodCall("org.ofono",
85 reply = QDBusConnection::systemBus().call(request);
87 foreach (QString property, map.keys()) {
88 emit propertyChanged(property, map[property]);
93 void OfonoInterface::requestProperty(const QString& name)
95 if (m_pendingProperty.length() > 0) {
96 // FIXME: should indicate that a get/setProperty is already in progress
97 m_errorName = QString();
98 m_errorMessage = QString("Already in progress");
99 emit requestPropertyComplete(false, name, QVariant());
103 if (m_properties.keys().contains(name)) {
104 emit requestPropertyComplete(true, name, m_properties[name]);
108 QDBusMessage request;
110 request = QDBusMessage::createMethodCall("org.ofono",
114 bool result = QDBusConnection::systemBus().callWithCallback(request, this,
115 SLOT(getPropertiesAsyncResp(QVariantMap)),
116 SLOT(getPropertiesAsyncErr(const QDBusError&)),
117 GET_PROPERTIES_TIMEOUT);
119 // FIXME: should indicate that sending a message failed
120 m_errorName = QString();
121 m_errorMessage = QString("Sending a message failed");
122 emit requestPropertyComplete(false, name, QVariant());
125 m_pendingProperty = name;
128 void OfonoInterface::getPropertiesAsyncResp(QVariantMap properties)
130 QString prop = m_pendingProperty;
131 m_properties = properties;
132 m_pendingProperty = QString();
133 if (m_properties.keys().contains(prop)) {
134 emit requestPropertyComplete(true, prop, m_properties[prop]);
136 // FIXME: should indicate that property is not available
137 m_errorName = QString();
138 m_errorMessage = QString("Property not available");
139 emit requestPropertyComplete(false, prop, QVariant());
141 foreach (QString property, properties.keys()) {
142 emit propertyChanged(property, properties[property]);
146 void OfonoInterface::getPropertiesAsyncErr(const QDBusError& error)
148 QString prop = m_pendingProperty;
149 m_errorName = error.name();
150 m_errorMessage = error.message();
151 m_pendingProperty = QString();
152 emit requestPropertyComplete(false, prop, QVariant());
155 void OfonoInterface::onPropertyChanged(QString property, QDBusVariant value)
157 m_properties[property] = value.variant();
158 emit propertyChanged(property, value.variant());
161 void OfonoInterface::setProperty(const QString& name, const QVariant& property)
163 if (m_pendingProperty.length() > 0) {
164 // FIXME: should indicate that a get/setProperty is already in progress
165 m_errorName = QString();
166 m_errorMessage = QString("Already in progress");
167 emit setPropertyFailed(name);
171 QDBusMessage request;
173 request = QDBusMessage::createMethodCall("org.ofono",
176 request.setArguments(QList<QVariant>()
178 << QVariant::fromValue(QDBusVariant(property)));
179 bool result = QDBusConnection::systemBus().callWithCallback(request, this,
180 SLOT(setPropertyResp()),
181 SLOT(setPropertyErr(const QDBusError&)),
182 SET_PROPERTY_TIMEOUT);
184 // FIXME: should indicate that sending a message failed
185 m_errorName = QString();
186 m_errorMessage = QString();
187 emit setPropertyFailed(name);
190 m_pendingProperty = name;
193 void OfonoInterface::setPropertyResp()
195 m_pendingProperty = QString();
196 // emit nothing; we will get a PropertyChanged signal
199 void OfonoInterface::setPropertyErr(const QDBusError& error)
201 QString prop = m_pendingProperty;
202 m_errorName = error.name();
203 m_errorMessage = error.message();
204 m_pendingProperty = QString();
205 emit setPropertyFailed(prop);