diff --git a/src/controller.cpp b/src/controller.cpp index d5ec59d67..afe9097e7 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -4,6 +4,7 @@ #include "controller.h" +#include #include #include @@ -50,6 +51,17 @@ Controller::Controller(QObject *parent) { Connection::setRoomType(); + 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(); diff --git a/src/controller.h b/src/controller.h index ed2f6db71..88d69dd58 100644 --- a/src/controller.h +++ b/src/controller.h @@ -142,4 +142,6 @@ Q_SIGNALS: void connectionDropped(NeoChatConnection *connection); void activeConnectionChanged(NeoChatConnection *connection); void accountsLoadingChanged(); + + void globalUrlPreviewDefaultChanged(); }; diff --git a/src/neochatconnection.cpp b/src/neochatconnection.cpp index da1ec8a36..4feb85bda 100644 --- a/src/neochatconnection.cpp +++ b/src/neochatconnection.cpp @@ -6,7 +6,6 @@ #include #include -#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; } diff --git a/src/neochatconnection.h b/src/neochatconnection.h index e3ea388ae..5aa1aba05 100644 --- a/src/neochatconnection.h +++ b/src/neochatconnection.h @@ -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 m_linkPreviewers; diff --git a/src/settings/RoomGeneralPage.qml b/src/settings/RoomGeneralPage.qml index 0ec324b99..2582de1a9 100644 --- a/src/settings/RoomGeneralPage.qml +++ b/src/settings/RoomGeneralPage.qml @@ -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 {