aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2018-03-05 10:16:20 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2018-03-05 16:14:43 +0900
commit6ea1fb621488910de055bd3af4d00343a763541a (patch)
treee371dc72e16329623f5468eaf85ac394030f1a52
parent9057fc02b06bdd3e38e9cf39e68287e02d58596b (diff)
downloadlibquotient-6ea1fb621488910de055bd3af4d00343a763541a.tar.gz
libquotient-6ea1fb621488910de055bd3af4d00343a763541a.zip
ReadMarkerEvent; TagEvent remade with less boilerplate code
tagevent.h -> accountdataevents.h now has a macro to define more simplistic events along the lines of simplestateevents.h but inheriting from Event instead. TagEvent and ReadMarkerEvent(m.fully_read) are defined using this macro. ReadMarkerEvent is also wired through event.* (but not further yet).
-rw-r--r--CMakeLists.txt1
-rw-r--r--events/accountdataevents.h78
-rw-r--r--events/event.cpp5
-rw-r--r--events/event.h2
-rw-r--r--events/tagevent.cpp71
-rw-r--r--events/tagevent.h74
-rw-r--r--libqmatrixclient.pri3
-rw-r--r--room.cpp36
-rw-r--r--room.h10
9 files changed, 112 insertions, 168 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d7762e17..e95c72d0 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -60,7 +60,6 @@ set(libqmatrixclient_SRCS
events/roomavatarevent.cpp
events/typingevent.cpp
events/receiptevent.cpp
- events/tagevent.cpp
jobs/requestdata.cpp
jobs/basejob.cpp
jobs/checkauthmethods.cpp
diff --git a/events/accountdataevents.h b/events/accountdataevents.h
new file mode 100644
index 00000000..78cf9c46
--- /dev/null
+++ b/events/accountdataevents.h
@@ -0,0 +1,78 @@
+#include <utility>
+
+/******************************************************************************
+ * Copyright (C) 2018 Kitsune Ral <kitsune-ral@users.sf.net>
+ *
+ * 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
+ */
+
+#pragma once
+
+#include "event.h"
+#include "eventcontent.h"
+
+namespace QMatrixClient
+{
+ static constexpr const char* FavouriteTag = "m.favourite";
+ static constexpr const char* LowPriorityTag = "m.lowpriority";
+
+ struct TagRecord
+ {
+ TagRecord (QString order) : order(std::move(order)) { }
+ explicit TagRecord(const QJsonValue& jv = {})
+ : order(jv.toObject().value("order").toString())
+ { }
+
+ QString order;
+
+ bool operator==(const TagRecord& other) const
+ { return order == other.order; }
+ bool operator!=(const TagRecord& other) const
+ { return !operator==(other); }
+ };
+
+ inline QJsonValue toJson(const TagRecord& rec)
+ {
+ return QJsonObject {{ QStringLiteral("order"), rec.order }};
+ }
+
+ using TagsMap = QHash<QString, TagRecord>;
+
+#define DEFINE_SIMPLE_EVENT(_Name, _TypeId, _EnumType, _ContentType, _ContentKey) \
+ class _Name : public Event \
+ { \
+ public: \
+ static constexpr const char* TypeId = _TypeId; \
+ static const char* typeId() { return TypeId; } \
+ explicit _Name(const QJsonObject& obj) \
+ : Event((_EnumType), obj) \
+ , _content(contentJson(), QStringLiteral(#_ContentKey)) \
+ { } \
+ template <typename... Ts> \
+ explicit _Name(Ts&&... contentArgs) \
+ : Event(_EnumType) \
+ , _content(QStringLiteral(#_ContentKey), \
+ std::forward<Ts>(contentArgs)...) \
+ { } \
+ const _ContentType& _ContentKey() const { return _content.value; } \
+ QJsonObject toJson() const { return _content.toJson(); } \
+ protected: \
+ EventContent::SimpleContent<_ContentType> _content; \
+ };
+
+ DEFINE_SIMPLE_EVENT(TagEvent, "m.tag", EventType::Tag, TagsMap, tags)
+ DEFINE_SIMPLE_EVENT(ReadMarkerEvent, "m.fully_read", EventType::ReadMarker,
+ QString, event_id)
+}
diff --git a/events/event.cpp b/events/event.cpp
index 74a2c3d7..f3e965e2 100644
--- a/events/event.cpp
+++ b/events/event.cpp
@@ -24,7 +24,7 @@
#include "roomavatarevent.h"
#include "typingevent.h"
#include "receiptevent.h"
-#include "tagevent.h"
+#include "accountdataevents.h"
#include "redactionevent.h"
#include "logging.h"
@@ -88,7 +88,8 @@ EventPtr _impl::doMakeEvent<Event>(const QJsonObject& obj)
return EventPtr(move(e));
return EventPtr { makeIfMatches<Event,
- TypingEvent, ReceiptEvent, TagEvent>(obj, obj["type"].toString()) };
+ TypingEvent, ReceiptEvent, TagEvent, ReadMarkerEvent>(
+ obj, obj["type"].toString()) };
}
RoomEvent::RoomEvent(Event::Type type) : Event(type) { }
diff --git a/events/event.h b/events/event.h
index f0ca2d15..eccfec41 100644
--- a/events/event.h
+++ b/events/event.h
@@ -45,7 +45,7 @@ namespace QMatrixClient
enum class Type : quint16
{
Unknown = 0,
- Typing, Receipt, Tag, DirectChat,
+ Typing, Receipt, Tag, DirectChat, ReadMarker,
RoomEventBase = 0x1000,
RoomMessage = RoomEventBase + 1,
RoomEncryptedMessage, Redaction,
diff --git a/events/tagevent.cpp b/events/tagevent.cpp
deleted file mode 100644
index c643ac62..00000000
--- a/events/tagevent.cpp
+++ /dev/null
@@ -1,71 +0,0 @@
-/******************************************************************************
- * Copyright (C) 2018 Kitsune Ral <kitsune-ral@users.sf.net>
- *
- * 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
- */
-
-#include "tagevent.h"
-
-using namespace QMatrixClient;
-
-TagRecord::TagRecord(const QJsonObject& json)
- : order(json.value("order").toString())
-{ }
-
-TagEvent::TagEvent()
- : Event(Type::Tag)
-{
- // TODO: Support getting a list of tags and saving it
-}
-
-TagEvent::TagEvent(const QJsonObject& obj)
- : Event(Type::Tag, obj)
-{
- Q_ASSERT(obj["type"].toString() == TypeId);
-}
-
-QStringList TagEvent::tagNames() const
-{
- return tagsObject().keys();
-}
-
-QHash<QString, TagRecord> TagEvent::tags() const
-{
- QHash<QString, TagRecord> result;
- auto allTags = tagsObject();
- for (auto it = allTags.begin(); it != allTags.end(); ++ it)
- result.insert(it.key(), TagRecord(it.value().toObject()));
- return result;
-}
-
-bool TagEvent::empty() const
-{
- return tagsObject().empty();
-}
-
-bool TagEvent::contains(const QString& name) const
-{
- return tagsObject().contains(name);
-}
-
-TagRecord TagEvent::recordForTag(const QString& name) const
-{
- return TagRecord(tagsObject().value(name).toObject());
-}
-
-QJsonObject TagEvent::tagsObject() const
-{
- return contentJson().value("tags").toObject();
-}
diff --git a/events/tagevent.h b/events/tagevent.h
deleted file mode 100644
index 26fe8788..00000000
--- a/events/tagevent.h
+++ /dev/null
@@ -1,74 +0,0 @@
-/******************************************************************************
- * Copyright (C) 2018 Kitsune Ral <kitsune-ral@users.sf.net>
- *
- * 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
- */
-
-#pragma once
-
-#include "event.h"
-
-namespace QMatrixClient
-{
- static constexpr const char* FavouriteTag = "m.favourite";
- static constexpr const char* LowPriorityTag = "m.lowpriority";
-
- struct TagRecord
- {
- explicit TagRecord(const QJsonObject& json = {});
-
- QString order;
- };
-
- class TagEvent : public Event
- {
- public:
- TagEvent();
- explicit TagEvent(const QJsonObject& obj);
-
- /** Get the list of tag names */
- QStringList tagNames() const;
-
- /** Get the list of tags along with information on each */
- QHash<QString, TagRecord> tags() const;
-
- /** Check if the event lists no tags */
- bool empty() const;
-
- /** Check whether the tags list contains the specified name */
- bool contains(const QString& name) const;
-
- /** Get the record for the given tag name */
- TagRecord recordForTag(const QString& name) const;
-
- /** Get the whole tags content as a JSON object
- * It's NOT recommended to use this method directly from client code.
- * Use other convenience methods provided by the class.
- */
- QJsonObject tagsObject() const;
-
- static constexpr const char * TypeId = "m.tag";
- };
-
- using TagEventPtr = event_ptr_tt<TagEvent>;
-
- inline QJsonValue toJson(const TagEventPtr& tagEvent)
- {
- return QJsonObject {{ "type", "m.tag" },
- // TODO: Replace tagsObject() with a genuine list of tags
- // (or make the needed JSON upon TagEvent creation)
- { "content", QJsonObject {{ "tags", tagEvent->tagsObject() }} }};
- }
-}
diff --git a/libqmatrixclient.pri b/libqmatrixclient.pri
index 7cfa94a1..c7b95617 100644
--- a/libqmatrixclient.pri
+++ b/libqmatrixclient.pri
@@ -24,7 +24,7 @@ HEADERS += \
$$PWD/events/roomavatarevent.h \
$$PWD/events/typingevent.h \
$$PWD/events/receiptevent.h \
- $$PWD/events/tagevent.h \
+ $$PWD/events/accountdataevents.h \
$$PWD/events/redactionevent.h \
$$PWD/jobs/requestdata.h \
$$PWD/jobs/basejob.h \
@@ -56,7 +56,6 @@ SOURCES += \
$$PWD/events/roommemberevent.cpp \
$$PWD/events/typingevent.cpp \
$$PWD/events/receiptevent.cpp \
- $$PWD/events/tagevent.cpp \
$$PWD/jobs/requestdata.cpp \
$$PWD/jobs/basejob.cpp \
$$PWD/jobs/checkauthmethods.cpp \
diff --git a/room.cpp b/room.cpp
index e03a2b5b..971e4121 100644
--- a/room.cpp
+++ b/room.cpp
@@ -65,7 +65,6 @@ enum EventsPlacement : int { Older = -1, Newer = 1 };
# define WORKAROUND_EXTENDED_INITIALIZER_LIST
#endif
-
class Room::Private
{
public:
@@ -106,7 +105,7 @@ class Room::Private
QString firstDisplayedEventId;
QString lastDisplayedEventId;
QHash<const User*, QString> lastReadEventIds;
- TagEventPtr tags = std::make_unique<TagEvent>();
+ TagsMap tags;
QHash<QString, QVariantHash> accountData;
QString prevBatch;
QPointer<RoomMessagesJob> roomMessagesJob;
@@ -574,27 +573,36 @@ void Room::resetHighlightCount()
QStringList Room::tagNames() const
{
- return d->tags->tagNames();
+ return d->tags.keys();
}
-QHash<QString, TagRecord> Room::tags() const
+TagsMap Room::tags() const
{
- return d->tags->tags();
+ return d->tags;
}
TagRecord Room::tag(const QString& name) const
{
- return d->tags->recordForTag(name);
+ return d->tags.value(name);
+}
+
+void Room::setTags(const TagsMap& newTags)
+{
+ if (newTags == d->tags)
+ return;
+ d->tags = newTags;
+ d->setAccountData(TagEvent(d->tags));
+ emit tagsChanged();
}
bool Room::isFavourite() const
{
- return d->tags->contains(FavouriteTag);
+ return d->tags.contains(FavouriteTag);
}
bool Room::isLowPriority() const
{
- return d->tags->contains(LowPriorityTag);
+ return d->tags.contains(LowPriorityTag);
}
const RoomMessageEvent*
@@ -1485,13 +1493,19 @@ void Room::processAccountDataEvent(EventPtr event)
switch (event->type())
{
case EventType::Tag:
- d->tags.reset(static_cast<TagEvent*>(event.release()));
+ {
+ auto newTags = static_cast<TagEvent*>(event.get())->tags();
+ if (newTags == d->tags)
+ break;
+ d->tags = newTags;
qCDebug(MAIN) << "Room" << id() << "is tagged with: "
<< tagNames().join(", ");
emit tagsChanged();
break;
+ }
default:
- d->accountData[event->jsonType()] = event->contentJson().toVariantHash();
+ d->accountData[event->jsonType()] =
+ event->contentJson().toVariantHash();
}
}
@@ -1644,7 +1658,7 @@ QJsonObject Room::Private::toJson() const
}
QJsonArray accountDataEvents;
- if (!tags->empty())
+ if (!tags.empty())
accountDataEvents.append(QMatrixClient::toJson(tags));
if (!accountData.empty())
diff --git a/room.h b/room.h
index 71d5c433..bdd11452 100644
--- a/room.h
+++ b/room.h
@@ -20,13 +20,9 @@
#include "jobs/syncjob.h"
#include "events/roommessageevent.h"
-#include "events/tagevent.h"
+#include "events/accountdataevents.h"
#include "joinstate.h"
-#include <QtCore/QList>
-#include <QtCore/QStringList>
-#include <QtCore/QObject>
-#include <QtCore/QJsonObject>
#include <QtGui/QPixmap>
#include <memory>
@@ -241,9 +237,11 @@ namespace QMatrixClient
Q_INVOKABLE void resetHighlightCount();
QStringList tagNames() const;
- QHash<QString, TagRecord> tags() const;
+ TagsMap tags() const;
TagRecord tag(const QString& name) const;
+ void setTags(const TagsMap& newTags);
+
/** Check whether the list of tags has m.favourite */
bool isFavourite() const;
/** Check whether the list of tags has m.lowpriority */