common: Make SyncableObject non-copyable
[quassel.git] / src / qtui / settingspages / ignorelistmodel.cpp
index 876a141..d08a280 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-2013 by the Quassel Project                        *
+ *   Copyright (C) 2005-2020 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
 #include "ignorelistmodel.h"
 
 #include <QDebug>
-#include <QStringList>
 #include <QPushButton>
+#include <QStringList>
 
 #include "client.h"
 #include "signalproxy.h"
 
-IgnoreListModel::IgnoreListModel(QObject *parent)
-    : QAbstractItemModel(parent),
-    _configChanged(false),
-    _modelReady(false)
+IgnoreListModel::IgnoreListModel(QObject* parent)
+    : QAbstractItemModel(parent)
 {
     // we need this signal for future connects to reset the data;
-    connect(Client::instance(), SIGNAL(connected()), this, SLOT(clientConnected()));
-    connect(Client::instance(), SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
+    connect(Client::instance(), &Client::connected, this, &IgnoreListModel::clientConnected);
+    connect(Client::instance(), &Client::disconnected, this, &IgnoreListModel::clientDisconnected);
 
     if (Client::isConnected())
         clientConnected();
@@ -42,8 +40,7 @@ IgnoreListModel::IgnoreListModel(QObject *parent)
         emit modelReady(false);
 }
 
-
-QVariant IgnoreListModel::data(const QModelIndex &index, int role) const
+QVariant IgnoreListModel::data(const QModelIndex& index, int role) const
 {
     if (!_modelReady)
         return QVariant();
@@ -87,19 +84,20 @@ QVariant IgnoreListModel::data(const QModelIndex &index, int role) const
     case Qt::DisplayRole:
         switch (index.column()) {
         case 1:
-            if (ignoreListManager()[index.row()].type == IgnoreListManager::SenderIgnore)
+            if (ignoreListManager()[index.row()].type() == IgnoreListManager::SenderIgnore)
                 return tr("By Sender");
             else
                 return tr("By Message");
         }
+        // Intentional fallthrough
     case Qt::EditRole:
         switch (index.column()) {
         case 0:
-            return ignoreListManager()[index.row()].isActive;
+            return ignoreListManager()[index.row()].isEnabled();
         case 1:
-            return ignoreListManager()[index.row()].type;
+            return ignoreListManager()[index.row()].type();
         case 2:
-            return ignoreListManager()[index.row()].ignoreRule;
+            return ignoreListManager()[index.row()].contents();
         default:
             return QVariant();
         }
@@ -108,8 +106,7 @@ QVariant IgnoreListModel::data(const QModelIndex &index, int role) const
     }
 }
 
-
-bool IgnoreListModel::setData(const QModelIndex &index, const QVariant &value, int role)
+bool IgnoreListModel::setData(const QModelIndex& index, const QVariant& value, int role)
 {
     if (!_modelReady)
         return false;
@@ -123,17 +120,17 @@ bool IgnoreListModel::setData(const QModelIndex &index, const QVariant &value, i
 
     switch (index.column()) {
     case 0:
-        cloneIgnoreListManager()[index.row()].isActive = newValue.toBool();
+        cloneIgnoreListManager()[index.row()].setIsEnabled(newValue.toBool());
         return true;
     case 1:
-        cloneIgnoreListManager()[index.row()].type = (IgnoreListManager::IgnoreType)newValue.toInt();
+        cloneIgnoreListManager()[index.row()].setType((IgnoreListManager::IgnoreType)newValue.toInt());
         return true;
     case 2:
         if (ignoreListManager().contains(newValue.toString())) {
             return false;
         }
         else {
-            cloneIgnoreListManager()[index.row()].ignoreRule = newValue.toString();
+            cloneIgnoreListManager()[index.row()].setContents(newValue.toString());
             return true;
         }
     default:
@@ -141,21 +138,17 @@ bool IgnoreListModel::setData(const QModelIndex &index, const QVariant &value, i
     }
 }
 
-
-bool IgnoreListModel::newIgnoreRule(const IgnoreListManager::IgnoreListItem &item)
+bool IgnoreListModel::newIgnoreRule(const IgnoreListManager::IgnoreListItem& item)
 {
-    IgnoreListManager &manager = cloneIgnoreListManager();
-    if (manager.contains(item.ignoreRule))
+    IgnoreListManagermanager = cloneIgnoreListManager();
+    if (manager.contains(item.contents()))
         return false;
     beginInsertRows(QModelIndex(), rowCount(), rowCount());
-    // manager.addIgnoreListItem(item);
-    manager.addIgnoreListItem(item.type, item.ignoreRule, item.isRegEx, item.strictness, item.scope,
-        item.scopeRule, item.isActive);
+    manager.addIgnoreListItem(item.type(), item.contents(), item.isRegEx(), item.strictness(), item.scope(), item.scopeRule(), item.isEnabled());
     endInsertRows();
     return true;
 }
 
-
 void IgnoreListModel::loadDefaults()
 {
     /*if(!_modelReady)
@@ -173,25 +166,24 @@ void IgnoreListModel::loadDefaults()
     IgnoreListManager::IgnoreList defaults = IgnoreListModel::defaults();
     beginInsertRows(QModelIndex(), 0, defaults.count() - 1);
     foreach(IgnoreListManager::IgnoreListItem item, defaults) {
-      manager.addIgnoreListItem(item.ignoreRule, item.isRegEx, item.strictness, item.scope, item.scopeRule);
+      manager.addIgnoreListItem(item.contents(), item.isRegEx(), item.strictness(), item.scope(),
+                                item.scopeRule());
     }
     endInsertRows();*/
 }
 
-
 void IgnoreListModel::removeIgnoreRule(int index)
 {
     if (index < 0 || index >= rowCount())
         return;
 
-    IgnoreListManager &manager = cloneIgnoreListManager();
+    IgnoreListManagermanager = cloneIgnoreListManager();
     beginRemoveRows(QModelIndex(), index, index);
     manager.removeAt(index);
     endRemoveRows();
 }
 
-
-Qt::ItemFlags IgnoreListModel::flags(const QModelIndex &index) const
+Qt::ItemFlags IgnoreListModel::flags(const QModelIndex& index) const
 {
     if (!index.isValid()) {
         return Qt::ItemIsDropEnabled;
@@ -201,13 +193,10 @@ Qt::ItemFlags IgnoreListModel::flags(const QModelIndex &index) const
     }
 }
 
-
 QVariant IgnoreListModel::headerData(int section, Qt::Orientation orientation, int role) const
 {
     QStringList header;
-    header << tr("Enabled")
-           << tr("Type")
-           << tr("Ignore Rule");
+    header << tr("Enabled") << tr("Type") << tr("Ignore Rule");
 
     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
         return header[section];
@@ -215,110 +204,94 @@ QVariant IgnoreListModel::headerData(int section, Qt::Orientation orientation, i
     return QVariant();
 }
 
-
-QModelIndex IgnoreListModel::index(int row, int column, const QModelIndex &parent) const
+QModelIndex IgnoreListModel::index(int row, int column, const QModelIndex& parent) const
 {
     Q_UNUSED(parent);
     if (row >= rowCount() || column >= columnCount())
-        return QModelIndex();
+        return {};
 
     return createIndex(row, column);
 }
 
-
-const IgnoreListManager &IgnoreListModel::ignoreListManager() const
+const IgnoreListManager& IgnoreListModel::ignoreListManager() const
 {
-    if (_configChanged)
-        return _clonedIgnoreListManager;
-    else
-        return *Client::ignoreListManager();
+    return _clonedIgnoreListManager ? *_clonedIgnoreListManager : *Client::ignoreListManager();
 }
 
-
-IgnoreListManager &IgnoreListModel::ignoreListManager()
+IgnoreListManager& IgnoreListModel::ignoreListManager()
 {
-    if (_configChanged)
-        return _clonedIgnoreListManager;
-    else
-        return *Client::ignoreListManager();
+    return _clonedIgnoreListManager ? *_clonedIgnoreListManager : *Client::ignoreListManager();
 }
 
-
-IgnoreListManager &IgnoreListModel::cloneIgnoreListManager()
+IgnoreListManager& IgnoreListModel::cloneIgnoreListManager()
 {
-    if (!_configChanged) {
-        _clonedIgnoreListManager = *Client::ignoreListManager();
-        _configChanged = true;
+    if (!_clonedIgnoreListManager) {
+        _clonedIgnoreListManager = std::make_unique<ClientIgnoreListManager>();
+        _clonedIgnoreListManager->fromVariantMap(Client::ignoreListManager()->toVariantMap());
         emit configChanged(true);
     }
-    return _clonedIgnoreListManager;
+    return *_clonedIgnoreListManager;
 }
 
-
 void IgnoreListModel::revert()
 {
-    if (!_configChanged)
+    if (!_clonedIgnoreListManager)
         return;
 
-    _configChanged = false;
+    beginResetModel();
+    _clonedIgnoreListManager.reset();
+    endResetModel();
     emit configChanged(false);
-    reset();
 }
 
-
 void IgnoreListModel::commit()
 {
-    if (!_configChanged)
+    if (!_clonedIgnoreListManager)
         return;
 
-    Client::ignoreListManager()->requestUpdate(_clonedIgnoreListManager.toVariantMap());
+    Client::ignoreListManager()->requestUpdate(_clonedIgnoreListManager->toVariantMap());
     revert();
 }
 
-
 void IgnoreListModel::initDone()
 {
     _modelReady = true;
-    reset();
+    beginResetModel();
+    endResetModel();
     emit modelReady(true);
 }
 
-
 void IgnoreListModel::clientConnected()
 {
-    connect(Client::ignoreListManager(), SIGNAL(updated()), SLOT(revert()));
+    connect(Client::ignoreListManager(), &IgnoreListManager::updated, this, &IgnoreListModel::revert);
     if (Client::ignoreListManager()->isInitialized())
         initDone();
     else
-        connect(Client::ignoreListManager(), SIGNAL(initDone()), SLOT(initDone()));
+        connect(Client::ignoreListManager(), &SyncableObject::initDone, this, &IgnoreListModel::initDone);
 }
 
-
 void IgnoreListModel::clientDisconnected()
 {
-    // clear
-    _clonedIgnoreListManager = ClientIgnoreListManager();
     _modelReady = false;
-    reset();
+    beginResetModel();
+    _clonedIgnoreListManager.reset();
+    endResetModel();
     emit modelReady(false);
 }
 
-
-const IgnoreListManager::IgnoreListItem &IgnoreListModel::ignoreListItemAt(int row) const
+const IgnoreListManager::IgnoreListItem& IgnoreListModel::ignoreListItemAt(int row) const
 {
     return ignoreListManager()[row];
 }
 
-
 // FIXME use QModelIndex?
-void IgnoreListModel::setIgnoreListItemAt(int row, const IgnoreListManager::IgnoreListItem &item)
+void IgnoreListModel::setIgnoreListItemAt(int row, const IgnoreListManager::IgnoreListItemitem)
 {
     cloneIgnoreListManager()[row] = item;
     emit dataChanged(createIndex(row, 0), createIndex(row, 2));
 }
 
-
-const QModelIndex IgnoreListModel::indexOf(const QString &rule)
+const QModelIndex IgnoreListModel::indexOf(const QString& rule)
 {
     return createIndex(ignoreListManager().indexOf(rule), 2);
 }