diff options
Diffstat (limited to 'framework/src/logmodel.cpp')
-rw-r--r-- | framework/src/logmodel.cpp | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/framework/src/logmodel.cpp b/framework/src/logmodel.cpp new file mode 100644 index 00000000..c3a692dc --- /dev/null +++ b/framework/src/logmodel.cpp | |||
@@ -0,0 +1,76 @@ | |||
1 | /* | ||
2 | Copyright (c) 2018 Christian Mollekopf <mollekopf@kolabsys.com> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU Library General Public License as published by | ||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
12 | License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301, USA. | ||
18 | */ | ||
19 | |||
20 | #include "logmodel.h" | ||
21 | |||
22 | #include <QDebug> | ||
23 | #include <QDateTime> | ||
24 | #include <QStandardItem> | ||
25 | |||
26 | LogModel::LogModel(QObject *parent) | ||
27 | : QStandardItemModel(parent) | ||
28 | { | ||
29 | QByteArrayList roles{"type", "subtype", "timestamp", "message", "details", "entities", "resource"}; | ||
30 | |||
31 | int role = Qt::UserRole + 1; | ||
32 | mRoles.insert("id", role); | ||
33 | role++; | ||
34 | for (const auto &r : roles) { | ||
35 | mRoles.insert(r, role); | ||
36 | role++; | ||
37 | } | ||
38 | |||
39 | QHash<int, QByteArray> roleNames; | ||
40 | for (const auto r : mRoles.keys()) { | ||
41 | roleNames.insert(mRoles[r], r); | ||
42 | } | ||
43 | setItemRoleNames(roleNames); | ||
44 | } | ||
45 | |||
46 | LogModel::~LogModel() | ||
47 | { | ||
48 | |||
49 | } | ||
50 | |||
51 | void LogModel::insert(const QVariantMap &message) | ||
52 | { | ||
53 | |||
54 | if (rowCount() > 0) { | ||
55 | auto i = item(0); | ||
56 | const auto subtype = i->data(mRoles["subtype"]).toString(); | ||
57 | if (!subtype.isEmpty() && (subtype == message.value("subtype").toString())) { | ||
58 | //TODO merge message into this entry | ||
59 | return; | ||
60 | } | ||
61 | } | ||
62 | |||
63 | auto item = new QStandardItem; | ||
64 | auto addProperty = [&] (const QByteArray &key) { | ||
65 | item->setData(message.value(key), mRoles[key]); | ||
66 | }; | ||
67 | item->setData(QDateTime::currentDateTime(), mRoles["timestamp"]); | ||
68 | addProperty("type"); | ||
69 | addProperty("subtype"); | ||
70 | addProperty("message"); | ||
71 | addProperty("details"); | ||
72 | addProperty("resource"); | ||
73 | addProperty("entities"); | ||
74 | insertRow(0, item); | ||
75 | } | ||
76 | |||