Commit 86eb025c authored by Ronan Abhamon's avatar Ronan Abhamon

feat(src/components/call/CallModel): handle auto answer

parent 1e83bd74
......@@ -22,6 +22,7 @@
#include <QDateTime>
#include <QtDebug>
#include <QTimer>
#include "../../app/App.hpp"
#include "../../utils.hpp"
......@@ -29,11 +30,28 @@
#include "CallModel.hpp"
#define AUTO_ANSWER_OBJECT_NAME "auto-answer-timer"
// =============================================================================
CallModel::CallModel (shared_ptr<linphone::Call> linphone_call) {
m_linphone_call = linphone_call;
// Deal with auto-answer.
{
SettingsModel *settings = CoreManager::getInstance()->getSettingsModel();
if (settings->getAutoAnswerStatus()) {
QTimer *timer = new QTimer(this);
timer->setInterval(settings->getAutoAnswerDelay());
timer->setSingleShot(true);
timer->setObjectName(AUTO_ANSWER_OBJECT_NAME);
QObject::connect(timer, &QTimer::timeout, this, &CallModel::accept);
timer->start();
}
}
QObject::connect(
&(*CoreManager::getInstance()->getHandlers()), &CoreHandlers::callStateChanged,
this, [this](const std::shared_ptr<linphone::Call> &call, linphone::CallState state) {
......@@ -41,9 +59,13 @@ CallModel::CallModel (shared_ptr<linphone::Call> linphone_call) {
return;
switch (state) {
case linphone::CallStateConnected:
case linphone::CallStateEnd:
case linphone::CallStateError:
stopAutoAnswerTimer();
m_paused_by_remote = false;
break;
case linphone::CallStateConnected:
case linphone::CallStateRefered:
case linphone::CallStateReleased:
case linphone::CallStateStreamsRunning:
......@@ -96,6 +118,8 @@ void CallModel::setRecordFile (shared_ptr<linphone::CallParams> &call_params) {
// -----------------------------------------------------------------------------
void CallModel::accept () {
stopAutoAnswerTimer();
shared_ptr<linphone::Core> core = CoreManager::getInstance()->getCore();
shared_ptr<linphone::CallParams> params = core->createCallParams(m_linphone_call);
params->enableVideo(false);
......@@ -106,6 +130,8 @@ void CallModel::accept () {
}
void CallModel::acceptWithVideo () {
stopAutoAnswerTimer();
shared_ptr<linphone::Core> core = CoreManager::getInstance()->getCore();
shared_ptr<linphone::CallParams> params = core->createCallParams(m_linphone_call);
params->enableVideo(true);
......@@ -180,6 +206,14 @@ void CallModel::stopRecording () {
// -----------------------------------------------------------------------------
void CallModel::stopAutoAnswerTimer () const {
QTimer *timer = findChild<QTimer *>(AUTO_ANSWER_OBJECT_NAME, Qt::FindDirectChildrenOnly);
if (timer) {
timer->stop();
timer->deleteLater();
}
}
QString CallModel::getSipAddress () const {
return ::Utils::linphoneStringToQString(m_linphone_call->getRemoteAddress()->asStringUriOnly());
}
......
......@@ -86,6 +86,8 @@ signals:
void recordingChanged (bool status);
private:
void stopAutoAnswerTimer () const;
QString getSipAddress () const;
CallStatus getStatus () const;
......
......@@ -111,8 +111,10 @@ Notifier::~Notifier () {
QObject *Notifier::createNotification (Notifier::NotificationType type) {
m_mutex.lock();
Q_ASSERT(m_n_instances <= N_MAX_NOTIFICATIONS);
// Check existing instances.
if (m_n_instances >= N_MAX_NOTIFICATIONS) {
if (m_n_instances == N_MAX_NOTIFICATIONS) {
qWarning() << "Unable to create another notification";
m_mutex.unlock();
return nullptr;
......@@ -148,12 +150,12 @@ void Notifier::showNotification (QObject *notification, int timeout) {
// Destroy it after timeout.
QObject::connect(
timer, &QTimer::timeout, this, [this, notification]() {
qDebug() << "toto";
deleteNotification(QVariant::fromValue(notification));
}
);
// Called explicitly (by a click on notification for example)
// or when single shot happen and if notification is visible.
QObject::connect(notification, SIGNAL(deleteNotification(QVariant)), this, SLOT(deleteNotification(QVariant)));
timer->start();
......@@ -162,18 +164,27 @@ void Notifier::showNotification (QObject *notification, int timeout) {
// -----------------------------------------------------------------------------
void Notifier::deleteNotification (QVariant notification) {
m_mutex.lock();
QObject *instance = notification.value<QObject *>();
instance->property(NOTIFICATION_PROPERTY_TIMER).value<QTimer *>()->stop();
// Notification marked destroyed.
if (instance->property("__valid").isValid()) {
m_mutex.unlock();
return;
}
qDebug() << "Delete notification.";
m_mutex.lock();
instance->setProperty("__valid", true);
instance->property(NOTIFICATION_PROPERTY_TIMER).value<QTimer *>()->stop();
m_n_instances--;
if (m_n_instances == 0)
m_offset = 0;
Q_ASSERT(m_n_instances >= 0);
m_mutex.unlock();
instance->deleteLater();
......@@ -217,7 +228,7 @@ void Notifier::notifyReceivedCall (const shared_ptr<linphone::Call> &call) {
QObject::connect(
model, &CallModel::statusChanged, notification, [this, notification](CallModel::CallStatus status) {
if (status == CallModel::CallStatusEnded)
if (status == CallModel::CallStatusEnded || status == CallModel::CallStatusConnected)
deleteNotification(QVariant::fromValue(notification));
}
);
......
......@@ -60,7 +60,7 @@ private:
QQmlComponent *m_components[MaxNbTypes];
int m_offset = 0;
unsigned int m_n_instances = 0;
int m_n_instances = 0;
QMutex m_mutex;
};
......
......@@ -42,8 +42,6 @@ SettingsModel::SettingsModel (QObject *parent) : QObject(parent) {
// Chat & calls.
// =============================================================================
// -----------------------------------------------------------------------------
int SettingsModel::getAutoAnswerDelay () const {
return m_config->getInt(UI_SECTION, "auto_answer_delay", 0);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment