CallsListModel.cpp 9.19 KB
Newer Older
1 2
/*
 * CallsListModel.cpp
3
 * Copyright (C) 2017-2018  Belledonne Communications, Grenoble, France
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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 2
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *  Created on: February 2, 2017
 *      Author: Ronan Abhamon
 */

23
#include <QQmlApplicationEngine>
24 25
#include <QQuickWindow>
#include <QTimer>
Ronan's avatar
Ronan committed
26

27 28 29 30 31 32
#include "app/App.hpp"
#include "components/call/CallModel.hpp"
#include "components/conference/ConferenceAddModel.hpp"
#include "components/conference/ConferenceHelperModel.hpp"
#include "components/core/CoreHandlers.hpp"
#include "components/core/CoreManager.hpp"
33
#include "components/settings/SettingsModel.hpp"
34
#include "utils/Utils.hpp"
Ronan's avatar
Ronan committed
35 36 37 38 39

#include "CallsListModel.hpp"

// =============================================================================

40 41
using namespace std;

42
namespace {
43 44
  // Delay before removing call in ms.
  constexpr int DelayBeforeRemoveCall = 3000;
45 46
}

Ronan's avatar
Ronan committed
47
static inline int findCallIndex (QList<CallModel *> &list, const shared_ptr<linphone::Call> &call) {
48
  auto it = find_if(list.begin(), list.end(), [call](CallModel *callModel) {
49 50
    return call == callModel->getCall();
  });
51 52 53

  Q_ASSERT(it != list.end());

54
  return int(distance(list.begin(), it));
55 56
}

Ronan's avatar
Ronan committed
57
static inline int findCallIndex (QList<CallModel *> &list, const CallModel &callModel) {
58
  return ::findCallIndex(list, callModel.getCall());
59 60 61 62
}

// -----------------------------------------------------------------------------

Ronan's avatar
Ronan committed
63
CallsListModel::CallsListModel (QObject *parent) : QAbstractListModel(parent) {
64
  mCoreHandlers = CoreManager::getInstance()->getHandlers();
Ronan's avatar
Ronan committed
65
  QObject::connect(
66
    mCoreHandlers.get(), &CoreHandlers::callStateChanged,
Ronan's avatar
Ronan committed
67
    this, &CallsListModel::handleCallStateChanged
Ronan's avatar
Ronan committed
68 69 70 71
  );
}

int CallsListModel::rowCount (const QModelIndex &) const {
72
  return mList.count();
Ronan's avatar
Ronan committed
73 74 75 76 77 78 79 80 81 82 83
}

QHash<int, QByteArray> CallsListModel::roleNames () const {
  QHash<int, QByteArray> roles;
  roles[Qt::DisplayRole] = "$call";
  return roles;
}

QVariant CallsListModel::data (const QModelIndex &index, int role) const {
  int row = index.row();

84
  if (!index.isValid() || row < 0 || row >= mList.count())
Ronan's avatar
Ronan committed
85 86 87
    return QVariant();

  if (role == Qt::DisplayRole)
88
    return QVariant::fromValue(mList[row]);
Ronan's avatar
Ronan committed
89 90 91 92 93 94

  return QVariant();
}

// -----------------------------------------------------------------------------

95 96 97 98 99 100
void CallsListModel::askForTransfer (CallModel *callModel) {
  emit callTransferAsked(callModel);
}

// -----------------------------------------------------------------------------

101
void CallsListModel::launchAudioCall (const QString &sipAddress, const QHash<QString, QString> &headers) const {
102
  shared_ptr<linphone::Core> core = CoreManager::getInstance()->getCore();
103

104
  shared_ptr<linphone::Address> address = core->interpretUrl(Utils::appStringToCoreString(sipAddress));
105
  if (!address)
106 107 108 109 110
    return;

  shared_ptr<linphone::CallParams> params = core->createCallParams(nullptr);
  params->enableVideo(false);

111 112 113
  QHashIterator<QString, QString> iterator(headers);
  while (iterator.hasNext()) {
    iterator.next();
114
    params->addCustomHeader(Utils::appStringToCoreString(iterator.key()), Utils::appStringToCoreString(iterator.value()));
115 116
  }

117
  CallModel::setRecordFile(params, Utils::coreStringToAppString(address->getUsername()));
118
  core->inviteAddressWithParams(address, params);
119 120
}

121
void CallsListModel::launchVideoCall (const QString &sipAddress) const {
122
  shared_ptr<linphone::Core> core = CoreManager::getInstance()->getCore();
123 124
  if (!core->videoSupported()) {
    qWarning() << QStringLiteral("Unable to launch video call. (Video not supported.) Launching audio call...");
125
    launchAudioCall(sipAddress);
126 127
    return;
  }
128

129
  shared_ptr<linphone::Address> address = core->interpretUrl(Utils::appStringToCoreString(sipAddress));
130
  if (!address)
131 132 133 134 135
    return;

  shared_ptr<linphone::CallParams> params = core->createCallParams(nullptr);
  params->enableVideo(true);

136
  CallModel::setRecordFile(params, Utils::coreStringToAppString(address->getUsername()));
137
  core->inviteAddressWithParams(address, params);
138 139 140 141
}

// -----------------------------------------------------------------------------

142 143 144 145 146 147 148 149 150 151
int CallsListModel::getRunningCallsNumber () const {
  return CoreManager::getInstance()->getCore()->getCallsNb();
}

void CallsListModel::terminateAllCalls () const {
  CoreManager::getInstance()->getCore()->terminateAllCalls();
}

// -----------------------------------------------------------------------------

Ronan's avatar
Ronan committed
152
static void joinConference (const shared_ptr<linphone::Call> &call) {
Wescoeur's avatar
Wescoeur committed
153 154 155 156 157 158 159 160 161 162
  if (call->getToHeader("method") != "join-conference")
    return;

  shared_ptr<linphone::Core> core = CoreManager::getInstance()->getCore();
  if (!core->getConference()) {
    qWarning() << QStringLiteral("Not in a conference. => Responding to `join-conference` as a simple call...");
    return;
  }

  shared_ptr<linphone::Conference> conference = core->getConference();
163
  const QString conferenceId = Utils::coreStringToAppString(call->getToHeader("conference-id"));
Wescoeur's avatar
Wescoeur committed
164

165
  if (conference->getId() != Utils::appStringToCoreString(conferenceId)) {
Wescoeur's avatar
Wescoeur committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    qWarning() << QStringLiteral("Trying to join conference with an invalid conference id: `%1`. Responding as a simple call...")
      .arg(conferenceId);
    return;
  }
  qInfo() << QStringLiteral("Join conference: `%1`.").arg(conferenceId);

  ConferenceHelperModel helperModel;
  ConferenceHelperModel::ConferenceAddModel *addModel = helperModel.getConferenceAddModel();

  CallModel *callModel = &call->getData<CallModel>("call-model");
  callModel->accept();
  addModel->addToConference(call->getRemoteAddress());
  addModel->update();
}

181
void CallsListModel::handleCallStateChanged (const shared_ptr<linphone::Call> &call, linphone::Call::State state) {
Ronan's avatar
Ronan committed
182
  switch (state) {
183
    case linphone::Call::State::IncomingReceived:
184
      addCall(call);
185
      joinConference(call);
186 187
      break;

188
    case linphone::Call::State::OutgoingInit:
Ronan's avatar
Ronan committed
189 190 191
      addCall(call);
      break;

192 193
    case linphone::Call::State::End:
    case linphone::Call::State::Error:
194 195
      if (call->getCallLog()->getStatus() == linphone::Call::Status::Missed)
        emit callMissed(&call->getData<CallModel>("call-model"));
Ronan's avatar
Ronan committed
196 197 198
      removeCall(call);
      break;

199
    case linphone::Call::State::StreamsRunning: {
200
      int index = findCallIndex(mList, call);
Ronan's avatar
Ronan committed
201
      emit callRunning(index, &call->getData<CallModel>("call-model"));
202
    } break;
Ronan's avatar
Ronan committed
203 204 205 206 207 208

    default:
      break;
  }
}

Ronan's avatar
Ronan committed
209 210 211 212 213 214 215
bool CallsListModel::removeRow (int row, const QModelIndex &parent) {
  return removeRows(row, 1, parent);
}

bool CallsListModel::removeRows (int row, int count, const QModelIndex &parent) {
  int limit = row + count - 1;

216
  if (row < 0 || count < 0 || limit >= mList.count())
Ronan's avatar
Ronan committed
217 218 219 220 221
    return false;

  beginRemoveRows(parent, row, limit);

  for (int i = 0; i < count; ++i)
222
    mList.takeAt(row)->deleteLater();
Ronan's avatar
Ronan committed
223 224 225 226 227 228 229 230

  endRemoveRows();

  return true;
}

// -----------------------------------------------------------------------------

231
void CallsListModel::addCall (const shared_ptr<linphone::Call> &call) {
232
  if (call->getDir() == linphone::Call::Dir::Outgoing) {
233
    QQuickWindow *callsWindow = App::getInstance()->getCallsWindow();
234 235 236 237 238 239 240
    if (callsWindow) {
      if (CoreManager::getInstance()->getSettingsModel()->getKeepCallsWindowInBackground()) {
        if (!callsWindow->isVisible())
          callsWindow->showMinimized();
      } else
        App::smartShowWindow(callsWindow);
    }
241
  }
Ronan's avatar
Ronan committed
242

243 244 245
  CallModel *callModel = new CallModel(call);
  qInfo() << QStringLiteral("Add call:") << callModel;
  App::getInstance()->getEngine()->setObjectOwnership(callModel, QQmlEngine::CppOwnership);
Ronan's avatar
Ronan committed
246

247
  // This connection is (only) useful for `CallsListProxyModel`.
248
  QObject::connect(callModel, &CallModel::isInConferenceChanged, this, [this, callModel](bool) {
249 250 251
    int id = findCallIndex(mList, *callModel);
    emit dataChanged(index(id, 0), index(id, 0));
  });
252

253
  int row = mList.count();
Ronan's avatar
Ronan committed
254 255

  beginInsertRows(QModelIndex(), row, row);
256
  mList << callModel;
Ronan's avatar
Ronan committed
257 258 259
  endInsertRows();
}

260 261 262 263 264 265
void CallsListModel::removeCall (const shared_ptr<linphone::Call> &call) {
  CallModel *callModel;

  try {
    callModel = &call->getData<CallModel>("call-model");
  } catch (const out_of_range &) {
266
    // The call model not exists because the linphone call state
267
    // `CallStateIncomingReceived`/`CallStateOutgoingInit` was not notified.
268
    qWarning() << QStringLiteral("Unable to find call:") << call.get();
269 270
    return;
  }
271

272
  QTimer::singleShot(DelayBeforeRemoveCall, this, [this, callModel] {
273 274 275 276 277 278 279 280 281 282
    removeCallCb(callModel);
  });
}

void CallsListModel::removeCallCb (CallModel *callModel) {
  qInfo() << QStringLiteral("Removing call:") << callModel;

  int index = mList.indexOf(callModel);
  if (index == -1 || !removeRow(index))
    qWarning() << QStringLiteral("Unable to remove call:") << callModel;
Ronan's avatar
Ronan committed
283
}