Various ignorelist fixes
authorSebastian Goth <seezer@roath.org>
Sun, 13 Sep 2009 13:27:21 +0000 (15:27 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Sun, 13 Sep 2009 19:18:48 +0000 (21:18 +0200)
- Now works even with KDE integration
- Don't add empty rules
- Don't leak memory

src/qtui/settingspages/ignorelistsettingspage.cpp
src/uisupport/contextmenuactionprovider.cpp
src/uisupport/contextmenuactionprovider.h
src/uisupport/networkmodelcontroller.cpp

index 61f0864..d496386 100644 (file)
@@ -108,7 +108,11 @@ void IgnoreListSettingsPage::deleteSelectedIgnoreRule() {
 
 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
@@ -118,7 +122,6 @@ void IgnoreListSettingsPage::newIgnoreRule(QString rule) {
 
   IgnoreListEditDlg *dlg = new IgnoreListEditDlg(newItem, this, enableOkButton);
   dlg->enableOkButton(enableOkButton);
-
   while(dlg->exec() == QDialog::Accepted) {
     if(!_ignoreListModel.newIgnoreRule(dlg->ignoreListItem())) {
       if(QMessageBox::warning(this,
@@ -260,6 +263,7 @@ IgnoreListEditDlg::IgnoreListEditDlg(const IgnoreListManager::IgnoreListItem &it
   connect(ui.isActiveCheckBox, SIGNAL(stateChanged(int)), this, SLOT(widgetHasChanged()));
 
   connect(ui.buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(aboutToAccept()));
+  widgetHasChanged();
 }
 
 void IgnoreListEditDlg::widgetHasChanged() {
index 03c2de6..fbe4541 100644 (file)
@@ -125,6 +125,13 @@ ContextMenuActionProvider::ContextMenuActionProvider(QObject *parent) : NetworkM
   QMenu *ignoreMenu = new QMenu();
   _nickIgnoreMenuAction = new Action(tr("Ignore"), 0);
   _nickIgnoreMenuAction->setMenu(ignoreMenu);
+
+  // These are disabled actions used as descriptions
+  // They don't need any of the Action fancyness so we use plain QActions
+  _ignoreDescriptions << new QAction(tr("Add Ignore Rule"), this);
+  _ignoreDescriptions << new QAction(tr("Existing Rules"), this);
+  foreach(QAction *act, _ignoreDescriptions)
+    act->setEnabled(false);
 }
 
 ContextMenuActionProvider::~ContextMenuActionProvider() {
@@ -136,6 +143,8 @@ ContextMenuActionProvider::~ContextMenuActionProvider() {
   _nickModeMenuAction->deleteLater();
   _nickIgnoreMenuAction->menu()->deleteLater();
   _nickIgnoreMenuAction->deleteLater();
+  qDeleteAll(_ignoreDescriptions);
+  _ignoreDescriptions.clear();
 }
 
 void ContextMenuActionProvider::addActions(QMenu *menu, BufferId bufId, QObject *receiver, const char *method) {
@@ -398,35 +407,50 @@ void ContextMenuActionProvider::addIgnoreMenu(QMenu *menu, const QString &hostma
   // if we don't have the data, we skip actions where we would need it
   bool haveWhoData = !ident.isEmpty() && !host.isEmpty();
 
-
-  ignoreMenu->addAction(tr("Add Ignore Rule"))->setEnabled(false);
+  // add "Add Ignore Rule" description
+  ignoreMenu->addAction(_ignoreDescriptions.at(0));
 
   if(haveWhoData) {
-    action(NickIgnoreUser)->setText(QString("*!%1@%2").arg(ident, host));
-    action(NickIgnoreHost)->setText(QString("*!*@%1").arg(host));
-    action(NickIgnoreDomain)->setText(domain.at(0) == '.' ? QString("*!%1@*%2").arg(ident, domain)
-                                      : QString("*!%1@%2").arg(ident, domain));
+    QString text;
+    text = QString("*!%1@%2").arg(ident, host);
+    action(NickIgnoreUser)->setText(text);
+    action(NickIgnoreUser)->setProperty("ignoreRule", text);
+
+    text = QString("*!*@%1").arg(host);
+    action(NickIgnoreHost)->setText(text);
+    action(NickIgnoreHost)->setProperty("ignoreRule", text);
+
+    text = domain.at(0) == '.' ? QString("*!%1@*%2").arg(ident, domain)
+                               : QString("*!%1@%2").arg(ident, domain);
 
-    if(!ignoreMap.contains(action(NickIgnoreUser)->text()))
+    action(NickIgnoreDomain)->setText(text);
+    action(NickIgnoreDomain)->setProperty("ignoreRule", text);
+
+    if(!ignoreMap.contains(action(NickIgnoreUser)->property("ignoreRule").toString()))
       ignoreMenu->addAction(action(NickIgnoreUser));
-    if(!ignoreMap.contains(action(NickIgnoreHost)->text()))
+    if(!ignoreMap.contains(action(NickIgnoreHost)->property("ignoreRule").toString()))
       ignoreMenu->addAction(action(NickIgnoreHost));
-    if(!ignoreMap.contains(action(NickIgnoreDomain)->text()))
+    if(!ignoreMap.contains(action(NickIgnoreDomain)->property("ignoreRule").toString()))
       ignoreMenu->addAction(action(NickIgnoreDomain));
   }
+
+  action(NickIgnoreCustom)->setProperty("ignoreRule", hostmask);
   ignoreMenu->addAction(action(NickIgnoreCustom));
+
   ignoreMenu->addSeparator();
 
   if(haveWhoData) {
     QMap<QString, bool>::const_iterator ruleIter = ignoreMap.begin();
     int counter = 0;
     if(!ignoreMap.isEmpty())
-      ignoreMenu->addAction(tr("Existing Rules"))->setEnabled(false);
+      // add "Existing Rules" description
+      ignoreMenu->addAction(_ignoreDescriptions.at(1));
     while(ruleIter != ignoreMap.constEnd()) {
       if(counter < 5) {
         ActionType type = static_cast<ActionType>(NickIgnoreToggleEnabled0 + counter*0x100000);
         Action *act = action(type);
         act->setText(ruleIter.key());
+        act->setProperty("ignoreRule", ruleIter.key());
         act->setChecked(ruleIter.value());
         ignoreMenu->addAction(act);
       }
index c6753fa..5bd7f6d 100644 (file)
@@ -71,6 +71,7 @@ private:
   Action *_nickCtcpMenuAction;
   Action *_nickModeMenuAction;
   Action *_nickIgnoreMenuAction;
+  QList<QAction *> _ignoreDescriptions;
 };
 
 #endif
index cb882f8..588071c 100644 (file)
@@ -366,9 +366,6 @@ void NetworkModelController::handleNickAction(ActionType type, QAction *action)
     if(!bufferInfo.isValid())
       continue;
 
-    // the validity of that cast is checked on contextmenu creation, take care
-    IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
-
     switch(type) {
       case NickWhois:
         Client::userInput(bufferInfo, QString("/WHOIS %1 %1").arg(nick));
@@ -414,36 +411,51 @@ void NetworkModelController::handleNickAction(ActionType type, QAction *action)
         Client::userInput(bufferInfo, QString("/QUERY %1").arg(nick));
         break;
       case NickIgnoreUser:
+      {
+        IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
+        if(!ircUser)
+          break;
         Client::ignoreListManager()->requestAddIgnoreListItem(IgnoreListManager::SenderIgnore,
-                                                       action->text(),
+                                                       action->property("ignoreRule").toString(),
                                                        false, IgnoreListManager::SoftStrictness,
                                                        IgnoreListManager::NetworkScope,
                                                        ircUser->network()->networkName(), true);
         break;
+      }
       case NickIgnoreHost:
+      {
+        IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
+        if(!ircUser)
+          break;
         Client::ignoreListManager()->requestAddIgnoreListItem(IgnoreListManager::SenderIgnore,
-                                                       action->text(),
+                                                       action->property("ignoreRule").toString(),
                                                        false, IgnoreListManager::SoftStrictness,
                                                        IgnoreListManager::NetworkScope,
                                                        ircUser->network()->networkName(), true);
         break;
+      }
       case NickIgnoreDomain:
+      {
+        IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
+        if(!ircUser)
+          break;
         Client::ignoreListManager()->requestAddIgnoreListItem(IgnoreListManager::SenderIgnore,
-                                                       action->text(),
+                                                       action->property("ignoreRule").toString(),
                                                        false, IgnoreListManager::SoftStrictness,
                                                        IgnoreListManager::NetworkScope,
                                                        ircUser->network()->networkName(), true);
         break;
+      }
       case NickIgnoreCustom:
         // forward that to mainwin since we can access the settingspage only from there
-        emit showIgnoreList(ircUser->hostmask());
+        emit showIgnoreList(action->property("ignoreRule").toString());
         break;
       case NickIgnoreToggleEnabled0:
       case NickIgnoreToggleEnabled1:
       case NickIgnoreToggleEnabled2:
       case NickIgnoreToggleEnabled3:
       case NickIgnoreToggleEnabled4:
-        Client::ignoreListManager()->requestToggleIgnoreRule(action->text());
+        Client::ignoreListManager()->requestToggleIgnoreRule(action->property("ignoreRule").toString());
         break;
       default:
         qWarning() << "Unhandled nick action";