core: Fix highlight nick config save/load
authorShane Synan <digitalcircuit36939@gmail.com>
Mon, 16 Jul 2018 21:47:37 +0000 (16:47 -0500)
committerManuel Nickschas <sputnick@quassel-irc.org>
Tue, 17 Jul 2018 17:57:45 +0000 (19:57 +0200)
Modernize CoreHighlightRuleManager to use the conventions of
DccConfig, saving/loading via SyncableObject serialization handling
for load/save.

This simplifies the code and fixes highlight nick configuration
(highlightNick, nicksCaseSensitive) not being persisted after core
restart.

Add some documentation, too.

Fixes regression from 17c39210b1bce04795046657642de66292518fe6.

src/core/corehighlightrulemanager.cpp
src/core/corehighlightrulemanager.h

index d7ccd8f..eed5655 100644 (file)
 #include "core.h"
 #include "coresession.h"
 
+constexpr auto settingsKey = "HighlightRuleList";
+
 INIT_SYNCABLE_OBJECT(CoreHighlightRuleManager)
-CoreHighlightRuleManager::CoreHighlightRuleManager(CoreSession *parent)
-    : HighlightRuleManager(parent)
+CoreHighlightRuleManager::CoreHighlightRuleManager(CoreSession *session)
+    : HighlightRuleManager(session)
+    , _coreSession{session}
 {
-    CoreSession *session = qobject_cast<CoreSession*>(parent);
-    if (!session) {
-        qWarning() << "CoreHighlightRuleManager: unable to load HighlightRuleList. Parent is not a Coresession!";
-        //loadDefaults();
-        return;
-    }
-
-    initSetHighlightRuleList(Core::getUserSetting(session->user(), "HighlightRuleList").toMap());
+    // Load config from database if it exists
+    auto configMap = Core::getUserSetting(session->user(), settingsKey).toMap();
+    if (!configMap.isEmpty())
+        update(configMap);
+    // Otherwise, we just use the defaults initialized in the base class
 
-    // we store our settings whenever they change
+    // We store our settings whenever they change
     connect(this, SIGNAL(updatedRemotely()), SLOT(save()));
 }
 
-void CoreHighlightRuleManager::save() const
+void CoreHighlightRuleManager::save()
 {
-    CoreSession *session = qobject_cast<CoreSession *>(parent());
-    if (!session) {
-        qWarning() << "CoreHighlightRuleManager: unable to save HighlightRuleList. Parent is not a Coresession!";
-        return;
-    }
-
-    Core::setUserSetting(session->user(), "HighlightRuleList", initHighlightRuleList());
+    Core::setUserSetting(_coreSession->user(), settingsKey, toVariantMap());
 }
 
-bool CoreHighlightRuleManager::match(const RawMessage &msg, const QString &currentNick, const QStringList &identityNicks)
+bool CoreHighlightRuleManager::match(const RawMessage &msg, const QString &currentNick,
+                                     const QStringList &identityNicks)
 {
     return match(msg.text, msg.sender, msg.type, msg.flags, msg.target, currentNick, identityNicks);
 }
index 85a26ff..4cf3427 100644 (file)
 class CoreSession;
 struct RawMessage;
 
+/**
+ * Core-side specialization for HighlightRuleManager.
+ *
+ * Adds the ability to load/save the settings from/to the database.
+ */
 class CoreHighlightRuleManager : public HighlightRuleManager
 {
     Q_OBJECT
@@ -33,7 +38,12 @@ class CoreHighlightRuleManager : public HighlightRuleManager
     using HighlightRuleManager::match;
 
 public:
-    explicit CoreHighlightRuleManager(CoreSession *parent);
+    /**
+     * Constructor.
+     *
+     * @param[in] session Pointer to the parent CoreSession (takes ownership)
+     */
+    explicit CoreHighlightRuleManager(CoreSession *session);
 
     virtual const QMetaObject *syncMetaObject() const override { return &HighlightRuleManager::staticMetaObject; }
 
@@ -50,5 +60,11 @@ public slots:
     }
 
 private slots:
-    void save() const;
+    /**
+     * Saves the config to the database.
+     */
+    void save();
+
+private:
+    CoreSession *_coreSession {nullptr};  ///< Pointer to the parent CoreSession
 };