Write a proper test for OfonoInterface
[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, GetPropertySetting 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 == GetAllOnStartup && 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 == GetAllOnStartup)
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         m_errorName = QString();
98         m_errorMessage = QString("Already in progress");
99         emit requestPropertyComplete(false, name, QVariant());
100         return;
101     }
102     
103     if (m_properties.keys().contains(name)) {
104         emit requestPropertyComplete(true, name, m_properties[name]);
105         return;
106     }
107     
108     QDBusMessage request;
109
110     request = QDBusMessage::createMethodCall("org.ofono",
111                                              m_path, m_ifname,
112                                              "GetProperties");
113
114     bool result = QDBusConnection::systemBus().callWithCallback(request, this,
115                                         SLOT(getPropertiesAsyncResp(QVariantMap)),
116                                         SLOT(getPropertiesAsyncErr(const QDBusError&)),
117                                         GET_PROPERTIES_TIMEOUT);
118     if (!result) {
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());
123         return;
124     }
125     m_pendingProperty = name;
126 }
127
128 void OfonoInterface::getPropertiesAsyncResp(QVariantMap properties)
129 {
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]);
135     } else {
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());
140     }
141     foreach (QString property, properties.keys()) {
142         emit propertyChanged(property, properties[property]);
143     }
144 }
145
146 void OfonoInterface::getPropertiesAsyncErr(const QDBusError& error)
147 {
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());
153 }
154
155 void OfonoInterface::onPropertyChanged(QString property, QDBusVariant value)
156 {
157     m_properties[property] = value.variant();
158     emit propertyChanged(property, value.variant());
159 }
160
161 void OfonoInterface::setProperty(const QString& name, const QVariant& property)
162 {
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);
168         return;
169     }
170
171     QDBusMessage request;
172
173     request = QDBusMessage::createMethodCall("org.ofono",
174                                              m_path, m_ifname,
175                                              "SetProperty");
176     request.setArguments(QList<QVariant>() 
177                         << QVariant(name) 
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);
183     if (!result) {
184         // FIXME: should indicate that sending a message failed
185         m_errorName = QString();
186         m_errorMessage = QString();
187         emit setPropertyFailed(name);
188         return;
189     }
190     m_pendingProperty = name;
191 }
192
193 void OfonoInterface::setPropertyResp()
194 {
195     m_pendingProperty = QString();
196     // emit nothing; we will get a PropertyChanged signal
197 }
198
199 void OfonoInterface::setPropertyErr(const QDBusError& error)
200 {
201     QString prop = m_pendingProperty;
202     m_errorName = error.name();
203     m_errorMessage = error.message();
204     m_pendingProperty = QString();
205     emit setPropertyFailed(prop);
206 }
207