From a69789502feb0235bddad0cf3cb9ed9ca7554632 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 4 Jul 2018 09:59:58 +0200 Subject: Introduced a logmodel To get rid of weird problems of lists converting to qmllistmodels. I'm relatively sure some crashes I've seen were related to this. --- framework/src/CMakeLists.txt | 1 + framework/src/domain/maillistmodel.cpp | 9 ++++ framework/src/frameworkplugin.cpp | 2 + framework/src/logmodel.cpp | 76 ++++++++++++++++++++++++++++++++++ framework/src/logmodel.h | 41 ++++++++++++++++++ 5 files changed, 129 insertions(+) create mode 100644 framework/src/logmodel.cpp create mode 100644 framework/src/logmodel.h (limited to 'framework/src') diff --git a/framework/src/CMakeLists.txt b/framework/src/CMakeLists.txt index 5ad5e910..901ec4a3 100644 --- a/framework/src/CMakeLists.txt +++ b/framework/src/CMakeLists.txt @@ -54,6 +54,7 @@ add_library(kubeframework SHARED extensionmodel.cpp viewhighlighter.cpp file.cpp + logmodel.cpp ) generate_export_header(kubeframework BASE_NAME Kube EXPORT_FILE_NAME kube_export.h) set_target_properties(kubeframework PROPERTIES diff --git a/framework/src/domain/maillistmodel.cpp b/framework/src/domain/maillistmodel.cpp index 5936c792..152b4f23 100644 --- a/framework/src/domain/maillistmodel.cpp +++ b/framework/src/domain/maillistmodel.cpp @@ -406,6 +406,15 @@ bool MailListModel::showInbox() const void MailListModel::setEntityId(const QString &id) { qDebug() << "Running mail query for mail with ID:" << id; + if (id.isEmpty()) { + mCurrentQueryItem.clear(); + setSourceModel(nullptr); + return; + } + if (mCurrentQueryItem == id) { + return; + } + mCurrentQueryItem = id.toLatin1(); using namespace Sink::ApplicationDomain; Sink::Query query; query.setFlags(Sink::Query::LiveQuery); diff --git a/framework/src/frameworkplugin.cpp b/framework/src/frameworkplugin.cpp index b5635733..9c81b7e1 100644 --- a/framework/src/frameworkplugin.cpp +++ b/framework/src/frameworkplugin.cpp @@ -46,6 +46,7 @@ #include "extensionmodel.h" #include "viewhighlighter.h" #include "file.h" +#include "logmodel.h" #include #include @@ -178,6 +179,7 @@ void FrameworkPlugin::registerTypes (const char *uri) qmlRegisterType(uri, 1, 0,"ContactController"); qmlRegisterType(uri, 1, 0,"PeopleModel"); qmlRegisterType(uri, 1, 0, "TextDocumentHandler"); + qmlRegisterType(uri, 1, 0, "LogModel"); qmlRegisterType(uri, 1, 0, "AccountFactory"); qmlRegisterType(uri, 1, 0, "AccountsModel"); 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 @@ +/* + Copyright (c) 2018 Christian Mollekopf + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#include "logmodel.h" + +#include +#include +#include + +LogModel::LogModel(QObject *parent) + : QStandardItemModel(parent) +{ + QByteArrayList roles{"type", "subtype", "timestamp", "message", "details", "entities", "resource"}; + + int role = Qt::UserRole + 1; + mRoles.insert("id", role); + role++; + for (const auto &r : roles) { + mRoles.insert(r, role); + role++; + } + + QHash roleNames; + for (const auto r : mRoles.keys()) { + roleNames.insert(mRoles[r], r); + } + setItemRoleNames(roleNames); +} + +LogModel::~LogModel() +{ + +} + +void LogModel::insert(const QVariantMap &message) +{ + + if (rowCount() > 0) { + auto i = item(0); + const auto subtype = i->data(mRoles["subtype"]).toString(); + if (!subtype.isEmpty() && (subtype == message.value("subtype").toString())) { + //TODO merge message into this entry + return; + } + } + + auto item = new QStandardItem; + auto addProperty = [&] (const QByteArray &key) { + item->setData(message.value(key), mRoles[key]); + }; + item->setData(QDateTime::currentDateTime(), mRoles["timestamp"]); + addProperty("type"); + addProperty("subtype"); + addProperty("message"); + addProperty("details"); + addProperty("resource"); + addProperty("entities"); + insertRow(0, item); +} + diff --git a/framework/src/logmodel.h b/framework/src/logmodel.h new file mode 100644 index 00000000..73909e87 --- /dev/null +++ b/framework/src/logmodel.h @@ -0,0 +1,41 @@ +/* + Copyright (c) 2016 Michael Bohlender + Copyright (c) 2016 Christian Mollekopf + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#pragma once +#include "kube_export.h" + +#include +#include +#include + +class KUBE_EXPORT LogModel : public QStandardItemModel +{ + Q_OBJECT + +public: + LogModel(QObject *parent = Q_NULLPTR); + ~LogModel(); + + Q_INVOKABLE void insert(const QVariantMap &); + +private: + // QSharedPointer mListener; + QHash mRoles; +}; -- cgit v1.2.3