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

23 24
#include "callmanager_interface_singleton.h"
#include "SFLPhone.h"
25
#include "sflphone_const.h"
26
#include "configurationmanager_interface_singleton.h"
Jérémy Quentin's avatar
Jérémy Quentin committed
27

28 29
#include <kabc/addressbook.h>
#include <kabc/stdaddressbook.h>
30

31 32
using namespace KABC;

33
const call_state Call::actionPerformedStateMap [11][5] = 
34
{
35 36 37 38 39 40 41 42 43 44 45 46
//                      ACCEPT                  REFUSE                  TRANSFER                   HOLD                           RECORD
/*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        }
47 48
};

49

50
const function Call::actionPerformedFunctionMap[11][5] = 
51 52
{ 
//                      ACCEPT               REFUSE            TRANSFER                 HOLD                  RECORD
53 54
/*INCOMING       */  {&Call::accept     , &Call::refuse   , &Call::acceptTransf   , &Call::acceptHold  ,  &Call::setRecord     },
/*RINGING        */  {&Call::nothing    , &Call::hangUp   , &Call::nothing        , &Call::nothing     ,  &Call::setRecord     },
55
/*CURRENT        */  {&Call::nothing    , &Call::hangUp   , &Call::nothing        , &Call::hold        ,  &Call::setRecord     },
56
/*DIALING        */  {&Call::call       , &Call::cancel  , &Call::nothing        , &Call::nothing     ,  &Call::nothing       },
57 58 59 60 61 62 63 64 65
/*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       }
};

66 67 68 69

const call_state Call::stateChangedStateMap [11][6] = 
{
//                      RINGING                  CURRENT             BUSY              HOLD                           HUNGUP           FAILURE
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    }
81 82
};

83 84 85
const function Call::stateChangedFunctionMap[11][6] = 
{ 
//                      RINGING                  CURRENT             BUSY              HOLD                    HUNGUP           FAILURE
86 87 88 89 90 91 92 93 94 95 96
/*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  },
/*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 }
97
};
98

Jérémy Quentin's avatar
Jérémy Quentin committed
99 100
const char * Call::historyIcons[3] = {ICON_HISTORY_INCOMING, ICON_HISTORY_OUTGOING, ICON_HISTORY_MISSED};

101
/*void Call::initCallItemWidget()
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
   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);
133
}*/
Jérémy Quentin's avatar
Jérémy Quentin committed
134

135
 /*void Call::setItemIcon(const QString pixmap)
Jérémy Quentin's avatar
Jérémy Quentin committed
136
{
137 138
   labelIcon->setPixmap(QPixmap(pixmap));
   }*/
139

140

141
Call::Call(call_state startState, QString callId, QString peerName, QString peerNumber, QString account)
142
   : conference(false)
143
{
144 145 146 147 148 149 150 151 152 153
   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();
154
        emit changed();
155
}
156 157 158 159 160 161 162

Call::Call(QString confId, QString account) 
   : conference(true)
{
   this->confId = confId;
   this->account = account;
}
163
#include <unistd.h>
Jérémy Quentin's avatar
Jérémy Quentin committed
164
Call * Call::buildExistingCall(QString callId)
165
{
166 167 168 169 170 171 172 173 174 175 176 177
   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;
178 179
}

180 181
Call::~Call()
{
182 183
   delete startTime;
   delete stopTime;
184
}
185
   
186
Call * Call::buildDialingCall(QString callId, const QString & peerName, QString account)
187
{
188 189 190
   Call * call = new Call(CALL_STATE_DIALING, callId, peerName, "", account);
   call->historyState = NONE;
   return call;
191 192
}

193
Call * Call::buildIncomingCall(const QString & callId/*, const QString & from, const QString & account*/)
194
{
195 196 197 198 199 200 201 202 203
   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;
204 205
}

206 207
Call * Call::buildRingingCall(const QString & callId)
{
208 209 210 211 212 213 214 215
   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;
216 217
}

218 219
Call * Call::buildHistoryCall(const QString & callId, uint startTimeStamp, uint stopTimeStamp, QString account, QString name, QString number, QString type)
{
220 221 222 223 224 225
   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;
226 227 228 229 230
}


history_state Call::getHistoryStateFromType(QString type)
{
231 232 233 234 235 236 237
   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;
238 239
}

240 241
QString Call::getTypeFromHistoryState(history_state historyState)
{
242 243 244 245 246 247 248
   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();
249 250
}

251 252
call_state Call::getStartStateFromDaemonCallState(QString daemonCallState, QString daemonCallType)
{
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
   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;
271 272 273 274
}

history_state Call::getHistoryStateFromDaemonCallState(QString daemonCallState, QString daemonCallType)
{
275 276 277 278 279 280 281 282 283 284 285 286
   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;
287 288
}

289
daemon_call_state Call::toDaemonCallState(const QString & stateName)
290
{
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
   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;
309 310
}

311 312 313

Contact * Call::findContactForNumberInKAddressBook(QString number)
{
314 315 316 317 318 319 320 321 322 323 324 325 326 327
   ConfigurationManagerInterface & configurationManager = ConfigurationManagerInterfaceSingleton::getInstance();
   MapStringInt addressBookSettings = configurationManager.getAddressbookSettings().value();
   bool displayPhoto = addressBookSettings[ADDRESSBOOK_DISPLAY_CONTACT_PHOTO];
   AddressBook * ab = KABC::StdAddressBook::self(true);
   QVector<Contact *> results = QVector<Contact *>();
   AddressBook::Iterator it;
   for ( it = ab->begin(); it != ab->end(); ++it ) {   
      for(int i = 0 ; i < it->phoneNumbers().count() ; i++) {
         if(it->phoneNumbers().at(i) == number) {
            return new Contact( *it, it->phoneNumbers().at(i), displayPhoto );
         }
      }
   }
   return NULL;
328 329
}

330
/*QWidget * Call::getItemWidget()
Jérémy Quentin's avatar
Jérémy Quentin committed
331
{
332 333
   return itemWidget;
   }*/
Jérémy Quentin's avatar
Jérémy Quentin committed
334

335 336
QString Call::getStopTimeStamp() const
{
337 338 339
   if (stopTime == NULL)
          return QString();
   return QString::number(stopTime->toTime_t());
340 341 342 343
}

QString Call::getStartTimeStamp() const
{
344 345 346
   if (startTime == NULL)
          return QString();
   return QString::number(startTime->toTime_t());
347 348
}

349 350
QString Call::getTransferNumber() const
{
351
   return transferNumber;
352 353 354 355
}

void Call::setTransferNumber(QString number)
{
356
   transferNumber = number;
357 358 359 360
}

QString Call::getCallNumber() const
{
361
   return callNumber;
362 363 364 365
}

void Call::setCallNumber(QString number)
{
366
   callNumber = number;
367
}
368
   
369 370

/*QWidget * Call::getHistoryItemWidget()
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 397 398 399 400 401 402
   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;
   }*/
403

404 405
call_state Call::getState() const
{
406
   return currentState;
407 408
}

409 410
history_state Call::getHistoryState() const
{
411
   return historyState;
412 413
}

414 415
bool Call::isHistory() const
{
416
   return (getState() == CALL_STATE_OVER);
417 418
}

Jérémy Quentin's avatar
Jérémy Quentin committed
419
call_state Call::stateChanged(const QString & newStateName)
420
{
421 422 423 424 425 426 427
   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;
428 429
}

Jérémy Quentin's avatar
Jérémy Quentin committed
430
call_state Call::actionPerformed(call_action action)
431
{
432 433 434 435 436 437 438 439
   call_state previousState = currentState;
   //update the state
   changeCurrentState(actionPerformedStateMap[previousState][action]);
   //execute the action associated with this transition
   (this->*(actionPerformedFunctionMap[previousState][action]))();
   qDebug() << "Calling action " << action << " on call with state " << previousState << ". Become " << currentState;
   //return the new state
   return currentState;
440 441
}

442
QString Call::getCallId() const
443
{
444
   return callId;
445 446
}

447 448
QString Call::getPeerPhoneNumber() const
{
449
   return peerPhoneNumber;
450 451 452 453
}

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

457 458
call_state Call::getCurrentState() const
{
459
   return currentState;
460 461
}

462 463
bool Call::getRecording() const
{
464
   return recording;
465
}
466

467 468
QString Call::getAccountId() const
{
469
   return account;
470 471
}

472 473 474
/*
void Call::putRecording()
{
475 476 477 478 479 480
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   bool daemonRecording = callManager.getIsRecording(this -> callId);
   if(daemonRecording != recording)
   {
      callManager.setRecording(this->callId);
   }
481 482
}
*/
483 484 485 486 487 488 489


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


Jérémy Quentin's avatar
Jérémy Quentin committed
490
void Call::nothing()
491
{
492
}
493

Jérémy Quentin's avatar
Jérémy Quentin committed
494
void Call::accept()
495
{
496 497 498 499 500
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   qDebug() << "Accepting call. callId : " << callId;
   callManager.accept(callId);
   this->startTime = new QDateTime(QDateTime::currentDateTime());
   this->historyState = INCOMING;
501 502
}

Jérémy Quentin's avatar
Jérémy Quentin committed
503
void Call::refuse()
504
{
505 506 507 508 509
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   qDebug() << "Refusing call. callId : " << callId;
   callManager.refuse(callId);
   this->startTime = new QDateTime(QDateTime::currentDateTime());
   this->historyState = MISSED;
510 511
}

Jérémy Quentin's avatar
Jérémy Quentin committed
512
void Call::acceptTransf()
513
{
514 515 516 517 518
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   qDebug() << "Accepting call and transfering it to number : " << transferNumber << ". callId : " << callId;
   callManager.accept(callId);
   callManager.transfert(callId, transferNumber);
//   historyState = TRANSFERED;
519 520
}

Jérémy Quentin's avatar
Jérémy Quentin committed
521
void Call::acceptHold()
522
{
523 524 525 526 527
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   qDebug() << "Accepting call and holding it. callId : " << callId;
   callManager.accept(callId);
   callManager.hold(callId);
   this->historyState = INCOMING;
528 529
}

Jérémy Quentin's avatar
Jérémy Quentin committed
530
void Call::hangUp()
531
{
532 533 534 535
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   this->stopTime = new QDateTime(QDateTime::currentDateTime());
   qDebug() << "Hanging up call. callId : " << callId;
   callManager.hangUp(callId);
536 537
}

538 539
void Call::cancel()
{
540 541 542
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   qDebug() << "Canceling call. callId : " << callId;
   callManager.hangUp(callId);
543 544
}

Jérémy Quentin's avatar
Jérémy Quentin committed
545
void Call::hold()
546
{
547 548 549
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   qDebug() << "Holding call. callId : " << callId;
   callManager.hold(callId);
550 551
}

Jérémy Quentin's avatar
Jérémy Quentin committed
552
void Call::call()
553
{
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   qDebug() << "account = " << account;
   if(account.isEmpty()) {
      qDebug() << "account is not set, taking the first registered.";
      this->account = SFLPhoneView::accountInUseId();
   }
   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!";
   }
575 576
}

Jérémy Quentin's avatar
Jérémy Quentin committed
577
void Call::transfer()
578
{
579 580 581 582
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   qDebug() << "Transfering call to number : " << transferNumber << ". callId : " << callId;
   callManager.transfert(callId, transferNumber);
   this->stopTime = new QDateTime(QDateTime::currentDateTime());
583 584
}

Jérémy Quentin's avatar
Jérémy Quentin committed
585
void Call::unhold()
586
{
587 588 589
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   qDebug() << "Unholding call. callId : " << callId;
   callManager.unhold(callId);
590 591
}

592
/*
Jérémy Quentin's avatar
Jérémy Quentin committed
593
void Call::switchRecord()
594
{
595 596
   qDebug() << "Switching record state for call automate. callId : " << callId;
   recording = !recording;
597
}
598
*/
599

Jérémy Quentin's avatar
Jérémy Quentin committed
600
void Call::setRecord()
601
{
602 603 604 605
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   qDebug() << "Setting record " << !recording << " for call. callId : " << callId;
   callManager.setRecording(callId);
   recording = !recording;
606 607
}

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

614 615
void Call::startStop()
{
616 617 618
   qDebug() << "Starting and stoping call. callId : " << callId;
   this->startTime = new QDateTime(QDateTime::currentDateTime());
   this->stopTime = new QDateTime(QDateTime::currentDateTime());
619 620 621 622
}

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

627 628
void Call::startWeird()
{
629 630 631
   qDebug() << "Starting call. callId : " << callId;
   this->startTime = new QDateTime(QDateTime::currentDateTime());
   qDebug() << "Warning : call " << callId << " had an unexpected transition of state at its start.";
632 633 634 635
}

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

639
void Call::appendText(QString str)
Jérémy Quentin's avatar
Jérémy Quentin committed
640
{
641 642 643 644 645 646 647 648 649 650 651 652 653 654
   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;
   }
655

656
   editNumber->append(str);
657

658
   emit changed();
659 660 661 662
}

void Call::backspaceItemText()
{
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
   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);
   }
Jérémy Quentin's avatar
Jérémy Quentin committed
687 688 689 690
}

void Call::changeCurrentState(call_state newState)
{
691
   qDebug() << "Call state changed to: " << newState;
692
   currentState = newState;
693

694
   emit changed();
695 696 697
   
   if (currentState == CALL_STATE_OVER)
      emit isOver(this);
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
}

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;
714 715 716 717 718
}

QString Call::getCurrentCodecName() {
   CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
   return callManager.getCurrentCodecName(callId);
719 720 721 722 723 724 725 726 727 728 729 730 731 732
}

//This function could also be called mayBeSecure or haveChancesToBeEncryptedButWeCantTell.
bool Call::isSecure() {
   ConfigurationManagerInterface & configurationManager = ConfigurationManagerInterfaceSingleton::getInstance();
   //MapStringInt addressBookSettings = configurationManager.getAddressbookSettings().value();
   AccountList accountList;
   Account* currentAccount = accountList.getAccountById(account);

   if ((currentAccount->getAccountDetail(TLS_ENABLE ) == "true") || (currentAccount->getAccountDetail(TLS_METHOD).toInt())) {
      return true;
   }
   return false;
}