Optimize room config
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<RoomEvent>(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<RoomEvent>(event->fullJson());
|
||||
|
||||
|
||||
38
src/roomlastmessageprovider.cpp
Normal file
38
src/roomlastmessageprovider.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
// SPDX-FileCopyrightText: 2023 Carl Schwan <carl@carlschwan.eu>
|
||||
// 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);
|
||||
}
|
||||
41
src/roomlastmessageprovider.h
Normal file
41
src/roomlastmessageprovider.h
Normal file
@@ -0,0 +1,41 @@
|
||||
// SPDX-FileCopyrightText: 2023 Carl Schwan <carl@carlschwan.eu>
|
||||
// SPDX-License-Identifier: LGPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <KConfigGroup>
|
||||
#include <KSharedConfig>
|
||||
|
||||
/**
|
||||
* 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;
|
||||
};
|
||||
Reference in New Issue
Block a user