Initial commit
[profile/ivi/ofono-qt.git] / lib / ofonocallbarring.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 <QtCore/QObject>
25 #include <QtDBus/QtDBus>
26 #include "ofonocallbarring.h"
27
28 #define SET_PROPERTY_TIMEOUT 300000
29
30 OfonoCallBarring::OfonoCallBarring(QString modemId, QObject *parent)
31     : OfonoModemInterface(modemId, "org.ofono.CallBarring", OfonoInterface::GetAllOnFirstRequest, parent)
32 {
33     connect(this, SIGNAL(propertyChanged(const QString&, const QVariant&)), 
34             this, SLOT(propertyChanged(const QString&, const QVariant&)));
35     connect(this, SIGNAL(setPropertyFailed(const QString&)), 
36             this, SLOT(setPropertyFailed(const QString&)));
37     connect(this, SIGNAL(requestPropertyComplete(bool, const QString&, const QVariant&)),
38             this, SLOT(requestPropertyComplete(bool, const QString&, const QVariant&)));
39     QDBusConnection::systemBus().connect("org.ofono", path(), ifname(), 
40                                          "IncomingBarringInEffect",
41                                          this,
42                                          SIGNAL(incomingBarringInEffect()));
43     QDBusConnection::systemBus().connect("org.ofono", path(), ifname(), 
44                                          "OutgoingBarringInEffect",
45                                          this,
46                                          SIGNAL(outgoingBarringInEffect()));
47     m_pendingProperty = QString();
48 }
49
50 OfonoCallBarring::~OfonoCallBarring()
51 {
52 }
53
54 void OfonoCallBarring::setProperty(const QString& name, 
55                                    const QVariant& property, 
56                                    const QString& password)
57 {
58     if (m_pendingProperty.length() > 0) {
59         // FIXME: should indicate that a setProperty is already in progress
60         m_errorName = QString();
61         m_errorMessage = QString("Operation already in progress");
62         emit setPropertyFailed(name);
63         return;
64     }
65
66
67     QDBusMessage request;
68
69     request = QDBusMessage::createMethodCall("org.ofono",
70                                              path(), ifname(),
71                                              "SetProperty");
72     request.setArguments(QList<QVariant>() 
73                          << QVariant(name) 
74                          << QVariant::fromValue(QDBusVariant(property))
75                          << QVariant(password));
76
77     bool result = QDBusConnection::systemBus().callWithCallback(request, this,
78                                         SLOT(setPropertyResp()),
79                                         SLOT(setPropertyErr(const QDBusError&)),
80                                         SET_PROPERTY_TIMEOUT);
81     if (!result) {
82         // FIXME: should indicate that sending a message failed
83         m_errorName = QString();
84         m_errorMessage = QString("DBUS sending failed");
85         emit setPropertyFailed(name);
86         return;
87     }
88     m_pendingProperty = name;
89
90
91 }
92
93 void OfonoCallBarring::setPropertyResp()
94 {
95     m_pendingProperty = QString();
96     // emit nothing; we will get a PropertyChanged signal
97 }
98
99 void OfonoCallBarring::setPropertyErr(const QDBusError& error)
100 {
101     QString prop = m_pendingProperty;
102     m_errorName = error.name();
103     m_errorMessage = error.message();
104     m_pendingProperty = QString();
105     emit setPropertyFailed(prop);
106 }
107
108
109 void OfonoCallBarring::requestChangePassword(QString old_password, 
110                                              QString new_password)
111 {
112     QDBusMessage request;
113
114     request = QDBusMessage::createMethodCall("org.ofono",
115                                              path(), ifname(),
116                                              "ChangePassword");
117     request << old_password << new_password;
118
119     QDBusConnection::systemBus().callWithCallback(request, this,
120                                         SLOT(changePasswordResp()),
121                                         SLOT(changePasswordErr(const QDBusError&)));
122 }
123
124 void OfonoCallBarring::requestDisableAll(QString password)
125 {
126     QDBusMessage request;
127
128     request = QDBusMessage::createMethodCall("org.ofono",
129                                              path(), ifname(),
130                                              "DisableAll");
131     request << password;
132
133     QDBusConnection::systemBus().callWithCallback(request, this,
134                                         SLOT(disableAllResp()),
135                                         SLOT(disableAllErr(const QDBusError&)));
136 }
137
138 void OfonoCallBarring::requestDisableAllIncoming(QString password)
139 {
140     QDBusMessage request;
141
142     request = QDBusMessage::createMethodCall("org.ofono",
143                                              path(), ifname(),
144                                              "DisableAllIncoming");
145     request << password;
146
147     QDBusConnection::systemBus().callWithCallback(request, this,
148                                         SLOT(disableAllIncomingResp()),
149                                         SLOT(disableAllIncomingErr(const QDBusError&)));
150 }
151
152 void OfonoCallBarring::requestDisableAllOutgoing(QString password)
153 {
154     QDBusMessage request;
155
156     request = QDBusMessage::createMethodCall("org.ofono",
157                                              path(), ifname(),
158                                              "DisableAllOutgoing");
159     request << password;
160
161     QDBusConnection::systemBus().callWithCallback(request, this,
162                                         SLOT(disableAllOutgoingResp()),
163                                         SLOT(disableAllOutgoingErr(const QDBusError&)));
164 }
165
166
167 void OfonoCallBarring::requestVoiceIncoming()
168 {
169     requestProperty("VoiceIncoming");
170 }
171
172 void OfonoCallBarring::setVoiceIncoming(QString barrings, QString password)
173 {
174     setProperty("VoiceIncoming", qVariantFromValue(barrings), password);
175 }
176
177 void OfonoCallBarring::requestVoiceOutgoing()
178 {
179     requestProperty("VoiceOutgoing");
180 }
181
182 void OfonoCallBarring::setVoiceOutgoing(QString barrings, QString password)
183 {
184     setProperty("VoiceOutgoing", qVariantFromValue(barrings), password);
185 }
186
187 void OfonoCallBarring::propertyChanged(const QString& property, const QVariant& value)
188 {
189     if (property == "VoiceIncoming") {  
190         emit voiceIncomingChanged(value.value<QString>());
191     } else  if (property == "VoiceOutgoing") {  
192         emit voiceOutgoingChanged(value.value<QString>());
193     }
194 }
195
196 void OfonoCallBarring::setPropertyFailed(const QString& property)
197 {
198     if (property == "VoiceIncoming") {  
199         emit setVoiceIncomingFailed();
200     } else if (property == "VoiceOutgoing") {   
201         emit setVoiceOutgoingFailed();
202     }  
203 }
204
205 void OfonoCallBarring::requestPropertyComplete(bool success, const QString& property, const QVariant& value)
206 {
207     if (property == "VoiceIncoming") {  
208         success ? emit voiceIncomingComplete(true, value.value<QString>()) : emit voiceIncomingComplete(false, QString());
209     } else if (property == "VoiceOutgoing") {   
210         success ? emit voiceOutgoingComplete(true, value.value<QString>()) : emit voiceOutgoingComplete(false, QString());
211     }
212 }
213
214 void OfonoCallBarring::changePasswordResp()
215 {
216     emit changePasswordComplete(TRUE);
217 }
218
219 void OfonoCallBarring::changePasswordErr(QDBusError error)
220 {
221     qDebug() << "ChangePassword failed" << error;
222     m_errorName = error.name();
223     m_errorMessage = error.message();
224     emit changePasswordComplete(FALSE);
225 }
226
227 void OfonoCallBarring::disableAllResp()
228 {
229     emit disableAllComplete(TRUE);
230 }
231
232 void OfonoCallBarring::disableAllErr(QDBusError error)
233 {
234     qDebug() << "DisableAll failed" << error;
235     m_errorName = error.name();
236     m_errorMessage = error.message();
237     emit disableAllComplete(FALSE);
238 }
239
240 void OfonoCallBarring::disableAllIncomingResp()
241 {
242     emit disableAllIncomingComplete(TRUE);
243 }
244
245 void OfonoCallBarring::disableAllIncomingErr(QDBusError error)
246 {
247     qDebug() << "DisableAllIncoming failed" << error;
248     m_errorName = error.name();
249     m_errorMessage = error.message();
250     emit disableAllIncomingComplete(FALSE);
251 }
252
253 void OfonoCallBarring::disableAllOutgoingResp()
254 {
255     emit disableAllOutgoingComplete(TRUE);
256 }
257
258 void OfonoCallBarring::disableAllOutgoingErr(QDBusError error)
259 {
260     qDebug() << "DisableAllOutgoing failed" << error;
261     m_errorName = error.name();
262     m_errorMessage = error.message();
263     emit disableAllOutgoingComplete(FALSE);
264 }