Adding CallVolume interface support in ofono-qt Bindings Signed-off-by: Arun Ravindra...
[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 <alexander.kanavin@nokia.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 #include <QtCore/QDebug>
27
28 #include "ofonointerface.h"
29
30 #define GET_PROPERTIES_TIMEOUT 300000
31 #define SET_PROPERTY_TIMEOUT 300000
32
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)
35 {
36     QDBusConnection::systemBus().connect("org.ofono", path, ifname, 
37                                              "PropertyChanged",
38                                              this,
39                                              SLOT(onPropertyChanged(QString, QDBusVariant)));
40     if (setting == OfonoGetAllOnStartup && path != "/")
41         m_properties = getAllPropertiesSync();
42 }
43
44 OfonoInterface::~OfonoInterface()
45 {
46 }
47
48 void OfonoInterface::setPath(const QString& path)
49 {
50     QDBusConnection::systemBus().disconnect("org.ofono", m_path, m_ifname, 
51                                              "PropertyChanged",
52                                              this,
53                                              SLOT(onPropertyChanged(QString, QDBusVariant)));
54     m_path = path;
55     QDBusConnection::systemBus().connect("org.ofono", m_path, m_ifname, 
56                                              "PropertyChanged",
57                                              this,
58                                              SLOT(onPropertyChanged(QString, QDBusVariant)));
59
60     if (m_getpropsetting == OfonoGetAllOnStartup)
61         m_properties = getAllPropertiesSync();
62     else
63         resetProperties();
64 }
65
66 QVariantMap OfonoInterface::properties() const
67 {
68     return m_properties;
69 }
70
71 void OfonoInterface::resetProperties()
72 {
73     m_properties = QVariantMap();
74 }
75
76 QVariantMap OfonoInterface::getAllPropertiesSync()
77 {
78     QDBusReply<QVariantMap> reply;
79     QVariantMap map;
80     QDBusMessage request;
81
82     request = QDBusMessage::createMethodCall("org.ofono",
83                                              m_path, m_ifname,
84                                              "GetProperties");
85     reply = QDBusConnection::systemBus().call(request);
86     map = reply;
87     foreach (QString property, map.keys()) {
88         emit propertyChanged(property, map[property]);
89     }
90     return map;
91 }
92
93 void OfonoInterface::requestProperty(const QString& name)
94 {
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());
99         return;
100     }
101     
102     if (m_properties.keys().contains(name)) {
103         emit requestPropertyComplete(true, name, m_properties[name]);
104         return;
105     }
106     
107     QDBusMessage request;
108
109     request = QDBusMessage::createMethodCall("org.ofono",
110                                              m_path, m_ifname,
111                                              "GetProperties");
112
113     bool result = QDBusConnection::systemBus().callWithCallback(request, this,
114                                         SLOT(getPropertiesAsyncResp(QVariantMap)),
115                                         SLOT(getPropertiesAsyncErr(const QDBusError&)),
116                                         GET_PROPERTIES_TIMEOUT);
117     if (!result) {
118         // FIXME: should indicate that sending a message failed
119         setError(QString(), QString("Sending a message failed"));
120         emit requestPropertyComplete(false, name, QVariant());
121         return;
122     }
123     m_pendingProperty = name;
124 }
125
126 void OfonoInterface::getPropertiesAsyncResp(QVariantMap properties)
127 {
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]);
133     } else {
134         // FIXME: should indicate that property is not available
135         setError(QString(), QString("Property not available"));
136         emit requestPropertyComplete(false, prop, QVariant());
137     }
138     foreach (QString property, properties.keys()) {
139         emit propertyChanged(property, properties[property]);
140     }
141 }
142
143 void OfonoInterface::getPropertiesAsyncErr(const QDBusError& error)
144 {
145     QString prop = m_pendingProperty;
146     setError(error.name(), error.message());
147     m_pendingProperty = QString();
148     emit requestPropertyComplete(false, prop, QVariant());
149 }
150
151 void OfonoInterface::onPropertyChanged(QString property, QDBusVariant value)
152 {
153     m_properties[property] = value.variant();
154     emit propertyChanged(property, value.variant());
155 }
156
157 void OfonoInterface::setProperty(const QString& name, const QVariant& property, const QString& password)
158 {
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);
163         return;
164     }
165
166     QDBusMessage request;
167     request = QDBusMessage::createMethodCall("org.ofono",
168                                              m_path, m_ifname,
169                                              "SetProperty");
170
171     QVariantList arguments;
172     arguments << QVariant(name) << QVariant::fromValue(QDBusVariant(property));
173     if (!password.isNull())
174         arguments << QVariant(password);
175
176     request.setArguments(arguments);
177     bool result = QDBusConnection::systemBus().callWithCallback(request, this,
178                                         SLOT(setPropertyResp()),
179                                         SLOT(setPropertyErr(const QDBusError&)),
180                                         SET_PROPERTY_TIMEOUT);
181     if (!result) {
182         // FIXME: should indicate that sending a message failed
183         setError(QString(), QString("Sending a message failed"));
184         emit setPropertyFailed(name);
185         return;
186     }
187     m_pendingProperty = name;
188 }
189
190 void OfonoInterface::setPropertyResp()
191 {
192     m_pendingProperty = QString();
193     // emit nothing; we will get a PropertyChanged signal
194 }
195
196 void OfonoInterface::setPropertyErr(const QDBusError& error)
197 {
198     QString prop = m_pendingProperty;
199     setError(error.name(), error.message());
200     m_pendingProperty = QString();
201     emit setPropertyFailed(prop);
202 }
203
204 void OfonoInterface::setError(const QString& errorName, const QString& errorMessage)
205 {
206     m_errorName = errorName;
207     m_errorMessage = errorMessage;
208 }