Rework NeoChatConnection to remove the dependency on NeoChatConfig

Title
This commit is contained in:
James Graham
2025-04-05 07:53:26 +00:00
parent 73623ec41a
commit d9b8b1c8ef
5 changed files with 40 additions and 32 deletions

View File

@@ -4,6 +4,7 @@
#include "controller.h"
#include <Quotient/connection.h>
#include <qt6keychain/keychain.h>
#include <KLocalizedString>
@@ -50,6 +51,17 @@ Controller::Controller(QObject *parent)
{
Connection::setRoomType<NeoChatRoom>();
Connection::setDirectChatEncryptionDefault(NeoChatConfig::preferUsingEncryption());
connect(NeoChatConfig::self(), &NeoChatConfig::PreferUsingEncryptionChanged, this, [] {
Connection::setDirectChatEncryptionDefault(NeoChatConfig::preferUsingEncryption());
});
NeoChatConnection::setGlobalUrlPreviewDefault(NeoChatConfig::showLinkPreview());
connect(NeoChatConfig::self(), &NeoChatConfig::ShowLinkPreviewChanged, this, [this] {
NeoChatConnection::setGlobalUrlPreviewDefault(NeoChatConfig::showLinkPreview());
Q_EMIT globalUrlPreviewDefaultChanged();
});
ProxyController::instance().setApplicationProxy();
#ifndef Q_OS_ANDROID
@@ -168,6 +180,7 @@ void Controller::addConnection(NeoChatConnection *c)
connect(c, &NeoChatConnection::syncDone, this, [this, c]() {
m_notificationsManager.handleNotifications(c);
});
connect(this, &Controller::globalUrlPreviewDefaultChanged, c, &NeoChatConnection::globalUrlPreviewEnabledChanged);
c->sync();

View File

@@ -142,4 +142,6 @@ Q_SIGNALS:
void connectionDropped(NeoChatConnection *connection);
void activeConnectionChanged(NeoChatConnection *connection);
void accountsLoadingChanged();
void globalUrlPreviewDefaultChanged();
};

View File

@@ -6,7 +6,6 @@
#include <QImageReader>
#include <QJsonDocument>
#include "neochatconfig.h"
#include "neochatroom.h"
#include "spacehierarchycache.h"
@@ -35,6 +34,8 @@
using namespace Quotient;
using namespace Qt::StringLiterals;
bool NeoChatConnection::m_globalUrlPreviewDefault = true;
NeoChatConnection::NeoChatConnection(QObject *parent)
: Connection(parent)
{
@@ -123,6 +124,12 @@ void NeoChatConnection::connectSignals()
Q_EMIT homeHaveHighlightNotificationsChanged();
});
connect(this, &NeoChatConnection::globalUrlPreviewEnabledChanged, this, [this]() {
if (!m_globalUrlPreviewDefault) {
m_linkPreviewers.clear();
}
});
// Fetch unstable features
// TODO: Expose unstableFeatures() in libQuotient
connect(
@@ -139,14 +146,6 @@ void NeoChatConnection::connectSignals()
});
},
Qt::SingleShotConnection);
setDirectChatEncryptionDefault(NeoChatConfig::preferUsingEncryption());
connect(NeoChatConfig::self(), &NeoChatConfig::PreferUsingEncryptionChanged, this, [] {
setDirectChatEncryptionDefault(NeoChatConfig::preferUsingEncryption());
});
setGlobalUrlPreviewEnabled(NeoChatConfig::showLinkPreview());
connect(NeoChatConfig::self(), &NeoChatConfig::ShowLinkPreviewChanged, this, [this]() {
setGlobalUrlPreviewEnabled(NeoChatConfig::showLinkPreview());
});
}
int NeoChatConnection::badgeNotificationCount() const
@@ -171,21 +170,12 @@ void NeoChatConnection::refreshBadgeNotificationCount()
bool NeoChatConnection::globalUrlPreviewEnabled()
{
return m_globalUrlPreviewEnabled;
return m_globalUrlPreviewDefault;
}
void NeoChatConnection::setGlobalUrlPreviewEnabled(bool newState)
void NeoChatConnection::setGlobalUrlPreviewDefault(bool useByDefault)
{
if (m_globalUrlPreviewEnabled == newState) {
return;
}
m_globalUrlPreviewEnabled = newState;
if (!m_globalUrlPreviewEnabled) {
m_linkPreviewers.clear();
}
NeoChatConfig::setShowLinkPreview(m_globalUrlPreviewEnabled);
Q_EMIT globalUrlPreviewEnabledChanged();
NeoChatConnection::m_globalUrlPreviewDefault = useByDefault;
}
void NeoChatConnection::logout(bool serverSideLogout)
@@ -521,7 +511,7 @@ QString NeoChatConnection::accountDataJsonString(const QString &type) const
LinkPreviewer *NeoChatConnection::previewerForLink(const QUrl &link)
{
if (!m_globalUrlPreviewEnabled) {
if (!m_globalUrlPreviewDefault) {
return nullptr;
}

View File

@@ -31,11 +31,6 @@ class NeoChatConnection : public Quotient::Connection
*/
Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged)
/**
* @brief Whether URL previews are enabled globally.
*/
Q_PROPERTY(bool globalUrlPreviewEnabled READ globalUrlPreviewEnabled WRITE setGlobalUrlPreviewEnabled NOTIFY globalUrlPreviewEnabledChanged)
/**
* @brief Whether an identity server is configured.
*/
@@ -180,7 +175,11 @@ public:
void refreshBadgeNotificationCount();
bool globalUrlPreviewEnabled();
void setGlobalUrlPreviewEnabled(bool newState);
/**
* @brief Whether URL previews are enabled globally by default for all connections.
*/
static void setGlobalUrlPreviewDefault(bool useByDefault);
bool directChatInvites() const;
@@ -223,13 +222,14 @@ Q_SIGNALS:
void errorOccured(const QString &error);
private:
static bool m_globalUrlPreviewDefault;
bool m_isOnline = true;
void setIsOnline(bool isOnline);
void connectSignals();
int m_badgeNotificationCount = 0;
bool m_globalUrlPreviewEnabled = true;
QCache<QUrl, LinkPreviewer> m_linkPreviewers;

View File

@@ -267,7 +267,7 @@ FormCard.FormCardPage {
}
}
FormCard.FormCheckDelegate {
enabled: root.connection.globalUrlPreviewEnabled
enabled: NeoChatConfig.showLinkPreview
text: i18n("Enable URL previews")
// Most users won't see the above setting so tell them the default.
description: room.defaultUrlPreviewState ? i18n("URL previews are enabled by default in this room") : i18n("URL previews are disabled by default in this room")
@@ -284,10 +284,13 @@ FormCard.FormCardPage {
Layout.alignment: Qt.AlignHCenter
text: i18nc("As in the user has switched off showing previews of hyperlinks in timeline messages", "URL previews are currently disabled for your account")
type: Kirigami.MessageType.Information
visible: !root.connection.globalUrlPreviewEnabled
visible: !NeoChatConfig.showLinkPreview
actions: Kirigami.Action {
text: i18n("Enable")
onTriggered: root.connection.globalUrlPreviewEnabled = true
onTriggered: {
NeoChatConfig.showLinkPreview = true;
NeoChatConfig.save();
}
}
}
FormCard.FormHeader {