2 * This file is part of ofono-qt
4 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
6 * Contact: Alexander Kanavin <alex.kanavin@gmail.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>
27 #include "ofonointerface.h"
29 #define GET_PROPERTIES_TIMEOUT 300000
30 #define SET_PROPERTY_TIMEOUT 300000
32 OfonoInterface::OfonoInterface(const QString& path, const QString& ifname, OfonoGetPropertySetting setting, QObject *parent)
33 : QObject(parent) , m_path(path), m_ifname(ifname), m_getpropsetting(setting)
35 QDBusConnection::systemBus().connect("org.ofono", path, ifname,
38 SLOT(onPropertyChanged(QString, QDBusVariant)));
39 if (setting == OfonoGetAllOnStartup && path != "/")
40 m_properties = getAllPropertiesSync();
43 OfonoInterface::~OfonoInterface()
47 void OfonoInterface::setPath(const QString& path)
49 QDBusConnection::systemBus().disconnect("org.ofono", m_path, m_ifname,
52 SLOT(onPropertyChanged(QString, QDBusVariant)));
54 QDBusConnection::systemBus().connect("org.ofono", m_path, m_ifname,
57 SLOT(onPropertyChanged(QString, QDBusVariant)));
59 if (m_getpropsetting == OfonoGetAllOnStartup)
60 m_properties = getAllPropertiesSync();
65 QVariantMap OfonoInterface::properties() const
70 void OfonoInterface::resetProperties()
72 m_properties = QVariantMap();
75 QVariantMap OfonoInterface::getAllPropertiesSync()
77 QDBusReply<QVariantMap> reply;
81 request = QDBusMessage::createMethodCall("org.ofono",
84 reply = QDBusConnection::systemBus().call(request);
86 foreach (QString property, map.keys()) {
87 emit propertyChanged(property, map[property]);
92 void OfonoInterface::requestProperty(const QString& name)
94 if (m_pendingProperty.length() > 0) {
95 // FIXME: should indicate that a get/setProperty is already in progress
96 setError(QString(), QString("Already in progress"));
97 emit requestPropertyComplete(false, name, QVariant());
101 if (m_properties.keys().contains(name)) {
102 emit requestPropertyComplete(true, name, m_properties[name]);
106 QDBusMessage request;
108 request = QDBusMessage::createMethodCall("org.ofono",
112 bool result = QDBusConnection::systemBus().callWithCallback(request, this,
113 SLOT(getPropertiesAsyncResp(QVariantMap)),
114 SLOT(getPropertiesAsyncErr(const QDBusError&)),
115 GET_PROPERTIES_TIMEOUT);
117 // FIXME: should indicate that sending a message failed
118 setError(QString(), QString("Sending a message failed"));
119 emit requestPropertyComplete(false, name, QVariant());
122 m_pendingProperty = name;
125 void OfonoInterface::getPropertiesAsyncResp(QVariantMap properties)
127 QString prop = m_pendingProperty;
128 m_properties = properties;
129 m_pendingProperty = QString();
130 if (m_properties.keys().contains(prop)) {
131 emit requestPropertyComplete(true, prop, m_properties[prop]);
133 // FIXME: should indicate that property is not available
134 setError(QString(), QString("Property not available"));
135 emit requestPropertyComplete(false, prop, QVariant());
137 foreach (QString property, properties.keys()) {
138 emit propertyChanged(property, properties[property]);
142 void OfonoInterface::getPropertiesAsyncErr(const QDBusError& error)
144 QString prop = m_pendingProperty;
145 setError(error.name(), error.message());
146 m_pendingProperty = QString();
147 emit requestPropertyComplete(false, prop, QVariant());
150 void OfonoInterface::onPropertyChanged(QString property, QDBusVariant value)
152 m_properties[property] = value.variant();
153 emit propertyChanged(property, value.variant());
156 void OfonoInterface::setProperty(const QString& name, const QVariant& property, const QString& password)
158 if (m_pendingProperty.length() > 0) {
159 // FIXME: should indicate that a get/setProperty is already in progress
160 setError(QString(), QString("Already in progress"));
161 emit setPropertyFailed(name);
165 QDBusMessage request;
166 request = QDBusMessage::createMethodCall("org.ofono",
170 QVariantList arguments;
171 arguments << QVariant(name) << QVariant::fromValue(QDBusVariant(property));
172 if (!password.isNull())
173 arguments << QVariant(password);
175 request.setArguments(arguments);
176 bool result = QDBusConnection::systemBus().callWithCallback(request, this,
177 SLOT(setPropertyResp()),
178 SLOT(setPropertyErr(const QDBusError&)),
179 SET_PROPERTY_TIMEOUT);
181 // FIXME: should indicate that sending a message failed
182 setError(QString(), QString("Sending a message failed"));
183 emit setPropertyFailed(name);
186 m_pendingProperty = name;
189 void OfonoInterface::setPropertyResp()
191 m_pendingProperty = QString();
192 // emit nothing; we will get a PropertyChanged signal
195 void OfonoInterface::setPropertyErr(const QDBusError& error)
197 QString prop = m_pendingProperty;
198 setError(error.name(), error.message());
199 m_pendingProperty = QString();
200 emit setPropertyFailed(prop);
203 void OfonoInterface::setError(const QString& errorName, const QString& errorMessage)
205 m_errorName = errorName;
206 m_errorMessage = errorMessage;