Commit 40236491 authored by Ronan Abhamon's avatar Ronan Abhamon

feat(components/chat): supports deletion of messages

parent 0c7387f8
#include <QDateTime> #include <QDateTime>
#include <QtDebug>
#include "../../utils.hpp" #include "../../utils.hpp"
#include "../core/CoreManager.hpp" #include "../core/CoreManager.hpp"
#include "ChatModel.hpp" #include "ChatModel.hpp"
using namespace std;
// =================================================================== // ===================================================================
QHash<int, QByteArray> ChatModel::roleNames () const { QHash<int, QByteArray> ChatModel::roleNames () const {
...@@ -22,14 +25,55 @@ QVariant ChatModel::data (const QModelIndex &index, int role) const { ...@@ -22,14 +25,55 @@ QVariant ChatModel::data (const QModelIndex &index, int role) const {
switch (role) { switch (role) {
case Roles::ChatEntry: case Roles::ChatEntry:
return QVariant::fromValue(m_entries[row]); return QVariant::fromValue(m_entries[row].first);
case Roles::SectionDate: case Roles::SectionDate:
return QVariant::fromValue(m_entries[row]["timestamp"].toDate()); return QVariant::fromValue(m_entries[row].first["timestamp"].toDate());
} }
return QVariant(); 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 { QString ChatModel::getSipAddress () const {
...@@ -50,25 +94,28 @@ void ChatModel::setSipAddress (const QString &sip_address) { ...@@ -50,25 +94,28 @@ void ChatModel::setSipAddress (const QString &sip_address) {
// Invalid old sip address entries. // Invalid old sip address entries.
m_entries.clear(); m_entries.clear();
std::shared_ptr<linphone::ChatRoom> chat_room = m_chat_room =
CoreManager::getInstance()->getCore()->getChatRoomFromUri( CoreManager::getInstance()->getCore()->getChatRoomFromUri(
Utils::qStringToLinphoneString(sip_address) Utils::qStringToLinphoneString(sip_address)
); );
for (auto &message : chat_room->getHistory(0)) { // Get messages.
for (auto &message : m_chat_room->getHistory(0)) {
QVariantMap map; QVariantMap map;
// UTC format. map["type"] = EntryType::MessageEntry;
map["timestamp"] = QDateTime::fromTime_t(message->getTime()); map["timestamp"] = QDateTime::fromTime_t(message->getTime());
map["type"] = "message";
map["content"] = Utils::linphoneStringToQString( map["content"] = Utils::linphoneStringToQString(
message->getText() message->getText()
); );
map["isOutgoing"] = message->isOutgoing(); map["isOutgoing"] = message->isOutgoing();
m_entries << map; m_entries << qMakePair(map, static_pointer_cast<void>(message));
} }
// Get calls.
// TODO.
endResetModel(); endResetModel();
emit sipAddressChanged(sip_address); emit sipAddressChanged(sip_address);
......
...@@ -22,6 +22,12 @@ public: ...@@ -22,6 +22,12 @@ public:
SectionDate SectionDate
}; };
enum EntryType {
MessageEntry,
CallEntry
};
Q_ENUM(EntryType);
ChatModel (QObject *parent = Q_NULLPTR) : QAbstractListModel(parent) {} ChatModel (QObject *parent = Q_NULLPTR) : QAbstractListModel(parent) {}
int rowCount (const QModelIndex &index = QModelIndex()) const { int rowCount (const QModelIndex &index = QModelIndex()) const {
...@@ -31,6 +37,12 @@ public: ...@@ -31,6 +37,12 @@ public:
QHash<int, QByteArray> roleNames () const; QHash<int, QByteArray> roleNames () const;
QVariant data (const QModelIndex &index, int role) 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: signals:
void sipAddressChanged (const QString &sipAddress); void sipAddressChanged (const QString &sipAddress);
...@@ -38,7 +50,7 @@ private: ...@@ -38,7 +50,7 @@ private:
QString getSipAddress () const; QString getSipAddress () const;
void setSipAddress (const QString &sip_address); 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; std::shared_ptr<linphone::ChatRoom> m_chat_room;
}; };
......
...@@ -57,11 +57,9 @@ bool ContactsListModel::removeRows (int row, int count, const QModelIndex &paren ...@@ -57,11 +57,9 @@ bool ContactsListModel::removeRows (int row, int count, const QModelIndex &paren
beginRemoveRows(parent, row, limit); beginRemoveRows(parent, row, limit);
for (int i = 0; i < count; ++i) { 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); m_linphone_friends->removeFriend(contact->m_linphone_friend);
contact->deleteLater(); contact->deleteLater();
} }
...@@ -94,5 +92,5 @@ void ContactsListModel::removeContact (ContactModel *contact) { ...@@ -94,5 +92,5 @@ void ContactsListModel::removeContact (ContactModel *contact) {
int index = m_list.indexOf(contact); int index = m_list.indexOf(contact);
if (index == -1 || !removeRow(index)) 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 ...@@ -11,14 +11,14 @@ import Linphone.Styles 1.0
ColumnLayout { ColumnLayout {
property var contact property var contact
property alias model: listView.model property alias model: chat.model
// ----------------------------------------------------------------- // -----------------------------------------------------------------
spacing: 0 spacing: 0
ScrollableListView { ScrollableListView {
id: listView id: chat
Layout.fillHeight: true Layout.fillHeight: true
Layout.fillWidth: true Layout.fillWidth: true
...@@ -84,8 +84,8 @@ ColumnLayout { ...@@ -84,8 +84,8 @@ ColumnLayout {
return mouseArea.containsMouse return mouseArea.containsMouse
} }
function deleteEntry () { function removeEntry () {
console.log('delete entry', index) chat.model.removeEntry(index)
} }
anchors { anchors {
...@@ -157,7 +157,7 @@ ColumnLayout { ...@@ -157,7 +157,7 @@ ColumnLayout {
// Display content. // Display content.
Loader { Loader {
Layout.fillWidth: true Layout.fillWidth: true
sourceComponent: $chatEntry.type === 'message' sourceComponent: $chatEntry.type === ChatModel.MessageEntry
? ($chatEntry.isOutgoing ? outgoingMessage : incomingMessage) ? ($chatEntry.isOutgoing ? outgoingMessage : incomingMessage)
: event : event
} }
......
...@@ -47,6 +47,6 @@ Row { ...@@ -47,6 +47,6 @@ Row {
iconSize: ChatStyle.entry.deleteIconSize iconSize: ChatStyle.entry.deleteIconSize
visible: isHoverEntry() visible: isHoverEntry()
onClicked: deleteEntry() onClicked: removeEntry()
} }
} }
...@@ -43,7 +43,7 @@ RowLayout { ...@@ -43,7 +43,7 @@ RowLayout {
iconSize: ChatStyle.entry.deleteIconSize iconSize: ChatStyle.entry.deleteIconSize
visible: isHoverEntry() visible: isHoverEntry()
onClicked: deleteEntry() onClicked: removeEntry()
} }
} }
} }
...@@ -39,7 +39,7 @@ Item { ...@@ -39,7 +39,7 @@ Item {
iconSize: ChatStyle.entry.deleteIconSize iconSize: ChatStyle.entry.deleteIconSize
visible: isHoverEntry() 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