Fix trimming
[quassel.git] / src / qtui / settingspages / ignorelistsettingspage.cpp
index 8ee11d4..90ea712 100644 (file)
 #include <QMessageBox>
 #include <QString>
 #include <QEvent>
-
+#include <QDebug>
 #include "iconloader.h"
 
 IgnoreListSettingsPage::IgnoreListSettingsPage(QWidget *parent)
-  : SettingsPage(tr("Misc"), tr("Ignorelist"), parent)
+  : SettingsPage(tr("IRC"), tr("Ignore List"), parent)
 {
   ui.setupUi(this);
   _delegate = new IgnoreListDelegate(ui.ignoreListView);
@@ -72,6 +72,8 @@ IgnoreListSettingsPage::~IgnoreListSettingsPage() {
 void IgnoreListSettingsPage::load() {
   if(_ignoreListModel.configChanged())
     _ignoreListModel.revert();
+  ui.ignoreListView->selectionModel()->reset();
+  ui.editIgnoreRuleButton->setEnabled(false);
 }
 
 void IgnoreListSettingsPage::defaults() {
@@ -83,6 +85,7 @@ void IgnoreListSettingsPage::save() {
     _ignoreListModel.commit();
   }
   ui.ignoreListView->selectionModel()->reset();
+  ui.editIgnoreRuleButton->setEnabled(false);
 }
 
 void IgnoreListSettingsPage::enableDialog(bool enabled) {
@@ -103,9 +106,22 @@ void IgnoreListSettingsPage::deleteSelectedIgnoreRule() {
   _ignoreListModel.removeIgnoreRule(ui.ignoreListView->selectionModel()->selectedIndexes()[0].row());
 }
 
-void IgnoreListSettingsPage::newIgnoreRule() {
-  IgnoreListEditDlg *dlg = new IgnoreListEditDlg(-1, IgnoreListManager::IgnoreListItem(), this);
+void IgnoreListSettingsPage::newIgnoreRule(QString rule) {
+  IgnoreListManager::IgnoreListItem newItem = IgnoreListManager::IgnoreListItem();
+  newItem.strictness = IgnoreListManager::SoftStrictness;
+  newItem.scope = IgnoreListManager::GlobalScope;
+  newItem.isRegEx = false;
+  newItem.isActive = true;
+
+  bool enableOkButton = false;
+  if(!rule.isEmpty()) {
+    // we're called from contextmenu
+    newItem.ignoreRule = rule;
+    enableOkButton = true;
+  }
 
+  IgnoreListEditDlg *dlg = new IgnoreListEditDlg(newItem, this, enableOkButton);
+  dlg->enableOkButton(enableOkButton);
   while(dlg->exec() == QDialog::Accepted) {
     if(!_ignoreListModel.newIgnoreRule(dlg->ignoreListItem())) {
       if(QMessageBox::warning(this,
@@ -118,7 +134,7 @@ void IgnoreListSettingsPage::newIgnoreRule() {
 
       IgnoreListManager::IgnoreListItem item = dlg->ignoreListItem();
       delete dlg;
-      dlg = new IgnoreListEditDlg(-1, item, this);
+      dlg = new IgnoreListEditDlg(item, this);
     }
     else {
       break;
@@ -131,13 +147,24 @@ void IgnoreListSettingsPage::editSelectedIgnoreRule() {
   if(!ui.ignoreListView->selectionModel()->hasSelection())
     return;
   int row = ui.ignoreListView->selectionModel()->selectedIndexes()[0].row();
-  IgnoreListEditDlg dlg(row, _ignoreListModel.ignoreListItemAt(row), this);
+  IgnoreListEditDlg dlg(_ignoreListModel.ignoreListItemAt(row), this);
   dlg.setAttribute(Qt::WA_DeleteOnClose, false);
   if(dlg.exec() == QDialog::Accepted) {
     _ignoreListModel.setIgnoreListItemAt(row, dlg.ignoreListItem());
   }
 }
 
+void IgnoreListSettingsPage::editIgnoreRule(const QString &ignoreRule) {
+  ui.ignoreListView->selectionModel()->select(_ignoreListModel.indexOf(ignoreRule), QItemSelectionModel::Select);
+  if(ui.ignoreListView->selectionModel()->hasSelection())// && ui.ignoreListView->selectionModel()->selectedIndexes()[0].row() != -1)
+    editSelectedIgnoreRule();
+  else
+    newIgnoreRule(ignoreRule);
+}
+
+/*
+  IgnoreListDelegate
+*/
 void IgnoreListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
   if(index.column() == 0) {
     QStyle *style = QApplication::style();
@@ -156,16 +183,38 @@ void IgnoreListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
     QStyledItemDelegate::paint(painter, option, index);
 }
 
-IgnoreListEditDlg::IgnoreListEditDlg(int row, const IgnoreListManager::IgnoreListItem &item, QWidget *parent)
-    : QDialog(parent), _selectedRow(row), _ignoreListItem(item), _hasChanged(false) {
+// provide interactivity for the checkboxes
+bool IgnoreListDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
+                                     const QStyleOptionViewItem &option, const QModelIndex &index) {
+  Q_UNUSED(option)
+  switch(event->type()) {
+    case QEvent::MouseButtonRelease:
+      model->setData(index, !index.data().toBool());
+      return true;
+      // don't show the default editor for the column
+    case QEvent::MouseButtonDblClick:
+      return true;
+    default:
+      return false;
+  }
+}
+
+/*
+  IgnoreListEditDlg
+*/
+IgnoreListEditDlg::IgnoreListEditDlg(const IgnoreListManager::IgnoreListItem &item, QWidget *parent, bool enabled)
+    : QDialog(parent), _ignoreListItem(item), _hasChanged(enabled) {
   ui.setupUi(this);
   setAttribute(Qt::WA_DeleteOnClose, false);
   setModal(true);
+  // FIXME patch out the bugger completely if it's good without it
+  ui.isActiveCheckBox->hide();
 
   // setup buttongroups
   // this could be moved to .ui file with qt4.5
   _typeButtonGroup.addButton(ui.senderTypeButton, 0);
   _typeButtonGroup.addButton(ui.messageTypeButton, 1);
+  _typeButtonGroup.addButton(ui.ctcpTypeButton, 2);
   _strictnessButtonGroup.addButton(ui.dynamicStrictnessButton, 0);
   _strictnessButtonGroup.addButton(ui.permanentStrictnessButton, 1);
   _scopeButtonGroup.addButton(ui.globalScopeButton, 0);
@@ -178,6 +227,8 @@ IgnoreListEditDlg::IgnoreListEditDlg(int row, const IgnoreListManager::IgnoreLis
 
   if(item.type == IgnoreListManager::MessageIgnore)
     ui.messageTypeButton->setChecked(true);
+  else if(item.type == IgnoreListManager::CtcpIgnore)
+    ui.ctcpTypeButton->setChecked(true);
   else
     ui.senderTypeButton->setChecked(true);
 
@@ -217,11 +268,14 @@ IgnoreListEditDlg::IgnoreListEditDlg(int row, const IgnoreListManager::IgnoreLis
   connect(ui.isActiveCheckBox, SIGNAL(stateChanged(int)), this, SLOT(widgetHasChanged()));
 
   connect(ui.buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(aboutToAccept()));
+  widgetHasChanged();
 }
 
 void IgnoreListEditDlg::widgetHasChanged() {
   if(ui.messageTypeButton->isChecked())
     _clonedIgnoreListItem.type = IgnoreListManager::MessageIgnore;
+  else if(ui.ctcpTypeButton->isChecked())
+    _clonedIgnoreListItem.type = IgnoreListManager::CtcpIgnore;
   else
     _clonedIgnoreListItem.type = IgnoreListManager::SenderIgnore;
 
@@ -249,8 +303,10 @@ void IgnoreListEditDlg::widgetHasChanged() {
   else {
     QStringList text = ui.scopeRuleTextEdit->toPlainText().split(";", QString::SkipEmptyParts);
     QStringList::iterator it = text.begin();
-    while(it != text.end())
-      (*it++).trimmed();
+    while(it != text.end()) {
+      *it = it->trimmed();
+      ++it;
+    }
 
     _clonedIgnoreListItem.scopeRule = text.join("; ");
   }
@@ -266,18 +322,6 @@ void IgnoreListEditDlg::widgetHasChanged() {
   ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(_hasChanged);
 }
 
-// provide interactivity for the checkboxes
-bool IgnoreListDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
-                                     const QStyleOptionViewItem &option, const QModelIndex &index) {
-  Q_UNUSED(option)
-  switch(event->type()) {
-    case QEvent::MouseButtonRelease:
-      model->setData(index, !index.data().toBool());
-      return true;
-      // don't show the default editor for the column
-    case QEvent::MouseButtonDblClick:
-      return true;
-    default:
-      return false;
-  }
+void IgnoreListEditDlg::enableOkButton(bool state) {
+  ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(state);
 }