Initial Import
[profile/ivi/hfdialer.git] / src / modemproxy.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 "modemproxy.h"
12 #include <QDebug>
13 #include "common.h"
14
15 ModemProxy::ModemProxy(const QString &objectPath)
16     : org::ofono::Modem(OFONO_SERVICE,
17                         objectPath,
18                         QDBusConnection::systemBus()),
19       m_interfaces(0)
20 {
21     TRACE;
22     if (!isValid()) {
23         qDebug() << "Failed to connect to Ofono: \n\t" << lastError().message();
24     } else {
25         m_path = objectPath;
26         QDBusPendingReply<QVariantMap> reply;
27         QDBusPendingCallWatcher * watcher;
28
29         reply = GetProperties();
30         watcher = new QDBusPendingCallWatcher(reply);
31
32         connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
33                 this, SLOT(modemDBusGetPropDone(QDBusPendingCallWatcher*)));
34
35         connect(this,
36                 SIGNAL(PropertyChanged(const QString&,const QDBusVariant&)),
37                 SLOT(propertyChanged(const QString&,const QDBusVariant&)));
38     }
39 }
40
41 ModemProxy::~ModemProxy()
42 {
43     TRACE;
44 }
45
46 QStringList ModemProxy::interfaces() const { return m_interfaces; }
47 QString ModemProxy::path() const { return m_path; }
48 QString ModemProxy::manufacturer() const { return m_manufacturer; }
49 QString ModemProxy::model() const { return m_model; }
50 QString ModemProxy::revision() const { return m_revision; }
51 QString ModemProxy::serial() const { return m_serial; }
52 bool    ModemProxy::powered() const { return m_powered; }
53 bool    ModemProxy::online() const { return m_online; }
54
55 void ModemProxy::setPowered(bool is_powered)
56 {
57     TRACE;
58     if (m_powered == is_powered)
59         return;
60
61     QVariant powered(is_powered);
62
63     QDBusPendingReply<QVariantMap> reply;
64     reply = SetProperty("Powered", QDBusVariant(powered));
65
66     if (reply.isError())
67         qCritical() << "SetProperty \"Powered\" failed!";
68 }
69
70 void ModemProxy::setOnline(bool is_online)
71 {
72     TRACE;
73     if (m_online == is_online)
74         return;
75
76     QDBusPendingReply<QVariantMap> reply;
77     reply = SetProperty("Online", QDBusVariant(m_online?"true":"false"));
78     if (reply.isError())
79         qCritical() << "SetProperty \"Powered\" failed!";
80     else
81         m_online = is_online;
82 }
83
84 void ModemProxy::modemDBusGetPropDone(QDBusPendingCallWatcher *call)
85 {
86     TRACE;
87     QDBusPendingReply<QVariantMap> reply = *call;
88
89     if (reply.isError()) {
90         // TODO: Handle this properly, by setting states, or disabling features...
91         qDebug() << "org.ofono.ModemProxy.getProperties() failed: " <<
92             reply.error().message();
93     } else {
94         QVariantMap properties = reply.value();
95         m_interfaces = qdbus_cast<QStringList >(properties["Interfaces"]);
96         m_manufacturer = qdbus_cast<QString>(properties["Manufacturer"]);
97         m_model = qdbus_cast<QString>(properties["Model"]);
98         m_powered = qdbus_cast<bool>(properties["Powered"]);
99         m_revision = qdbus_cast<QString>(properties["Revision"]);
100         m_serial = qdbus_cast<QString>(properties["Serial"]);
101         m_online = qdbus_cast<bool>(properties["Online"]);
102
103         // First sucessfull GetProperties == connected
104         if (!m_connected) {
105             m_connected = true;
106             emit connected();
107         }
108     }
109 }
110
111 void ModemProxy::propertyChanged(const QString &in0, const QDBusVariant &in1)
112 {
113     TRACE;
114     qDebug() << "org.ofono.ModemProxy.propertyChanged()"
115              << in0 << ": " << in1.variant();
116     if (in0 == "Interfaces") {
117         m_interfaces = qdbus_cast<QStringList>(in1.variant());
118         emit interfacesChanged(m_interfaces);
119     }
120     else if (in0 == "Powered") {
121         m_powered = qdbus_cast<bool>(in1.variant());
122         emit poweredChanged(m_powered);
123     } else if (in0 == "Online") {
124         m_online = qdbus_cast<bool>(in1.variant());
125         emit onlineChanged(m_online);
126     } else {
127         qDebug() << QString("Unhandled property \"%1\" changed...").arg(in0);
128     }
129 }
130
131 /*
132  * VoicemailProxy (aka MessageWaiting) implementation
133  */
134
135 VoicemailProxy::VoicemailProxy(const QString &objectPath)
136     : org::ofono::MessageWaiting(OFONO_SERVICE,
137                                  objectPath,
138                                  QDBusConnection::systemBus()),
139       m_connected(false)
140 {
141     TRACE;
142     if (!isValid()) {
143         qDebug() << "Failed to connect to Ofono: \n\t" << lastError().message();
144     } else {
145         m_path = objectPath;
146         QDBusPendingReply<QVariantMap> reply;
147         QDBusPendingCallWatcher * watcher;
148
149         reply = GetProperties();
150         watcher = new QDBusPendingCallWatcher(reply);
151
152         connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
153                 this, SLOT(voicemailDBusGetPropDone(QDBusPendingCallWatcher*)));
154         connect(this, SIGNAL(PropertyChanged(const QString&, const QDBusVariant&)),
155                 SLOT(voicemailPropertyChanged(const QString&, const QDBusVariant&)));
156     }
157 }
158
159 VoicemailProxy::~VoicemailProxy()
160 {
161     TRACE;
162 }
163
164 bool    VoicemailProxy::isConnected() const { return m_connected; }
165 QString VoicemailProxy::path() const { return m_path; }
166 QString VoicemailProxy::mailbox() const { return m_mailbox; }
167 int     VoicemailProxy::count() const { return m_count; }
168 bool    VoicemailProxy::waiting() const { return m_waiting; }
169
170 void VoicemailProxy::setMailbox(QString lineid)
171 {
172     TRACE;
173     if (lineid.isEmpty() || (m_mailbox == lineid))
174         return;
175
176     QDBusPendingReply<> reply;
177     reply = SetProperty("VoicemailMailboxNumber", QDBusVariant(lineid));
178     reply.waitForFinished();
179
180     if (reply.isError())
181         qCritical() << "SetProperty \"VoicemailMailboxNumber\" failed: " <<
182             reply.error().message();
183     else
184         m_mailbox = lineid;
185 }
186
187 void VoicemailProxy::voicemailDBusGetPropDone(QDBusPendingCallWatcher *call)
188 {
189     TRACE;
190     QDBusPendingReply<QVariantMap> reply = *call;
191
192     if (reply.isError()) {
193         // TODO: Handle this properly, by setting states, or disabling features...
194         qDebug() << "org.ofono.MessageWaiting.getProperties() failed: " <<
195             reply.error().message();
196     } else {
197         QVariantMap properties = reply.value();
198         bool waiting = qdbus_cast<bool>(properties["VoicemailWaiting"]);
199         int count = qdbus_cast<int>(properties["VoicemailMessageCount"]);
200         QString mailbox = qdbus_cast<QString>(properties["VoicemailMailboxNumber"]);
201
202         if (count != m_count) {
203             m_count = count;
204             emit messagesWaitingChanged();
205         }
206         if (waiting != m_waiting) {
207             m_waiting = waiting;
208             emit messagesWaitingChanged();
209         }
210         if (!mailbox.isEmpty() && (mailbox != m_mailbox)) {
211             m_mailbox = mailbox;
212             emit mailboxChanged();
213         }
214
215         // First sucessfull GetProperties == connected
216         if (!m_connected) {
217             m_connected = true;
218             emit connected();
219         }
220     }
221 }
222
223 void VoicemailProxy::voicemailPropertyChanged(const QString &in0, const QDBusVariant &in1)
224 {
225     TRACE;
226     qDebug() << QString("Property \"%1\" changed...").arg(in0);
227     bool waiting;
228     int count;
229     QString mailbox;
230     if (in0 == "VoicemailWaiting") {
231         waiting = qdbus_cast<bool>(in1.variant());
232     } else if (in0 == "VoicemailMessageCount") {
233         count = qdbus_cast<int>(in1.variant());
234     } else if (in0 == "VoicemailMailboxNumber") {
235         mailbox = qdbus_cast<QString>(in1.variant());
236     } else
237         qDebug() << QString("Unexpected property changed...");
238
239     if ((count != m_count) || (waiting != m_waiting)) {
240         m_count = count;
241         m_waiting = waiting;
242         emit messagesWaitingChanged();
243     }
244     if (!mailbox.isEmpty() && (mailbox != m_mailbox)) {
245         m_mailbox = mailbox;
246         emit mailboxChanged();
247     }
248 }
249
250 /*
251  * CallVolume Manager implementation
252  */
253
254 VolumeManager::VolumeManager(const QString &objectPath)
255     : org::ofono::CallVolume(OFONO_SERVICE,
256                              objectPath,
257                              QDBusConnection::systemBus())
258 {
259     TRACE;
260     if (!isValid()) {
261         qDebug() << "Failed to connect to Ofono: \n\t" << lastError().message();
262     } else {
263         m_path = objectPath;
264         QDBusPendingReply<QVariantMap> reply;
265         QDBusPendingCallWatcher * watcher;
266
267         reply = GetProperties();
268         watcher = new QDBusPendingCallWatcher(reply);
269
270         connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
271                 this, SLOT(volumeDBusGetPropDone(QDBusPendingCallWatcher*)));
272     }
273 }
274
275 VolumeManager::~VolumeManager()
276 {
277     TRACE;
278 }
279
280 QString VolumeManager::path() const { return m_path; }
281 int     VolumeManager::speakerVolume() const { return m_speakerVolume; }
282 int     VolumeManager::micVolume() const { return m_micVolume; }
283 bool    VolumeManager::muted() const { return m_muted; }
284 bool    VolumeManager::isConnected() const { return m_connected; }
285
286 void VolumeManager::setSpeakerVolume(int volume)
287 {
288     TRACE;
289     if (m_speakerVolume == volume)
290         return;
291
292     if ((volume < 0) || (volume > 100)) {
293         qWarning() << "SpeakerVolume value out of range (0<>100)";
294         return;
295     }
296
297     QDBusPendingReply<> reply;
298     reply = SetProperty("SpeakerVolume", QDBusVariant(volume));
299     reply.waitForFinished();
300
301     if (reply.isError())
302         qCritical() << "SetProperty \"SpeakerVolume\" failed: " <<
303             reply.error().message();
304     else
305         m_speakerVolume = volume;
306 }
307
308 void VolumeManager::setMicVolume(int volume)
309 {
310     TRACE;
311     if (m_micVolume == volume)
312         return;
313
314     if ((volume < 0) || (volume > 100)) {
315         qWarning() << "MicrophoneVolume value out of range (0<>100)";
316         return;
317     }
318
319     QDBusPendingReply<> reply;
320     reply = SetProperty("MicrophoneVolume", QDBusVariant(volume));
321     reply.waitForFinished();
322
323     if (reply.isError())
324         qCritical() << "SetProperty \"MicrophoneVolume\" failed: " <<
325             reply.error().message();
326     else
327         m_micVolume = volume;
328 }
329
330 void VolumeManager::setMuted(bool is_muted)
331 {
332     TRACE;
333     if (m_muted == is_muted)
334         return;
335
336     QDBusPendingReply<> reply;
337     reply = SetProperty("Muted", QDBusVariant(is_muted));
338     reply.waitForFinished();
339
340     if (reply.isError())
341         qCritical() << "SetProperty \"Muted\" failed: " <<
342             reply.error().message();
343     else
344         m_muted = is_muted;
345 }
346
347 void VolumeManager::volumeDBusGetPropDone(QDBusPendingCallWatcher *call)
348 {
349     TRACE;
350     QDBusPendingReply<QVariantMap> reply = *call;
351
352     if (reply.isError()) {
353         // TODO: Handle this properly, by setting states, or disabling features...
354         qDebug() << "org.ofono.CallVolume.getProperties() failed: " <<
355             reply.error().message();
356     } else {
357         QVariantMap properties = reply.value();
358         m_speakerVolume = qdbus_cast<int>(properties["SpeakerVolume"]);
359         m_micVolume = qdbus_cast<int>(properties["MicrophoneVolume"]);
360         m_muted = qdbus_cast<bool>(properties["Muted"]);
361
362         // First sucessfull GetProperties == connected
363         if (!m_connected) {
364             m_connected = true;
365             emit connected();
366         }
367     }
368 }
369
370 /* Local Variables:      */
371 /* mode:c++              */
372 /* c-basic-offset:4      */
373 /* indent-tabs-mode: nil */
374 /* End:                  */