mirror of
https://github.com/logos-co/logos-chat-ui.git
synced 2026-08-27 09:11:11 +00:00
- Replace Q_INVOKABLE calls (initChat/startChat/stopChat, hex payloads, push events) with the new C-FFI wrapper (module_init/module_shutdown, UTF-8 content, polled module_drain_events_json) - Collapse ChatStatus to Stopped/Initialising/Online/Error, mirroring the module's deliveryStateChanged - Rehydrate conversations and messages on init/eventsDropped via module_list_conversations_json and module_get_messages_json - Resolve instance_path from CHAT_MODULE_INSTANCE_PATH or QStandardPaths::AppDataLocation - Drop ConversationListModel.peerId (peer_label folds into displayName) - Drop ChatConfig.h (obsolete Waku JSON config)
118 lines
3.1 KiB
C++
118 lines
3.1 KiB
C++
#include "ConversationListModel.h"
|
|
|
|
ConversationListModel::ConversationListModel(QObject* parent)
|
|
: QAbstractListModel(parent)
|
|
{
|
|
}
|
|
|
|
int ConversationListModel::rowCount(const QModelIndex& parent) const
|
|
{
|
|
if (parent.isValid()) return 0;
|
|
return m_items.size();
|
|
}
|
|
|
|
QVariant ConversationListModel::data(const QModelIndex& index, int role) const
|
|
{
|
|
if (!index.isValid() || index.row() >= m_items.size())
|
|
return {};
|
|
|
|
const auto& item = m_items.at(index.row());
|
|
switch (role) {
|
|
case ConversationIdRole: return item.conversationId;
|
|
case DisplayNameRole: return item.displayName;
|
|
case LastActivityRole: return item.lastActivity;
|
|
case UnreadCountRole: return item.unreadCount;
|
|
default: return {};
|
|
}
|
|
}
|
|
|
|
QHash<int, QByteArray> ConversationListModel::roleNames() const
|
|
{
|
|
return {
|
|
{ ConversationIdRole, "conversationId" },
|
|
{ DisplayNameRole, "displayName" },
|
|
{ LastActivityRole, "lastActivity" },
|
|
{ UnreadCountRole, "unreadCount" }
|
|
};
|
|
}
|
|
|
|
void ConversationListModel::addConversation(const QString& id, const QString& displayName,
|
|
const QDateTime& lastActivity)
|
|
{
|
|
if (contains(id)) return;
|
|
|
|
beginInsertRows(QModelIndex(), m_items.size(), m_items.size());
|
|
m_items.append({ id, displayName, lastActivity, 0 });
|
|
endInsertRows();
|
|
}
|
|
|
|
void ConversationListModel::updateDisplayName(const QString& id, const QString& displayName)
|
|
{
|
|
int idx = indexOf(id);
|
|
if (idx < 0) return;
|
|
|
|
if (m_items[idx].displayName == displayName) return;
|
|
m_items[idx].displayName = displayName;
|
|
emit dataChanged(index(idx), index(idx), { DisplayNameRole });
|
|
}
|
|
|
|
void ConversationListModel::updateLastActivity(const QString& id, const QDateTime& lastActivity)
|
|
{
|
|
int idx = indexOf(id);
|
|
if (idx < 0) return;
|
|
|
|
m_items[idx].lastActivity = lastActivity;
|
|
emit dataChanged(index(idx), index(idx), { LastActivityRole });
|
|
}
|
|
|
|
void ConversationListModel::incrementUnread(const QString& id)
|
|
{
|
|
int idx = indexOf(id);
|
|
if (idx < 0) return;
|
|
|
|
m_items[idx].unreadCount++;
|
|
emit dataChanged(index(idx), index(idx), { UnreadCountRole });
|
|
}
|
|
|
|
void ConversationListModel::clearUnread(const QString& id)
|
|
{
|
|
int idx = indexOf(id);
|
|
if (idx < 0) return;
|
|
|
|
if (m_items[idx].unreadCount == 0) return;
|
|
m_items[idx].unreadCount = 0;
|
|
emit dataChanged(index(idx), index(idx), { UnreadCountRole });
|
|
}
|
|
|
|
void ConversationListModel::removeConversation(const QString& id)
|
|
{
|
|
int idx = indexOf(id);
|
|
if (idx < 0) return;
|
|
|
|
beginRemoveRows(QModelIndex(), idx, idx);
|
|
m_items.removeAt(idx);
|
|
endRemoveRows();
|
|
}
|
|
|
|
void ConversationListModel::clear()
|
|
{
|
|
if (m_items.isEmpty()) return;
|
|
beginResetModel();
|
|
m_items.clear();
|
|
endResetModel();
|
|
}
|
|
|
|
bool ConversationListModel::contains(const QString& id) const
|
|
{
|
|
return indexOf(id) >= 0;
|
|
}
|
|
|
|
int ConversationListModel::indexOf(const QString& id) const
|
|
{
|
|
for (int i = 0; i < m_items.size(); ++i) {
|
|
if (m_items[i].conversationId == id)
|
|
return i;
|
|
}
|
|
return -1;
|
|
}
|