From 2fb69d9fd644c517a2e10d29479dbe41af2c21f6 Mon Sep 17 00:00:00 2001 From: Julien Wadel Date: Mon, 6 Mar 2023 09:10:11 +0100 Subject: [PATCH] Sort chat messages from received date and not send date. --- .../src/components/chat-events/ChatCallModel.cpp | 9 +++++++++ .../src/components/chat-events/ChatCallModel.hpp | 1 + .../src/components/chat-events/ChatEvent.cpp | 9 +++++++++ .../src/components/chat-events/ChatEvent.hpp | 4 ++++ .../src/components/chat-events/ChatMessageModel.cpp | 11 +++++++++++ .../src/components/chat-events/ChatMessageModel.hpp | 2 ++ .../src/components/chat-events/ChatNoticeModel.cpp | 11 ++++++++--- .../src/components/chat-events/ChatNoticeModel.hpp | 5 +++-- .../src/components/chat-room/ChatRoomModel.cpp | 4 ++-- .../src/components/chat-room/ChatRoomProxyModel.cpp | 2 +- linphone-app/src/components/content/ContentModel.cpp | 5 +++++ linphone-app/src/components/core/CoreHandlers.cpp | 8 ++++++++ linphone-app/src/components/history/HistoryModel.cpp | 12 ++++++++++-- .../components/sip-addresses/SipAddressesModel.cpp | 2 +- .../ui/modules/Linphone/Styles/Chat/ChatStyle.qml | 5 ++++- 15 files changed, 78 insertions(+), 12 deletions(-) diff --git a/linphone-app/src/components/chat-events/ChatCallModel.cpp b/linphone-app/src/components/chat-events/ChatCallModel.cpp index 8d155b000..42bf6e1cc 100644 --- a/linphone-app/src/components/chat-events/ChatCallModel.cpp +++ b/linphone-app/src/components/chat-events/ChatCallModel.cpp @@ -31,10 +31,19 @@ ChatCallModel::ChatCallModel ( std::shared_ptr callLog, const App::getInstance()->getEngine()->setObjectOwnership(this, QQmlEngine::CppOwnership);// Avoid QML to destroy it when passing by Q_INVOKABLE mCallLog = callLog; mIsStart = isStart; + bool hasReceived = mCallLog->dataExists("receivedTime"); if(isStart){ mTimestamp = QDateTime::fromMSecsSinceEpoch(callLog->getStartDate() * 1000); + if(hasReceived) + mReceivedTimestamp = QDateTime::fromMSecsSinceEpoch(mCallLog->getData("receivedTime")); + else + mReceivedTimestamp = mTimestamp; }else{ mTimestamp = QDateTime::fromMSecsSinceEpoch((callLog->getStartDate() + callLog->getDuration()) * 1000); + if(hasReceived) + mReceivedTimestamp = QDateTime::fromMSecsSinceEpoch((mCallLog->getData("receivedTime") + callLog->getDuration()) * 1000); + else + mReceivedTimestamp = mTimestamp; } mIsOutgoing = (mCallLog->getDir() == linphone::Call::Dir::Outgoing); mStatus = (LinphoneEnums::fromLinphone(mCallLog->getStatus())); diff --git a/linphone-app/src/components/chat-events/ChatCallModel.hpp b/linphone-app/src/components/chat-events/ChatCallModel.hpp index 32021a2ca..dcfebe7e2 100644 --- a/linphone-app/src/components/chat-events/ChatCallModel.hpp +++ b/linphone-app/src/components/chat-events/ChatCallModel.hpp @@ -37,6 +37,7 @@ public: Q_PROPERTY(ChatRoomModel::EntryType type MEMBER mType CONSTANT) Q_PROPERTY(QDateTime timestamp MEMBER mTimestamp CONSTANT) + Q_PROPERTY(QDateTime receivedTimestamp MEMBER mReceivedTimestamp CONSTANT) Q_PROPERTY(bool isStart MEMBER mIsStart WRITE setIsStart NOTIFY isStartChanged) Q_PROPERTY(LinphoneEnums::CallStatus status MEMBER mStatus WRITE setStatus NOTIFY statusChanged) diff --git a/linphone-app/src/components/chat-events/ChatEvent.cpp b/linphone-app/src/components/chat-events/ChatEvent.cpp index 2765b0783..9a19e7ec3 100644 --- a/linphone-app/src/components/chat-events/ChatEvent.cpp +++ b/linphone-app/src/components/chat-events/ChatEvent.cpp @@ -35,9 +35,18 @@ ChatEvent::~ChatEvent(){ QDateTime ChatEvent::getTimestamp() const{ return mTimestamp; } + +QDateTime ChatEvent::getReceivedTimestamp() const{ + return mReceivedTimestamp; +} + void ChatEvent::setTimestamp(const QDateTime& timestamp){ mTimestamp = timestamp; } +void ChatEvent::setReceivedTimestamp(const QDateTime& timestamp){ + mReceivedTimestamp = timestamp; +} + void ChatEvent::deleteEvent(){ } \ No newline at end of file diff --git a/linphone-app/src/components/chat-events/ChatEvent.hpp b/linphone-app/src/components/chat-events/ChatEvent.hpp index c4b17f802..956652723 100644 --- a/linphone-app/src/components/chat-events/ChatEvent.hpp +++ b/linphone-app/src/components/chat-events/ChatEvent.hpp @@ -34,12 +34,16 @@ public: ChatRoomModel::EntryType mType; virtual QDateTime getTimestamp() const; + virtual QDateTime getReceivedTimestamp() const; + virtual void setTimestamp(const QDateTime& timestamp = QDateTime::currentDateTime()); + virtual void setReceivedTimestamp(const QDateTime& timestamp = QDateTime::currentDateTime()); virtual void deleteEvent(); protected: QDateTime mTimestamp; + QDateTime mReceivedTimestamp; }; Q_DECLARE_METATYPE(ChatEvent*) #endif diff --git a/linphone-app/src/components/chat-events/ChatMessageModel.cpp b/linphone-app/src/components/chat-events/ChatMessageModel.cpp index 62fd79860..3fd0b7f76 100644 --- a/linphone-app/src/components/chat-events/ChatMessageModel.cpp +++ b/linphone-app/src/components/chat-events/ChatMessageModel.cpp @@ -109,6 +109,13 @@ ChatMessageModel::ChatMessageModel ( std::shared_ptr chat mContent = txt; mTimestamp = QDateTime::fromMSecsSinceEpoch(chatMessage->getTime() * 1000); + auto appdata = ChatMessageModel::AppDataManager(QString::fromStdString(chatMessage->getAppdata())); + if(!appdata.mData.contains("receivedTime")){ + appdata.mData["receivedTime"] = QString::number(mTimestamp.toSecsSinceEpoch()); + chatMessage->setAppdata(Utils::appStringToCoreString(appdata.toString())); + mReceivedTimestamp = mTimestamp; + }else + mReceivedTimestamp = QDateTime::fromMSecsSinceEpoch(appdata.mData["receivedTime"].toULongLong() * 1000); } mWasDownloaded = false; @@ -235,6 +242,10 @@ void ChatMessageModel::setTimestamp(const QDateTime& timestamp) { mTimestamp = timestamp; } +void ChatMessageModel::setReceivedTimestamp(const QDateTime& timestamp) { + mReceivedTimestamp = timestamp; +} + //----------------------------------------------------------------------------------------------------------------------- void ChatMessageModel::resendMessage (){ diff --git a/linphone-app/src/components/chat-events/ChatMessageModel.hpp b/linphone-app/src/components/chat-events/ChatMessageModel.hpp index f9aaa1551..7f82fe959 100644 --- a/linphone-app/src/components/chat-events/ChatMessageModel.hpp +++ b/linphone-app/src/components/chat-events/ChatMessageModel.hpp @@ -83,6 +83,7 @@ public: Q_PROPERTY(bool wasDownloaded MEMBER mWasDownloaded WRITE setWasDownloaded NOTIFY wasDownloadedChanged) Q_PROPERTY(ChatRoomModel::EntryType type MEMBER mType CONSTANT) Q_PROPERTY(QDateTime timestamp MEMBER mTimestamp CONSTANT) + Q_PROPERTY(QDateTime receivedTimestamp MEMBER mReceivedTimestamp CONSTANT) Q_PROPERTY(QString content MEMBER mContent NOTIFY contentChanged) @@ -125,6 +126,7 @@ public: void setWasDownloaded(bool wasDownloaded); virtual void setTimestamp(const QDateTime& timestamp = QDateTime::currentDateTime()) override; + virtual void setReceivedTimestamp(const QDateTime& timestamp = QDateTime::currentDateTime()) override; //---------------------------------------------------------------------------- diff --git a/linphone-app/src/components/chat-events/ChatNoticeModel.cpp b/linphone-app/src/components/chat-events/ChatNoticeModel.cpp index bb3746327..bcdc73d1e 100644 --- a/linphone-app/src/components/chat-events/ChatNoticeModel.cpp +++ b/linphone-app/src/components/chat-events/ChatNoticeModel.cpp @@ -34,14 +34,19 @@ ChatNoticeModel::ChatNoticeModel ( std::shared_ptr eventLog, mEventLogType = LinphoneEnums::EventLogType::EventLogTypeNone; setEventLogType(LinphoneEnums::fromLinphone(mEventLog->getType())); mTimestamp = QDateTime::fromMSecsSinceEpoch(eventLog->getCreationTime() * 1000); + if(mEventLog->dataExists("receivedTime")) + mReceivedTimestamp = QDateTime::fromMSecsSinceEpoch(mEventLog->getData("receivedTime") * 1000); + else + mReceivedTimestamp = mTimestamp; } -ChatNoticeModel::ChatNoticeModel ( NoticeType noticeType, const QDateTime& timestamp, const QString& txt, QObject * parent) : ChatEvent(ChatRoomModel::EntryType::NoticeEntry, parent) { +ChatNoticeModel::ChatNoticeModel ( NoticeType noticeType, const QDateTime& timestamp, const QDateTime& receivedTimestamp, const QString& txt, QObject * parent) : ChatEvent(ChatRoomModel::EntryType::NoticeEntry, parent) { App::getInstance()->getEngine()->setObjectOwnership(this, QQmlEngine::CppOwnership);// Avoid QML to destroy it when passing by Q_INVOKABLE mEventLogType = LinphoneEnums::EventLogType::EventLogTypeNone; setStatus(noticeType); setName(txt); mTimestamp = timestamp; + mReceivedTimestamp = receivedTimestamp; } ChatNoticeModel::~ChatNoticeModel(){ @@ -55,8 +60,8 @@ QSharedPointer ChatNoticeModel::create(std::shared_ptr ChatNoticeModel::create(NoticeType noticeType, const QDateTime& timestamp, const QString& txt, QObject * parent){ - auto model = QSharedPointer::create(noticeType, timestamp, txt, parent); +QSharedPointer ChatNoticeModel::create(NoticeType noticeType, const QDateTime& timestamp, const QDateTime& receivedTimestamp, const QString& txt, QObject * parent){ + auto model = QSharedPointer::create(noticeType, timestamp, receivedTimestamp, txt, parent); if(model ){ return model; }else diff --git a/linphone-app/src/components/chat-events/ChatNoticeModel.hpp b/linphone-app/src/components/chat-events/ChatNoticeModel.hpp index 03c889a41..eb4e6b6c3 100644 --- a/linphone-app/src/components/chat-events/ChatNoticeModel.hpp +++ b/linphone-app/src/components/chat-events/ChatNoticeModel.hpp @@ -39,13 +39,14 @@ public: Q_ENUM(NoticeType); static QSharedPointer create(std::shared_ptr eventLog, QObject * parent = nullptr);// Call it instead constructor - static QSharedPointer create(NoticeType noticeType, const QDateTime& timestamp,const QString& txt, QObject * parent = nullptr); + static QSharedPointer create(NoticeType noticeType, const QDateTime& timestamp, const QDateTime& receivedTimestamp,const QString& txt, QObject * parent = nullptr); ChatNoticeModel (std::shared_ptr eventLog, QObject * parent = nullptr); - ChatNoticeModel (NoticeType noticeType, const QDateTime& timestamp, const QString& txt, QObject * parent = nullptr); + ChatNoticeModel (NoticeType noticeType, const QDateTime& timestamp, const QDateTime& receivedTimestamp, const QString& txt, QObject * parent = nullptr); virtual ~ChatNoticeModel(); Q_PROPERTY(ChatRoomModel::EntryType type MEMBER mType CONSTANT)// NoticeEntry Q_PROPERTY(QDateTime timestamp MEMBER mTimestamp CONSTANT) + Q_PROPERTY(QDateTime receivedTimestamp MEMBER mReceivedTimestamp CONSTANT) Q_PROPERTY(QString name MEMBER mName WRITE setName NOTIFY nameChanged) Q_PROPERTY(NoticeType status MEMBER mStatus WRITE setStatus NOTIFY statusChanged) Q_PROPERTY(LinphoneEnums::EventLogType eventLogType MEMBER mEventLogType WRITE setEventLogType NOTIFY eventLogTypeChanged) diff --git a/linphone-app/src/components/chat-room/ChatRoomModel.cpp b/linphone-app/src/components/chat-room/ChatRoomModel.cpp index be187e550..08d60ac22 100644 --- a/linphone-app/src/components/chat-room/ChatRoomModel.cpp +++ b/linphone-app/src/components/chat-room/ChatRoomModel.cpp @@ -232,7 +232,7 @@ QVariant ChatRoomModel::data (const QModelIndex &index, int role) const { switch (role) { case Roles::ChatEntry: return QVariant::fromValue(mList[row].get()); - case Roles::SectionDate: return QVariant::fromValue(mList[row].objectCast()->getTimestamp().date()); + case Roles::SectionDate: return QVariant::fromValue(mList[row].objectCast()->getReceivedTimestamp().date()); } return QVariant(); @@ -856,7 +856,7 @@ void ChatRoomModel::updateNewMessageNotice(const int& count){ lastUnreadMessage = min(lastUnreadMessage, QDateTime::fromMSecsSinceEpoch(message->getTime() * 1000 - 1 )); //-1 to be sure that event will be before the message } } - mUnreadMessageNotice = ChatNoticeModel::create(ChatNoticeModel::NoticeType::NoticeUnreadMessages, lastUnreadMessage, QString::number(count)); + mUnreadMessageNotice = ChatNoticeModel::create(ChatNoticeModel::NoticeType::NoticeUnreadMessages, lastUnreadMessage,lastUnreadMessage, QString::number(count)); beginInsertRows(QModelIndex(), 0, 0); mList.prepend(mUnreadMessageNotice); endInsertRows(); diff --git a/linphone-app/src/components/chat-room/ChatRoomProxyModel.cpp b/linphone-app/src/components/chat-room/ChatRoomProxyModel.cpp index 75e6b10ae..48b47baae 100644 --- a/linphone-app/src/components/chat-room/ChatRoomProxyModel.cpp +++ b/linphone-app/src/components/chat-room/ChatRoomProxyModel.cpp @@ -181,7 +181,7 @@ bool ChatRoomProxyModel::lessThan (const QModelIndex &left, const QModelIndex &r return true; if(!a) return false; - return a->getTimestamp() < b->getTimestamp(); + return a->getReceivedTimestamp() < b->getReceivedTimestamp(); } // ----------------------------------------------------------------------------- diff --git a/linphone-app/src/components/content/ContentModel.cpp b/linphone-app/src/components/content/ContentModel.cpp index e7f6499b1..ff332826e 100644 --- a/linphone-app/src/components/content/ContentModel.cpp +++ b/linphone-app/src/components/content/ContentModel.cpp @@ -230,7 +230,12 @@ void ContentModel::removeThumbnail(){ QFile(thumbnailPath).remove(); } } + QString backup; + if( mAppData.mData.contains("receivedTime")) + backup = mAppData.mData["receivedTime"]; mAppData.mData.clear(); + if( !backup.isEmpty()) + mAppData.mData["receivedTime"] = backup; } void ContentModel::removeDownloadedFile(){ diff --git a/linphone-app/src/components/core/CoreHandlers.cpp b/linphone-app/src/components/core/CoreHandlers.cpp index ff3c83646..bca08f971 100644 --- a/linphone-app/src/components/core/CoreHandlers.cpp +++ b/linphone-app/src/components/core/CoreHandlers.cpp @@ -26,6 +26,7 @@ #include "app/App.hpp" #include "components/call/CallModel.hpp" +#include "components/chat-events/ChatMessageModel.hpp" #include "components/contact/ContactModel.hpp" #include "components/notifier/Notifier.hpp" #include "components/settings/AccountSettingsModel.hpp" @@ -228,6 +229,13 @@ void CoreHandlers::onMessagesReceived ( appSettings.beginGroup("chatrooms"); for(auto message : messages){ + if(message){ + auto chatRoom = message->getChatRoom(); + auto dbMessage = chatRoom->findMessage(message->getMessageId()); + auto appdata = ChatMessageModel::AppDataManager(QString::fromStdString(dbMessage->getAppdata())); + appdata.mData["receivedTime"] = QString::number(QDateTime::currentMSecsSinceEpoch()/1000); + dbMessage->setAppdata(Utils::appStringToCoreString(appdata.toString())); + } if( !message || message->isOutgoing() ) continue; diff --git a/linphone-app/src/components/history/HistoryModel.cpp b/linphone-app/src/components/history/HistoryModel.cpp index 507135a5c..e4be99edf 100644 --- a/linphone-app/src/components/history/HistoryModel.cpp +++ b/linphone-app/src/components/history/HistoryModel.cpp @@ -50,6 +50,10 @@ using namespace std; static inline void fillCallStartEntry (QVariantMap &dest, const shared_ptr &callLog) { dest["type"] = HistoryModel::CallEntry; dest["timestamp"] = QDateTime::fromMSecsSinceEpoch(callLog->getStartDate() * 1000); + if(callLog->dataExists("receivedTime")) + dest["receivedTimestamp"] = QDateTime::fromMSecsSinceEpoch(callLog->getData("receivedTime") * 1000); + else + dest["receivedTimestamp"] = dest["timestamp"]; dest["isOutgoing"] = callLog->getDir() == linphone::Call::Dir::Outgoing; dest["status"] = static_cast(callLog->getStatus()); dest["isStart"] = true; @@ -63,6 +67,10 @@ static inline void fillCallStartEntry (QVariantMap &dest, const shared_ptr &callLog) { dest["type"] = HistoryModel::CallEntry; dest["timestamp"] = QDateTime::fromMSecsSinceEpoch((callLog->getStartDate() + callLog->getDuration()) * 1000); + if(callLog->dataExists("receivedTime")) + dest["receivedTimestamp"] = QDateTime::fromMSecsSinceEpoch((callLog->getData("receivedTime") + callLog->getDuration()) * 1000); + else + dest["receivedTimestamp"] = dest["timestamp"]; dest["isOutgoing"] = callLog->getDir() == linphone::Call::Dir::Outgoing; dest["status"] = static_cast(callLog->getStatus()); dest["isStart"] = false; @@ -112,7 +120,7 @@ QVariant HistoryModel::data (const QModelIndex &index, int role) const { return QVariant::fromValue(data); } case Roles::SectionDate: - return QVariant::fromValue(mEntries[row].first["timestamp"].toDate()); + return QVariant::fromValue(mEntries[row].first["receivedTimestamp"].toDate()); } return QVariant(); @@ -231,7 +239,7 @@ void HistoryModel::insertCall (const shared_ptr &callLog) { const QList::iterator *start = nullptr ) { auto it = lower_bound(start ? *start : mEntries.begin(), mEntries.end(), entry, [](const HistoryEntryData &a, const HistoryEntryData &b) { - return a.first["timestamp"] < b.first["timestamp"]; + return a.first["receivedTimestamp"] < b.first["receivedTimestamp"]; }); int row = int(distance(mEntries.begin(), it)); diff --git a/linphone-app/src/components/sip-addresses/SipAddressesModel.cpp b/linphone-app/src/components/sip-addresses/SipAddressesModel.cpp index b4b17cc7e..b0d73e0fe 100644 --- a/linphone-app/src/components/sip-addresses/SipAddressesModel.cpp +++ b/linphone-app/src/components/sip-addresses/SipAddressesModel.cpp @@ -418,7 +418,7 @@ void SipAddressesModel::handleLastEntryRemoved (ChatRoomModel *chatRoomModel) { ).toMap(); // Update the timestamp with the new last chat message timestamp. - it2->timestamp = map["timestamp"].toDateTime(); + it2->timestamp = map["receivedTimestamp"].toDateTime(); emit dataChanged(index(row, 0), index(row, 0)); } diff --git a/linphone-app/ui/modules/Linphone/Styles/Chat/ChatStyle.qml b/linphone-app/ui/modules/Linphone/Styles/Chat/ChatStyle.qml index b363eab10..e84240bba 100644 --- a/linphone-app/ui/modules/Linphone/Styles/Chat/ChatStyle.qml +++ b/linphone-app/ui/modules/Linphone/Styles/Chat/ChatStyle.qml @@ -274,7 +274,10 @@ QtObject { property QtObject time: QtObject { property var colorModel: ColorsList.add(sectionName+'_time', 'd') property int pointSize: Units.dp * 10 - property int width: 44 + property int width: 70 + } + property QtObject date: QtObject { + property int pointSize: Units.dp * 8 } } }