Commit f9901b5e authored by Ronan Abhamon's avatar Ronan Abhamon

feat(app): deal correctly with unread messages count

parent ce2a71ce
......@@ -102,7 +102,11 @@ void AsyncObjectBuilder::createObject (QQmlEngine *engine, const char *path, Dec
qInfo() << QStringLiteral("Start async creation of: `%1`. Component:").arg(path) << m_component;
QObject::connect(m_component, &QQmlComponent::statusChanged, this, &AsyncObjectBuilder::handleComponentCreation);
QObject::connect(
m_component, &QQmlComponent::statusChanged,
this, &AsyncObjectBuilder::handleComponentCreation,
Qt::DirectConnection
);
}
QObject *AsyncObjectBuilder::getObject () const {
......
......@@ -43,3 +43,11 @@ void SipAddressObserver::setPresenceStatus (const Presence::PresenceStatus &pres
m_presence_status = presence_status;
emit presenceStatusChanged(presence_status);
}
void SipAddressObserver::setUnreadMessagesCount (int unread_messages_count) {
if (unread_messages_count == m_unread_messages_count)
return;
m_unread_messages_count = unread_messages_count;
emit unreadMessagesCountChanged(unread_messages_count);
}
......@@ -36,6 +36,7 @@ class SipAddressObserver : public QObject {
Q_PROPERTY(ContactModel * contact READ getContact NOTIFY contactChanged);
Q_PROPERTY(Presence::PresenceStatus presenceStatus READ getPresenceStatus NOTIFY presenceStatusChanged);
Q_PROPERTY(int unreadMessagesCount READ getUnreadMessagesCount NOTIFY unreadMessagesCountChanged);
public:
SipAddressObserver (const QString &sip_address);
......@@ -44,28 +45,42 @@ public:
signals:
void contactChanged (ContactModel *contact);
void presenceStatusChanged (const Presence::PresenceStatus &presenceStatus);
void unreadMessagesCountChanged (int unreadMessagesCount);
private:
QString getSipAddress () const {
return m_sip_address;
}
// ---------------------------------------------------------------------------
ContactModel *getContact () const {
return m_contact;
}
void setContact (ContactModel *contact);
// ---------------------------------------------------------------------------
Presence::PresenceStatus getPresenceStatus () const {
return m_presence_status;
}
void setPresenceStatus (const Presence::PresenceStatus &presence_status);
// ---------------------------------------------------------------------------
int getUnreadMessagesCount () const {
return m_unread_messages_count;
}
void setUnreadMessagesCount (int unread_messages_count);
QString m_sip_address;
ContactModel *m_contact = nullptr;
Presence::PresenceStatus m_presence_status = Presence::PresenceStatus::Offline;
int m_unread_messages_count = 0;
};
Q_DECLARE_METATYPE(SipAddressObserver *);
......
......@@ -76,57 +76,18 @@ QVariant SipAddressesModel::data (const QModelIndex &index, int role) const {
return QVariant();
}
void SipAddressesModel::connectToChatModel (ChatModel *chat_model) {
QObject::connect(
chat_model, &ChatModel::allEntriesRemoved,
this, [this, chat_model]() {
const QString sip_address = chat_model->getSipAddress();
auto it = m_sip_addresses.find(sip_address);
if (it == m_sip_addresses.end()) {
qWarning() << QStringLiteral("Unable to found sip address: `%1`.").arg(sip_address);
return;
}
int row = m_refs.indexOf(&(*it));
Q_ASSERT(row != -1);
// No history, no contact => Remove sip address from list.
if (!it->contains("contact")) {
removeRow(row);
return;
}
// Signal changes.
it->remove("timestamp");
emit dataChanged(index(row, 0), index(row, 0));
}
);
QObject::connect(
chat_model, &ChatModel::messageSent,
this, [this](const shared_ptr<linphone::ChatMessage> &message) {
addOrUpdateSipAddress(
::Utils::linphoneStringToQString(message->getToAddress()->asStringUriOnly()), message
);
}
);
QObject::connect(
chat_model, &ChatModel::messagesCountReset, this, [this, chat_model]() {
const QString &sip_address = chat_model->getSipAddress();
// -----------------------------------------------------------------------------
auto it = m_sip_addresses.find(sip_address);
if (it != m_sip_addresses.end()) {
(*it)["unreadMessagesCount"] = 0;
void SipAddressesModel::connectToChatModel (ChatModel *chat_model) {
QObject::connect(chat_model, &ChatModel::allEntriesRemoved, this, [this, chat_model] {
handleAllEntriesRemoved(chat_model->getSipAddress());
});
int row = m_refs.indexOf(&(*it));
Q_ASSERT(row != -1);
emit dataChanged(index(row, 0), index(row, 0));
QObject::connect(chat_model, &ChatModel::messageSent, this, &SipAddressesModel::handleMessageSent);
return;
}
}
);
QObject::connect(chat_model, &ChatModel::messagesCountReset, this, [this, chat_model] {
handleMessagesCountReset(chat_model->getSipAddress());
});
}
// -----------------------------------------------------------------------------
......@@ -151,6 +112,9 @@ SipAddressObserver *SipAddressesModel::getSipAddressObserver (const QString &sip
model->setPresenceStatus(
it->value("presenceStatus", Presence::PresenceStatus::Offline).value<Presence::PresenceStatus>()
);
model->setUnreadMessagesCount(
it->value("unreadMessagesCount", 0).toInt()
);
}
}
......@@ -288,6 +252,47 @@ void SipAddressesModel::handlePresenceReceived (
updateObservers(sip_address, status);
}
void SipAddressesModel::handleAllEntriesRemoved (const QString &sip_address) {
auto it = m_sip_addresses.find(sip_address);
if (it == m_sip_addresses.end()) {
qWarning() << QStringLiteral("Unable to found sip address: `%1`.").arg(sip_address);
return;
}
int row = m_refs.indexOf(&(*it));
Q_ASSERT(row != -1);
// No history, no contact => Remove sip address from list.
if (!it->contains("contact")) {
removeRow(row);
return;
}
// Signal changes.
it->remove("timestamp");
emit dataChanged(index(row, 0), index(row, 0));
}
void SipAddressesModel::handleMessageSent (const shared_ptr<linphone::ChatMessage> &message) {
addOrUpdateSipAddress(
::Utils::linphoneStringToQString(message->getToAddress()->asStringUriOnly()),
message
);
}
void SipAddressesModel::handleMessagesCountReset (const QString &sip_address) {
auto it = m_sip_addresses.find(sip_address);
if (it != m_sip_addresses.end()) {
(*it)["unreadMessagesCount"] = 0;
int row = m_refs.indexOf(&(*it));
Q_ASSERT(row != -1);
emit dataChanged(index(row, 0), index(row, 0));
}
updateObservers(sip_address, 0);
}
// -----------------------------------------------------------------------------
void SipAddressesModel::addOrUpdateSipAddress (QVariantMap &map, ContactModel *contact) {
......@@ -304,8 +309,14 @@ void SipAddressesModel::addOrUpdateSipAddress (QVariantMap &map, const shared_pt
}
void SipAddressesModel::addOrUpdateSipAddress (QVariantMap &map, const shared_ptr<linphone::ChatMessage> &message) {
// FIXME: Bug in the core, count is incremented after this function call.
// So... +1!
int count = message->getChatRoom()->getUnreadMessagesCount() + 1;
map["timestamp"] = QDateTime::fromMSecsSinceEpoch(message->getTime() * 1000);
map["unreadMessagesCount"] = message->getChatRoom()->getUnreadMessagesCount();
map["unreadMessagesCount"] = count;
updateObservers(map["sipAddress"].toString(), count);
}
template<typename T>
......@@ -418,6 +429,8 @@ void SipAddressesModel::initSipAddresses () {
handleContactAdded(contact);
}
// -----------------------------------------------------------------------------
void SipAddressesModel::updateObservers (const QString &sip_address, ContactModel *contact) {
for (auto &observer : m_observers.values(sip_address))
observer->setContact(contact);
......@@ -427,3 +440,8 @@ void SipAddressesModel::updateObservers (const QString &sip_address, const Prese
for (auto &observer : m_observers.values(sip_address))
observer->setPresenceStatus(presence_status);
}
void SipAddressesModel::updateObservers (const QString &sip_address, int messages_count) {
for (auto &observer : m_observers.values(sip_address))
observer->setUnreadMessagesCount(messages_count);
}
......@@ -68,6 +68,10 @@ private:
void handleCallStateChanged (const std::shared_ptr<linphone::Call> &call, linphone::CallState state);
void handlePresenceReceived (const QString &sip_address, const shared_ptr<const linphone::PresenceModel> &presence_model);
void handleAllEntriesRemoved (const QString &sip_address);
void handleMessageSent (const std::shared_ptr<linphone::ChatMessage> &message);
void handleMessagesCountReset (const QString &sip_address);
// ---------------------------------------------------------------------------
// A sip address exists in this list if a contact is linked to it, or a call, or a message.
......@@ -87,6 +91,7 @@ private:
void updateObservers (const QString &sip_address, ContactModel *contact);
void updateObservers (const QString &sip_address, const Presence::PresenceStatus &presence_status);
void updateObservers (const QString &sip_address, int messages_count);
QHash<QString, QVariantMap> m_sip_addresses;
QList<const QVariantMap *> m_refs;
......
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