Commit 217e2f45 authored by Ronan Abhamon's avatar Ronan Abhamon

feat(src/components/sip-addresses/SipAddressesModel): deal with calls

parent 4b97a94d
...@@ -36,6 +36,7 @@ using namespace std; ...@@ -36,6 +36,7 @@ using namespace std;
SipAddressesModel::SipAddressesModel (QObject *parent) : QAbstractListModel(parent) { SipAddressesModel::SipAddressesModel (QObject *parent) : QAbstractListModel(parent) {
initSipAddresses(); initSipAddresses();
ContactsListModel *contacts = CoreManager::getInstance()->getContactsListModel(); ContactsListModel *contacts = CoreManager::getInstance()->getContactsListModel();
QObject::connect(contacts, &ContactsListModel::contactAdded, this, &SipAddressesModel::handleContactAdded); QObject::connect(contacts, &ContactsListModel::contactAdded, this, &SipAddressesModel::handleContactAdded);
...@@ -46,7 +47,7 @@ SipAddressesModel::SipAddressesModel (QObject *parent) : QAbstractListModel(pare ...@@ -46,7 +47,7 @@ SipAddressesModel::SipAddressesModel (QObject *parent) : QAbstractListModel(pare
&(*m_core_handlers), &CoreHandlers::messageReceived, &(*m_core_handlers), &CoreHandlers::messageReceived,
this, [this](const shared_ptr<linphone::ChatMessage> &message) { this, [this](const shared_ptr<linphone::ChatMessage> &message) {
const QString &sip_address = ::Utils::linphoneStringToQString(message->getFromAddress()->asStringUriOnly()); const QString &sip_address = ::Utils::linphoneStringToQString(message->getFromAddress()->asStringUriOnly());
addOrUpdateSipAddress(sip_address, nullptr, message); addOrUpdateSipAddress(sip_address, message);
} }
); );
...@@ -74,6 +75,20 @@ SipAddressesModel::SipAddressesModel (QObject *parent) : QAbstractListModel(pare ...@@ -74,6 +75,20 @@ SipAddressesModel::SipAddressesModel (QObject *parent) : QAbstractListModel(pare
removeContactOfSipAddress(sip_address); removeContactOfSipAddress(sip_address);
} }
); );
QObject::connect(
&(*CoreManager::getInstance()->getHandlers()), &CoreHandlers::callStateChanged,
this, [this](const std::shared_ptr<linphone::Call> &call, linphone::CallState state) {
// Ignore aborted calls.
if (call->getCallLog()->getStatus() == linphone::CallStatus::CallStatusAborted)
return;
if (state == linphone::CallStateEnd || state == linphone::CallStateError)
addOrUpdateSipAddress(
::Utils::linphoneStringToQString(call->getRemoteAddress()->asStringUriOnly()), call
);
}
);
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
...@@ -126,16 +141,14 @@ void SipAddressesModel::connectToChatModel (ChatModel *chat_model) { ...@@ -126,16 +141,14 @@ void SipAddressesModel::connectToChatModel (ChatModel *chat_model) {
} }
); );
for (auto &signal : { &ChatModel::messageSent, &ChatModel::messageReceived }) { QObject::connect(
QObject::connect( chat_model, &ChatModel::messageSent,
chat_model, signal, this, [this](const shared_ptr<linphone::ChatMessage> &message) {
this, [this](const shared_ptr<linphone::ChatMessage> &message) { addOrUpdateSipAddress(
addOrUpdateSipAddress( ::Utils::linphoneStringToQString(message->getToAddress()->asStringUriOnly()), message
::Utils::linphoneStringToQString(message->getToAddress()->asStringUriOnly()), nullptr, message );
); }
} );
);
}
QObject::connect( QObject::connect(
chat_model, &ChatModel::messagesCountReset, this, [this, chat_model]() { chat_model, &ChatModel::messagesCountReset, this, [this, chat_model]() {
...@@ -230,30 +243,31 @@ void SipAddressesModel::handleContactRemoved (const ContactModel *contact) { ...@@ -230,30 +243,31 @@ void SipAddressesModel::handleContactRemoved (const ContactModel *contact) {
removeContactOfSipAddress(sip_address.toString()); removeContactOfSipAddress(sip_address.toString());
} }
void SipAddressesModel::addOrUpdateSipAddress ( // -----------------------------------------------------------------------------
QVariantMap &map,
ContactModel *contact,
const shared_ptr<linphone::ChatMessage> &message
) {
if (contact) {
map["contact"] = QVariant::fromValue(contact);
updateObservers(map["sipAddress"].toString(), contact);
}
if (message) { void SipAddressesModel::addOrUpdateSipAddress (QVariantMap &map, ContactModel *contact) {
map["timestamp"] = QDateTime::fromMSecsSinceEpoch(message->getTime() * 1000); map["contact"] = QVariant::fromValue(contact);
map["unreadMessagesCount"] = message->getChatRoom()->getUnreadMessagesCount(); updateObservers(map["sipAddress"].toString(), contact);
}
} }
void SipAddressesModel::addOrUpdateSipAddress ( void SipAddressesModel::addOrUpdateSipAddress (QVariantMap &map, const shared_ptr<linphone::Call> &call) {
const QString &sip_address, const shared_ptr<linphone::CallLog> call_log = call->getCallLog();
ContactModel *contact,
const shared_ptr<linphone::ChatMessage> &message map["timestamp"] = call_log->getStatus() == linphone::CallStatus::CallStatusSuccess
) { ? QDateTime::fromMSecsSinceEpoch((call_log->getStartDate() + call_log->getDuration()) * 1000)
: QDateTime::fromMSecsSinceEpoch(call_log->getStartDate() * 1000);
}
void SipAddressesModel::addOrUpdateSipAddress (QVariantMap &map, const shared_ptr<linphone::ChatMessage> &message) {
map["timestamp"] = QDateTime::fromMSecsSinceEpoch(message->getTime() * 1000);
map["unreadMessagesCount"] = message->getChatRoom()->getUnreadMessagesCount();
}
template<typename T>
void SipAddressesModel::addOrUpdateSipAddress (const QString &sip_address, T data) {
auto it = m_sip_addresses.find(sip_address); auto it = m_sip_addresses.find(sip_address);
if (it != m_sip_addresses.end()) { if (it != m_sip_addresses.end()) {
addOrUpdateSipAddress(*it, contact, message); addOrUpdateSipAddress(*it, data);
int row = m_refs.indexOf(&(*it)); int row = m_refs.indexOf(&(*it));
Q_ASSERT(row != -1); Q_ASSERT(row != -1);
...@@ -264,7 +278,7 @@ void SipAddressesModel::addOrUpdateSipAddress ( ...@@ -264,7 +278,7 @@ void SipAddressesModel::addOrUpdateSipAddress (
QVariantMap map; QVariantMap map;
map["sipAddress"] = sip_address; map["sipAddress"] = sip_address;
addOrUpdateSipAddress(map, contact, message); addOrUpdateSipAddress(map, data);
int row = m_refs.count(); int row = m_refs.count();
...@@ -278,6 +292,8 @@ void SipAddressesModel::addOrUpdateSipAddress ( ...@@ -278,6 +292,8 @@ void SipAddressesModel::addOrUpdateSipAddress (
endInsertRows(); endInsertRows();
} }
// -----------------------------------------------------------------------------
void SipAddressesModel::removeContactOfSipAddress (const QString &sip_address) { void SipAddressesModel::removeContactOfSipAddress (const QString &sip_address) {
auto it = m_sip_addresses.find(sip_address); auto it = m_sip_addresses.find(sip_address);
if (it == m_sip_addresses.end()) { if (it == m_sip_addresses.end()) {
......
...@@ -59,17 +59,18 @@ private: ...@@ -59,17 +59,18 @@ private:
void handleContactAdded (ContactModel *contact); void handleContactAdded (ContactModel *contact);
void handleContactRemoved (const ContactModel *contact); void handleContactRemoved (const ContactModel *contact);
void addOrUpdateSipAddress ( // ---------------------------------------------------------------------------
QVariantMap &map,
ContactModel *contact, // A sip address exists in this list if a contact is linked to it, or a call, or a message.
const std::shared_ptr<linphone::ChatMessage> &message
); void addOrUpdateSipAddress (QVariantMap &map, ContactModel *contact);
void addOrUpdateSipAddress (QVariantMap &map, const std::shared_ptr<linphone::Call> &call);
void addOrUpdateSipAddress ( void addOrUpdateSipAddress (QVariantMap &map, const std::shared_ptr<linphone::ChatMessage> &message);
const QString &sip_address,
ContactModel *contact = nullptr, template<class T>
const std::shared_ptr<linphone::ChatMessage> &message = std::shared_ptr<linphone::ChatMessage>() void addOrUpdateSipAddress (const QString &sip_address, T data);
);
// ---------------------------------------------------------------------------
void removeContactOfSipAddress (const QString &sip_address); void removeContactOfSipAddress (const QString &sip_address);
......
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