Only enable the relevant highlight depending on core features
[quassel.git] / src / qtui / settingspages / corehighlightsettingspage.cpp
index 7bedc00..3a6d6eb 100644 (file)
  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
  ***************************************************************************/
 
+#include <QHeaderView>
 #include <QTableWidget>
-#include <QtWidgets/QHeaderView>
 
+#include "client.h"
 #include "corehighlightsettingspage.h"
 #include "qtui.h"
-#include "client.h"
 
 CoreHighlightSettingsPage::CoreHighlightSettingsPage(QWidget *parent)
-    : SettingsPage(tr("Interface"), tr("Core-Side Highlights"), parent)
+    : SettingsPage(tr("Interface"), tr("Remote Highlights"), parent)
 {
     ui.setupUi(this);
 
     setupRuleTable(ui.highlightTable);
     setupRuleTable(ui.ignoredTable);
 
-    ui.highlightNicksComboBox->addItem(tr("All Nicks from Identity"), HighlightRuleManager::AllNicks);
-    ui.highlightNicksComboBox->addItem(tr("Current Nick"), HighlightRuleManager::CurrentNick);
-    ui.highlightNicksComboBox->addItem(tr("None"), HighlightRuleManager::NoNick);
+    ui.highlightNicksComboBox->addItem(tr("All Nicks from Identity"), QVariant(HighlightRuleManager::AllNicks));
+    ui.highlightNicksComboBox->addItem(tr("Current Nick"), QVariant(HighlightRuleManager::CurrentNick));
+    ui.highlightNicksComboBox->addItem(tr("None"), QVariant(HighlightRuleManager::NoNick));
+
+    coreConnectionStateChanged(Client::isConnected()); // need a core connection!
+    connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), this, SLOT(coreConnectionStateChanged(bool)));
 
     connect(ui.highlightAdd, SIGNAL(clicked(bool)), this, SLOT(addNewHighlightRow()));
     connect(ui.highlightRemove, SIGNAL(clicked(bool)), this, SLOT(removeSelectedHighlightRows()));
+    connect(ui.highlightImport, SIGNAL(clicked(bool)), this, SLOT(importRules()));
 
     connect(ui.ignoredAdd, SIGNAL(clicked(bool)), this, SLOT(addNewIgnoredRow()));
     connect(ui.ignoredRemove, SIGNAL(clicked(bool)), this, SLOT(removeSelectedIgnoredRows()));
@@ -75,24 +79,35 @@ CoreHighlightSettingsPage::CoreHighlightSettingsPage(QWidget *parent)
 
     connect(Client::instance(), SIGNAL(connected()), this, SLOT(clientConnected()));
 }
+
+void CoreHighlightSettingsPage::coreConnectionStateChanged(bool state)
+{
+    setEnabled(state);
+    if (state) {
+        load();
+    } else {
+        revert();
+    }
+}
+
 void CoreHighlightSettingsPage::setupRuleTable(QTableWidget *table) const
 {
     table->verticalHeader()->hide();
     table->setShowGrid(false);
 
-    table->horizontalHeaderItem(RegExColumn)->setToolTip(
+    table->horizontalHeaderItem(CoreHighlightSettingsPage::RegExColumn)->setToolTip(
         tr("<b>RegEx</b>: This option determines if the highlight rule should be interpreted as a <b>regular expression</b> or just as a keyword."));
-    table->horizontalHeaderItem(RegExColumn)->setWhatsThis(
+    table->horizontalHeaderItem(CoreHighlightSettingsPage::RegExColumn)->setWhatsThis(
         tr("<b>RegEx</b>: This option determines if the highlight rule should be interpreted as a <b>regular expression</b> or just as a keyword."));
 
-    table->horizontalHeaderItem(CsColumn)->setToolTip(
+    table->horizontalHeaderItem(CoreHighlightSettingsPage::CsColumn)->setToolTip(
         tr("<b>CS</b>: This option determines if the highlight rule should be interpreted <b>case sensitive</b>."));
-    table->horizontalHeaderItem(CsColumn)->setWhatsThis(
+    table->horizontalHeaderItem(CoreHighlightSettingsPage::CsColumn)->setWhatsThis(
         tr("<b>CS</b>: This option determines if the highlight rule should be interpreted <b>case sensitive</b>."));
 
-    table->horizontalHeaderItem(ChanColumn)->setToolTip(
+    table->horizontalHeaderItem(CoreHighlightSettingsPage::ChanColumn)->setToolTip(
         tr("<b>Channel</b>: This regular expression determines for which <b>channels</b> the highlight rule works. Leave blank to match any channel. Put <b>!</b> in the beginning to negate. Case insensitive."));
-    table->horizontalHeaderItem(ChanColumn)->setWhatsThis(
+    table->horizontalHeaderItem(CoreHighlightSettingsPage::ChanColumn)->setWhatsThis(
         tr("<b>Channel</b>: This regular expression determines for which <b>channels</b> the highlight rule works. Leave blank to match any channel. Put <b>!</b> in the beginning to negate. Case insensitive."));
 
 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
@@ -102,11 +117,11 @@ void CoreHighlightSettingsPage::setupRuleTable(QTableWidget *table) const
     table->horizontalHeader()->setResizeMode(CoreHighlightSettingsPage::CsColumn, QHeaderView::ResizeToContents);
     table->horizontalHeader()->setResizeMode(CoreHighlightSettingsPage::ChanColumn, QHeaderView::ResizeToContents);
 #else
-    table->horizontalHeader()->setSectionResizeMode(EnableColumn, QHeaderView::ResizeToContents);
-    table->horizontalHeader()->setSectionResizeMode(NameColumn, QHeaderView::Stretch);
-    table->horizontalHeader()->setSectionResizeMode(RegExColumn, QHeaderView::ResizeToContents);
-    table->horizontalHeader()->setSectionResizeMode(CsColumn, QHeaderView::ResizeToContents);
-    table->horizontalHeader()->setSectionResizeMode(ChanColumn, QHeaderView::ResizeToContents);
+    table->horizontalHeader()->setSectionResizeMode(CoreHighlightSettingsPage::EnableColumn, QHeaderView::ResizeToContents);
+    table->horizontalHeader()->setSectionResizeMode(CoreHighlightSettingsPage::NameColumn, QHeaderView::Stretch);
+    table->horizontalHeader()->setSectionResizeMode(CoreHighlightSettingsPage::RegExColumn, QHeaderView::ResizeToContents);
+    table->horizontalHeader()->setSectionResizeMode(CoreHighlightSettingsPage::CsColumn, QHeaderView::ResizeToContents);
+    table->horizontalHeader()->setSectionResizeMode(CoreHighlightSettingsPage::ChanColumn, QHeaderView::ResizeToContents);
 #endif
 }
 
@@ -131,8 +146,9 @@ bool CoreHighlightSettingsPage::hasDefaults() const
 
 void CoreHighlightSettingsPage::defaults()
 {
-    ui.highlightNicksComboBox->setCurrentIndex(ui.highlightNicksComboBox
-                                                   ->findData(QVariant(HighlightRuleManager::HighlightNickType::CurrentNick)));
+    int highlightNickType = HighlightRuleManager::HighlightNickType::CurrentNick;
+    int defaultIndex = ui.highlightNicksComboBox->findData(QVariant(highlightNickType));
+    ui.highlightNicksComboBox->setCurrentIndex(defaultIndex);
     ui.nicksCaseSensitive->setChecked(false);
     emptyHighlightTable();
     emptyIgnoredTable();
@@ -292,7 +308,7 @@ void CoreHighlightSettingsPage::emptyHighlightTable()
     if (ui.highlightTable->rowCount() != highlightList.size()) {
         qDebug() << "something is wrong: ui.highlight and highlightList don't have the same size!";
     }
-    ui.highlightTable->clear();
+    ui.highlightTable->clearContents();
     highlightList.clear();
 }
 
@@ -302,7 +318,7 @@ void CoreHighlightSettingsPage::emptyIgnoredTable()
     if (ui.ignoredTable->rowCount() != ignoredList.size()) {
         qDebug() << "something is wrong: ui.highlight and highlightList don't have the same size!";
     }
-    ui.ignoredTable->clear();
+    ui.ignoredTable->clearContents();
     ignoredList.clear();
 }
 
@@ -317,6 +333,7 @@ void CoreHighlightSettingsPage::highlightTableChanged(QTableWidgetItem *item)
     switch (item->column()) {
         case CoreHighlightSettingsPage::EnableColumn:
             highlightRule.isEnabled = (item->checkState() == Qt::Checked);
+            break;
         case CoreHighlightSettingsPage::NameColumn:
             if (item->text() == "")
                 item->setText(tr("this shouldn't be empty"));
@@ -354,6 +371,7 @@ void CoreHighlightSettingsPage::ignoredTableChanged(QTableWidgetItem *item)
     switch (item->column()) {
         case CoreHighlightSettingsPage::EnableColumn:
             ignoredRule.isEnabled = (item->checkState() == Qt::Checked);
+            break;
         case CoreHighlightSettingsPage::NameColumn:
             if (item->text() == "")
                 item->setText(tr("this shouldn't be empty"));
@@ -402,12 +420,14 @@ void CoreHighlightSettingsPage::load()
             }
         }
 
-        ui.highlightNicksComboBox
-            ->setCurrentIndex(ui.highlightNicksComboBox->findData(QVariant(ruleManager->highlightNick())));
+        int highlightNickType = ruleManager->highlightNick();
+        ui.highlightNicksComboBox->setCurrentIndex(ui.highlightNicksComboBox->findData(QVariant(highlightNickType)));
         ui.nicksCaseSensitive->setChecked(ruleManager->nicksCaseSensitive());
 
         setChangedState(false);
         _initialized = true;
+    } else {
+        defaults();
     }
 }
 
@@ -437,9 +457,9 @@ void CoreHighlightSettingsPage::save()
                                        rule.sender, rule.chanName);
     }
 
-    auto highlightNickType = ui.highlightNicksComboBox->currentData().value<HighlightRuleManager::HighlightNickType>();
+    auto highlightNickType = ui.highlightNicksComboBox->itemData(ui.highlightNicksComboBox->currentIndex()).value<int>();
 
-    clonedManager.setHighlightNick(highlightNickType);
+    clonedManager.setHighlightNick(HighlightRuleManager::HighlightNickType(highlightNickType));
     clonedManager.setNicksCaseSensitive(ui.nicksCaseSensitive->isChecked());
 
     ruleManager->requestUpdate(clonedManager.toVariantMap());
@@ -450,4 +470,33 @@ void CoreHighlightSettingsPage::save()
 void CoreHighlightSettingsPage::widgetHasChanged()
 {
     setChangedState(true);
-}
\ No newline at end of file
+}
+
+void CoreHighlightSettingsPage::importRules() {
+    NotificationSettings notificationSettings;
+
+    auto clonedManager = HighlightRuleManager();
+    clonedManager.fromVariantMap(Client::highlightRuleManager()->toVariantMap());
+
+    for (const auto &variant : notificationSettings.highlightList()) {
+        auto highlightRule = variant.toMap();
+
+        clonedManager.addHighlightRule(
+                highlightRule["Name"].toString(),
+                highlightRule["RegEx"].toBool(),
+                highlightRule["CS"].toBool(),
+                highlightRule["Enable"].toBool(),
+                false,
+                "",
+                highlightRule["Channel"].toString()
+        );
+    }
+
+    Client::highlightRuleManager()->requestUpdate(clonedManager.toVariantMap());
+    setChangedState(false);
+    load();
+}
+
+bool CoreHighlightSettingsPage::isSelectable() const {
+    return Client::isConnected() && Client::isCoreFeatureEnabled(Quassel::Feature::CoreSideHighlights);
+}