Commit a3f0062d authored by Ronan Abhamon's avatar Ronan Abhamon

feat(src/components/sip-addresses/SipAddressesModel): connect to contacts changes

parent b8780f79
...@@ -65,6 +65,8 @@ bool ContactsListModel::removeRows (int row, int count, const QModelIndex &paren ...@@ -65,6 +65,8 @@ bool ContactsListModel::removeRows (int row, int count, const QModelIndex &paren
ContactModel *contact = m_list.takeAt(row); ContactModel *contact = m_list.takeAt(row);
m_linphone_friends->removeFriend(contact->m_linphone_friend); m_linphone_friends->removeFriend(contact->m_linphone_friend);
emit contactRemoved(contact);
contact->deleteLater(); contact->deleteLater();
} }
...@@ -96,6 +98,8 @@ ContactModel *ContactsListModel::addContact (VcardModel *vcard) { ...@@ -96,6 +98,8 @@ ContactModel *ContactsListModel::addContact (VcardModel *vcard) {
m_list << contact; m_list << contact;
endInsertRows(); endInsertRows();
emit contactAdded(contact);
return contact; return contact;
} }
......
...@@ -29,6 +29,10 @@ public slots: ...@@ -29,6 +29,10 @@ public slots:
ContactModel *addContact (VcardModel *vcard); ContactModel *addContact (VcardModel *vcard);
void removeContact (ContactModel *contact); void removeContact (ContactModel *contact);
signals:
void contactAdded (ContactModel *contact);
void contactRemoved (const ContactModel *contact);
private: private:
QList<ContactModel *> m_list; QList<ContactModel *> m_list;
std::shared_ptr<linphone::FriendList> m_linphone_friends; std::shared_ptr<linphone::FriendList> m_linphone_friends;
......
#include <QDateTime> #include <QDateTime>
#include <QSet> #include <QSet>
#include <QtDebug>
#include "../../utils.hpp" #include "../../utils.hpp"
#include "../contacts/ContactsListModel.hpp"
#include "../core/CoreManager.hpp" #include "../core/CoreManager.hpp"
#include "SipAddressesModel.hpp" #include "SipAddressesModel.hpp"
...@@ -10,61 +10,34 @@ ...@@ -10,61 +10,34 @@
// ============================================================================= // =============================================================================
SipAddressesModel::SipAddressesModel (QObject *parent) : QAbstractListModel(parent) { SipAddressesModel::SipAddressesModel (QObject *parent) : QAbstractListModel(parent) {
shared_ptr<linphone::Core> core = CoreManager::getInstance()->getCore(); fetchSipAddresses();
// Get sip addresses from chatrooms.
for (const auto &chat_room : core->getChatRooms()) {
list<shared_ptr<linphone::ChatMessage> > history = chat_room->getHistory(0);
if (history.size() == 0)
continue;
QString sip_address = ::Utils::linphoneStringToQString(chat_room->getPeerAddress()->asString());
QVariantMap map;
map["sipAddress"] = sip_address;
map["timestamp"] = QDateTime::fromMSecsSinceEpoch(static_cast<qint64>(history.back()->getTime()) * 1000);
m_sip_addresses[sip_address] = map; QObject::connect(
} CoreManager::getInstance()->getContactsListModel(), &ContactsListModel::contactAdded,
this, &SipAddressesModel::updateFromContact
// Get sip addresses from calls.
QSet<QString> address_done;
for (const auto &call_log : core->getCallLogs()) {
QString sip_address = ::Utils::linphoneStringToQString(call_log->getRemoteAddress()->asString());
if (address_done.contains(sip_address))
continue; // Already used.
address_done << sip_address;
QVariantMap map;
map["sipAddress"] = sip_address;
map["timestamp"] = QDateTime::fromMSecsSinceEpoch(
static_cast<qint64>(call_log->getStartDate() + call_log->getDuration()) * 1000
); );
auto it = m_sip_addresses.find(sip_address); QObject::connect(
if (it == m_sip_addresses.end() || map["timestamp"] > (*it)["timestamp"]) CoreManager::getInstance()->getContactsListModel(), &ContactsListModel::contactRemoved,
m_sip_addresses[sip_address] = map; this, [this](const ContactModel *contact) {
}
// Get sip addresses from contacts.
for (auto &contact : CoreManager::getInstance()->getContactsListModel()->m_list) {
for (const auto &sip_address : contact->getVcardModel()->getSipAddresses()) { for (const auto &sip_address : contact->getVcardModel()->getSipAddresses()) {
auto it = m_sip_addresses.find(sip_address.toString()); auto it = m_sip_addresses.find(sip_address.toString());
if (it == m_sip_addresses.end()) { if (it == m_sip_addresses.end()) {
QVariantMap map; qWarning() << QStringLiteral("Unable to remove contact from sip address: `%1`.").arg(
map["sipAddress"] = sip_address; sip_address.toString()
map["contact"] = QVariant::fromValue(contact); );
m_sip_addresses[sip_address.toString()] = map; continue;
} else }
(*it)["contact"] = QVariant::fromValue(contact);
if (it->remove("contact") != 0) {
int row = static_cast<int>(distance(m_sip_addresses.begin(), it));
emit dataChanged(index(row, 0), index(row, 0));
}
} }
} }
);
for (const auto &map : m_sip_addresses) // TODO: handle data changed from contact
m_refs << &map;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
...@@ -91,7 +64,7 @@ QVariant SipAddressesModel::data (const QModelIndex &index, int role) const { ...@@ -91,7 +64,7 @@ QVariant SipAddressesModel::data (const QModelIndex &index, int role) const {
return QVariant(); return QVariant();
} }
// ------------------------------------------------------------------------------ // -----------------------------------------------------------------------------
ContactModel *SipAddressesModel::mapSipAddressToContact (const QString &sip_address) const { ContactModel *SipAddressesModel::mapSipAddressToContact (const QString &sip_address) const {
auto it = m_sip_addresses.find(sip_address); auto it = m_sip_addresses.find(sip_address);
...@@ -100,3 +73,80 @@ ContactModel *SipAddressesModel::mapSipAddressToContact (const QString &sip_addr ...@@ -100,3 +73,80 @@ ContactModel *SipAddressesModel::mapSipAddressToContact (const QString &sip_addr
return it->value("contact").value<ContactModel *>(); return it->value("contact").value<ContactModel *>();
} }
// -----------------------------------------------------------------------------
void SipAddressesModel::updateFromContact (ContactModel *contact) {
for (const auto &sip_address : contact->getVcardModel()->getSipAddresses()) {
const QString &sip_address_str = sip_address.toString();
auto it = m_sip_addresses.find(sip_address_str);
// New sip address from contact = new entry.
if (it == m_sip_addresses.end()) {
QVariantMap map;
map["sipAddress"] = sip_address;
map["contact"] = QVariant::fromValue(contact);
m_sip_addresses[sip_address_str] = map;
m_refs << &m_sip_addresses[sip_address_str];
int row = m_refs.count() - 1;
emit dataChanged(index(row, 0), index(row, 0));
continue;
}
// Sip address exists, update contact.
(*it)["contact"] = QVariant::fromValue(contact);
int row = m_refs.indexOf(&(*it));
Q_ASSERT(row != -1);
emit dataChanged(index(row, 0), index(row, 0));
}
}
void SipAddressesModel::fetchSipAddresses () {
shared_ptr<linphone::Core> core = CoreManager::getInstance()->getCore();
// Get sip addresses from chatrooms.
for (const auto &chat_room : core->getChatRooms()) {
list<shared_ptr<linphone::ChatMessage> > history = chat_room->getHistory(0);
if (history.size() == 0)
continue;
QString sip_address = ::Utils::linphoneStringToQString(chat_room->getPeerAddress()->asString());
QVariantMap map;
map["sipAddress"] = sip_address;
map["timestamp"] = QDateTime::fromMSecsSinceEpoch(static_cast<qint64>(history.back()->getTime()) * 1000);
m_sip_addresses[sip_address] = map;
}
// Get sip addresses from calls.
QSet<QString> address_done;
for (const auto &call_log : core->getCallLogs()) {
QString sip_address = ::Utils::linphoneStringToQString(call_log->getRemoteAddress()->asString());
if (address_done.contains(sip_address))
continue; // Already used.
address_done << sip_address;
QVariantMap map;
map["sipAddress"] = sip_address;
map["timestamp"] = QDateTime::fromMSecsSinceEpoch(
static_cast<qint64>(call_log->getStartDate() + call_log->getDuration()) * 1000
);
auto it = m_sip_addresses.find(sip_address);
if (it == m_sip_addresses.end() || map["timestamp"] > (*it)["timestamp"])
m_sip_addresses[sip_address] = map;
}
for (const auto &map : m_sip_addresses)
m_refs << &map;
// Get sip addresses from contacts.
for (auto &contact : CoreManager::getInstance()->getContactsListModel()->m_list)
updateFromContact(contact);
}
...@@ -23,6 +23,9 @@ public slots: ...@@ -23,6 +23,9 @@ public slots:
ContactModel *mapSipAddressToContact (const QString &sip_address) const; ContactModel *mapSipAddressToContact (const QString &sip_address) const;
private: private:
void updateFromContact (ContactModel *contact);
void fetchSipAddresses ();
QHash<QString, QVariantMap> m_sip_addresses; QHash<QString, QVariantMap> m_sip_addresses;
QList<const QVariantMap *> m_refs; QList<const QVariantMap *> m_refs;
}; };
......
...@@ -5,6 +5,7 @@ import Common 1.0 ...@@ -5,6 +5,7 @@ import Common 1.0
import Linphone 1.0 import Linphone 1.0
import LinphoneUtils 1.0 import LinphoneUtils 1.0
import Linphone.Styles 1.0 import Linphone.Styles 1.0
import Utils 1.0
// ============================================================================= // =============================================================================
...@@ -16,11 +17,20 @@ Rectangle { ...@@ -16,11 +17,20 @@ Rectangle {
property alias actions: actionBar.data property alias actions: actionBar.data
property alias sipAddressColor: description.sipAddressColor property alias sipAddressColor: description.sipAddressColor
property alias usernameColor: description.usernameColor property alias usernameColor: description.usernameColor
property string sipAddress
property var _contact: SipAddressesModel.mapSipAddressToContact(sipAddress) // Can be a SipAddress object of SipAddressesModel or just a string.
property var sipAddress
property var _contact: Utils.isObject(sipAddress)
? sipAddress.contact
: SipAddressesModel.mapSipAddressToContact(sipAddress)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function _getSipAddress () {
return item.sipAddress.sipAddress || item.sipAddress
}
color: 'transparent' // No color by default. color: 'transparent' // No color by default.
height: ContactStyle.height height: ContactStyle.height
...@@ -39,7 +49,7 @@ Rectangle { ...@@ -39,7 +49,7 @@ Rectangle {
Layout.preferredWidth: ContactStyle.contentHeight Layout.preferredWidth: ContactStyle.contentHeight
image: _contact && _contact.vcard.avatar image: _contact && _contact.vcard.avatar
presenceLevel: _contact ? _contact.presenceLevel : Presence.White presenceLevel: _contact ? _contact.presenceLevel : Presence.White
username: LinphoneUtils.getContactUsername(_contact || sipAddress) username: LinphoneUtils.getContactUsername(_contact || _getSipAddress())
} }
ContactDescription { ContactDescription {
...@@ -47,7 +57,7 @@ Rectangle { ...@@ -47,7 +57,7 @@ Rectangle {
Layout.fillHeight: true Layout.fillHeight: true
Layout.fillWidth: true Layout.fillWidth: true
sipAddress: item.sipAddress sipAddress: _getSipAddress()
username: avatar.username username: avatar.username
} }
......
...@@ -85,17 +85,6 @@ ColumnLayout { ...@@ -85,17 +85,6 @@ ColumnLayout {
currentIndex: -1 currentIndex: -1
delegate: Item { delegate: Item {
property var contact: {
Utils.assert(
!Utils.isArray($timelineEntry.sipAddress),
'Conferences are not supported at this moment.'
)
return SipAddressesModel.mapSipAddressToContact(
$timelineEntry.sipAddress
) || $timelineEntry.sipAddress
}
height: TimelineStyle.contact.height height: TimelineStyle.contact.height
width: parent ? parent.width : 0 width: parent ? parent.width : 0
...@@ -108,7 +97,7 @@ ColumnLayout { ...@@ -108,7 +97,7 @@ ColumnLayout {
? TimelineStyle.contact.backgroundColor.a ? TimelineStyle.contact.backgroundColor.a
: TimelineStyle.contact.backgroundColor.b : TimelineStyle.contact.backgroundColor.b
) )
sipAddress: $timelineEntry.sipAddress sipAddress: $timelineEntry
sipAddressColor: view.currentIndex === index sipAddressColor: view.currentIndex === index
? TimelineStyle.contact.sipAddress.color.selected ? TimelineStyle.contact.sipAddress.color.selected
: TimelineStyle.contact.sipAddress.color.normal : TimelineStyle.contact.sipAddress.color.normal
......
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