Remove unnecessary includes
[profile/ivi/ofono-qt.git] / lib / ofononetworkregistration.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
27 #include "ofononetworkregistration.h"
28
29 #define REGISTER_TIMEOUT 300000
30 #define SCAN_TIMEOUT 300000
31
32 QDBusArgument &operator<<(QDBusArgument &argument, const OfonoOperatorStruct &op)
33 {
34     argument.beginStructure();
35     argument << op.path << op.properties;
36     argument.endStructure();
37     return argument;
38 }   
39
40 const QDBusArgument &operator>>(const QDBusArgument &argument, OfonoOperatorStruct &op)
41 {   
42     argument.beginStructure();
43     argument >> op.path >> op.properties; 
44     argument.endStructure();
45     return argument;
46 }
47
48
49 OfonoNetworkRegistration::OfonoNetworkRegistration(OfonoModem::SelectionSetting modemSetting, const QString &modemPath, QObject *parent)
50     : OfonoModemInterface(modemSetting, modemPath, "org.ofono.NetworkRegistration", OfonoInterface::GetAllOnStartup, parent)
51 {
52     qDBusRegisterMetaType<OfonoOperatorStruct>();
53     qDBusRegisterMetaType<OfonoOperatorList>();
54
55     connect(this, SIGNAL(propertyChanged(const QString&, const QVariant&)), 
56             this, SLOT(propertyChanged(const QString&, const QVariant&)));
57 }
58
59 OfonoNetworkRegistration::~OfonoNetworkRegistration()
60 {
61 }
62
63 void OfonoNetworkRegistration::requestRegister()
64 {
65     QDBusMessage request;
66
67     request = QDBusMessage::createMethodCall("org.ofono",
68                                              path(), ifname(),
69                                              "Register");
70
71     QDBusConnection::systemBus().callWithCallback(request, this,
72                                         SLOT(registerResp()),
73                                         SLOT(registerErr(const QDBusError&)),
74                                         REGISTER_TIMEOUT);
75 }
76
77 void OfonoNetworkRegistration::requestDeregister()
78 {
79     QDBusMessage request;
80
81     request = QDBusMessage::createMethodCall("org.ofono",
82                                              path(), ifname(),
83                                              "Deregister");
84
85     QDBusConnection::systemBus().callWithCallback(request, this,
86                                         SLOT(deregisterResp()),
87                                         SLOT(deregisterErr(const QDBusError&)),
88                                         REGISTER_TIMEOUT);
89 }
90
91 void OfonoNetworkRegistration::requestScan()
92 {
93     QDBusMessage request;
94
95     request = QDBusMessage::createMethodCall("org.ofono",
96                                              path(), ifname(),
97                                              "Scan");
98
99     QDBusConnection::systemBus().callWithCallback(request, this,
100                                         SLOT(scanResp(OfonoOperatorList)),
101                                         SLOT(scanErr(const QDBusError&)),
102                                         REGISTER_TIMEOUT);
103 }
104
105 void OfonoNetworkRegistration::requestGetOperators()
106 {
107     QDBusMessage request;
108
109     request = QDBusMessage::createMethodCall("org.ofono",
110                                              path(), ifname(),
111                                              "GetOperators");
112
113     QDBusConnection::systemBus().callWithCallback(request, this,
114                                         SLOT(getOperatorsResp(OfonoOperatorList)),
115                                         SLOT(getOperatorsErr(const QDBusError&)),
116                                         SCAN_TIMEOUT);
117 }
118
119 QString OfonoNetworkRegistration::mode() const
120 {
121     return properties()["Mode"].value<QString>();
122 }
123
124 QString OfonoNetworkRegistration::status() const
125 {
126     return properties()["Status"].value<QString>();
127 }
128
129 uint OfonoNetworkRegistration::locationAreaCode() const
130 {
131     return properties()["LocationAreaCode"].value<uint>();
132 }
133
134 uint OfonoNetworkRegistration::cellId() const
135 {
136     return properties()["CellId"].value<uint>();
137 }
138
139 QString OfonoNetworkRegistration::mcc() const
140 {
141     return properties()["MobileCountryCode"].value<QString>();
142 }
143
144 QString OfonoNetworkRegistration::mnc() const
145 {
146     return properties()["MobileNetworkCode"].value<QString>();
147 }
148
149 QString OfonoNetworkRegistration::technology() const
150 {
151     return properties()["Technology"].value<QString>();
152 }
153
154 QString OfonoNetworkRegistration::name() const
155 {
156     return properties()["Name"].value<QString>();
157 }
158
159 uint OfonoNetworkRegistration::strength() const
160 {
161     return properties()["Strength"].value<uint>();
162 }
163
164 QString OfonoNetworkRegistration::baseStation() const
165 {
166     return properties()["BaseStation"].value<QString>();
167 }
168
169 void OfonoNetworkRegistration::propertyChanged(const QString& property, const QVariant& value)
170 {
171     if (property == "Mode") {   
172         emit modeChanged(value.value<QString>());
173     } else if (property == "Status") {  
174         emit statusChanged(value.value<QString>());
175     } else if (property == "LocationAreaCode") {        
176         emit locationAreaCodeChanged(value.value<uint>());
177     } else if (property == "CellId") {  
178         emit cellIdChanged(value.value<uint>());
179     } else if (property == "MobileCountryCode") {       
180         emit mccChanged(value.value<QString>());
181     } else if (property == "MobileNetworkCode") {       
182         emit mncChanged(value.value<QString>());
183     } else if (property == "Technology") {      
184         emit technologyChanged(value.value<QString>());
185     } else if (property == "Name") {    
186         emit nameChanged(value.value<QString>());
187     } else if (property == "Strength") {        
188         emit strengthChanged(value.value<uint>());
189     } else if (property == "BaseStation") {     
190         emit baseStationChanged(value.value<QString>());
191     }
192 }
193
194 void OfonoNetworkRegistration::registerResp()
195 {
196     emit registerComplete(TRUE);
197 }
198
199 void OfonoNetworkRegistration::registerErr(QDBusError error)
200 {
201     qDebug() << "Register failed" << error;
202     m_errorName = error.name();
203     m_errorMessage = error.message();
204     emit registerComplete(FALSE);
205 }
206
207 void OfonoNetworkRegistration::deregisterResp()
208 {
209     emit deregisterComplete(TRUE);
210 }
211
212 void OfonoNetworkRegistration::deregisterErr(QDBusError error)
213 {
214     qDebug() << "Deregister failed" << error;
215     m_errorName = error.name();
216     m_errorMessage = error.message();
217     emit deregisterComplete(FALSE);
218 }
219
220 void OfonoNetworkRegistration::getOperatorsResp(OfonoOperatorList list)
221 {
222     QStringList oplist;
223     foreach(OfonoOperatorStruct op, list) {
224         oplist << op.path.path();
225     }
226     emit getOperatorsComplete(TRUE, oplist);
227 }
228
229 void OfonoNetworkRegistration::getOperatorsErr(QDBusError error)
230 {
231     qDebug() << "GetOperators failed" << error;
232     m_errorName = error.name();
233     m_errorMessage = error.message();
234     emit getOperatorsComplete(FALSE, QStringList());
235 }
236
237 void OfonoNetworkRegistration::scanResp(OfonoOperatorList list)
238 {
239     QStringList oplist;
240     foreach(OfonoOperatorStruct op, list) {
241         oplist << op.path.path();
242     }
243     emit scanComplete(TRUE, oplist);
244 }
245
246 void OfonoNetworkRegistration::scanErr(QDBusError error)
247 {
248     qDebug() << "Scan failed" << error;
249     m_errorName = error.name();
250     m_errorMessage = error.message();
251     emit scanComplete(FALSE, QStringList());
252 }
253