Change contact email address as the nokia one will be no longer valid soon
[profile/ivi/ofono-qt.git] / lib / ofonointerface.cpp
1 /*
2  * This file is part of ofono-qt
3  *
4  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
5  *
6  * Contact: Alexander Kanavin <alex.kanavin@gmail.com>
7  *
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.
11  *
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.
16  *
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
20  * 02110-1301 USA
21  *
22  */
23  
24 #include <QtDBus/QtDBus>
25 #include <QtCore/QObject>
26
27 #include "ofonointerface.h"
28
29 #define GET_PROPERTIES_TIMEOUT 300000
30 #define SET_PROPERTY_TIMEOUT 300000
31
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)
34 {
35     QDBusConnection::systemBus().connect("org.ofono", path, ifname, 
36                                              "PropertyChanged",
37                                              this,
38                                              SLOT(onPropertyChanged(QString, QDBusVariant)));
39     if (setting == OfonoGetAllOnStartup && path != "/")
40         m_properties = getAllPropertiesSync();
41 }
42
43 OfonoInterface::~OfonoInterface()
44 {
45 }
46
47 void OfonoInterface::setPath(const QString& path)
48 {
49     QDBusConnection::systemBus().disconnect("org.ofono", m_path, m_ifname, 
50                                              "PropertyChanged",
51                                              this,
52                                              SLOT(onPropertyChanged(QString, QDBusVariant)));
53     m_path = path;
54     QDBusConnection::systemBus().connect("org.ofono", m_path, m_ifname, 
55                                              "PropertyChanged",
56                                              this,
57                                              SLOT(onPropertyChanged(QString, QDBusVariant)));
58
59     if (m_getpropsetting == OfonoGetAllOnStartup)
60         m_properties = getAllPropertiesSync();
61     else
62         resetProperties();
63 }
64
65 QVariantMap OfonoInterface::properties() const
66 {
67     return m_properties;
68 }
69
70 void OfonoInterface::resetProperties()
71 {
72     m_properties = QVariantMap();
73 }
74
75 QVariantMap OfonoInterface::getAllPropertiesSync()
76 {
77     QDBusReply<QVariantMap> reply;
78     QVariantMap map;
79     QDBusMessage request;
80
81     request = QDBusMessage::createMethodCall("org.ofono",
82                                              m_path, m_ifname,
83                                              "GetProperties");
84     reply = QDBusConnection::systemBus().call(request);
85     map = reply;
86     foreach (QString property, map.keys()) {
87         emit propertyChanged(property, map[property]);
88     }
89     return map;
90 }
91
92 void OfonoInterface::requestProperty(const QString& name)
93 {
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());
98         return;
99     }
100     
101     if (m_properties.keys().contains(name)) {
102         emit requestPropertyComplete(true, name, m_properties[name]);
103         return;
104     }
105     
106     QDBusMessage request;
107
108     request = QDBusMessage::createMethodCall("org.ofono",
109                                              m_path, m_ifname,
110                                              "GetProperties");
111
112     bool result = QDBusConnection::systemBus().callWithCallback(request, this,
113                                         SLOT(getPropertiesAsyncResp(QVariantMap)),
114                                         SLOT(getPropertiesAsyncErr(const QDBusError&)),
115                                         GET_PROPERTIES_TIMEOUT);
116     if (!result) {
117         // FIXME: should indicate that sending a message failed
118         setError(QString(), QString("Sending a message failed"));
119         emit requestPropertyComplete(false, name, QVariant());
120         return;
121     }
122     m_pendingProperty = name;
123 }
124
125 void OfonoInterface::getPropertiesAsyncResp(QVariantMap properties)
126 {
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]);
132     } else {
133         // FIXME: should indicate that property is not available
134         setError(QString(), QString("Property not available"));
135         emit requestPropertyComplete(false, prop, QVariant());
136     }
137     foreach (QString property, properties.keys()) {
138         emit propertyChanged(property, properties[property]);
139     }
140 }
141
142 void OfonoInterface::getPropertiesAsyncErr(const QDBusError& error)
143 {
144     QString prop = m_pendingProperty;
145     setError(error.name(), error.message());
146     m_pendingProperty = QString();
147     emit requestPropertyComplete(false, prop, QVariant());
148 }
149
150 void OfonoInterface::onPropertyChanged(QString property, QDBusVariant value)
151 {
152     m_properties[property] = value.variant();
153     emit propertyChanged(property, value.variant());
154 }
155
156 void OfonoInterface::setProperty(const QString& name, const QVariant& property, const QString& password)
157 {
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);
162         return;
163     }
164
165     QDBusMessage request;
166     request = QDBusMessage::createMethodCall("org.ofono",
167                                              m_path, m_ifname,
168                                              "SetProperty");
169
170     QVariantList arguments;
171     arguments << QVariant(name) << QVariant::fromValue(QDBusVariant(property));
172     if (!password.isNull())
173         arguments << QVariant(password);
174
175     request.setArguments(arguments);
176     bool result = QDBusConnection::systemBus().callWithCallback(request, this,
177                                         SLOT(setPropertyResp()),
178                                         SLOT(setPropertyErr(const QDBusError&)),
179                                         SET_PROPERTY_TIMEOUT);
180     if (!result) {
181         // FIXME: should indicate that sending a message failed
182         setError(QString(), QString("Sending a message failed"));
183         emit setPropertyFailed(name);
184         return;
185     }
186     m_pendingProperty = name;
187 }
188
189 void OfonoInterface::setPropertyResp()
190 {
191     m_pendingProperty = QString();
192     // emit nothing; we will get a PropertyChanged signal
193 }
194
195 void OfonoInterface::setPropertyErr(const QDBusError& error)
196 {
197     QString prop = m_pendingProperty;
198     setError(error.name(), error.message());
199     m_pendingProperty = QString();
200     emit setPropertyFailed(prop);
201 }
202
203 void OfonoInterface::setError(const QString& errorName, const QString& errorMessage)
204 {
205     m_errorName = errorName;
206     m_errorMessage = errorMessage;
207 }