initial commit
[profile/ivi/hfdialer.git] / src / networkproxy.cpp
1 /*
2  * hfdialer - Hands Free Voice Call Manager
3  * Copyright (c) 2012, Intel Corporation.
4  *
5  * This program is licensed under the terms and conditions of the
6  * Apache License, version 2.0.  The full text of the Apache License is at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  */
10
11 #include "networkproxy.h"
12 #include <QDebug>
13
14 /* **************************************************************
15  * Network Operator Class
16  * **************************************************************/
17 /* TODO: make property reference/storage more generic
18
19 typedef struct {
20     unsigned int  id;
21     const char   *name;
22 } OperatorProperty;
23
24 enum OperatorPropertyID {
25         CountryCode = 0,
26         NetworkCode,
27         Name,
28         Status,
29         Technologies,
30 };
31
32 static OperatorProperty operatorProperty[] = {
33         { CountryCode,  "MobileCountryCode", },
34         { NetworkCode,  "MobileNetworkCode", },
35         { Name,         "Name",              },
36         { Status,       "Status",            },
37         { Technologies, "Technologies",      },
38 };
39 */
40
41 OperatorProxy::OperatorProxy(const QString &operatorPath)
42     : org::ofono::NetworkOperator(OFONO_SERVICE,
43                                   operatorPath,
44                                   QDBusConnection::systemBus()),
45       m_path(operatorPath), m_countryCode(""),
46       m_networkCode(""), m_name(""), m_status(""), m_technologies("")
47 {
48     if (!isValid()) {
49         qCritical() << org::ofono::NetworkOperator::staticInterfaceName() <<
50                        " connection failed: " << lastError().message();
51     } else {
52         QDBusPendingReply<QVariantMap> reply;
53         QDBusPendingCallWatcher * watcher;
54
55         reply = GetProperties();
56         watcher = new QDBusPendingCallWatcher(reply);
57
58         connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
59                 this, SLOT(operatorDBusGetPropDone(QDBusPendingCallWatcher*)));
60     }
61 }
62
63 OperatorProxy::~OperatorProxy()
64 {
65 }
66
67 QString OperatorProxy::path() const { return m_path; }
68 QString OperatorProxy::countryCode() const { return m_countryCode; }
69 QString OperatorProxy::networkCode() const { return m_networkCode; }
70 QString OperatorProxy::name() const { return m_name; }
71 QString OperatorProxy::status() const { return m_status; }
72 QStringList OperatorProxy::technologies() const { return m_technologies; }
73
74 void OperatorProxy::operatorDBusGetPropDone(QDBusPendingCallWatcher *call)
75 {
76     QDBusPendingReply<QVariantMap> reply = *call;
77
78     if (reply.isError()) {
79         // TODO: Handle this properly
80         qCritical() << org::ofono::NetworkOperator::staticInterfaceName() <<
81                        ".GetProperties() failed: " << reply.error().message();
82     } else {
83         QVariantMap properties = reply.value();
84         m_countryCode  = qdbus_cast<QString>(properties["MobileCountryCode"]);
85         m_networkCode  = qdbus_cast<QString>(properties["MobileNetworkCode"]);
86         m_name         = qdbus_cast<QString>(properties["Name"]);
87         m_status       = qdbus_cast<QString>(properties["Status"]);
88         m_technologies = qdbus_cast<QStringList>(properties["Technologies"]);
89     }
90 }
91
92 /* **************************************************************
93  * Network Registration Class
94  * **************************************************************/
95 NetworkProxy::NetworkProxy(const QString &modemPath)
96     : org::ofono::NetworkRegistration(OFONO_SERVICE,
97                                       modemPath,
98                                       QDBusConnection::systemBus()),
99       m_mode(""), m_name(""), m_status(""), m_connected(false)
100 {
101     if (!isValid()) {
102         qCritical() << org::ofono::NetworkRegistration::staticInterfaceName() <<
103                        " connection failed: " << lastError().message();
104     } else {
105         QDBusPendingReply<QVariantMap> reply;
106         QDBusPendingCallWatcher * watcher;
107
108         reply = GetProperties();
109         watcher = new QDBusPendingCallWatcher(reply);
110
111         connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
112                 this, SLOT(networkDBusGetPropDone(QDBusPendingCallWatcher*)));
113     }
114 }
115
116 NetworkProxy::~NetworkProxy()
117 {
118 }
119
120 QList<OperatorProxy*> NetworkProxy::operators() const { return m_operators; }
121 OperatorProxy* NetworkProxy::currentOperator() const
122 {
123     return m_currentOperator;
124 }
125 QString NetworkProxy::mode() const { return m_mode; }
126 QString NetworkProxy::name() const { return m_name; }
127 QString NetworkProxy::status() const { return m_status; }
128
129 void NetworkProxy::networkDBusGetPropDone(QDBusPendingCallWatcher *call)
130 {
131     QDBusPendingReply<QVariantMap> reply = *call;
132
133     if (reply.isError()) {
134         // TODO: Handle this properly
135         qCritical() << org::ofono::NetworkRegistration::staticInterfaceName() <<
136                        ".GetProperties() failed: " << reply.error().message();
137     } else {
138         QVariantMap properties = reply.value();
139         QList<QDBusObjectPath> paths =
140           qdbus_cast<QList<QDBusObjectPath> >(properties["AvailableOperators"]);
141
142         foreach (QDBusObjectPath p, paths) {
143             QString path = QString(p.path());
144             OperatorProxy *op = new OperatorProxy(path);
145
146             m_operatorPaths.append(path);
147             m_operators.append(op);
148
149             // GetProperties() has probably not completed yet, so this
150             // test will be unlikely to work.
151             // TODO: connect to the propertyChanged() signal and do this there
152 /*
153             if (op->status() == "current") {
154                 m_currentOperator = op;
155                 qDebug() << "Current network operator is " <<
156                             m_currentOperator->name() << " (" <<
157                             m_currentOperator->path() << ")";
158             }
159 */
160         }
161         m_mode   = qdbus_cast<QString>(properties["Mode"]);
162         m_name   = qdbus_cast<QString>(properties["Operator"]);
163         m_status = qdbus_cast<QString>(properties["Status"]);
164
165         // First sucessfull GetProperties == connected
166         if (!m_connected) {
167             m_connected = true;
168             emit connected();
169         }
170     }
171 }
172