Use KSharedConfig::openStateConfig() instead of using a "data" file

This function will automatically create a "neochatstarerc" for us, and
KConfig will decide the best place for us to place our state. It won't
always be in AppDataLocation.
This commit is contained in:
Joshua Goins
2023-10-19 16:35:05 -04:00
committed by Tobias Fella
parent 26fd26f9fd
commit 83b7e7d121
5 changed files with 27 additions and 17 deletions

View File

@@ -11,12 +11,13 @@
#include <KConfig> #include <KConfig>
#include <KConfigGroup> #include <KConfigGroup>
#include <KSharedConfig>
ServerListModel::ServerListModel(QObject *parent) ServerListModel::ServerListModel(QObject *parent)
: QAbstractListModel(parent) : QAbstractListModel(parent)
{ {
KConfig dataResource(QStringLiteral("data"), KConfig::SimpleConfig, QStandardPaths::AppDataLocation); const auto stateConfig = KSharedConfig::openStateConfig();
KConfigGroup serverGroup(&dataResource, QStringLiteral("Servers")); const KConfigGroup serverGroup = stateConfig->group(QStringLiteral("Servers"));
QString domain = Controller::instance().activeConnection()->domain(); QString domain = Controller::instance().activeConnection()->domain();
@@ -91,8 +92,8 @@ int ServerListModel::rowCount(const QModelIndex &parent) const
void ServerListModel::checkServer(const QString &url) void ServerListModel::checkServer(const QString &url)
{ {
KConfig dataResource(QStringLiteral("data"), KConfig::SimpleConfig, QStandardPaths::AppDataLocation); const auto stateConfig = KSharedConfig::openStateConfig();
KConfigGroup serverGroup(&dataResource, QStringLiteral("Servers")); const KConfigGroup serverGroup = stateConfig->group(QStringLiteral("Servers"));
if (!serverGroup.hasKey(url)) { if (!serverGroup.hasKey(url)) {
if (Quotient::isJobPending(m_checkServerJob)) { if (Quotient::isJobPending(m_checkServerJob)) {
@@ -108,8 +109,8 @@ void ServerListModel::checkServer(const QString &url)
void ServerListModel::addServer(const QString &url) void ServerListModel::addServer(const QString &url)
{ {
KConfig dataResource(QStringLiteral("data"), KConfig::SimpleConfig, QStandardPaths::AppDataLocation); const auto stateConfig = KSharedConfig::openStateConfig();
KConfigGroup serverGroup(&dataResource, QStringLiteral("Servers")); KConfigGroup serverGroup = stateConfig->group(QStringLiteral("Servers"));
if (!serverGroup.hasKey(url)) { if (!serverGroup.hasKey(url)) {
Server newServer = Server{ Server newServer = Server{
@@ -125,17 +126,21 @@ void ServerListModel::addServer(const QString &url)
} }
serverGroup.writeEntry(url, url); serverGroup.writeEntry(url, url);
stateConfig->sync();
} }
void ServerListModel::removeServerAtIndex(int row) void ServerListModel::removeServerAtIndex(int row)
{ {
KConfig dataResource(QStringLiteral("data"), KConfig::SimpleConfig, QStandardPaths::AppDataLocation); const auto stateConfig = KSharedConfig::openStateConfig();
KConfigGroup serverGroup(&dataResource, QStringLiteral("Servers")); KConfigGroup serverGroup = stateConfig->group(QStringLiteral("Servers"));
serverGroup.deleteEntry(data(index(row), UrlRole).toString()); serverGroup.deleteEntry(data(index(row), UrlRole).toString());
beginRemoveRows(QModelIndex(), row, row); beginRemoveRows(QModelIndex(), row, row);
m_servers.removeAt(row); m_servers.removeAt(row);
endRemoveRows(); endRemoveRows();
stateConfig->sync();
} }
QHash<int, QByteArray> ServerListModel::roleNames() const QHash<int, QByteArray> ServerListModel::roleNames() const

View File

@@ -6,7 +6,7 @@
using namespace Qt::Literals::StringLiterals; using namespace Qt::Literals::StringLiterals;
RoomLastMessageProvider::RoomLastMessageProvider() RoomLastMessageProvider::RoomLastMessageProvider()
: m_config(KSharedConfig::openConfig(u"data"_s, KConfig::SimpleConfig, QStandardPaths::AppDataLocation)) : m_config(KSharedConfig::openStateConfig())
, m_configGroup(KConfigGroup(m_config, u"EventCache"_s)) , m_configGroup(KConfigGroup(m_config, u"EventCache"_s))
{ {
} }

View File

@@ -28,12 +28,12 @@ RoomManager::RoomManager(QObject *parent)
: QObject(parent) : QObject(parent)
, m_currentRoom(nullptr) , m_currentRoom(nullptr)
, m_lastCurrentRoom(nullptr) , m_lastCurrentRoom(nullptr)
, m_config(KConfig(QStringLiteral("data"), KConfig::SimpleConfig, QStandardPaths::AppDataLocation)) , m_config(KSharedConfig::openStateConfig())
, m_messageEventModel(new MessageEventModel(this)) , m_messageEventModel(new MessageEventModel(this))
, m_messageFilterModel(new MessageFilterModel(this, m_messageEventModel)) , m_messageFilterModel(new MessageFilterModel(this, m_messageEventModel))
, m_mediaMessageFilterModel(new MediaMessageFilterModel(this, m_messageFilterModel)) , m_mediaMessageFilterModel(new MediaMessageFilterModel(this, m_messageFilterModel))
{ {
m_lastRoomConfig = m_config.group(QStringLiteral("LastOpenRoom")); m_lastRoomConfig = m_config->group(QStringLiteral("LastOpenRoom"));
connect(this, &RoomManager::currentRoomChanged, this, [this]() { connect(this, &RoomManager::currentRoomChanged, this, [this]() {
m_messageEventModel->setRoom(m_currentRoom); m_messageEventModel->setRoom(m_currentRoom);

View File

@@ -3,8 +3,8 @@
#pragma once #pragma once
#include <KConfig>
#include <KConfigGroup> #include <KConfigGroup>
#include <KSharedConfig>
#include <QObject> #include <QObject>
#include <QQmlEngine> #include <QQmlEngine>
#include <Quotient/room.h> #include <Quotient/room.h>
@@ -371,7 +371,7 @@ private:
NeoChatRoom *m_currentRoom; NeoChatRoom *m_currentRoom;
NeoChatRoom *m_lastCurrentRoom; NeoChatRoom *m_lastCurrentRoom;
QString m_arg; QString m_arg;
KConfig m_config; KSharedConfig::Ptr m_config;
KConfigGroup m_lastRoomConfig; KConfigGroup m_lastRoomConfig;
QPointer<ChatDocumentHandler> m_chatDocumentHandler; QPointer<ChatDocumentHandler> m_chatDocumentHandler;

View File

@@ -13,6 +13,7 @@
#include <KWindowSystem> #include <KWindowSystem>
#endif #endif
#include <KSharedConfig>
#include <QStandardPaths> #include <QStandardPaths>
WindowController &WindowController::instance() WindowController &WindowController::instance()
@@ -35,18 +36,22 @@ QWindow *WindowController::window() const
void WindowController::restoreGeometry() void WindowController::restoreGeometry()
{ {
KConfig dataResource(QStringLiteral("data"), KConfig::SimpleConfig, QStandardPaths::AppDataLocation); const auto stateConfig = KSharedConfig::openStateConfig();
KConfigGroup windowGroup(&dataResource, QStringLiteral("Window")); const KConfigGroup windowGroup = stateConfig->group(QStringLiteral("Window"));
KWindowConfig::restoreWindowSize(m_window, windowGroup); KWindowConfig::restoreWindowSize(m_window, windowGroup);
KWindowConfig::restoreWindowPosition(m_window, windowGroup); KWindowConfig::restoreWindowPosition(m_window, windowGroup);
} }
void WindowController::saveGeometry() void WindowController::saveGeometry()
{ {
KConfig dataResource(QStringLiteral("data"), KConfig::SimpleConfig, QStandardPaths::AppDataLocation); const auto stateConfig = KSharedConfig::openStateConfig();
KConfigGroup windowGroup(&dataResource, QStringLiteral("Window")); KConfigGroup windowGroup = stateConfig->group(QStringLiteral("Window"));
KWindowConfig::saveWindowPosition(m_window, windowGroup); KWindowConfig::saveWindowPosition(m_window, windowGroup);
KWindowConfig::saveWindowSize(m_window, windowGroup); KWindowConfig::saveWindowSize(m_window, windowGroup);
stateConfig->sync();
} }
void WindowController::showAndRaiseWindow(const QString &startupId) void WindowController::showAndRaiseWindow(const QString &startupId)