initial commit
[profile/ivi/hfdialer.git] / src / callitem.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 "callitem.h"
13 #include "dialerapplication.h"
14 #include "pacontrol.h"
15 #include <QGraphicsItem>
16 #include <QGraphicsWidget>
17 #include <QDebug>
18
19 #define DEFAULT_RINGTONE "ring-1.wav"
20
21 CallItem::CallItem(const QString path, QObject *parent)
22     : QObject(parent),
23       m_path(path),
24       m_rtKey(new MGConfItem("/apps/dialer/defaultRingtone")),
25       m_isconnected(FALSE)//,
26       //m_ringtonefile("")
27 {
28     TRACE
29     
30    /*  QString l_ringtoneFile = QString("%1/%2/stereo/%3")
31                                      .arg(SOUNDS_DIR)
32                                      .arg("tizen")
33                                      .arg(DEFAULT_RINGTONE);
34     */
35
36     QString l_ringtoneFile = QString("/usr/share/hfdialer/sounds/%1").arg(DEFAULT_RINGTONE);
37     QString l_rtKeyValue = m_rtKey->value(QVariant(l_ringtoneFile)).toString();
38
39     if (QFileInfo(l_rtKeyValue).exists()) {
40         /*
41         m_ringtonefile = l_ringtoneFile;
42         qDebug() << QString("CallItem: %1 using ringtone: %2")
43                            .arg(m_path)
44                            .arg(m_ringtonefile);
45         */
46     } else {
47         qWarning() << QString("CallItem: %1 ringtone not found: %2")
48                            .arg(m_path)
49                            .arg(l_rtKeyValue);
50     }
51
52     //m_ringtone->setMedia(QMediaContent(QUrl::fromLocalFile(m_ringtonefile)));
53     //m_ringtone->setVolume(100);
54
55     if (isValid())
56         init();
57 }
58
59 CallItem::~CallItem()
60 {
61     TRACE
62 /*
63     if (m_ringtone) {
64         disconnect(m_ringtone, SIGNAL(positionChanged(qint64)));
65         m_ringtone->stop();
66         delete m_ringtone;
67         m_ringtone = 0;
68     }
69 */
70    PAControl::instance()->unrouteAudio();
71    
72     if (m_rtKey)
73         delete m_rtKey;
74     m_rtKey = 0;
75 /*
76     if (m_peopleItem)
77         delete m_peopleItem;
78     m_peopleItem = 0;
79 */
80     // delete the callproxy object
81     if (callProxy())
82         delete callProxy();
83 }
84
85 void CallItem::init()
86 {
87     TRACE
88     if (!m_path.isEmpty()) {
89         m_call = new CallProxy(m_path);
90         if (m_call->isValid()) {
91             // dynamic_cast<CallItemModel*>(model())->setCall(call);
92             connect(m_call,SIGNAL(stateChanged()),this,SLOT(callStateChanged()));
93             connect(m_call,SIGNAL(dataChanged()),this,SLOT(callDataChanged()));
94             connect(m_call,SIGNAL(multipartyChanged()),this,SLOT(callMultipartyChanged()));
95         } else
96             qCritical("Invalid CallProxy instance!");
97     } else
98         qCritical("Empty call path.  Can not create CallProxy!");
99
100     //populatePeopleItem();
101
102     if (state() == STATE_INCOMING ||
103         state() == STATE_WAITING)
104     {
105  
106    /*
107         // Start ringing
108         if (!m_isconnected && m_ringtone) {
109            connect(m_ringtone, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),
110                                SLOT(ringtoneStatusChanged(QMediaPlayer::MediaStatus)));
111            m_isconnected = TRUE;
112            m_ringtone->play();
113         }
114 */
115     }
116 }
117
118 bool CallItem::isValid()
119 {
120     TRACE
121     return (!path().isEmpty());
122 }
123
124 bool CallItem::isValid() const
125 {
126     TRACE
127     return (!path().isEmpty());
128 }
129
130 QString CallItem::path() const
131 {
132     TRACE
133     return m_path;
134 }
135
136 bool CallItem::setPath(QString path)
137 {
138     TRACE
139     if (!m_path.isEmpty()) {
140         qCritical("Path already set and can not be changed once it is set");
141         return false;
142     } else if (path.isEmpty()) {
143         qCritical("It makes no sense to set Path to an empty string!?!?");
144         return false;
145     }
146
147     m_path = path;
148
149     init();
150
151     return true;
152 }
153
154 void CallItem::setDirection(CallDirection direction)
155 {
156     TRACE 
157   //  dynamic_cast<CallItemModel*>(model())->setDirection(direction);
158 }
159
160 QString CallItem::lineID() const
161 {
162     TRACE
163     return m_call->lineID();
164 }
165
166 QString CallItem::name() const
167 {
168     TRACE
169     return m_call->name();
170 }
171
172 CallState CallItem::state() const
173 {
174     TRACE
175     CallState cs = STATE_NONE;
176     QString state = m_call->state();
177
178         if (state == "active")
179            cs = STATE_ACTIVE;
180            
181         else if (state == "held")
182            cs = STATE_HELD;
183         else if (state == "dialing")
184            cs = STATE_DIALING;
185         else if (state == "alerting")
186            cs = STATE_ALERTING;
187         else if (state == "incoming")
188            cs = STATE_INCOMING;
189         else if (state == "waiting")
190            cs = STATE_WAITING;
191         else if (state == "disconnected")
192            cs = STATE_DISCONNECTED;
193
194     return cs;
195 }
196
197 CallDirection CallItem::direction() const
198 {
199     TRACE
200     return DIRECTION_NONE;
201          //return dynamic_cast<const CallItemModel*>(model())->direction();
202 }
203
204 CallDisconnectReason CallItem::reason() const
205 {
206     TRACE
207     return DISCONNECT_NONE; 
208     //return dynamic_cast<const CallItemModel*>(model())->reasonType();
209 }
210
211 int CallItem::duration() const
212 {
213     TRACE
214     return m_call->duration();
215 }
216
217 QDateTime CallItem::startTime() const
218 {
219     TRACE
220     return m_call->startTime();
221 }
222 /*
223 PeopleItem * CallItem::peopleItem() const
224 {
225     TRACE
226     return m_peopleItem;
227 }
228 */
229 CallProxy* CallItem::callProxy() const
230 {
231     TRACE
232  /*
233    if (model())
234      return dynamic_cast<const CallItemModel*>(model())->call();
235     else
236         return NULL;
237 */
238 return m_call;
239 }
240 /*
241 void CallItem::setPeopleItem(PeopleItem *person)
242 {
243     TRACE
244     if (m_peopleItem)
245         delete m_peopleItem;
246     m_peopleItem = person;
247 }
248 */
249 void CallItem::click()
250 {
251     TRACE
252
253     emit clicked();
254 }
255
256 void CallItem::silenceRingtone()
257 {
258     TRACE
259 /*
260     if(m_ringtone)
261     {
262         m_ringtone->stop();
263     }
264 */
265 }
266
267 void CallItem::callStateChanged()
268 {
269     TRACE
270    /*
271     if (state() == STATE_INCOMING ||
272         state() == STATE_WAITING)
273     {
274         // Start ringing
275         if (!m_isconnected && m_ringtone) {
276             connect(m_ringtone, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),
277                                 SLOT(ringtoneStatusChanged(QMediaPlayer::MediaStatus)));
278             m_isconnected = TRUE;
279             m_ringtone->play();
280         }
281     } else {
282         // Stop ringing
283         if (m_ringtone) {
284             disconnect(m_ringtone, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)));
285             m_isconnected = FALSE;
286             m_ringtone->stop();
287         }
288     }*/
289     emit stateChanged();
290 }
291
292 void CallItem::callDataChanged()
293 {
294     TRACE
295    // populatePeopleItem();
296 }
297
298 void CallItem::callDisconnected(const QString &reason)
299 {
300     TRACE
301     Q_UNUSED(reason);
302 }
303
304 /*
305 QVariant CallItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &val)
306 {
307     TRACE
308     if (change == QGraphicsItem::ItemSelectedHasChanged)
309         dynamic_cast<const CallItemModel*>(model())->setSelected(val.toBool());
310     return QGraphicsItem::itemChange(change, val);
311 }
312 */
313 /*
314 void CallItem::populatePeopleItem()
315 {
316     TRACE
317
318     QModelIndexList matches;
319     matches.clear();
320     int role = Seaside::SearchRole;
321     int hits = -1;
322
323     //% "Unknown Caller"
324     QString pi_name   = qtTrId("xx_unknown_caller");
325     QString pi_photo  = "icon-m-content-avatar-placeholder";
326     //% "Private"
327     QString pi_lineid = qtTrId("xx_private");
328
329     if (!lineID().isEmpty()) {
330         pi_lineid = stripLineID(lineID());
331         SeasideSyncModel *contacts = DA_SEASIDEMODEL;
332         QModelIndex first = contacts->index(0,Seaside::ColumnPhoneNumbers);
333         matches = contacts->match(first, role, QVariant(pi_lineid), hits);
334
335         QString firstName = QString();
336         QString lastName = QString();
337
338         if (!matches.isEmpty()) {
339             QModelIndex person = matches.at(0); //First match is all we look at
340             SEASIDE_SHORTCUTS
341             SEASIDE_SET_MODEL_AND_ROW(person.model(), person.row());
342
343             firstName = SEASIDE_FIELD(FirstName, String);
344             lastName = SEASIDE_FIELD(LastName, String);
345             pi_photo = SEASIDE_FIELD(Avatar, String);
346         } else if (!name().isEmpty()) {
347             // We don't have a contact, but we have a callerid name, let's use it
348             firstName = name();
349         }
350
351         if (lastName.isEmpty() && !firstName.isEmpty())
352             // Contacts first (common) name
353             //% "%1"
354             pi_name = qtTrId("xx_first_name").arg(firstName);
355         else if (firstName.isEmpty() && !lastName.isEmpty())
356             // BMC# 8079 - NW
357             // Contacts last (sur) name
358             //% "%1"
359             pi_name = qtTrId("xx_last_name").arg(lastName);
360         else if (!firstName.isEmpty() && !lastName.isEmpty())
361             // Contacts full, sortable name, is "Firstname Lastname"
362             //% "%1 %2"
363             pi_name = qtTrId("xx_first_last_name").arg(firstName)
364                 .arg(lastName);
365
366     } else {
367         //% "Unavailable"
368         pi_lineid = qtTrId("xx_unavailable");
369     }
370
371     if (m_peopleItem != NULL)
372         delete m_peopleItem;
373     m_peopleItem = new PeopleItem();
374
375     m_peopleItem->setName(pi_name);
376     m_peopleItem->setPhoto(pi_photo);
377     m_peopleItem->setPhone(pi_lineid);
378 }
379 */
380 /*
381 void CallItem::ringtoneStatusChanged(QMediaPlayer::MediaStatus status)
382 {
383     TRACE
384     if (status == QMediaPlayer::EndOfMedia)
385     {
386       m_ringtone->setMedia(QMediaContent(QUrl::fromLocalFile(m_ringtonefile)));
387       m_ringtone->play();
388     }
389 }
390 */
391 bool CallItem::multiparty()
392 {
393     TRACE 
394     return false;   
395  //  return (isValid())?dynamic_cast<const CallItemModel*>(model())->multiparty():false;
396 }
397
398 void CallItem::callMultipartyChanged()
399 {
400     TRACE
401     emit multipartyChanged();
402 }