File indexing completed on 2025-01-19 04:52:04

0001 /*
0002     Copyright (c) 2018 Christian Mollekopf <mollekopf@kolabsys.com>
0003 
0004     This library is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to the
0016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017     02110-1301, USA.
0018 */
0019 
0020 #include "logmodel.h"
0021 
0022 #include <QDebug>
0023 #include <QDateTime>
0024 #include <QStandardItem>
0025 
0026 LogModel::LogModel(QObject *parent)
0027     : QStandardItemModel(parent)
0028 {
0029     QByteArrayList roles{"type", "subtype", "timestamp", "message", "details", "entities", "resource"};
0030 
0031     int role = Qt::UserRole + 1;
0032     mRoles.insert("id", role);
0033     role++;
0034     for (const auto &r : roles) {
0035         mRoles.insert(r, role);
0036         role++;
0037     }
0038 
0039     QHash<int, QByteArray> roleNames;
0040     for (const auto &r : mRoles.keys()) {
0041         roleNames.insert(mRoles[r], r);
0042     }
0043     setItemRoleNames(roleNames);
0044 }
0045 
0046 LogModel::~LogModel()
0047 {
0048 
0049 }
0050 
0051 void LogModel::insert(const QVariantMap &message)
0052 {
0053 
0054     if (rowCount() > 0) {
0055         auto i = item(0);
0056         const auto subtype = i->data(mRoles["subtype"]).toString();
0057         if (!subtype.isEmpty() && (subtype == message.value("subtype").toString())) {
0058             //TODO merge message into this entry
0059             return;
0060         }
0061     }
0062 
0063     auto item = new QStandardItem;
0064     auto addProperty = [&] (const QByteArray &key) {
0065         item->setData(message.value(key), mRoles[key]);
0066     };
0067     item->setData(QDateTime::currentDateTime(), mRoles["timestamp"]);
0068     addProperty("type");
0069     addProperty("subtype");
0070     addProperty("message");
0071     addProperty("details");
0072     addProperty("resource");
0073     addProperty("entities");
0074     insertRow(0, item);
0075     emit entryAdded(message);
0076 }
0077