From 8945e004e27d04ee1b4bf0b94d7dbc804c63c557 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Tue, 26 Sep 2023 16:28:53 +0200 Subject: [PATCH] Optimize room config --- src/CMakeLists.txt | 2 ++ src/neochatroom.cpp | 14 +++++------ src/roomlastmessageprovider.cpp | 38 ++++++++++++++++++++++++++++++ src/roomlastmessageprovider.h | 41 +++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 src/roomlastmessageprovider.cpp create mode 100644 src/roomlastmessageprovider.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 673cd242c..e848fc8e7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -127,6 +127,8 @@ add_library(neochat STATIC mediasizehelper.h eventhandler.cpp enums/delegatetype.h + roomlastmessageprovider.cpp + roomlastmessageprovider.h ) qt_add_qml_module(neochat URI org.kde.neochat NO_PLUGIN diff --git a/src/neochatroom.cpp b/src/neochatroom.cpp index bab0df880..779765da7 100644 --- a/src/neochatroom.cpp +++ b/src/neochatroom.cpp @@ -47,6 +47,7 @@ #include "filetransferpseudojob.h" #include "neochatconfig.h" #include "notificationsmanager.h" +#include "roomlastmessageprovider.h" #include "texthandler.h" #include "urlhelper.h" #include "utils.h" @@ -72,12 +73,10 @@ NeoChatRoom::NeoChatRoom(Connection *connection, QString roomId, JoinState joinS connect(this, &Room::aboutToAddHistoricalMessages, this, &NeoChatRoom::readMarkerLoadedChanged); - // Load cached event if available. - KConfig dataResource("data"_ls, KConfig::SimpleConfig, QStandardPaths::AppDataLocation); - KConfigGroup eventCacheGroup(&dataResource, "EventCache"_ls); + const auto &roomLastMessageProvider = RoomLastMessageProvider::self(); - if (eventCacheGroup.hasKey(id())) { - auto eventJson = QJsonDocument::fromJson(eventCacheGroup.readEntry(id(), QByteArray())).object(); + if (roomLastMessageProvider.hasKey(id())) { + auto eventJson = QJsonDocument::fromJson(roomLastMessageProvider.read(id())).object(); if (!eventJson.isEmpty()) { auto event = loadEvent(eventJson); @@ -309,11 +308,10 @@ void NeoChatRoom::cacheLastEvent() { auto event = lastEvent(); if (event != nullptr) { - KConfig dataResource("data"_ls, KConfig::SimpleConfig, QStandardPaths::AppDataLocation); - KConfigGroup eventCacheGroup(&dataResource, "EventCache"_ls); + auto &roomLastMessageProvider = RoomLastMessageProvider::self(); auto eventJson = QJsonDocument(event->fullJson()).toJson(); - eventCacheGroup.writeEntry(id(), eventJson); + roomLastMessageProvider.write(id(), eventJson); auto uniqueEvent = loadEvent(event->fullJson()); diff --git a/src/roomlastmessageprovider.cpp b/src/roomlastmessageprovider.cpp new file mode 100644 index 000000000..686473bc6 --- /dev/null +++ b/src/roomlastmessageprovider.cpp @@ -0,0 +1,38 @@ +// SPDX-FileCopyrightText: 2023 Carl Schwan +// SPDX-License-Identifier: LGPL-2.0-or-later + +#include "roomlastmessagestate.h" + +using namespace Qt::Literals::StringLiterals; + +RoomLastMessageState::RoomLastMessageState() + : m_config(KSharedConfig::openConfig(u"data"_s, KConfig::SimpleConfig, QStandardPaths::AppDataLocation)) + , m_configGroup(KConfigGroup(m_config, u"EventCache"_s)) +{ +} + +RoomLastMessageState::~RoomLastMessageState() +{ + m_config->sync(); +} + +RoomLastMessageState &RoomLastMessageState::self() +{ + static RoomLastMessageState instance; + return instance; +} + +bool RoomLastMessageState::hasKey(const QString &roomId) const +{ + return m_configGroup.hasKey(roomId); +} + +QByteArray RoomLastMessageState::read(const QString &roomId) const +{ + return m_configGroup.readEntry(roomId, QByteArray{}); +} + +void RoomLastMessageState::write(const QString &roomId, const QByteArray &json) +{ + m_configGroup.writeEntry(roomId, json); +} diff --git a/src/roomlastmessageprovider.h b/src/roomlastmessageprovider.h new file mode 100644 index 000000000..c98b03fca --- /dev/null +++ b/src/roomlastmessageprovider.h @@ -0,0 +1,41 @@ +// SPDX-FileCopyrightText: 2023 Carl Schwan +// SPDX-License-Identifier: LGPL-2.0-or-later + +#pragma once + +#include +#include + +/** + * Store and retrieve the last message of a room. + */ +class RoomLastMessageProvider +{ +public: + /** + * Get the global instance of RoomLastMessageProvider. + */ + static RoomLastMessageProvider &self(); + ~RoomLastMessageProvider(); + + /** + * Check if we have the last message content for the specified roomId. + */ + bool hasKey(const QString &roomId) const; + + /** + * Read the last message content of the specified roomId. + */ + QByteArray read(const QString &roomId) const; + + /** + * Write the last message content for the specified roomId. + */ + void write(const QString &roomId, const QByteArray &json); + +private: + RoomLastMessageProvider(); + + KSharedConfig::Ptr m_config; + KConfigGroup m_configGroup; +};