diff --git a/tests/src/components/chat/ChatModel.cpp b/tests/src/components/chat/ChatModel.cpp index a4c0b376c..65a909dd6 100644 --- a/tests/src/components/chat/ChatModel.cpp +++ b/tests/src/components/chat/ChatModel.cpp @@ -1,10 +1,13 @@ #include +#include #include "../../utils.hpp" #include "../core/CoreManager.hpp" #include "ChatModel.hpp" +using namespace std; + // =================================================================== QHash ChatModel::roleNames () const { @@ -22,14 +25,55 @@ QVariant ChatModel::data (const QModelIndex &index, int role) const { switch (role) { case Roles::ChatEntry: - return QVariant::fromValue(m_entries[row]); + return QVariant::fromValue(m_entries[row].first); case Roles::SectionDate: - return QVariant::fromValue(m_entries[row]["timestamp"].toDate()); + return QVariant::fromValue(m_entries[row].first["timestamp"].toDate()); } return QVariant(); } +bool ChatModel::removeRow (int row, const QModelIndex &) { + return removeRows(row, 1); +} + +bool ChatModel::removeRows (int row, int count, const QModelIndex &parent) { + int limit = row + count - 1; + + if (row < 0 || count < 0 || limit >= m_entries.count()) + return false; + + beginRemoveRows(parent, row, limit); + + for (int i = 0; i < count; ++i) { + QPair > pair = m_entries.takeAt(row); + + switch (pair.first["type"].toInt()) { + case ChatModel::MessageEntry: + m_chat_room->deleteMessage( + static_pointer_cast(pair.second) + ); + break; + case ChatModel::CallEntry: + + break; + } + } + + endRemoveRows(); + + return true; +} + +// ------------------------------------------------------------------- + +void ChatModel::removeEntry (int id) { + qInfo() << "Removing chat entry:" << id << "of:" << getSipAddress(); + + if (!removeRow(id)) + qWarning() << "Unable to remove chat entry:" << id; +} + // ------------------------------------------------------------------- QString ChatModel::getSipAddress () const { @@ -50,25 +94,28 @@ void ChatModel::setSipAddress (const QString &sip_address) { // Invalid old sip address entries. m_entries.clear(); - std::shared_ptr chat_room = + m_chat_room = CoreManager::getInstance()->getCore()->getChatRoomFromUri( Utils::qStringToLinphoneString(sip_address) ); - for (auto &message : chat_room->getHistory(0)) { + // Get messages. + for (auto &message : m_chat_room->getHistory(0)) { QVariantMap map; - // UTC format. + map["type"] = EntryType::MessageEntry; map["timestamp"] = QDateTime::fromTime_t(message->getTime()); - map["type"] = "message"; map["content"] = Utils::linphoneStringToQString( message->getText() ); map["isOutgoing"] = message->isOutgoing(); - m_entries << map; + m_entries << qMakePair(map, static_pointer_cast(message)); } + // Get calls. + // TODO. + endResetModel(); emit sipAddressChanged(sip_address); diff --git a/tests/src/components/chat/ChatModel.hpp b/tests/src/components/chat/ChatModel.hpp index 14e950e8d..c0c214246 100644 --- a/tests/src/components/chat/ChatModel.hpp +++ b/tests/src/components/chat/ChatModel.hpp @@ -22,6 +22,12 @@ public: SectionDate }; + enum EntryType { + MessageEntry, + CallEntry + }; + Q_ENUM(EntryType); + ChatModel (QObject *parent = Q_NULLPTR) : QAbstractListModel(parent) {} int rowCount (const QModelIndex &index = QModelIndex()) const { @@ -31,6 +37,12 @@ public: QHash roleNames () const; QVariant data (const QModelIndex &index, int role) const; + bool removeRow (int row, const QModelIndex &parent = QModelIndex()); + bool removeRows (int row, int count, const QModelIndex &parent = QModelIndex()); + +public slots: + void removeEntry (int id); + signals: void sipAddressChanged (const QString &sipAddress); @@ -38,7 +50,7 @@ private: QString getSipAddress () const; void setSipAddress (const QString &sip_address); - QList m_entries; + QList > > m_entries; std::shared_ptr m_chat_room; }; diff --git a/tests/src/components/contacts/ContactsListModel.cpp b/tests/src/components/contacts/ContactsListModel.cpp index d9d52e5e4..ee38519ad 100644 --- a/tests/src/components/contacts/ContactsListModel.cpp +++ b/tests/src/components/contacts/ContactsListModel.cpp @@ -57,11 +57,9 @@ bool ContactsListModel::removeRows (int row, int count, const QModelIndex &paren beginRemoveRows(parent, row, limit); for (int i = 0; i < count; ++i) { - ContactModel *contact = m_list[row]; + ContactModel *contact = m_list.takeAt(row); - m_list.removeAt(row); m_linphone_friends->removeFriend(contact->m_linphone_friend); - contact->deleteLater(); } @@ -94,5 +92,5 @@ void ContactsListModel::removeContact (ContactModel *contact) { int index = m_list.indexOf(contact); if (index == -1 || !removeRow(index)) - qWarning() << "Unable to remove contact:" << index; + qWarning() << "Unable to remove contact:" << contact; } diff --git a/tests/ui/modules/Linphone/Chat/Chat.qml b/tests/ui/modules/Linphone/Chat/Chat.qml index 28d2908d8..340fdc4e2 100644 --- a/tests/ui/modules/Linphone/Chat/Chat.qml +++ b/tests/ui/modules/Linphone/Chat/Chat.qml @@ -11,14 +11,14 @@ import Linphone.Styles 1.0 ColumnLayout { property var contact - property alias model: listView.model + property alias model: chat.model // ----------------------------------------------------------------- spacing: 0 ScrollableListView { - id: listView + id: chat Layout.fillHeight: true Layout.fillWidth: true @@ -84,8 +84,8 @@ ColumnLayout { return mouseArea.containsMouse } - function deleteEntry () { - console.log('delete entry', index) + function removeEntry () { + chat.model.removeEntry(index) } anchors { @@ -157,7 +157,7 @@ ColumnLayout { // Display content. Loader { Layout.fillWidth: true - sourceComponent: $chatEntry.type === 'message' + sourceComponent: $chatEntry.type === ChatModel.MessageEntry ? ($chatEntry.isOutgoing ? outgoingMessage : incomingMessage) : event } diff --git a/tests/ui/modules/Linphone/Chat/Event.qml b/tests/ui/modules/Linphone/Chat/Event.qml index f42da8ffe..94eb696a4 100644 --- a/tests/ui/modules/Linphone/Chat/Event.qml +++ b/tests/ui/modules/Linphone/Chat/Event.qml @@ -47,6 +47,6 @@ Row { iconSize: ChatStyle.entry.deleteIconSize visible: isHoverEntry() - onClicked: deleteEntry() + onClicked: removeEntry() } } diff --git a/tests/ui/modules/Linphone/Chat/IncomingMessage.qml b/tests/ui/modules/Linphone/Chat/IncomingMessage.qml index 6f6d9ef15..652ff63a3 100644 --- a/tests/ui/modules/Linphone/Chat/IncomingMessage.qml +++ b/tests/ui/modules/Linphone/Chat/IncomingMessage.qml @@ -43,7 +43,7 @@ RowLayout { iconSize: ChatStyle.entry.deleteIconSize visible: isHoverEntry() - onClicked: deleteEntry() + onClicked: removeEntry() } } } diff --git a/tests/ui/modules/Linphone/Chat/OutgoingMessage.qml b/tests/ui/modules/Linphone/Chat/OutgoingMessage.qml index 657fbc689..d8b6b0022 100644 --- a/tests/ui/modules/Linphone/Chat/OutgoingMessage.qml +++ b/tests/ui/modules/Linphone/Chat/OutgoingMessage.qml @@ -39,7 +39,7 @@ Item { iconSize: ChatStyle.entry.deleteIconSize visible: isHoverEntry() - onClicked: deleteEntry() + onClicked: removeEntry() } } }