Ignorelist settingspage
[quassel.git] / src / qtui / settingspages / ignorelistsettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "ignorelistsettingspage.h"
22
23 #include <QHeaderView>
24 #include <QItemSelectionModel>
25 #include <QModelIndex>
26 #include <QPainter>
27 #include <QMessageBox>
28 #include <QString>
29 #include <QEvent>
30
31 #include "iconloader.h"
32
33 IgnoreListSettingsPage::IgnoreListSettingsPage(QWidget *parent)
34   : SettingsPage(tr("Misc"), tr("Ignorelist"), parent)
35 {
36   ui.setupUi(this);
37   _delegate = new IgnoreListDelegate(ui.ignoreListView);
38   ui.newIgnoreRuleButton->setIcon(SmallIcon("list-add"));
39   ui.deleteIgnoreRuleButton->setIcon(SmallIcon("edit-delete"));
40   ui.editIgnoreRuleButton->setIcon(SmallIcon("configure"));
41
42   ui.ignoreListView->setSelectionBehavior(QAbstractItemView::SelectRows);
43   ui.ignoreListView->setSelectionMode(QAbstractItemView::SingleSelection);
44   ui.ignoreListView->setAlternatingRowColors(true);
45   ui.ignoreListView->setTabKeyNavigation(false);
46   ui.ignoreListView->setModel(&_ignoreListModel);
47  // ui.ignoreListView->setEditTriggers(QAbstractItemView::NoEditTriggers);
48
49   // ui.ignoreListView->setSortingEnabled(true);
50   ui.ignoreListView->verticalHeader()->hide();
51   ui.ignoreListView->hideColumn(1);
52   ui.ignoreListView->resizeColumnToContents(0);
53   ui.ignoreListView->horizontalHeader()->setStretchLastSection(true);
54   ui.ignoreListView->setItemDelegateForColumn(0, _delegate);
55   ui.ignoreListView->viewport()->setAttribute(Qt::WA_Hover);
56   ui.ignoreListView->viewport()->setMouseTracking(true);
57
58   connect(ui.ignoreListView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), this, SLOT(selectionChanged(const QItemSelection &, const QItemSelection &)));
59   connect(ui.newIgnoreRuleButton, SIGNAL(clicked()), this, SLOT(newIgnoreRule()));
60   connect(ui.deleteIgnoreRuleButton, SIGNAL(clicked()), this, SLOT(deleteSelectedIgnoreRule()));
61   connect(ui.editIgnoreRuleButton, SIGNAL(clicked()), this, SLOT(editSelectedIgnoreRule()));
62   connect(&_ignoreListModel, SIGNAL(configChanged(bool)), this, SLOT(setChangedState(bool)));
63   connect(&_ignoreListModel, SIGNAL(modelReady(bool)), this, SLOT(enableDialog(bool)));
64
65   enableDialog(_ignoreListModel.isReady());
66 }
67
68 IgnoreListSettingsPage::~IgnoreListSettingsPage() {
69   delete _delegate;
70 }
71
72 void IgnoreListSettingsPage::load() {
73   if(_ignoreListModel.configChanged())
74     _ignoreListModel.revert();
75 }
76
77 void IgnoreListSettingsPage::defaults() {
78   _ignoreListModel.loadDefaults();
79 }
80
81 void IgnoreListSettingsPage::save() {
82   if(_ignoreListModel.configChanged()) {
83     _ignoreListModel.commit();
84   }
85 }
86
87 void IgnoreListSettingsPage::enableDialog(bool enabled) {
88   ui.newIgnoreRuleButton->setEnabled(enabled);
89   setEnabled(enabled);
90 }
91
92 void IgnoreListSettingsPage::selectionChanged(const QItemSelection &selection, const QItemSelection &) {
93   bool state = !selection.isEmpty();
94   ui.deleteIgnoreRuleButton->setEnabled(state);
95   ui.editIgnoreRuleButton->setEnabled(state);
96 }
97
98 void IgnoreListSettingsPage::deleteSelectedIgnoreRule() {
99   if(!ui.ignoreListView->selectionModel()->hasSelection())
100     return;
101
102   _ignoreListModel.removeIgnoreRule(ui.ignoreListView->selectionModel()->selectedIndexes()[0].row());
103 }
104
105 void IgnoreListSettingsPage::newIgnoreRule() {
106   IgnoreListEditDlg *dlg = new IgnoreListEditDlg(-1, IgnoreListManager::IgnoreListItem(), this);
107
108   while(dlg->exec() == QDialog::Accepted) {
109     if(!_ignoreListModel.newIgnoreRule(dlg->ignoreListItem())) {
110       if(QMessageBox::warning(this,
111                          tr("Rule already exists"),
112                          tr("There is already a rule\n\"%1\"\nPlease choose another rule.")
113                          .arg(dlg->ignoreListItem().ignoreRule),
114                          QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok)
115           == QMessageBox::Cancel)
116         break;
117
118       IgnoreListManager::IgnoreListItem item = dlg->ignoreListItem();
119       delete dlg;
120       dlg = new IgnoreListEditDlg(-1, item, this);
121     }
122     else {
123       break;
124     }
125   }
126   dlg->deleteLater();
127 }
128
129 void IgnoreListSettingsPage::editSelectedIgnoreRule() {
130   if(!ui.ignoreListView->selectionModel()->hasSelection())
131     return;
132   int row = ui.ignoreListView->selectionModel()->selectedIndexes()[0].row();
133   IgnoreListEditDlg dlg(row, _ignoreListModel.ignoreListItemAt(row), this);
134   dlg.setAttribute(Qt::WA_DeleteOnClose, false);
135   if(dlg.exec() == QDialog::Accepted) {
136     _ignoreListModel.setIgnoreListItemAt(row, dlg.ignoreListItem());
137   }
138 }
139
140 void IgnoreListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
141   if(index.column() == 0) {
142     QStyle *style = QApplication::style();
143     if(option.state & QStyle::State_Selected)
144       painter->fillRect(option.rect, option.palette.highlight());
145
146     QStyleOptionButton opts;
147     opts.direction = option.direction;
148     opts.rect = option.rect;
149     opts.rect.moveLeft(option.rect.center().rx()-10);
150     opts.state = option.state;
151     opts.state |= index.data().toBool() ? QStyle::State_On : QStyle::State_Off;
152     style->drawControl(QStyle::CE_CheckBox, &opts, painter);
153   }
154   else
155     QStyledItemDelegate::paint(painter, option, index);
156 }
157
158 IgnoreListEditDlg::IgnoreListEditDlg(int row, const IgnoreListManager::IgnoreListItem &item, QWidget *parent)
159     : QDialog(parent), _selectedRow(row), _ignoreListItem(item), _hasChanged(false) {
160   ui.setupUi(this);
161   setAttribute(Qt::WA_DeleteOnClose, false);
162   setModal(true);
163
164   ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
165
166   ui.ignoreRuleLineEdit->setText(item.ignoreRule);
167
168   if(item.type == IgnoreListManager::MessageIgnore)
169     ui.messageTypeButton->setChecked(true);
170   else
171     ui.senderTypeButton->setChecked(true);
172
173   ui.isRegExCheckBox->setChecked(item.isRegEx);
174   ui.isActiveCheckBox->setChecked(item.isActive);
175
176   if(item.strictness == IgnoreListManager::HardStrictness)
177     ui.permanentStrictnessButton->setChecked(true);
178   else
179     ui.dynamicStrictnessButton->setChecked(true);
180
181   switch(item.scope) {
182     case IgnoreListManager::NetworkScope:
183       ui.networkScopeButton->setChecked(true);
184       ui.scopeRuleTextEdit->setEnabled(true);
185       break;
186     case IgnoreListManager::ChannelScope:
187       ui.channelScopeButton->setChecked(true);
188       ui.scopeRuleTextEdit->setEnabled(true);
189       break;
190     default:
191       ui.globalScopeButton->setChecked(true);
192       ui.scopeRuleTextEdit->setEnabled(false);
193   }
194
195   if(item.scope == IgnoreListManager::GlobalScope)
196     ui.scopeRuleTextEdit->clear();
197   else
198     ui.scopeRuleTextEdit->setPlainText(item.scopeRule);
199
200   connect(ui.ignoreRuleLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(widgetHasChanged()));
201   connect(ui.scopeRuleTextEdit, SIGNAL(textChanged()), this, SLOT(widgetHasChanged()));
202   connect(ui.typeButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(widgetHasChanged()));
203   connect(ui.strictnessButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(widgetHasChanged()));
204   connect(ui.scopeButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(widgetHasChanged()));
205   connect(ui.typeButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(widgetHasChanged()));
206   connect(ui.isRegExCheckBox, SIGNAL(stateChanged(int)), this, SLOT(widgetHasChanged()));
207   connect(ui.isRegExCheckBox, SIGNAL(stateChanged(int)), this, SLOT(widgetHasChanged()));
208   connect(ui.isActiveCheckBox, SIGNAL(stateChanged(int)), this, SLOT(widgetHasChanged()));
209
210   connect(ui.buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(aboutToAccept()));
211 }
212
213 void IgnoreListEditDlg::widgetHasChanged() {
214   if(ui.messageTypeButton->isChecked())
215     _clonedIgnoreListItem.type = IgnoreListManager::MessageIgnore;
216   else
217     _clonedIgnoreListItem.type = IgnoreListManager::SenderIgnore;
218
219   if(ui.permanentStrictnessButton->isChecked())
220     _clonedIgnoreListItem.strictness = IgnoreListManager::HardStrictness;
221   else
222     _clonedIgnoreListItem.strictness = IgnoreListManager::SoftStrictness;
223
224   if(ui.networkScopeButton->isChecked()) {
225     _clonedIgnoreListItem.scope = IgnoreListManager::NetworkScope;
226     ui.scopeRuleTextEdit->setEnabled(true);
227   }
228   else if(ui.channelScopeButton->isChecked()) {
229     _clonedIgnoreListItem.scope = IgnoreListManager::ChannelScope;
230     ui.scopeRuleTextEdit->setEnabled(true);
231   }
232   else {
233     _clonedIgnoreListItem.scope = IgnoreListManager::GlobalScope;
234     ui.scopeRuleTextEdit->setEnabled(false);
235   }
236
237   if(_clonedIgnoreListItem.scope == IgnoreListManager::GlobalScope) {
238     _clonedIgnoreListItem.scopeRule = QString();
239   }
240   else {
241     QStringList text = ui.scopeRuleTextEdit->toPlainText().split(";", QString::SkipEmptyParts);
242     QStringList::iterator it = text.begin();
243     while(it != text.end())
244       (*it++).trimmed();
245
246     _clonedIgnoreListItem.scopeRule = text.join("; ");
247   }
248
249   _clonedIgnoreListItem.ignoreRule = ui.ignoreRuleLineEdit->text();
250   _clonedIgnoreListItem.isRegEx = ui.isRegExCheckBox->isChecked();
251   _clonedIgnoreListItem.isActive = ui.isActiveCheckBox->isChecked();
252
253   if(!_clonedIgnoreListItem.ignoreRule.isEmpty() && _clonedIgnoreListItem != _ignoreListItem)
254     _hasChanged = true;
255   else
256     _hasChanged = false;
257   ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(_hasChanged);
258 }
259
260 // provide interactivity for the checkboxes
261 bool IgnoreListDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
262                                      const QStyleOptionViewItem &option, const QModelIndex &index) {
263   Q_UNUSED(option)
264   switch(event->type()) {
265     case QEvent::MouseButtonRelease:
266       model->setData(index, !index.data().toBool());
267       return true;
268       // don't show the default editor for the column
269     case QEvent::MouseButtonDblClick:
270       return true;
271     default:
272       return false;
273   }
274 }