Added the ability to edit the PID number using the number pad.
[profile/ivi/hfdialer.git] / src / dialerapplication.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 "common.h"
12 #include "dbustypes.h"
13 #include "dialerapplication.h"
14 #include "dialercontext.h"
15 #include "dbusdialeradapter.h"
16 #include "pacontrol.h"
17 #include "qmlmainwindow.h"
18 #include <QtGui>
19 #include <QDebug>
20 #include <QApplication>
21
22 #define OFONO_VOICECALLMANAGER_INTERFACE "org.ofono.VoiceCallManager"
23
24 DialerApplication::DialerApplication(int &argc, char **argv)
25     : QApplication(argc, argv)
26 {
27     TRACE
28     init();
29 }
30
31 void DialerApplication::releasePrestart()
32 {
33     TRACE
34     // Now is the time for set up and display of information
35     // that needs to be done to allow the dialeror other
36     // pages to display correctly when opened. GenericPage has a
37     // activateWidgets() method for common setup and
38     // each Page type (dialer, people, favorites, etc) can also
39     // implement the method for Page specific setup and signal
40     // and slot connections
41
42     
43 }
44
45 void DialerApplication::restorePrestart()
46 {
47     TRACE
48     // Now is the time for clean up and resetting an information
49     // that needs to be done to allow the dialer pages to display
50     // correctly when reopened. GenericPage has a
51     // deactivateAndResetWidgets() method for common setup and
52     // each Page type (dialer, people, favorites, etc) can also
53     // implement the method for Page specific clean up
54
55     // Call the default implementation to hide the window.
56 }
57
58 void DialerApplication::connectAll()
59 {
60     TRACE
61
62     ManagerProxy *m_manager = ManagerProxy::instance();
63     if (m_manager->modem() &&
64         m_manager->modem()->isValid() &&
65         !m_manager->modem()->path().isEmpty())
66     {
67         qDebug() << QString("Connecting to CallManager....");
68         m_manager->setNetwork(m_manager->modem()->path());
69         m_manager->setCallManager(m_manager->modem()->path());
70         m_manager->setVolumeManager(m_manager->modem()->path());
71         m_manager->setVoicemail(m_manager->modem()->path());
72
73         connect(m_manager->network(), SIGNAL(connected()), this,
74                                       SLOT(networkConnected()));
75         connect(m_manager->network(), SIGNAL(disconnected()), this,
76                                       SLOT(networkDisconnected()));
77         connect(m_manager->callManager(), SIGNAL(connected()), this,
78                                           SLOT(callManagerConnected()));
79         connect(m_manager->callManager(), SIGNAL(disconnected()), this,
80                                           SLOT(callManagerDisconnected()));
81         connect(m_manager->callManager(), SIGNAL(callsChanged()), this,
82                                           SLOT(onCallsChanged()));
83         connect(m_manager->voicemail(), SIGNAL(messagesWaitingChanged()), this,
84                                           SLOT(messagesWaitingChanged()));
85         PAControl* paControl = PAControl::instance();
86         connect(m_manager->callManager(), SIGNAL(callsChanged()), paControl,
87                 SLOT(onCallsChanged()));
88     }
89 }
90
91 bool DialerApplication::isConnected()
92 {
93     TRACE
94     return m_connected;
95 }
96
97 bool DialerApplication::hasError() const
98 {
99     TRACE
100     return !m_lastError.isEmpty();
101 }
102
103 QString DialerApplication::lastError() const
104 {
105     TRACE
106     return m_lastError;
107 }
108
109 void DialerApplication::setError(const QString msg)
110 {
111     TRACE
112     m_lastError.clear();
113     m_lastError = QString(msg);
114 }
115
116 DialerApplication *DialerApplication::instance()
117 {
118     TRACE
119     return qobject_cast<DialerApplication *>(QApplication::instance());
120 }
121 /*
122 SeasideSyncModel *DialerApplication::seasideModel()
123 {
124     TRACE
125     return m_seasideModel;
126 }
127
128 SeasideProxyModel *DialerApplication::seasideProxy()
129 {
130     TRACE
131     return m_seasideProxy;
132 }
133 */
134 /*
135 HistoryTableModel *DialerApplication::historyModel()
136 {
137     TRACE
138  //   return m_historyModel;
139 }
140
141 QSortFilterProxyModel *DialerApplication::historyProxy()
142 {
143     TRACE
144     return m_historyProxy;
145 }
146 */
147 void DialerApplication::init()
148 {
149     TRACE
150     m_connected = false;
151     m_lastError = QString();
152
153     // Notify Qt of our custom DBus MetaTypes
154     registerMyDataTypes();
155
156     m_manager = ManagerProxy::instance();
157     if (!m_manager || !m_manager->isValid())
158         //% "Failed to connect to org.ofono.Manager: is ofonod running?"
159         setError(qtTrId("xx_no_ofono_error"));
160     else
161         m_connected = true;
162
163     DBusDialerAdapter *adapter = new DBusDialerAdapter(this);
164     if(!adapter)
165     {
166         qWarning() << "DBus adapter instantiation failed.";
167     }
168
169     if(!QDBusConnection::sessionBus().registerObject(DBUS_SERVICE_PATH, this))
170     {
171         qCritical() << "Error registering dbus object:" <<
172                        QDBusConnection::sessionBus().lastError().message();
173     }
174    connect(m_manager, SIGNAL(modemChanged()),
175                                 SLOT(modemChanged()));
176
177 }
178
179 void DialerApplication::modemChanged()
180 {
181     TRACE
182     if (m_manager->modem() != NULL)
183     {
184         connect(m_manager->modem(), SIGNAL(connected()),
185                                 SLOT(modemConnected()));
186     connect(m_manager->modem(), SIGNAL(disconnected()),
187                                 SLOT(modemDisconnected()));
188     }
189 }
190
191 void DialerApplication::modemConnected()
192 {
193     TRACE
194     //TODO: Handle multiple modems
195     if (m_manager->modem() && m_manager->modem()->isValid())
196     {
197         m_modem = m_manager->modem();
198
199         m_modem->setPowered(true);
200         
201         qDebug() << QString("Modem connected");
202         connect(m_modem, SIGNAL(interfacesChanged(QStringList)), this,
203                            SLOT(modemInterfacesChanged(QStringList)));
204         connect(m_modem, SIGNAL(poweredChanged(bool)), this,
205                            SLOT(modemPowered(bool)));
206
207         if (m_modem->powered() &&
208             m_modem->interfaces().contains(OFONO_VOICECALLMANAGER_INTERFACE))
209         {
210             /* connect all now, modem is enabled */
211             this->connectAll();
212         }
213     }
214 }
215
216 void DialerApplication::modemDisconnected()
217 {
218     TRACE
219     //TODO: Handle multiple modems
220 }
221
222 void DialerApplication::modemInterfacesChanged(QStringList interfaces)
223 {
224     TRACE
225     qDebug() << QString("Modem Interfaces: ") << interfaces;
226
227     if (interfaces.contains(OFONO_VOICECALLMANAGER_INTERFACE) &&
228         m_manager->modem()->powered())
229     {
230         this->connectAll();
231     }
232 }
233
234 void DialerApplication::modemPowered(bool isPowered)
235 {
236     TRACE
237     qDebug() << QString("Modem Powered: ") << isPowered;
238
239     if (isPowered &&
240         m_manager->modem()->interfaces().contains(OFONO_VOICECALLMANAGER_INTERFACE))
241     {
242         this->connectAll();
243     }
244 }
245
246 void DialerApplication::networkConnected()
247 {
248     TRACE
249     if (m_manager->network() && m_manager->network()->isValid())
250         m_network = m_manager->network();
251 }
252
253 void DialerApplication::networkDisconnected()
254 {
255     TRACE
256 }
257
258 void DialerApplication::callManagerConnected()
259 {
260     TRACE
261     if (m_manager->callManager() && m_manager->callManager()->isValid())
262         m_callManager = m_manager->callManager();
263
264
265     qDebug() << QString("Disconnect calls changed signal");
266     disconnect(m_callManager, SIGNAL(callsChanged()));
267
268     qDebug() << QString("Disconnect incoming signal");
269     disconnect(m_callManager, SIGNAL(incomingCall(CallItem*)));
270
271     qDebug() << QString("Disconnect resource lost");
272     disconnect(m_callManager, SIGNAL(callResourceLost(const QString)));
273 }
274
275 void DialerApplication::callManagerDisconnected()
276 {
277     TRACE
278     qDebug() << QString("CallMgr disconnected");
279 }
280
281 void DialerApplication::messagesWaitingChanged()
282 {
283     TRACE
284 /*
285     static MNotification *vmail = NULL;
286
287     if (!m_manager->voicemail() || !m_manager->voicemail()->isValid()) {
288         qDebug() << QString("Voicemail proxy is invalid, ignoring");
289         return;
290     }
291
292
293     if (!vmail) {
294         bool found = false;
295         foreach (MNotification *notice, MNotification::notifications()) {
296             if (notice->eventType() == MNotification::MessageArrivedEvent) {
297                 // If we've already found a MessageArrived notification,
298                 // we must delete others since we only want one
299                 if (found) {
300                     qDebug() << QString("Removing duplicate voicemail notice");
301                     notice->remove();
302                     delete notice;
303                 }
304                 else {
305                     vmail = notice;
306                     found = true;
307                 }
308             }
309             else {
310                 // We're only interested in MessageArrived events, all others
311                 // can need to be deleted here since they are copies
312                 delete notice;
313             }
314         }
315
316         if (!found) {
317             if (!m_manager->voicemail()->waiting()) {
318                 qDebug() << QString("No waiting Voicemail messages");
319                 return;
320             }
321             else {
322                 // This is the first instance of a MessageArrived event
323                 qDebug() << QString("Creating new voicemail notice instance");
324                 vmail = new MNotification(MNotification::MessageArrivedEvent);
325                 vmail->setCount(0);
326             }
327         }
328         else {
329             if (!m_manager->voicemail()->waiting()) {
330                 qDebug() << QString("No waiting Voicemail messages");
331                 vmail->remove();
332                 return;
333             }
334         }
335     }
336
337     // We've got a valid notification and we have messages waiting...
338     int vCount = m_manager->voicemail()->count();
339     int nCount = vmail->count();
340
341     if (vCount < 0) vCount = 0;
342     if (nCount < 0) nCount = 0;
343
344     qDebug() << QString("Voicemails: %1").arg(QString::number(vCount));
345     qDebug() << QString("Notices:    %1").arg(QString::number(nCount));
346
347     // No more Voicemail messages waiting, remove notification
348     if (vCount <= 0) {
349         qDebug() << QString("No Voicemails waiting, removing notification");
350         vmail->remove();
351         return;
352     }
353
354     // The waiting voicemail count has changed, update and [re]publish it
355     //% "You have %1 voice messages"
356     vmail->setSummary(qtTrId("xx_messages_waiting").arg(vCount));
357     vmail->setImage("icon-m-telephony-voicemail");
358     vmail->setCount(vCount);
359     qDebug() << QString("Voicemail count changed, publishing notification");
360     vmail->publish();
361 */
362 }
363
364 void DialerApplication::onCallsChanged()
365 {
366     TRACE
367     QMLMainWindow::instance()->tryToShow();
368 }