CallView.cpp 29.3 KB
Newer Older
1
/***************************************************************************
2
 *   Copyright (C) 2009-2012 by Savoir-Faire Linux                         *
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 *   Author : Emmanuel Lepage Valle <emmanuel.lepage@savoirfairelinux.com >*
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 **************************************************************************/

//Parent
22
#include "CallView.h"
23
24
25
26
27

//Qt
#include <QtGui/QInputDialog>
#include <QtGui/QTreeWidget>
#include <QtGui/QTreeWidgetItem>
28
29
30
#include <QtGui/QPushButton>
#include <QtGui/QSpacerItem>
#include <QtGui/QGridLayout>
31
#include <QtGui/QLabel>
32

33
34
//KDE
#include <KDebug>
35
#include <KLineEdit>
36
#include <KStandardDirs>
37
38

//SFLPhone library
39
#include "lib/Contact.h"
Emmanuel Lepage's avatar
Emmanuel Lepage committed
40
41
#include "lib/sflphone_const.h"
#include "lib/callmanager_interface_singleton.h"
42

43
44
//SFLPhone
#include "widgets/CallTreeItem.h"
45
#include "SFLPhone.h"
Emmanuel Lepage's avatar
Emmanuel Lepage committed
46
#include "SFLPhoneView.h"
47
#include "klib/AkonadiBackend.h"
48

49
50

///Retrieve current and older calls from the daemon, fill history and the calls TreeView and enable drag n' drop
51
CallView::CallView(QWidget* parent) : QTreeWidget(parent),m_pActiveOverlay(0),m_pCallPendingTransfer(0)
52
53
54
55
{
   //Widget part
   setAcceptDrops(true);
   setDragEnabled(true);
Emmanuel Lepage's avatar
Emmanuel Lepage committed
56
   setAnimated   (true);
57
   CallTreeItemDelegate *delegate = new CallTreeItemDelegate();
58
   setItemDelegate(delegate);
59
   setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding));
60

Emmanuel Lepage's avatar
Emmanuel Lepage committed
61
62
63
64
65
66
67
68
   QString image = "<img width=100 height=100  src='"+KStandardDirs::locate("data","sflphone-client-kde/transferarraw.png")+"' />";

   m_pTransferOverlay = new CallViewOverlay ( this               );
   m_pTransferB       = new QPushButton     ( m_pTransferOverlay );
   m_pTransferLE      = new KLineEdit       ( m_pTransferOverlay );
   QGridLayout* gl    = new QGridLayout     ( m_pTransferOverlay );
   QLabel* lblImg     = new QLabel          ( image              );
   
69
70
   m_pTransferOverlay->setVisible(false);
   m_pTransferOverlay->resize(size());
71
   m_pTransferOverlay->setCornerWidget(lblImg);
72

73
   m_pTransferB->setText(i18n("Transfer"));
74
   m_pTransferB->setMaximumSize(70,9000);
Emmanuel Lepage's avatar
Emmanuel Lepage committed
75
76
77
78
79
   
   gl->addItem  (new QSpacerItem(0,0,QSizePolicy::Expanding,QSizePolicy::Minimum), 0 , 0 , 1 , 3 );
   gl->addWidget(m_pTransferLE                                                   , 1 , 1 , 1 , 2 );
   gl->addWidget(m_pTransferB                                                    , 1 , 4 , 1 , 2 );
   gl->addItem  (new QSpacerItem(0,0,QSizePolicy::Expanding,QSizePolicy::Minimum), 2 , 0 , 1 , 3 );
80

Emmanuel Lepage's avatar
Emmanuel Lepage committed
81
   //User Interface even
82
83
84
85
86
87
88
89
   //              SENDER                                   SIGNAL                              RECEIVER                     SLOT                        /
   /**/connect(this              , SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)              ) , this, SLOT( itemDoubleClicked(QTreeWidgetItem*,int)) );
   /**/connect(this              , SIGNAL(itemClicked(QTreeWidgetItem*,int)                    ) , this, SLOT( itemClicked(QTreeWidgetItem*,int))       );
   /**/connect(this              , SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)) , this, SLOT( itemClicked(QTreeWidgetItem*))           );
   /**/connect(SFLPhone::model() , SIGNAL(conferenceCreated(Call*)                             ) , this, SLOT( addConference(Call*))                    );
   /**/connect(SFLPhone::model() , SIGNAL(conferenceChanged(Call*)                             ) , this, SLOT( conferenceChanged(Call*))                );
   /**/connect(SFLPhone::model() , SIGNAL(aboutToRemoveConference(Call*)                       ) , this, SLOT( conferenceRemoved(Call*))                );
   /**/connect(SFLPhone::model() , SIGNAL(callAdded(Call*,Call*)                               ) , this, SLOT( addCall(Call*,Call*))                    );
90
91
   /**/connect(m_pTransferB      , SIGNAL(clicked()                                            ) , this, SLOT( transfer())                              );
   /**/connect(m_pTransferLE     , SIGNAL(returnPressed()                                      ) , this, SLOT( transfer())                              );
92
   /*                                                                                                                                                   */
93

Emmanuel Lepage's avatar
Emmanuel Lepage committed
94
} //CallView
95
96
97
98
99
100
101
102


/*****************************************************************************
 *                                                                           *
 *                        Drag and drop related code                         *
 *                                                                           *
 ****************************************************************************/

103
104
105
106
107
108
109
110
111
112
///Called when someone try to drop something on the tree
void CallView::dragEnterEvent ( QDragEnterEvent *e )
{
   kDebug() << "Potential drag event enter";
   e->accept();
}

///When a drag position change
void CallView::dragMoveEvent  ( QDragMoveEvent  *e )
{
113
   e->accept();
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
}

///When a drag event is leaving the widget
void CallView::dragLeaveEvent ( QDragLeaveEvent *e )
{
   kDebug() << "Potential drag event leave";
   e->accept();
}

///Proxy to handle transfer mime data
void CallView::transferDropEvent(Call* call,QMimeData* data)
{
   QByteArray encodedCallId = data->data( MIME_CALLID );
   SFLPhone::model()->attendedTransfer(SFLPhone::model()->getCall(encodedCallId),call);
}

///Proxy to handle conversation mime data
void CallView::conversationDropEvent(Call* call,QMimeData* data)
{
   kDebug() << "Calling real drag and drop function";
   dropMimeData(SFLPhone::model()->getIndex(call), 0, data, (Qt::DropAction)0);
}

Emmanuel Lepage's avatar
Emmanuel Lepage committed
137
///A call is dropped on another call
138
bool CallView::callToCall(QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action)
139
140
141
{
   Q_UNUSED(index)
   Q_UNUSED(action)
142
143
   QByteArray encodedCallId      = data->data( MIME_CALLID      );
   if (!QString(encodedCallId).isEmpty()) {
144
145
      if (SFLPhone::model()->getIndex(encodedCallId))
        clearArtefact(SFLPhone::model()->getIndex(encodedCallId));
146

147
      if (!parent) {
148
         kDebug() << "Call dropped on empty space";
149
         if (SFLPhone::model()->getIndex(encodedCallId)->parent()) {
150
            kDebug() << "Detaching participant";
151
            SFLPhone::model()->detachParticipant(SFLPhone::model()->getCall(encodedCallId));
152
153
         }
         else
154
            kDebug() << "The call is not in a conversation (doing nothing)";
155
156
         return true;
      }
157

158
      if (SFLPhone::model()->getCall(parent)->getCallId() == QString(encodedCallId)) {
159
         kDebug() << "Call dropped on itself (doing nothing)";
160
161
         return true;
      }
162

163
      if ((parent->childCount()) && (SFLPhone::model()->getIndex(encodedCallId)->childCount())) {
164
         kDebug() << "Merging two conferences";
165
         SFLPhone::model()->mergeConferences(SFLPhone::model()->getCall(parent),SFLPhone::model()->getCall(encodedCallId));
166
167
168
         return true;
      }
      else if ((parent->parent()) || (parent->childCount())) {
169
         kDebug() << "Call dropped on a conference";
170

171
         if ((SFLPhone::model()->getIndex(encodedCallId)->childCount()) && (!parent->childCount())) {
172
            kDebug() << "Conference dropped on a call (doing nothing)";
173
174
            return true;
         }
175

176
         QTreeWidgetItem* call1 = SFLPhone::model()->getIndex(encodedCallId);
177
         QTreeWidgetItem* call2 = (parent->parent())?parent->parent():parent;
178

179
         if (call1->parent()) {
180
            kDebug() << "Call 1 is part of a conference";
181
            if (call1->parent() == call2) {
182
               kDebug() << "Call dropped on it's own conference (doing nothing)";
183
184
               return true;
            }
185
            else if (SFLPhone::model()->getIndex(call1)->childCount()) {
186
               kDebug() << "Merging two conferences";
187
               SFLPhone::model()->mergeConferences(SFLPhone::model()->getCall(call1),SFLPhone::model()->getCall(call2));
188
189
            }
            else if (call1->parent()) {
190
               kDebug() << "Moving call from a conference to an other";
191
               SFLPhone::model()->detachParticipant(SFLPhone::model()->getCall(encodedCallId));
192
193
            }
         }
194
         kDebug() << "Adding participant";
195
         int state = SFLPhone::model()->getCall(call1)->getState();
196
         if(state == CALL_STATE_INCOMING || state == CALL_STATE_DIALING || state == CALL_STATE_TRANSFER || state == CALL_STATE_TRANSF_HOLD) {
197
            SFLPhone::model()->getCall(call1)->actionPerformed(CALL_ACTION_ACCEPT);
198
         }
199
         state = SFLPhone::model()->getCall(call2)->getState();
200
         if(state == CALL_STATE_INCOMING || state == CALL_STATE_DIALING || state == CALL_STATE_TRANSFER || state == CALL_STATE_TRANSF_HOLD) {
201
            SFLPhone::model()->getCall(call2)->actionPerformed(CALL_ACTION_ACCEPT);
202
         }
203
         SFLPhone::model()->addParticipant(SFLPhone::model()->getCall(call1),SFLPhone::model()->getCall(call2));
204
205
         return true;
      }
206
      else if ((SFLPhone::model()->getIndex(encodedCallId)->childCount()) && (!parent->childCount())) {
207
         kDebug() << "Call dropped on it's own conference (doing nothing)";
208
209
210
         return true;
      }

211
      kDebug() << "Call dropped on another call";
212
      SFLPhone::model()->createConferenceFromCall(SFLPhone::model()->getCall(encodedCallId),SFLPhone::model()->getCall(parent));
213
214
      return true;
   }
215
   return false;
Emmanuel Lepage's avatar
Emmanuel Lepage committed
216
} //callToCall
217

Emmanuel Lepage's avatar
Emmanuel Lepage committed
218
///A string containing a call number is dropped on a call
219
220
221
222
223
224
225
226
227
228
229
bool CallView::phoneNumberToCall(QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action)
{
   Q_UNUSED(index)
   Q_UNUSED(action)
   QByteArray encodedPhoneNumber = data->data( MIME_PHONENUMBER );
   if (!QString(encodedPhoneNumber).isEmpty()) {
      Contact* contact = AkonadiBackend::getInstance()->getContactByPhone(encodedPhoneNumber);
      QString name;
      if (contact)
         name = contact->getFormattedName();
      else
230
         name = i18n("Unknow");
231
      Call* call2 = SFLPhone::model()->addDialingCall(name, SFLPhone::model()->getCurrentAccountId());
232
233
234
      call2->appendText(QString(encodedPhoneNumber));
      if (!parent) {
         //Dropped on free space
235
         kDebug() << "Adding new dialing call";
236
237
238
239
      }
      else if (parent->childCount() || parent->parent()) {
         //Dropped on a conversation
         QTreeWidgetItem* call = (parent->parent())?parent->parent():parent;
240
         SFLPhone::model()->addParticipant(SFLPhone::model()->getCall(call),call2);
241
242
243
244
      }
      else {
         //Dropped on call
         call2->actionPerformed(CALL_ACTION_ACCEPT);
245
         int state = SFLPhone::model()->getCall(parent)->getState();
246
         if(state == CALL_STATE_INCOMING || state == CALL_STATE_DIALING || state == CALL_STATE_TRANSFER || state == CALL_STATE_TRANSF_HOLD) {
247
            SFLPhone::model()->getCall(parent)->actionPerformed(CALL_ACTION_ACCEPT);
248
         }
249
         SFLPhone::model()->createConferenceFromCall(call2,SFLPhone::model()->getCall(parent));
250
251
252
      }
   }
   return false;
Emmanuel Lepage's avatar
Emmanuel Lepage committed
253
} //phoneNumberToCall
254

Emmanuel Lepage's avatar
Emmanuel Lepage committed
255
///A contact ID is dropped on a call
256
257
bool CallView::contactToCall(QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action)
{
258
   kDebug() << "contactToCall";
259
260
   Q_UNUSED( index  )
   Q_UNUSED( action )
261
262
263
264
   QByteArray encodedContact = data->data( MIME_CONTACT );
   if (!QString(encodedContact).isEmpty()) {
      Contact* contact = AkonadiBackend::getInstance()->getContactByUid(encodedContact);
      if (contact) {
Emmanuel Lepage's avatar
Emmanuel Lepage committed
265
         Call* call2 = NULL;
266
         if (!SFLPhone::app()->view()->selectCallPhoneNumber(call2,contact))
267
268
269
            return false;
         if (!parent) {
            //Dropped on free space
270
            kDebug() << "Adding new dialing call";
271
272
273
274
         }
         else if (parent->childCount() || parent->parent()) {
            //Dropped on a conversation
            QTreeWidgetItem* call = (parent->parent())?parent->parent():parent;
275
            SFLPhone::model()->addParticipant(SFLPhone::model()->getCall(call),call2);
276
277
278
279
         }
         else {
            //Dropped on call
            call2->actionPerformed(CALL_ACTION_ACCEPT);
280
            int state = SFLPhone::model()->getCall(parent)->getState();
281
            if(state == CALL_STATE_INCOMING || state == CALL_STATE_DIALING || state == CALL_STATE_TRANSFER || state == CALL_STATE_TRANSF_HOLD) {
282
               SFLPhone::model()->getCall(parent)->actionPerformed(CALL_ACTION_ACCEPT);
283
            }
284
            SFLPhone::model()->createConferenceFromCall(call2,SFLPhone::model()->getCall(parent));
285
286
287
288
         }
      }
   }
   return false;
Emmanuel Lepage's avatar
Emmanuel Lepage committed
289
} //contactToCall
290
291

///Action performed when an item is dropped on the TreeView
292
bool CallView::dropMimeData(QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action)
293
294
295
{
   Q_UNUSED(index)
   Q_UNUSED(action)
296

297
298
299
300
301
   QByteArray encodedCallId      = data->data( MIME_CALLID      );
   QByteArray encodedPhoneNumber = data->data( MIME_PHONENUMBER );
   QByteArray encodedContact     = data->data( MIME_CONTACT     );

   if (!QString(encodedCallId).isEmpty()) {
302
      kDebug() << "CallId dropped"<< QString(encodedCallId);
303
304
305
      callToCall(parent, index, data, action);
   }
   else if (!QString(encodedPhoneNumber).isEmpty()) {
306
      kDebug() << "PhoneNumber dropped"<< QString(encodedPhoneNumber);
307
308
309
      phoneNumberToCall(parent, index, data, action);
   }
   else if (!QString(encodedContact).isEmpty()) {
310
      kDebug() << "Contact dropped"<< QString(encodedContact);
311
312
      contactToCall(parent, index, data, action);
   }
313
   return false;
Emmanuel Lepage's avatar
Emmanuel Lepage committed
314
} //dropMimeData
315
316
317

///Encode data to be tranported during the drag n' drop operation
QMimeData* CallView::mimeData( const QList<QTreeWidgetItem *> items) const
318
{
319
   kDebug() << "A call is being dragged";
320
321
322
   if (items.size() < 1) {
      return NULL;
   }
323

324
   QMimeData *mimeData = new QMimeData();
325

326
   //Call ID for internal call merging and spliting
327
328
   if (SFLPhone::model()->getCall(items[0])->isConference()) {
      mimeData->setData(MIME_CALLID, SFLPhone::model()->getCall(items[0])->getConfId().toAscii());
329
330
   }
   else {
331
      mimeData->setData(MIME_CALLID, SFLPhone::model()->getCall(items[0])->getCallId().toAscii());
332
   }
333

334
   //Plain text for other applications
335
   mimeData->setData(MIME_PLAIN_TEXT, QString(SFLPhone::model()->getCall(items[0])->getPeerName()+"\n"+SFLPhone::model()->getCall(items[0])->getPeerPhoneNumber()).toAscii());
336

337
   //TODO Comment this line if you don't want to see ugly artefact, but the caller details will not be visible while dragged
338
   items[0]->setText(0, SFLPhone::model()->getCall(items[0])->getPeerName() + "\n" + SFLPhone::model()->getCall(items[0])->getPeerPhoneNumber());
339
   return mimeData;
Emmanuel Lepage's avatar
Emmanuel Lepage committed
340
} //mimeData
341
342
343
344
345
346
347
348
349


/*****************************************************************************
 *                                                                           *
 *                            Call related code                              *
 *                                                                           *
 ****************************************************************************/

///Add a call in the model structure, the call must exist before being added to the model
350
Call* CallView::addCall(Call* call, Call* parent)
351
352
{
   QTreeWidgetItem* callItem = new QTreeWidgetItem();
353
   SFLPhone::model()->updateIndex(call,callItem);
354
   insertItem(callItem,parent);
355

356
   setCurrentItem(callItem);
357

358
359
360
361
   connect(call, SIGNAL(isOver(Call*)), this, SLOT(destroyCall(Call*)));
   return call;
}

362
363
364
///Transfer a call
void CallView::transfer()
{
365
   if (m_pCallPendingTransfer && !m_pTransferLE->text().isEmpty()) {
366
367
      SFLPhone::model()->transfer(m_pCallPendingTransfer,m_pTransferLE->text());
   }
368
369
370
371

   m_pCallPendingTransfer = 0;
   m_pTransferLE->clear();
   m_pTransferOverlay->setVisible(false);
372
373
}

374
375
376
377
378
379
/*****************************************************************************
 *                                                                           *
 *                            View related code                              *
 *                                                                           *
 ****************************************************************************/

380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
///Show the transfer overlay
void CallView::showTransferOverlay(Call* call)
{
   if (!m_pTransferOverlay) {
      kDebug() << "Creating overlay";
   }
   m_pTransferOverlay->setVisible(true);
   m_pCallPendingTransfer = call;
   m_pActiveOverlay = m_pTransferOverlay;
   m_pTransferLE->setFocus();
   connect(call,SIGNAL(changed()),this,SLOT(hideOverlay()));
}

///Is there an active overlay
bool CallView::haveOverlay()
{
   return (m_pActiveOverlay && m_pActiveOverlay->isVisible());
}

///Remove the active overlay
void CallView::hideOverlay()
{
   if (m_pActiveOverlay)
      m_pActiveOverlay->setVisible(false);
   m_pActiveOverlay = 0;
405
406
407
408
409
410
411

   if (m_pCallPendingTransfer && m_pCallPendingTransfer->getState() == CALL_STATE_TRANSFER ) {
      m_pCallPendingTransfer->actionPerformed(CALL_ACTION_TRANSFER);
   }
   
   m_pCallPendingTransfer = 0;
   m_pTransferLE->clear();
Emmanuel Lepage's avatar
Emmanuel Lepage committed
412
} //hideOverlay
413
414
415
416
417
418
419
420
421

///Be sure the size of the overlay stay the same
void CallView::resizeEvent (QResizeEvent *e)
{
   if (m_pTransferOverlay)
      m_pTransferOverlay->resize(size());
   QTreeWidget::resizeEvent(e);
}

422
///Set the TreeView header text
423
void CallView::setTitle(const QString& title)
424
425
426
427
428
{
   headerItem()->setText(0,title);
}

///Select an item in the TreeView
429
bool CallView::selectItem(Call* item)
430
{
431
432
   if (SFLPhone::model()->getIndex(item)) {
      setCurrentItem(SFLPhone::model()->getIndex(item));
433
434
435
436
437
438
439
      return true;
   }
   else
      return false;
}

///Return the current item
440
Call* CallView::getCurrentItem()
441
{
442
443
   if (currentItem() && SFLPhone::model()->getCall(QTreeWidget::currentItem()))
      return SFLPhone::model()->getCall(QTreeWidget::currentItem());
444
445
446
447
448
   else
      return false;
}

///Remove a TreeView item and delete it
449
bool CallView::removeItem(Call* item)
450
{
451
452
453
454
455
456
   if (indexOfTopLevelItem(SFLPhone::model()->getIndex(item)) != -1) {
      QTreeWidgetItem* parent = itemAt(indexOfTopLevelItem(SFLPhone::model()->getIndex(item)),0);
      removeItemWidget(SFLPhone::model()->getIndex(item),0);
      if (parent->childCount() == 0) //TODO this have to be done in the daemon, not here, but oops still happen too often to ignore
         removeItemWidget(parent,0);
      return true;
457
458
459
460
461
462
   }
   else
      return false;
}

///Return the TreeView, this
463
QWidget* CallView::getWidget()
464
465
466
467
468
{
   return this;
}

///Convenience wrapper around extractItem(QTreeWidgetItem*)
469
QTreeWidgetItem* CallView::extractItem(const QString& callId)
470
{
471
   QTreeWidgetItem* currentItem = SFLPhone::model()->getIndex(callId);
472
473
474
475
   return extractItem(currentItem);
}

///Extract an item from the TreeView and return it, the item is -not- deleted
476
QTreeWidgetItem* CallView::extractItem(QTreeWidgetItem* item)
477
478
{
   QTreeWidgetItem* parentItem = item->parent();
479

480
481
   if (parentItem) {
      if ((indexOfTopLevelItem(parentItem) == -1 ) || (parentItem->indexOfChild(item) == -1)) {
482
         kDebug() << "The conversation does not exist";
483
484
         return 0;
      }
485

486
487
488
489
490
491
      QTreeWidgetItem* toReturn = parentItem->takeChild(parentItem->indexOfChild(item));

      return toReturn;
   }
   else
      return takeTopLevelItem(indexOfTopLevelItem(item));
Emmanuel Lepage's avatar
Emmanuel Lepage committed
492
} //extractItem
493
494

///Convenience wrapper around insertItem(QTreeWidgetItem*, QTreeWidgetItem*)
495
CallTreeItem* CallView::insertItem(QTreeWidgetItem* item, Call* parent)
496
{
497
   return insertItem(item,(parent)?SFLPhone::model()->getIndex(parent):0);
498
499
500
}

///Insert a TreeView item in the TreeView as child of parent or as a top level item, also restore the item Widget
501
CallTreeItem* CallView::insertItem(QTreeWidgetItem* item, QTreeWidgetItem* parent)
502
503
{
   if (!item) {
504
      kDebug() << "This is not a valid call";
505
506
      return 0;
   }
507

508
509
510
511
   if (!parent)
      insertTopLevelItem(0,item);
   else
      parent->addChild(item);
512

513
   CallTreeItem* callItem = new CallTreeItem();
514
515
516
517
   connect(callItem, SIGNAL(showChilds(CallTreeItem*)), this, SLOT(showDropOptions(CallTreeItem*)));
   connect(callItem, SIGNAL(askTransfer(Call*)), this, SLOT(showTransferOverlay(Call*)));
   connect(callItem, SIGNAL(transferDropEvent(Call*,QMimeData*)), this, SLOT(transferDropEvent(Call*,QMimeData*)));
   connect(callItem, SIGNAL(conversationDropEvent(Call*,QMimeData*)), this, SLOT(conversationDropEvent(Call*,QMimeData*)));
518

519
520
   SFLPhone::model()->updateWidget(SFLPhone::model()->getCall(item), callItem);
   callItem->setCall(SFLPhone::model()->getCall(item));
521

522
   setItemWidget(item,0,callItem);
523

524
525
   expandAll();
   return callItem;
Emmanuel Lepage's avatar
Emmanuel Lepage committed
526
} //insertItem
527
528

///Remove a call from the interface
529
void CallView::destroyCall(Call* toDestroy)
530
{
531
   if (SFLPhone::model()->getIndex(toDestroy) == currentItem())
532
      setCurrentItem(0);
533

534
   if (!SFLPhone::model()->getIndex(toDestroy))
535
       kDebug() << "Call not found";
536
537
   else if (indexOfTopLevelItem(SFLPhone::model()->getIndex(toDestroy)) != -1)
      takeTopLevelItem(indexOfTopLevelItem(SFLPhone::model()->getIndex(toDestroy)));
538
539
   else if (SFLPhone::model()->getIndex(toDestroy)->parent()) {
      QTreeWidgetItem* parent = SFLPhone::model()->getIndex(toDestroy)->parent();
540
      SFLPhone::model()->getIndex(toDestroy)->parent()->removeChild(SFLPhone::model()->getIndex(toDestroy));
541
542
543
544
545
546
547
      if (parent->childCount() == 0) /*This should never happen, but it does*/
         takeTopLevelItem(indexOfTopLevelItem(parent));
      else if (parent->childCount() == 1) {
         addTopLevelItem(extractItem(parent->child(0)));
         takeTopLevelItem(indexOfTopLevelItem(parent));
      } //TODO make sure it just never happen and remove this logic code
   }
548
   else
549
      kDebug() << "Call not found";
Emmanuel Lepage's avatar
Emmanuel Lepage committed
550
} //destroyCall
551
552

/// @todo Remove the text partially covering the TreeView item widget when it is being dragged, a beter implementation is needed
553
void CallView::clearArtefact(QTreeWidgetItem* item)
554
{
555
556
   if (item)
      item->setText(0,"");
557
558
559
560
561
562
563
564
565
566
567
}


/*****************************************************************************
 *                                                                           *
 *                           Event related code                              *
 *                                                                           *
 ****************************************************************************/

void CallView::itemDoubleClicked(QTreeWidgetItem* item, int column) {
   Q_UNUSED(column)
568
   kDebug() << "Item doubleclicked" << SFLPhone::model()->getCall(item)->getState();
569
570
571
572
   switch(SFLPhone::model()->getCall(item)->getState()) {
      case CALL_STATE_INCOMING:
         SFLPhone::model()->getCall(item)->actionPerformed(CALL_ACTION_ACCEPT);
         break;
573
      case CALL_STATE_HOLD:
574
         SFLPhone::model()->getCall(item)->actionPerformed(CALL_ACTION_HOLD);
575
576
         break;
      case CALL_STATE_DIALING:
577
         SFLPhone::model()->getCall(item)->actionPerformed(CALL_ACTION_ACCEPT);
578
579
         break;
      default:
580
         kDebug() << "Double clicked an item with no action on double click.";
581
    }
Emmanuel Lepage's avatar
Emmanuel Lepage committed
582
} //itemDoubleClicked
583
584
585

void CallView::itemClicked(QTreeWidgetItem* item, int column) {
   Q_UNUSED(column)
586
   emit itemChanged(SFLPhone::model()->getCall(item));
587
   kDebug() << "Item clicked";
588
589
590
591
592
593
594
595
596
597
}


/*****************************************************************************
 *                                                                           *
 *                         Conference related code                           *
 *                                                                           *
 ****************************************************************************/

///Add a new conference, get the call list and update the interface as needed
598
Call* CallView::addConference(Call* conf)
599
{
600
   kDebug() << "Conference created";
601
   Call* newConf =  conf;//SFLPhone::model()->addConference(confID);//TODO ELV?
602

603
   QTreeWidgetItem* confItem = new QTreeWidgetItem();
604
   SFLPhone::model()->updateIndex(conf,confItem);
Emmanuel Lepage's avatar
Emmanuel Lepage committed
605

606
   insertItem(confItem,(QTreeWidgetItem*)0);
Emmanuel Lepage's avatar
Emmanuel Lepage committed
607

608

609
610
611
   setCurrentItem(confItem);

   CallManagerInterface& callManager = CallManagerInterfaceSingleton::getInstance();
612
   QStringList callList = callManager.getParticipantList(conf->getConfId());
613

614
   foreach (QString callId, callList) {
615
      kDebug() << "Adding " << callId << "to the conversation";
616
      insertItem(extractItem(SFLPhone::model()->getIndex(callId)),confItem);
617
   }
618

619
620
   Q_ASSERT_X(confItem->childCount() == 0, "add conference","Conference created, but without any participants");
   return newConf;
Emmanuel Lepage's avatar
Emmanuel Lepage committed
621
} //addConference
622
623

///Executed when the daemon signal a modification in an existing conference. Update the call list and update the TreeView
624
bool CallView::conferenceChanged(Call* conf)
625
{
626
   if (!dynamic_cast<Call*>(conf)) return false;
627
   kDebug() << "Conference changed";
628
629
   //if (!SFLPhone::model()->conferenceChanged(confId, state))
   //  return false;
630
631

   CallManagerInterface& callManager = CallManagerInterfaceSingleton::getInstance();
632
   QStringList callList = callManager.getParticipantList(conf->getConfId());
633
634
635

   QList<QTreeWidgetItem*> buffer;
   foreach (QString callId, callList) {
636
637
638
639
      if (SFLPhone::model()->getCall(callId)) {
         QTreeWidgetItem* item3 = extractItem(SFLPhone::model()->getIndex(callId));
         insertItem(item3, SFLPhone::model()->getIndex(conf));
         buffer << SFLPhone::model()->getIndex(callId);
640
641
      }
      else
642
         kDebug() << "Call " << callId << " does not exist";
643
644
   }

645
646
647
648
649
   if (SFLPhone::model()->getIndex(conf)) /*Can happen is the daemon crashed*/
      for (int j =0; j < SFLPhone::model()->getIndex(conf)->childCount();j++) {
         if (buffer.indexOf(SFLPhone::model()->getIndex(conf)->child(j)) == -1)
            insertItem(extractItem(SFLPhone::model()->getIndex(conf)->child(j)));
      }
650

651
   Q_ASSERT_X(SFLPhone::model()->getIndex(conf)->childCount() == 0,"changing conference","A conference can't have no participants");
652

653
   return true;
Emmanuel Lepage's avatar
Emmanuel Lepage committed
654
} //conferenceChanged
655
656

///Remove a conference from the model and the TreeView
657
void CallView::conferenceRemoved(Call* conf)
658
{
659
   kDebug() << "Attempting to remove conference";
660
661
662
663
   QTreeWidgetItem* idx = SFLPhone::model()->getIndex(conf);
   if (idx) {
   while (idx->childCount()) {
      insertItem(extractItem(SFLPhone::model()->getIndex(conf)->child(0)));
664
   }
665
666
   takeTopLevelItem(indexOfTopLevelItem(SFLPhone::model()->getIndex(conf)));
   //SFLPhone::model()->conferenceRemoved(confId);
667
   kDebug() << "Conference removed";
668
669
   }
   else {
670
      kDebug() << "Conference not found";
671
   }
Emmanuel Lepage's avatar
Emmanuel Lepage committed
672
} //conferenceRemoved
673
674
675
676

///Clear the list of old calls //TODO Clear them from the daemon
void CallView::clearHistory()
{
677
   //SFLPhone::model()->getHistory().clear();
678
679
}

680
681
682
683
///Redirect keypresses to parent
void CallView::keyPressEvent(QKeyEvent* event) {
   SFLPhone::app()->view()->keyPressEvent(event);
}
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712


/*****************************************************************************
 *                                                                           *
 *                                 Overlay                                   *
 *                                                                           *
 ****************************************************************************/

///Constructor
CallViewOverlay::CallViewOverlay(QWidget* parent) : QWidget(parent),m_pIcon(0),m_pTimer(0),m_enabled(true),m_black("black")
{
   m_black.setAlpha(75);
}

///Add a widget (usually an icon) in the corner
void CallViewOverlay::setCornerWidget(QWidget* wdg) {
   wdg->setParent      ( this                       );
   wdg->setMinimumSize ( 100         , 100          );
   wdg->resize         ( 100         , 100          );
   wdg->move           ( width()-100 , height()-100 );
   m_pIcon = wdg;
}

///Overload the setVisible method to trigger the timer
void CallViewOverlay::setVisible(bool enabled) {
   if (m_enabled != enabled) {
      if (m_pTimer) {
         m_pTimer->stop();
         disconnect(m_pTimer);
713
         delete m_pTimer;
714
      }
715
      m_pTimer = new QTimer(this);
716
717
718
719
720
721
722
723
      connect(m_pTimer, SIGNAL(timeout()), this, SLOT(changeVisibility()));
      m_step = 0;
      m_black.setAlpha(0);
      repaint();
      m_pTimer->start(10);
   }
   m_enabled = enabled;
   QWidget::setVisible(enabled);
Emmanuel Lepage's avatar
Emmanuel Lepage committed
724
} //setVisible
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749

///How to paint the overlay
void CallViewOverlay::paintEvent(QPaintEvent* event) {
   Q_UNUSED(event)
   QPainter customPainter(this);
   customPainter.fillRect(rect(),m_black);
}

///Be sure the event is always the right size
void CallViewOverlay::resizeEvent(QResizeEvent *e) {
   Q_UNUSED(e)
   if (m_pIcon) {
      m_pIcon->setMinimumSize(100,100);
      m_pIcon->move(width()-100,height()-100);
   }
}

///Step by step animation to fade in/out
void CallViewOverlay::changeVisibility() {
   m_step++;
   m_black.setAlpha(0.1*m_step*m_step);
   repaint();
   if (m_step >= 35)
      m_pTimer->stop();
}