aboutsummaryrefslogtreecommitdiff
path: root/lib/events/event.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/events/event.cpp')
-rw-r--r--lib/events/event.cpp100
1 files changed, 52 insertions, 48 deletions
diff --git a/lib/events/event.cpp b/lib/events/event.cpp
index fd6e3939..da7de919 100644
--- a/lib/events/event.cpp
+++ b/lib/events/event.cpp
@@ -1,72 +1,67 @@
-/******************************************************************************
- * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 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
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
+// SPDX-FileCopyrightText: 2016 Kitsune Ral <Kitsune-Ral@users.sf.net>
+// SPDX-License-Identifier: LGPL-2.1-or-later
#include "event.h"
+#include "callevents.h"
#include "logging.h"
+#include "stateevent.h"
#include <QtCore/QJsonDocument>
-using namespace QMatrixClient;
+using namespace Quotient;
-event_type_t EventTypeRegistry::initializeTypeId(event_mtype_t matrixTypeId)
-{
- const auto id = get().eventTypes.size();
- get().eventTypes.push_back(matrixTypeId);
- if (strncmp(matrixTypeId, "", 1) == 0)
- qDebug(EVENTS) << "Initialized unknown event type with id" << id;
- else
- qDebug(EVENTS) << "Initialized event type" << matrixTypeId
- << "with id" << id;
- return id;
-}
+QString EventTypeRegistry::getMatrixType(event_type_t typeId) { return typeId; }
-QString EventTypeRegistry::getMatrixType(event_type_t typeId)
+void AbstractEventMetaType::addDerived(AbstractEventMetaType* newType)
{
- return typeId < get().eventTypes.size() ? get().eventTypes[typeId] : "";
+ if (const auto existing =
+ std::find_if(derivedTypes.cbegin(), derivedTypes.cend(),
+ [&newType](const AbstractEventMetaType* t) {
+ return t->matrixId == newType->matrixId;
+ });
+ existing != derivedTypes.cend())
+ {
+ if (*existing == newType)
+ return;
+ // Two different metatype objects claim the same Matrix type id; this
+ // is not normal, so give as much information as possible to diagnose
+ if ((*existing)->className == newType->className) {
+ qCritical(EVENTS)
+ << newType->className << "claims" << newType->matrixId
+ << "repeatedly; check that it's exported across translation "
+ "units or shared objects";
+ Q_ASSERT(false); // That situation is plain wrong
+ return; // So maybe std::terminate() even?
+ }
+ qWarning(EVENTS).nospace()
+ << newType->matrixId << " is already mapped to "
+ << (*existing)->className << " before " << newType->className
+ << "; unless the two have different isValid() conditions, the "
+ "latter class will never be used";
+ }
+ derivedTypes.emplace_back(newType);
+ qDebug(EVENTS).nospace()
+ << newType->matrixId << " -> " << newType->className << "; "
+ << derivedTypes.size() << " derived type(s) registered for "
+ << className;
}
-Event::Event(Type type, const QJsonObject& json)
- : _type(type), _json(json)
+Event::Event(const QJsonObject& json)
+ : _json(json)
{
- if (!json.contains(ContentKeyL) &&
- !json.value(UnsignedKeyL).toObject().contains(RedactedCauseKeyL))
- {
+ if (!json.contains(ContentKeyL)
+ && !json.value(UnsignedKeyL).toObject().contains(RedactedCauseKeyL)) {
qCWarning(EVENTS) << "Event without 'content' node";
qCWarning(EVENTS) << formatJson << json;
}
}
-Event::Event(Type type, event_mtype_t matrixType, const QJsonObject& contentJson)
- : Event(type, basicEventJson(matrixType, contentJson))
-{ }
-
Event::~Event() = default;
-QString Event::matrixType() const
-{
- return fullJson()[TypeKeyL].toString();
-}
+QString Event::matrixType() const { return fullJson()[TypeKeyL].toString(); }
-QByteArray Event::originalJson() const
-{
- return QJsonDocument(_json).toJson();
-}
+QByteArray Event::originalJson() const { return QJsonDocument(_json).toJson(); }
const QJsonObject Event::contentJson() const
{
@@ -77,3 +72,12 @@ const QJsonObject Event::unsignedJson() const
{
return fullJson()[UnsignedKeyL].toObject();
}
+
+bool Event::isStateEvent() const { return is<StateEvent>(); }
+
+bool Event::isCallEvent() const { return is<CallEvent>(); }
+
+void Event::dumpTo(QDebug dbg) const
+{
+ dbg << QJsonDocument(contentJson()).toJson(QJsonDocument::Compact);
+}