Call.cpp 27.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/***************************************************************************
 *   Copyright (C) 2009-2010 by Savoir-Faire Linux                         *
 *   Author : Jérémy Quentin <jeremy.quentin@savoirfairelinux.com>         *
 *            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.             *
 ***************************************************************************/
#include "Call.h"

23
24
#include "CallModel.h"

25
26
27
28
29
30
#include "callmanager_interface_singleton.h"
#include "configurationmanager_interface_singleton.h"


const call_state Call::actionPerformedStateMap [11][5] = 
{
31
//                      ACCEPT                  REFUSE                  TRANSFER                   HOLD                           RECORD           
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*INCOMING     */  {CALL_STATE_INCOMING   , CALL_STATE_INCOMING    , CALL_STATE_ERROR        , CALL_STATE_INCOMING     ,  CALL_STATE_INCOMING     },
/*RINGING      */  {CALL_STATE_ERROR      , CALL_STATE_RINGING     , CALL_STATE_ERROR        , CALL_STATE_ERROR        ,  CALL_STATE_RINGING      },
/*CURRENT      */  {CALL_STATE_ERROR      , CALL_STATE_CURRENT     , CALL_STATE_TRANSFER     , CALL_STATE_CURRENT      ,  CALL_STATE_CURRENT      },
/*DIALING      */  {CALL_STATE_DIALING    , CALL_STATE_OVER        , CALL_STATE_ERROR        , CALL_STATE_ERROR        ,  CALL_STATE_ERROR        },
/*HOLD         */  {CALL_STATE_ERROR      , CALL_STATE_HOLD        , CALL_STATE_TRANSF_HOLD  , CALL_STATE_HOLD         ,  CALL_STATE_HOLD         },
/*FAILURE      */  {CALL_STATE_ERROR      , CALL_STATE_FAILURE     , CALL_STATE_ERROR        , CALL_STATE_ERROR        ,  CALL_STATE_ERROR        },
/*BUSY         */  {CALL_STATE_ERROR      , CALL_STATE_BUSY        , CALL_STATE_ERROR        , CALL_STATE_ERROR        ,  CALL_STATE_ERROR        },
/*TRANSFER     */  {CALL_STATE_TRANSFER   , CALL_STATE_TRANSFER    , CALL_STATE_CURRENT      , CALL_STATE_TRANSFER     ,  CALL_STATE_TRANSFER     },
/*TRANSF_HOLD  */  {CALL_STATE_TRANSF_HOLD, CALL_STATE_TRANSF_HOLD , CALL_STATE_HOLD         , CALL_STATE_TRANSF_HOLD  ,  CALL_STATE_TRANSF_HOLD  },
/*OVER         */  {CALL_STATE_ERROR      , CALL_STATE_ERROR       , CALL_STATE_ERROR        , CALL_STATE_ERROR        ,  CALL_STATE_ERROR        },
/*ERROR        */  {CALL_STATE_ERROR      , CALL_STATE_ERROR       , CALL_STATE_ERROR        , CALL_STATE_ERROR        ,  CALL_STATE_ERROR        }
};


const function Call::actionPerformedFunctionMap[11][5] = 
{ 
48
//                      ACCEPT               REFUSE            TRANSFER                 HOLD                  RECORD            
49
50
51
/*INCOMING       */  {&Call::accept     , &Call::refuse   , &Call::acceptTransf   , &Call::acceptHold  ,  &Call::setRecord     },
/*RINGING        */  {&Call::nothing    , &Call::hangUp   , &Call::nothing        , &Call::nothing     ,  &Call::setRecord     },
/*CURRENT        */  {&Call::nothing    , &Call::hangUp   , &Call::nothing        , &Call::hold        ,  &Call::setRecord     },
52
/*DIALING        */  {&Call::call       , &Call::cancel   , &Call::nothing        , &Call::nothing     ,  &Call::nothing       },
53
54
55
56
57
58
59
60
61
62
63
64
/*HOLD           */  {&Call::nothing    , &Call::hangUp   , &Call::nothing        , &Call::unhold      ,  &Call::setRecord     },
/*FAILURE        */  {&Call::nothing    , &Call::hangUp   , &Call::nothing        , &Call::nothing     ,  &Call::nothing       },
/*BUSY           */  {&Call::nothing    , &Call::hangUp   , &Call::nothing        , &Call::nothing     ,  &Call::nothing       },
/*TRANSFERT      */  {&Call::transfer   , &Call::hangUp   , &Call::nothing        , &Call::hold        ,  &Call::setRecord     },
/*TRANSFERT_HOLD */  {&Call::transfer   , &Call::hangUp   , &Call::nothing        , &Call::unhold      ,  &Call::setRecord     },
/*OVER           */  {&Call::nothing    , &Call::nothing  , &Call::nothing        , &Call::nothing     ,  &Call::nothing       },
/*ERROR          */  {&Call::nothing    , &Call::nothing  , &Call::nothing        , &Call::nothing     ,  &Call::nothing       }
};


const call_state Call::stateChangedStateMap [11][6] = 
{
65
//                      RINGING                  CURRENT             BUSY              HOLD                           HUNGUP           FAILURE            
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*INCOMING     */ {CALL_STATE_INCOMING    , CALL_STATE_CURRENT  , CALL_STATE_BUSY   , CALL_STATE_HOLD         ,  CALL_STATE_OVER  ,  CALL_STATE_FAILURE  },
/*RINGING      */ {CALL_STATE_RINGING     , CALL_STATE_CURRENT  , CALL_STATE_BUSY   , CALL_STATE_HOLD         ,  CALL_STATE_OVER  ,  CALL_STATE_FAILURE  },
/*CURRENT      */ {CALL_STATE_CURRENT     , CALL_STATE_CURRENT  , CALL_STATE_BUSY   , CALL_STATE_HOLD         ,  CALL_STATE_OVER  ,  CALL_STATE_FAILURE  },
/*DIALING      */ {CALL_STATE_RINGING     , CALL_STATE_CURRENT  , CALL_STATE_BUSY   , CALL_STATE_HOLD         ,  CALL_STATE_OVER  ,  CALL_STATE_FAILURE  },
/*HOLD         */ {CALL_STATE_HOLD        , CALL_STATE_CURRENT  , CALL_STATE_BUSY   , CALL_STATE_HOLD         ,  CALL_STATE_OVER  ,  CALL_STATE_FAILURE  },
/*FAILURE      */ {CALL_STATE_FAILURE     , CALL_STATE_FAILURE  , CALL_STATE_BUSY   , CALL_STATE_FAILURE      ,  CALL_STATE_OVER  ,  CALL_STATE_FAILURE  },
/*BUSY         */ {CALL_STATE_BUSY        , CALL_STATE_CURRENT  , CALL_STATE_BUSY   , CALL_STATE_BUSY         ,  CALL_STATE_OVER  ,  CALL_STATE_FAILURE  },
/*TRANSFER     */ {CALL_STATE_TRANSFER    , CALL_STATE_TRANSFER , CALL_STATE_BUSY   , CALL_STATE_TRANSF_HOLD  ,  CALL_STATE_OVER  ,  CALL_STATE_FAILURE  },
/*TRANSF_HOLD  */ {CALL_STATE_TRANSF_HOLD , CALL_STATE_TRANSFER , CALL_STATE_BUSY   , CALL_STATE_TRANSF_HOLD  ,  CALL_STATE_OVER  ,  CALL_STATE_FAILURE  },
/*OVER         */ {CALL_STATE_OVER        , CALL_STATE_OVER     , CALL_STATE_OVER   , CALL_STATE_OVER         ,  CALL_STATE_OVER  ,  CALL_STATE_OVER     },
/*ERROR        */ {CALL_STATE_ERROR       , CALL_STATE_ERROR    , CALL_STATE_ERROR  , CALL_STATE_ERROR        ,  CALL_STATE_ERROR ,  CALL_STATE_ERROR    }
};

const function Call::stateChangedFunctionMap[11][6] = 
{ 
81
82
83
//                      RINGING                  CURRENT             BUSY              HOLD                    HUNGUP           FAILURE           
/*INCOMING       */  {&Call::nothing    , &Call::start     , &Call::startWeird     , &Call::startWeird   ,  &Call::startStop    , &Call::start   },
/*RINGING        */  {&Call::nothing    , &Call::start     , &Call::start          , &Call::start        ,  &Call::startStop    , &Call::start   },
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*CURRENT        */  {&Call::nothing    , &Call::nothing   , &Call::warning        , &Call::nothing      ,  &Call::stop         , &Call::nothing },
/*DIALING        */  {&Call::nothing    , &Call::warning   , &Call::warning        , &Call::warning      ,  &Call::stop         , &Call::warning },
/*HOLD           */  {&Call::nothing    , &Call::nothing   , &Call::warning        , &Call::nothing      ,  &Call::stop         , &Call::nothing },
/*FAILURE        */  {&Call::nothing    , &Call::warning   , &Call::warning        , &Call::warning      ,  &Call::stop         , &Call::nothing },
/*BUSY           */  {&Call::nothing    , &Call::nothing   , &Call::nothing        , &Call::warning      ,  &Call::stop         , &Call::nothing },
/*TRANSFERT      */  {&Call::nothing    , &Call::nothing   , &Call::warning        , &Call::nothing      ,  &Call::stop         , &Call::nothing },
/*TRANSFERT_HOLD */  {&Call::nothing    , &Call::nothing   , &Call::warning        , &Call::nothing      ,  &Call::stop         , &Call::nothing },
/*OVER           */  {&Call::nothing    , &Call::warning   , &Call::warning        , &Call::warning      ,  &Call::stop         , &Call::warning },
/*ERROR          */  {&Call::nothing    , &Call::nothing   , &Call::nothing        , &Call::nothing      ,  &Call::stop         , &Call::nothing }
};

const char * Call::historyIcons[3] = {ICON_HISTORY_INCOMING, ICON_HISTORY_OUTGOING, ICON_HISTORY_MISSED};

/*void Call::initCallItemWidget()
{
   itemWidget = new QWidget();
   labelIcon = new QLabel();
   labelCallNumber = new QLabel(peerPhoneNumber);
   labelTransferPrefix = new QLabel(i18n("Transfer to : "));
   labelTransferNumber = new QLabel();
   QSpacerItem * horizontalSpacer = new QSpacerItem(16777215, 20, QSizePolicy::Preferred, QSizePolicy::Minimum);
   
   QHBoxLayout * mainLayout = new QHBoxLayout();
   mainLayout->setContentsMargins ( 3, 1, 2, 1);
   mainLayout->setSpacing(4);
   QVBoxLayout * descr = new QVBoxLayout();
   descr->setMargin(1);
   descr->setSpacing(1);
   QHBoxLayout * transfer = new QHBoxLayout();
   transfer->setMargin(0);
   transfer->setSpacing(0);
   mainLayout->addWidget(labelIcon);
   if(! peerName.isEmpty())
   {
      labelPeerName = new QLabel(peerName);
      descr->addWidget(labelPeerName);
   }
   descr->addWidget(labelCallNumber);
   transfer->addWidget(labelTransferPrefix);
   transfer->addWidget(labelTransferNumber);
   descr->addLayout(transfer);
   mainLayout->addLayout(descr);
   mainLayout->addItem(horizontalSpacer);

   itemWidget->setLayout(mainLayout);
}*/

 /*void Call::setItemIcon(const QString pixmap)
{
   labelIcon->setPixmap(QPixmap(pixmap));
   }*/


Call::Call(call_state startState, QString callId, QString peerName, QString peerNumber, QString account)
   : conference(false)
{
   this->callId = callId;
   this->peerPhoneNumber = peerNumber;
   this->peerName = peerName;
   changeCurrentState(startState);
   this->account = account;
   this->recording = false;
   //   this->historyItemWidget = NULL;
   this->startTime = NULL;
   this->stopTime = NULL;
   //   this->initCallItemWidget();
        emit changed();
}

Call::Call(QString confId, QString account) 
   : conference(true)
{
   this->confId = confId;
   this->account = account;
}
#include <unistd.h>
Call * Call::buildExistingCall(QString callId)
{
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   MapStringString details = callManager.getCallDetails(callId).value();
   qDebug() << "Constructing existing call with details : " << details;
   QString peerNumber = details[CALL_PEER_NUMBER];
   QString peerName = details[CALL_PEER_NAME];
   call_state startState = getStartStateFromDaemonCallState(details[CALL_STATE], details[CALL_TYPE]);
   QString account = details[CALL_ACCOUNTID];
   Call * call = new Call(startState, callId, peerName, peerNumber, account);
   call->startTime = new QDateTime(QDateTime::currentDateTime());
   call->recording = callManager.getIsRecording(callId);
   call->historyState = getHistoryStateFromDaemonCallState(details[CALL_STATE], details[CALL_TYPE]);
   return call;
}

Call::~Call()
{
   delete startTime;
   delete stopTime;
}
   
Call * Call::buildDialingCall(QString callId, const QString & peerName, QString account)
{
   Call * call = new Call(CALL_STATE_DIALING, callId, peerName, "", account);
   call->historyState = NONE;
   return call;
}

Call * Call::buildIncomingCall(const QString & callId/*, const QString & from, const QString & account*/)
{
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   MapStringString details = callManager.getCallDetails(callId).value();
   qDebug() << "details = " << details;
   QString from = details[CALL_PEER_NUMBER];
   QString account = details[CALL_ACCOUNTID];
   QString peerName = details[CALL_PEER_NAME];
   Call * call = new Call(CALL_STATE_INCOMING, callId, peerName, from, account);
   call->historyState = MISSED;
   return call;
}

Call * Call::buildRingingCall(const QString & callId)
{
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   MapStringString details = callManager.getCallDetails(callId).value();
   QString from = details[CALL_PEER_NUMBER];
   QString account = details[CALL_ACCOUNTID];
   QString peerName = details[CALL_PEER_NAME];
   Call * call = new Call(CALL_STATE_RINGING, callId, peerName, from, account);
   call->historyState = OUTGOING;
   return call;
}

Call * Call::buildHistoryCall(const QString & callId, uint startTimeStamp, uint stopTimeStamp, QString account, QString name, QString number, QString type)
{
   if(name == "empty") name = "";
   Call * call = new Call(CALL_STATE_OVER, callId, name, number, account);
   call->startTime = new QDateTime(QDateTime::fromTime_t(startTimeStamp));
   call->stopTime = new QDateTime(QDateTime::fromTime_t(stopTimeStamp));
   call->historyState = getHistoryStateFromType(type);
   return call;
}


history_state Call::getHistoryStateFromType(QString type)
{
   if(type == DAEMON_HISTORY_TYPE_MISSED)
      return MISSED;
   else if(type == DAEMON_HISTORY_TYPE_OUTGOING)
      return OUTGOING;
   else if(type == DAEMON_HISTORY_TYPE_INCOMING)
      return INCOMING;
   return NONE;
}

QString Call::getTypeFromHistoryState(history_state historyState)
{
   if(historyState == MISSED)
      return DAEMON_HISTORY_TYPE_MISSED;
   else if(historyState == OUTGOING)
      return DAEMON_HISTORY_TYPE_OUTGOING;
   else if(historyState == INCOMING)
      return DAEMON_HISTORY_TYPE_INCOMING;
   return QString();
}

call_state Call::getStartStateFromDaemonCallState(QString daemonCallState, QString daemonCallType)
{
   if(daemonCallState == DAEMON_CALL_STATE_INIT_CURRENT)
      return CALL_STATE_CURRENT;
   else if(daemonCallState == DAEMON_CALL_STATE_INIT_HOLD)
      return CALL_STATE_HOLD;
   else if(daemonCallState == DAEMON_CALL_STATE_INIT_BUSY)
      return CALL_STATE_BUSY;
   else if(daemonCallState == DAEMON_CALL_STATE_INIT_INACTIVE && daemonCallType == DAEMON_CALL_TYPE_INCOMING)
   {
      return CALL_STATE_INCOMING;
   }
   else if(daemonCallState == DAEMON_CALL_STATE_INIT_INACTIVE && daemonCallType == DAEMON_CALL_TYPE_OUTGOING)
      return CALL_STATE_RINGING;
   else if(daemonCallState == DAEMON_CALL_STATE_INIT_INCOMING)
      return CALL_STATE_INCOMING;
   else if(daemonCallState == DAEMON_CALL_STATE_INIT_RINGING)
      return CALL_STATE_RINGING;
   else
      return CALL_STATE_FAILURE;
}

history_state Call::getHistoryStateFromDaemonCallState(QString daemonCallState, QString daemonCallType)
{
   if((daemonCallState == DAEMON_CALL_STATE_INIT_CURRENT || daemonCallState == DAEMON_CALL_STATE_INIT_HOLD) && daemonCallType == DAEMON_CALL_TYPE_INCOMING)
      return INCOMING;
   else if((daemonCallState == DAEMON_CALL_STATE_INIT_CURRENT || daemonCallState == DAEMON_CALL_STATE_INIT_HOLD) && daemonCallType == DAEMON_CALL_TYPE_OUTGOING)
      return OUTGOING;
   else if(daemonCallState == DAEMON_CALL_STATE_INIT_BUSY)
      return OUTGOING;
   else if(daemonCallState == DAEMON_CALL_STATE_INIT_INACTIVE && daemonCallType == DAEMON_CALL_TYPE_INCOMING)
      return INCOMING;
   else if(daemonCallState == DAEMON_CALL_STATE_INIT_INACTIVE && daemonCallType == DAEMON_CALL_TYPE_OUTGOING)
      return MISSED;
   else
      return NONE;
}

daemon_call_state Call::toDaemonCallState(const QString & stateName)
{
   if(stateName == QString(CALL_STATE_CHANGE_HUNG_UP))
      return DAEMON_CALL_STATE_HUNG_UP;
   if(stateName == QString(CALL_STATE_CHANGE_RINGING))
      return DAEMON_CALL_STATE_RINGING;
   if(stateName == QString(CALL_STATE_CHANGE_CURRENT))
      return DAEMON_CALL_STATE_CURRENT;
   if(stateName == QString(CALL_STATE_CHANGE_UNHOLD_CURRENT))
      return DAEMON_CALL_STATE_CURRENT;
   if(stateName == QString(CALL_STATE_CHANGE_UNHOLD_RECORD))
      return DAEMON_CALL_STATE_CURRENT;
   if(stateName == QString(CALL_STATE_CHANGE_HOLD))
      return DAEMON_CALL_STATE_HOLD;
   if(stateName == QString(CALL_STATE_CHANGE_BUSY))
      return DAEMON_CALL_STATE_BUSY;
   if(stateName == QString(CALL_STATE_CHANGE_FAILURE))
      return DAEMON_CALL_STATE_FAILURE;
   qDebug() << "stateChanged signal received with unknown state.";
   return DAEMON_CALL_STATE_FAILURE;
}

/*QWidget * Call::getItemWidget()
{
   return itemWidget;
   }*/

QString Call::getStopTimeStamp() const
{
   if (stopTime == NULL)
          return QString();
   return QString::number(stopTime->toTime_t());
}

QString Call::getStartTimeStamp() const
{
   if (startTime == NULL)
          return QString();
   return QString::number(startTime->toTime_t());
}

QString Call::getTransferNumber() const
{
   return transferNumber;
}

void Call::setTransferNumber(QString number)
{
   transferNumber = number;
}

QString Call::getCallNumber() const
{
   return callNumber;
}

void Call::setCallNumber(QString number)
{
   callNumber = number;
344
   emit changed();
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
}
   

/*QWidget * Call::getHistoryItemWidget()
{
   historyItemWidget = new QWidget();
   labelHistoryIcon = new QLabel();
   labelHistoryIcon->setPixmap(QPixmap(historyIcons[historyState]));
   labelHistoryCallNumber = new QLabel(peerPhoneNumber);
   if(startTime)
      labelHistoryTime = new QLabel(startTime->toString(Qt::LocaleDate));
   else
      labelHistoryTime = new QLabel();
   QSpacerItem * horizontalSpacer = new QSpacerItem(16777215, 20, QSizePolicy::Preferred, QSizePolicy::Minimum);

   QHBoxLayout * mainLayout = new QHBoxLayout();
   mainLayout->setContentsMargins ( 3, 1, 2, 1);
   mainLayout->setSpacing(4);
   QVBoxLayout * descr = new QVBoxLayout();
   descr->setMargin(1);
   descr->setSpacing(1);
   descr->setMargin(0);
   descr->setSpacing(1);
   mainLayout->addWidget(labelHistoryIcon);
   if(! peerName.isEmpty())
   {
      labelHistoryPeerName = new QLabel(peerName);
      descr->addWidget(labelHistoryPeerName);
   }
   descr->addWidget(labelHistoryCallNumber);
   descr->addWidget(labelHistoryTime);
   mainLayout->addLayout(descr);
   mainLayout->addItem(horizontalSpacer);
   historyItemWidget->setLayout(mainLayout);
   return historyItemWidget;
   }*/

call_state Call::getState() const
{
   return currentState;
}

history_state Call::getHistoryState() const
{
   return historyState;
}

bool Call::isHistory() const
{
   return (getState() == CALL_STATE_OVER);
}

397
call_state Call::stateChanged(const QString& newStateName)
398
{
399
400
401
402
403
404
405
406
407
408
409
410
411
   if (!conference) {
      call_state previousState = currentState;
      daemon_call_state dcs = toDaemonCallState(newStateName);
      //(this->*(stateChangedFunctionMap[currentState][dcs]))();
      changeCurrentState(stateChangedStateMap[currentState][dcs]);
      (this->*(stateChangedFunctionMap[previousState][dcs]))();
      qDebug() << "Calling stateChanged " << newStateName << " -> " << toDaemonCallState(newStateName) << " on call with state " << previousState << ". Become " << currentState;
      return currentState;
   }
   else {
      qDebug() << "A conference have no call state";
      return CALL_STATE_ERROR;
   }
412
413
414
415
416
}

call_state Call::actionPerformed(call_action action)
{
   call_state previousState = currentState;
417
418
      Q_ASSERT_X((previousState>10) || (previousState<0),"perform action","Invalid previous state ("+QString::number(previousState)+")");
   Q_ASSERT_X((state>4) || (state < 0),"perform action","Invalid action ("+QString::number(actuon)+")");
419
420
421
   //update the state
   changeCurrentState(actionPerformedStateMap[previousState][action]);
   //execute the action associated with this transition
422
   (this->*(actionPerformedFunctionMap[previousState][action]))(); //WARNING BUG //WARNING SEGFAULT //TODO remove this node, it was not a good idea, it is not stable
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
   qDebug() << "Calling action " << action << " on call with state " << previousState << ". Become " << currentState;
   //return the new state
   return currentState;
}

QString Call::getCallId() const
{
   return callId;
}

QString Call::getPeerPhoneNumber() const
{
   return peerPhoneNumber;
}

QString Call::getPeerName() const
{
   return peerName;
}

call_state Call::getCurrentState() const
{
   return currentState;
}

bool Call::getRecording() const
{
   return recording;
}

QString Call::getAccountId() const
{
   return account;
}

/*
void Call::putRecording()
{
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   bool daemonRecording = callManager.getIsRecording(this -> callId);
   if(daemonRecording != recording)
   {
      callManager.setRecording(this->callId);
   }
}
*/


/*************************************************
*************   Automate functions   *************
*************************************************/


void Call::nothing()
{
}

void Call::accept()
{
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   qDebug() << "Accepting call. callId : " << callId;
   callManager.accept(callId);
   this->startTime = new QDateTime(QDateTime::currentDateTime());
   this->historyState = INCOMING;
}

void Call::refuse()
{
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   qDebug() << "Refusing call. callId : " << callId;
   callManager.refuse(callId);
   this->startTime = new QDateTime(QDateTime::currentDateTime());
   this->historyState = MISSED;
}

void Call::acceptTransf()
{
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   qDebug() << "Accepting call and transfering it to number : " << transferNumber << ". callId : " << callId;
   callManager.accept(callId);
Emmanuel Lepage's avatar
Emmanuel Lepage committed
503
   callManager.transfer(callId, transferNumber);
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
//   historyState = TRANSFERED;
}

void Call::acceptHold()
{
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   qDebug() << "Accepting call and holding it. callId : " << callId;
   callManager.accept(callId);
   callManager.hold(callId);
   this->historyState = INCOMING;
}

void Call::hangUp()
{
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   this->stopTime = new QDateTime(QDateTime::currentDateTime());
   qDebug() << "Hanging up call. callId : " << callId;
   callManager.hangUp(callId);
}

void Call::cancel()
{
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   qDebug() << "Canceling call. callId : " << callId;
   callManager.hangUp(callId);
}

void Call::hold()
{
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   qDebug() << "Holding call. callId : " << callId;
   callManager.hold(callId);
}

void Call::call()
{
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   qDebug() << "account = " << account;
   if(account.isEmpty()) {
      qDebug() << "account is not set, taking the first registered.";
544
      this->account = CallModelConvenience::getCurrentAccountId();
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
   }
   if(!account.isEmpty()) {
      qDebug() << "Calling " << callNumber << " with account " << account << ". callId : " << callId;
      callManager.placeCall(account, callId, callNumber);
      this->account = account;
      this->peerPhoneNumber = callNumber;
//       Contact * contact = findContactForNumberInKAddressBook(peerPhoneNumber);
//       if(contact) this->peerName = contact->getNickName();
      this->startTime = new QDateTime(QDateTime::currentDateTime());
      this->historyState = OUTGOING;
   }
   else {
      qDebug() << "Trying to call " << transferNumber << " with no account registered . callId : " << callId;
      this->historyState = NONE;
      throw "No account registered!";
   }
}

void Call::transfer()
{
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   qDebug() << "Transfering call to number : " << transferNumber << ". callId : " << callId;
Emmanuel Lepage's avatar
Emmanuel Lepage committed
567
   callManager.transfer(callId, transferNumber);
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
   this->stopTime = new QDateTime(QDateTime::currentDateTime());
}

void Call::unhold()
{
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   qDebug() << "Unholding call. callId : " << callId;
   callManager.unhold(callId);
}

/*
void Call::switchRecord()
{
   qDebug() << "Switching record state for call automate. callId : " << callId;
   recording = !recording;
}
*/

void Call::setRecord()
{
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   qDebug() << "Setting record " << !recording << " for call. callId : " << callId;
   callManager.setRecording(callId);
   recording = !recording;
}

void Call::start()
{
   qDebug() << "Starting call. callId : " << callId;
   this->startTime = new QDateTime(QDateTime::currentDateTime());
}

void Call::startStop()
{
   qDebug() << "Starting and stoping call. callId : " << callId;
   this->startTime = new QDateTime(QDateTime::currentDateTime());
   this->stopTime = new QDateTime(QDateTime::currentDateTime());
}

void Call::stop()
{
   qDebug() << "Stoping call. callId : " << callId;
   this->stopTime = new QDateTime(QDateTime::currentDateTime());
}

void Call::startWeird()
{
   qDebug() << "Starting call. callId : " << callId;
   this->startTime = new QDateTime(QDateTime::currentDateTime());
   qDebug() << "Warning : call " << callId << " had an unexpected transition of state at its start.";
}

void Call::warning()
{
   qDebug() << "Warning : call " << callId << " had an unexpected transition of state.";
}

void Call::appendText(QString str)
{
   QString * editNumber;
   
   switch (currentState) {
   case CALL_STATE_TRANSFER:
   case CALL_STATE_TRANSF_HOLD:
      editNumber = &transferNumber;
      break;
   case CALL_STATE_DIALING:
      editNumber = &callNumber;
      break;
   default:
      qDebug() << "Backspace on call not editable. Doing nothing.";
      return;
   }

   editNumber->append(str);

   emit changed();
}

void Call::backspaceItemText()
{
   QString * editNumber;

   switch (currentState) {
      case CALL_STATE_TRANSFER:
      case CALL_STATE_TRANSF_HOLD:
         editNumber = &transferNumber;
         break;
      case CALL_STATE_DIALING:
         editNumber = &callNumber;
         break;
      default:
         qDebug() << "Backspace on call not editable. Doing nothing.";
         return;
   }
   QString text = *editNumber;
   int textSize = text.size();
   if(textSize > 0) {
      *editNumber = text.remove(textSize-1, 1);

      emit changed();
   }
   else {
      changeCurrentState(CALL_STATE_OVER);
   }
}

void Call::changeCurrentState(call_state newState)
{
677
   //qDebug() << "Call state changed to: " << newState;
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
   currentState = newState;

   emit changed();
   
   if (currentState == CALL_STATE_OVER)
      emit isOver(this);
}

bool Call::isConference() const {
   return conference;
}
   
void Call::setConference(bool value) {
   conference = value;
}

QString Call::getConfId() const {
   return confId;
}

void Call::setConfId(QString value) {
   confId = value;
}

QString Call::getCurrentCodecName() {
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
Emmanuel Lepage's avatar
Emmanuel Lepage committed
704
   return callManager.getCurrentAudioCodecName(callId);
705
706
707
708
}

//This function could also be called mayBeSecure or haveChancesToBeEncryptedButWeCantTell.
bool Call::isSecure() {
709
   //ConfigurationManagerInterface& configurationManager = ConfigurationManagerInterfaceSingleton::getInstance();
710
   //MapStringInt addressBookSettings = configurationManager.getAddressbookSettings().value();
711
712
713
714
715
716
717
   
   if (account.isEmpty()) {
      qDebug() << "Account not set, can't check security";
      return false;
   }
   
   AccountList accountList(true);
718
   Account* currentAccount = accountList.getAccountById(account);
719
   
720
721
722
723
724
   if ((currentAccount->getAccountDetail(TLS_ENABLE ) == "true") || (currentAccount->getAccountDetail(TLS_METHOD).toInt())) {
      return true;
   }
   return false;
}