Added changes to properly power up the modem once paired, and keep it powered. Added...
[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
56     gManager = this;
57
58     if (m_modem && m_modem->isValid() && !m_modem->powered())
59         m_modem->setPowered(true);
60 }
61
62 ManagerProxy::~ManagerProxy()
63 {
64     TRACE;
65     if (m_volumeManager)
66         delete m_volumeManager;
67     m_volumeManager = 0;
68
69     if (m_voicemail)
70         delete m_voicemail;
71     m_voicemail = 0;
72
73     if (m_callManager)
74         delete m_callManager;
75     m_callManager = 0;
76
77     if (m_network)
78         delete m_network;
79     m_network = 0;
80
81     if (m_modem)
82         delete m_modem;
83     m_modem = 0;
84
85     gManager=0;
86 }
87
88 void ManagerProxy::managerDBusGetModemsDone(QDBusPendingCallWatcher *call)
89 {
90     QDBusPendingReply<QArrayOfPathProperties> reply = *call;
91     TRACE;
92     if (reply.isError()) {
93         // TODO: Handle this properly, by setting states, or disabling features
94         qWarning() << "org.ofono.Manager.GetModems() failed: " <<
95             reply.error().message();
96     } else {
97         QArrayOfPathProperties modems = reply.value();
98         if (modems.count() >= 1) {
99             // FIXME: Handle multiple modems...
100             foreach (OfonoPathProperties p, modems)
101                 {
102                     qDebug() << "modem: " << p.path.path();
103                     m_modemList << QString(p.path.path());
104                 }
105
106             OfonoPathProperties p = modems[0];
107             if (m_modemPath.isNull() || m_modemPath.isEmpty()) {
108                 qDebug() << QString("\n======\nUsing modem: \"%1\"\n======").arg(p.path.path());
109                 m_modemPath = QString(p.path.path());
110                 setModem(m_modemPath);
111                 setNetwork(m_modemPath);
112                 setCallManager(m_modemPath);
113                 setVolumeManager(m_modemPath);
114                 setVoicemail(m_modemPath);
115                 // TODO: Connect to service proxies as available/needed here
116             }
117         }
118     }
119 }
120
121 void ManagerProxy::modemAdded(const QDBusObjectPath &in0,const QVariantMap &in1)
122 {
123     Q_UNUSED(in1)
124         TRACE;
125
126     // TODO: Handle modem additions, maybe...
127     qWarning() << QString("Unhandled ModemAdded event: \"%1\"")
128         .arg(in0.path());
129
130     qDebug() << QString("modem added: %1").arg(in0.path());
131     m_modemList << QString(in0.path());
132     m_modemList.removeDuplicates();
133
134     setModem(in0.path());
135     setCallManager(m_modemPath);
136 }
137
138 void ManagerProxy::modemRemoved(const QDBusObjectPath &in0)
139 {
140     TRACE;
141
142     // TODO: Handle modem removals, currently active for sure, others, maybe...
143     qWarning() << QString("Unhandled ModemRemoved event: \"%1\"")
144         .arg(in0.path());
145
146     qDebug() << QString("modem removed: ").arg(in0.path());
147     m_modemList.removeOne(QString(in0.path()));
148 }
149
150 ManagerProxy *ManagerProxy::instance()
151 {
152     if (!gManager)
153         gManager = new ManagerProxy();
154
155     return gManager;
156 }
157
158 ModemProxy* ManagerProxy::modem() const
159 {
160     return m_modem;
161 }
162
163 NetworkProxy* ManagerProxy::network() const
164 {
165     return m_network;
166 }
167
168 CallManager* ManagerProxy::callManager() const
169 {
170     return m_callManager;
171 }
172
173 VolumeManager* ManagerProxy::volumeManager() const
174 {
175     return m_volumeManager;
176 }
177
178 VoicemailProxy* ManagerProxy::voicemail() const
179 {
180     return m_voicemail;
181 }
182
183 QStringList ManagerProxy::getModemList()
184 {
185     return m_modemList;
186 }
187
188 void ManagerProxy::setModem(QString modemPath)
189 {
190     if (m_modem &&
191         m_modem->isValid() &&
192         m_modem->path() == modemPath)
193     {
194         //If we have a modem, it's valid, but not powered, power it up.
195         if (!m_modem->powered())
196                 m_modem->setPowered(true);
197
198         return;
199     }
200
201     if (m_modem)
202         {
203             if (m_modemList.contains(m_modem->path()))
204                 {
205                     m_modemList.removeAll(m_modem->path());
206                 }
207             delete m_modem;
208             m_modem = NULL;
209         }
210
211     if (m_modemList.contains(modemPath)) {
212         m_modem = new ModemProxy(modemPath);
213         emit modemChanged();
214     }
215 }
216
217 void ManagerProxy::setNetwork(QString modempath)
218 {
219     if (!m_modem || !m_modem->isValid())
220         return;
221
222     if (modempath == m_modem->path()) {
223         if (m_network && m_network->isValid()) {
224             return;
225         }
226         else {
227             delete m_network;
228             m_network = new NetworkProxy(modempath);
229             emit networkChanged();
230         }
231     }
232     else {
233         if(m_network || !m_network->isValid()) {
234             delete m_network;
235             m_network = new NetworkProxy(modempath);
236             emit networkChanged();
237         }
238     }
239 }
240
241 void ManagerProxy::setCallManager(QString modempath)
242 {
243     TRACE
244     if (!m_modem || !m_modem->isValid())
245         return;
246
247     if (modempath == m_modem->path()) {
248         if (m_callManager && m_callManager->isValid()) {
249             return;
250         }
251         else {
252             delete m_callManager;
253             m_callManager = new CallManager(modempath);
254             emit callManagerChanged();
255         }
256     }
257     else {
258         
259          if(m_callManager)
260             delete m_callManager;        
261          
262          m_callManager = new CallManager(modempath);
263          connect(m_callManager, SIGNAL(connected()), this, SIGNAL(callManagerChanged()));
264          emit callManagerChanged();
265     }
266 }
267
268 void ManagerProxy::setVolumeManager(QString modempath)
269 {
270     if (!m_modem || !m_modem->isValid())
271         return;
272
273     if (modempath == m_modem->path()) {
274         if (m_volumeManager && m_volumeManager->isValid()) {
275             return;
276         }
277         else {
278             delete m_volumeManager;
279             m_volumeManager = new VolumeManager(modempath);
280             emit volumeManagerChanged();
281         }
282     }
283     else {
284         if(m_volumeManager || !m_volumeManager->isValid()) {
285             delete m_volumeManager;
286             m_volumeManager = new VolumeManager(modempath);
287             emit volumeManagerChanged();
288         }
289     }
290 }
291
292 void ManagerProxy::setVoicemail(QString modempath)
293 {
294     if (!m_modem || !m_modem->isValid())
295         return;
296
297     if (modempath == m_modem->path()) {
298         if (m_voicemail && m_voicemail->isValid()) {
299             return;
300         }
301         else {
302             delete m_voicemail;
303             m_voicemail = new VoicemailProxy(modempath);
304             emit voicemailChanged();
305         }
306     }
307     else {
308         if(m_voicemail || !m_voicemail->isValid()) {
309             delete m_voicemail;
310             m_voicemail = new VoicemailProxy(modempath);
311             emit voicemailChanged();
312         }
313     }
314 }
315
316 /* Local Variables:      */
317 /* mode:c++              */
318 /* c-basic-offset:4      */
319 /* indent-tabs-mode: nil */
320 /* End:                  */