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

View File

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

View File

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

View File

@@ -31,11 +31,6 @@ class NeoChatConnection : public Quotient::Connection
*/ */
Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged) 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. * @brief Whether an identity server is configured.
*/ */
@@ -180,7 +175,11 @@ public:
void refreshBadgeNotificationCount(); void refreshBadgeNotificationCount();
bool globalUrlPreviewEnabled(); 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; bool directChatInvites() const;
@@ -223,13 +222,14 @@ Q_SIGNALS:
void errorOccured(const QString &error); void errorOccured(const QString &error);
private: private:
static bool m_globalUrlPreviewDefault;
bool m_isOnline = true; bool m_isOnline = true;
void setIsOnline(bool isOnline); void setIsOnline(bool isOnline);
void connectSignals(); void connectSignals();
int m_badgeNotificationCount = 0; int m_badgeNotificationCount = 0;
bool m_globalUrlPreviewEnabled = true;
QCache<QUrl, LinkPreviewer> m_linkPreviewers; QCache<QUrl, LinkPreviewer> m_linkPreviewers;

View File

@@ -267,7 +267,7 @@ FormCard.FormCardPage {
} }
} }
FormCard.FormCheckDelegate { FormCard.FormCheckDelegate {
enabled: root.connection.globalUrlPreviewEnabled enabled: NeoChatConfig.showLinkPreview
text: i18n("Enable URL previews") text: i18n("Enable URL previews")
// Most users won't see the above setting so tell them the default. // 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") 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 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") 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 type: Kirigami.MessageType.Information
visible: !root.connection.globalUrlPreviewEnabled visible: !NeoChatConfig.showLinkPreview
actions: Kirigami.Action { actions: Kirigami.Action {
text: i18n("Enable") text: i18n("Enable")
onTriggered: root.connection.globalUrlPreviewEnabled = true onTriggered: {
NeoChatConfig.showLinkPreview = true;
NeoChatConfig.save();
}
} }
} }
FormCard.FormHeader { FormCard.FormHeader {