/** * @RDE_HEADER@ * The MessageModel class handles message related data * $Id$ * $HeadURL$ * $Author$ * Created by Hannes Fritsche, SLSE, 2017-09-06 */ #include "message_model.h" #include #include #include MessageModel::MessageModel(QObject *parent) : LtsAbstractTableModel( { tr("Time"), tr("Direction"), tr("Type"), tr("UUID"), tr("Data") }, parent) { setData(QVector()); } void MessageModel::addMessage(MessageModel::MessageDirection direction, const QString &message) { QJsonParseError parseError; QString uuid = QLatin1String("-"); QString data = uuid; QJsonDocument doc = QJsonDocument::fromJson(message.toLatin1(), &parseError); QString type; if (parseError.error != QJsonParseError::NoError) type = parseError.errorString(); else if (!doc.isObject()) type = QLatin1String("invalid"); else { QJsonObject obj = doc.object(); type = obj.value(QLatin1String("type")).toString(QLatin1String("not present")); uuid = obj.value(QLatin1String("uuid")).toString(QLatin1String("not present")); QJsonObject dataObj = obj.value(QLatin1String("data")).toObject(); if (dataObj.isEmpty()) data = QLatin1String("not present"); else data = QString::fromUtf8(QJsonDocument(dataObj).toJson()); } addMessage(direction, type, uuid, data); } void MessageModel::addMessage(MessageModel::MessageDirection direction, const QString &type, const QString &uuid, const QString &data) { int32_t rows = rowCount(); onDataChanged({rows, toString(direction), type, uuid, data}, DataModification::eAdded); } QVariant MessageModel::data(const QModelIndex &index, int role) const { if (role != Qt::DisplayRole || !indexIsValid(index)) return QVariant(); const MessageInfo &infoData = m_vecData.at(index.row()); switch (index.column()) { case TIME: return infoData.time.toString(); case DIRECTION: return infoData.direction; case TYPE: return infoData.type; case UUID: return infoData.uuid; case DATA: { if (!infoData.data.size()) return infoData.data; QJsonObject obj = QJsonDocument::fromJson(infoData.data.toLatin1()).object(); QString result = infoData.data; const QStringList keys = obj.keys(); for (const QString ¤tKey : keys) { const QJsonValue subObj = obj.value(currentKey); if (subObj.isArray()) result.prepend(QStringLiteral("%1 size: %2\n").arg(currentKey).arg(subObj.toArray().size())); } return result; } } return QVariant(); } QString MessageModel::toString(MessageModel::MessageDirection direction) const { switch (direction) { case TO_PROXY: return tr(" > to Proxy"); case FROM_PROXY: return tr(" < from Proxy"); default: return QString(); } }