Commit 40236491 authored by Ronan Abhamon's avatar Ronan Abhamon

feat(components/chat): supports deletion of messages

parent 0c7387f8
#include <QDateTime>
#include <QtDebug>
#include "../../utils.hpp"
#include "../core/CoreManager.hpp"
#include "ChatModel.hpp"
using namespace std;
// ===================================================================
QHash<int, QByteArray> 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<QVariantMap, shared_ptr<void> > pair = m_entries.takeAt(row);
switch (pair.first["type"].toInt()) {
case ChatModel::MessageEntry:
m_chat_room->deleteMessage(
static_pointer_cast<linphone::ChatMessage>(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<linphone::ChatRoom> 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<void>(message));
}
// Get calls.
// TODO.
endResetModel();
emit sipAddressChanged(sip_address);
......
......@@ -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<int, QByteArray> 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<QVariantMap> m_entries;
QList<QPair<QVariantMap, std::shared_ptr<void> > > m_entries;
std::shared_ptr<linphone::ChatRoom> m_chat_room;
};
......
......@@ -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;
}
......@@ -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
}
......
......@@ -47,6 +47,6 @@ Row {
iconSize: ChatStyle.entry.deleteIconSize
visible: isHoverEntry()
onClicked: deleteEntry()
onClicked: removeEntry()
}
}
......@@ -43,7 +43,7 @@ RowLayout {
iconSize: ChatStyle.entry.deleteIconSize
visible: isHoverEntry()
onClicked: deleteEntry()
onClicked: removeEntry()
}
}
}
......@@ -39,7 +39,7 @@ Item {
iconSize: ChatStyle.entry.deleteIconSize
visible: isHoverEntry()
onClicked: deleteEntry()
onClicked: removeEntry()
}
}
}
......
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