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, OfonoGetPropertySetting 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 == OfonoGetAllOnStartup && 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 == OfonoGetAllOnStartup)
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 setError(QString(), QString("Already in progress"));
98 emit requestPropertyComplete(false, name, QVariant());
102 if (m_properties.keys().contains(name)) {
103 emit requestPropertyComplete(true, name, m_properties[name]);
107 QDBusMessage request;
109 request = QDBusMessage::createMethodCall("org.ofono",
113 bool result = QDBusConnection::systemBus().callWithCallback(request, this,
114 SLOT(getPropertiesAsyncResp(QVariantMap)),
115 SLOT(getPropertiesAsyncErr(const QDBusError&)),
116 GET_PROPERTIES_TIMEOUT);
118 // FIXME: should indicate that sending a message failed
119 setError(QString(), QString("Sending a message failed"));
120 emit requestPropertyComplete(false, name, QVariant());
123 m_pendingProperty = name;
126 void OfonoInterface::getPropertiesAsyncResp(QVariantMap properties)
128 QString prop = m_pendingProperty;
129 m_properties = properties;
130 m_pendingProperty = QString();
131 if (m_properties.keys().contains(prop)) {
132 emit requestPropertyComplete(true, prop, m_properties[prop]);
134 // FIXME: should indicate that property is not available
135 setError(QString(), QString("Property not available"));
136 emit requestPropertyComplete(false, prop, QVariant());
138 foreach (QString property, properties.keys()) {
139 emit propertyChanged(property, properties[property]);
143 void OfonoInterface::getPropertiesAsyncErr(const QDBusError& error)
145 QString prop = m_pendingProperty;
146 setError(error.name(), error.message());
147 m_pendingProperty = QString();
148 emit requestPropertyComplete(false, prop, QVariant());
151 void OfonoInterface::onPropertyChanged(QString property, QDBusVariant value)
153 m_properties[property] = value.variant();
154 emit propertyChanged(property, value.variant());
157 void OfonoInterface::setProperty(const QString& name, const QVariant& property, const QString& password)
159 if (m_pendingProperty.length() > 0) {
160 // FIXME: should indicate that a get/setProperty is already in progress
161 setError(QString(), QString("Already in progress"));
162 emit setPropertyFailed(name);
166 QDBusMessage request;
167 request = QDBusMessage::createMethodCall("org.ofono",
171 QVariantList arguments;
172 arguments << QVariant(name) << QVariant::fromValue(QDBusVariant(property));
173 if (!password.isNull())
174 arguments << QVariant(password);
176 request.setArguments(arguments);
177 bool result = QDBusConnection::systemBus().callWithCallback(request, this,
178 SLOT(setPropertyResp()),
179 SLOT(setPropertyErr(const QDBusError&)),
180 SET_PROPERTY_TIMEOUT);
182 // FIXME: should indicate that sending a message failed
183 setError(QString(), QString("Sending a message failed"));
184 emit setPropertyFailed(name);
187 m_pendingProperty = name;
190 void OfonoInterface::setPropertyResp()
192 m_pendingProperty = QString();
193 // emit nothing; we will get a PropertyChanged signal
196 void OfonoInterface::setPropertyErr(const QDBusError& error)
198 QString prop = m_pendingProperty;
199 setError(error.name(), error.message());
200 m_pendingProperty = QString();
201 emit setPropertyFailed(prop);
204 void OfonoInterface::setError(const QString& errorName, const QString& errorMessage)
206 m_errorName = errorName;
207 m_errorMessage = errorMessage;