Added the ability to edit the PID number using the number pad.
[profile/ivi/hfdialer.git] / src / managerproxy.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 "managerproxy.h"
12 #include "manager_interface.h"
13 #include "dialerapplication.h"
14 #include <QDebug>
15
16 ManagerProxy *ManagerProxy::gManager = 0;
17
18 ManagerProxy::ManagerProxy(const QString &service,
19                            const QString &path,
20                            const QDBusConnection &connection,
21                            QObject *parent)
22     : org::ofono::Manager(service, path, connection, parent),
23       m_modemPath (""),
24       m_modem(0),
25       m_network(0),
26       m_callManager(0),
27       m_volumeManager(0),
28       m_voicemail(0)
29 {
30 TRACE
31     if (gManager)
32         qFatal("ManagerProxy: There can be only one!");
33
34     if (!isValid()) {
35         qDebug() << "Failed to connect to Ofono: \n\t" << lastError().message();
36     } else {
37         QDBusPendingReply<QArrayOfPathProperties> reply;
38         QDBusPendingCallWatcher * watcher;
39
40         reply = GetModems();
41         watcher = new QDBusPendingCallWatcher(reply);
42
43         // Force this to be sync to ensure we have initial properties
44         watcher->waitForFinished();
45         managerDBusGetModemsDone(watcher);
46
47         connect(this,
48                 SIGNAL(ModemAdded(const QDBusObjectPath&, const QVariantMap&)),
49                 SLOT(modemAdded(const QDBusObjectPath&, const QVariantMap&)));
50         connect(this,
51                 SIGNAL(ModemRemoved(const QDBusObjectPath&)),
52                 SLOT(modemRemoved(const QDBusObjectPath&)));
53     }
54
55     gManager = this;
56 }
57
58 ManagerProxy::~ManagerProxy()
59 {
60     TRACE
61     if (m_volumeManager)
62         delete m_volumeManager;
63     m_volumeManager = 0;
64
65     if (m_voicemail)
66         delete m_voicemail;
67     m_voicemail = 0;
68
69     if (m_callManager)
70         delete m_callManager;
71     m_callManager = 0;
72
73     if (m_network)
74         delete m_network;
75     m_network = 0;
76
77     if (m_modem)
78         delete m_modem;
79     m_modem = 0;
80
81     gManager=0;
82 }
83
84 void ManagerProxy::managerDBusGetModemsDone(QDBusPendingCallWatcher *call)
85 {
86     QDBusPendingReply<QArrayOfPathProperties> reply = *call;
87 TRACE
88     if (reply.isError()) {
89         // TODO: Handle this properly, by setting states, or disabling features
90         qWarning() << "org.ofono.Manager.GetModems() failed: " <<
91                       reply.error().message();
92     } else {
93         QArrayOfPathProperties modems = reply.value();
94         if (modems.count() >= 1) {
95             // FIXME: Handle multiple modems...
96             foreach (OfonoPathProperties p, modems)
97             {
98                 qDebug() << "modem: " << p.path.path();
99                 m_modemList << QString(p.path.path());
100             }
101
102             OfonoPathProperties p = modems[0];
103             if (m_modemPath.isNull() || m_modemPath.isEmpty()) {
104                 qDebug() << QString("\n======\nUsing modem: \"%1\"\n======").arg(p.path.path());
105                 m_modemPath = QString(p.path.path());
106                 setModem(m_modemPath);
107                 setNetwork(m_modemPath);
108                 setCallManager(m_modemPath);
109                 setVolumeManager(m_modemPath);
110                 setVoicemail(m_modemPath);
111             // TODO: Connect to service proxies as available/needed here
112             }
113         }
114     }
115 }
116
117 void ManagerProxy::modemAdded(const QDBusObjectPath &in0,const QVariantMap &in1)
118 {
119     Q_UNUSED(in1)
120     TRACE
121
122     // TODO: Handle modem additions, maybe...
123     qWarning() << QString("Unhandled ModemAdded event: \"%1\"")
124                   .arg(in0.path());
125
126     qDebug() << QString("modem added: %1").arg(in0.path());
127     m_modemList << QString(in0.path());
128     m_modemList.removeDuplicates();
129
130     setModem(in0.path());
131 }
132
133 void ManagerProxy::modemRemoved(const QDBusObjectPath &in0)
134 {
135     TRACE
136
137     // TODO: Handle modem removals, currently active for sure, others, maybe...
138     qWarning() << QString("Unhandled ModemRemoved event: \"%1\"")
139                   .arg(in0.path());
140
141     qDebug() << QString("modem removed: ").arg(in0.path());
142     m_modemList.removeOne(QString(in0.path()));
143 }
144
145 ManagerProxy *ManagerProxy::instance()
146 {
147     if (!gManager)
148         gManager = new ManagerProxy();
149
150     return gManager;
151 }
152
153 ModemProxy* ManagerProxy::modem() const
154 {
155     return m_modem;
156 }
157
158 NetworkProxy* ManagerProxy::network() const
159 {
160     return m_network;
161 }
162
163 CallManager* ManagerProxy::callManager() const
164 {
165     return m_callManager;
166 }
167
168 VolumeManager* ManagerProxy::volumeManager() const
169 {
170     return m_volumeManager;
171 }
172
173 VoicemailProxy* ManagerProxy::voicemail() const
174 {
175     return m_voicemail;
176 }
177
178 QStringList ManagerProxy::getModemList()
179 {
180     return m_modemList;
181 }
182
183 void ManagerProxy::setModem(QString modemPath)
184 {
185     if (m_modem &&
186         m_modem->isValid() &&
187         m_modem->path() == modemPath)
188         return;
189
190     if (m_modem)
191     {
192         if (m_modemList.contains(m_modem->path()))
193         {
194           m_modemList.removeAll(m_modem->path());
195         }
196         delete m_modem;
197         m_modem = NULL;
198     }
199
200     if (m_modemList.contains(modemPath)) {
201         m_modem = new ModemProxy(modemPath);
202         emit modemChanged();
203     }
204 }
205
206 void ManagerProxy::setNetwork(QString modempath)
207 {
208     if (!m_modem || !m_modem->isValid())
209         return;
210
211     if (modempath == m_modem->path()) {
212         if (m_network && m_network->isValid()) {
213             return;
214         }
215         else {
216             delete m_network;
217             m_network = new NetworkProxy(modempath);
218             emit networkChanged();
219         }
220     }
221     else {
222         if(m_network || !m_network->isValid()) {
223             delete m_network;
224             m_network = new NetworkProxy(modempath);
225             emit networkChanged();
226         }
227     }
228 }
229
230 void ManagerProxy::setCallManager(QString modempath)
231 {
232     if (!m_modem || !m_modem->isValid())
233         return;
234
235     if (modempath == m_modem->path()) {
236         if (m_callManager && m_callManager->isValid()) {
237             return;
238         }
239         else {
240             delete m_callManager;
241             m_callManager = new CallManager(modempath);
242             emit callManagerChanged();
243         }
244     }
245     else {
246         if(m_callManager || !m_callManager->isValid()) {
247             delete m_callManager;
248             m_callManager = new CallManager(modempath);
249             emit callManagerChanged();
250         }
251     }
252 }
253
254 void ManagerProxy::setVolumeManager(QString modempath)
255 {
256     if (!m_modem || !m_modem->isValid())
257         return;
258
259     if (modempath == m_modem->path()) {
260         if (m_volumeManager && m_volumeManager->isValid()) {
261             return;
262         }
263         else {
264             delete m_volumeManager;
265             m_volumeManager = new VolumeManager(modempath);
266             emit volumeManagerChanged();
267         }
268     }
269     else {
270         if(m_volumeManager || !m_volumeManager->isValid()) {
271             delete m_volumeManager;
272             m_volumeManager = new VolumeManager(modempath);
273             emit volumeManagerChanged();
274         }
275     }
276 }
277
278 void ManagerProxy::setVoicemail(QString modempath)
279 {
280     if (!m_modem || !m_modem->isValid())
281         return;
282
283     if (modempath == m_modem->path()) {
284         if (m_voicemail && m_voicemail->isValid()) {
285             return;
286         }
287         else {
288             delete m_voicemail;
289             m_voicemail = new VoicemailProxy(modempath);
290             emit voicemailChanged();
291         }
292     }
293     else {
294         if(m_voicemail || !m_voicemail->isValid()) {
295             delete m_voicemail;
296             m_voicemail = new VoicemailProxy(modempath);
297             emit voicemailChanged();
298         }
299     }
300 }