7a000e9b742a7d3e1dc1fd9f8cfb153eb429f5e1
[quassel.git] / src / qtui / settingspages / highlightsettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "highlightsettingspage.h"
22
23 #include "client.h"
24 #include "qtui.h"
25 #include "uisettings.h"
26
27 #include <QHeaderView>
28 #include <QMessageBox>
29
30 HighlightSettingsPage::HighlightSettingsPage(QWidget *parent)
31     : SettingsPage(tr("Interface"), tr("Local Highlights"), parent)
32 {
33     ui.setupUi(this);
34     ui.highlightTable->verticalHeader()->hide();
35     ui.highlightTable->setShowGrid(false);
36
37     ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::RegExColumn)->setToolTip(
38                 tr("<b>RegEx</b>: This option determines if the highlight rule should be "
39                    "interpreted as a <b>regular expression</b> or just as a keyword."));
40     ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::RegExColumn)->setWhatsThis(
41                 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::RegExColumn)->toolTip());
42
43     ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::CsColumn)->setToolTip(
44                 tr("<b>CS</b>: This option determines if the highlight rule should be interpreted "
45                    "<b>case sensitive</b>."));
46     ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::CsColumn)->setWhatsThis(
47                 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::CsColumn)->toolTip());
48
49     ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::ChanColumn)->setToolTip(
50                 tr("<p><b>Channel</b>: Semicolon separated list of channel names.</p>"
51                    "<p><i>Example:</i><br />"
52                    "<i>#quassel*; #foobar; !#quasseldroid</i><br />"
53                    "would match on #foobar and on any channel starting with <i>#quassel</i> except "
54                    "for <i>#quasseldroid</i><br />"
55                    "<p>If only inverted names are specified, it will match anything except for "
56                    "what's specified (implicit wildcard).</p>"
57                    "<p><i>Example:</i><br />"
58                    "<i>!#quassel*; !#foobar</i><br />"
59                    "would match anything except for #foobar or any channel starting with "
60                    "<i>#quassel</i></p>"));
61     ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::ChanColumn)->setWhatsThis(
62                 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::ChanColumn)->toolTip());
63
64 #if QT_VERSION < 0x050000
65     ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::NameColumn, QHeaderView::Stretch);
66     ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::RegExColumn, QHeaderView::ResizeToContents);
67     ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::CsColumn, QHeaderView::ResizeToContents);
68     ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::EnableColumn, QHeaderView::ResizeToContents);
69     ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::ChanColumn, QHeaderView::ResizeToContents);
70 #else
71     ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::NameColumn, QHeaderView::Stretch);
72     ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::RegExColumn, QHeaderView::ResizeToContents);
73     ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::CsColumn, QHeaderView::ResizeToContents);
74     ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::EnableColumn, QHeaderView::ResizeToContents);
75     ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::ChanColumn, QHeaderView::ResizeToContents);
76 #endif
77
78     // Information icon
79     ui.localHighlightsIcon->setPixmap(QIcon::fromTheme("dialog-information").pixmap(16));
80
81     // Set up client/monolithic local highlights information
82     if (Quassel::runMode() == Quassel::Monolithic) {
83         // We're running in Monolithic mode, core/client version in total sync.  Discourage the use
84         // of local highlights as it's identical to setting remote highlights.
85         ui.localHighlightsLabel->setText(
86                     tr("Local Highlights are replaced by Remote Highlights"));
87     } else {
88         // We're running in client/split mode, allow for splitting the details.
89         ui.localHighlightsLabel->setText(tr("Local Highlights apply to this device only"));
90     }
91
92     connect(ui.add, SIGNAL(clicked(bool)), this, SLOT(addNewRow()));
93     connect(ui.remove, SIGNAL(clicked(bool)), this, SLOT(removeSelectedRows()));
94     //TODO: search for a better signal (one that emits everytime a selection has been changed for one item)
95     connect(ui.highlightTable, SIGNAL(itemClicked(QTableWidgetItem *)), this, SLOT(selectRow(QTableWidgetItem *)));
96
97     connect(ui.highlightAllNicks, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
98     connect(ui.highlightCurrentNick, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
99     connect(ui.highlightNoNick, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
100     connect(ui.nicksCaseSensitive, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
101     connect(ui.add, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
102     connect(ui.remove, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
103     connect(ui.highlightTable, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(tableChanged(QTableWidgetItem *)));
104 }
105
106
107 bool HighlightSettingsPage::hasDefaults() const
108 {
109     return true;
110 }
111
112
113 void HighlightSettingsPage::defaults()
114 {
115     ui.highlightNoNick->setChecked(true);
116     ui.nicksCaseSensitive->setChecked(false);
117     emptyTable();
118
119     widgetHasChanged();
120 }
121
122
123 void HighlightSettingsPage::addNewRow(QString name, bool regex, bool cs, bool enable, QString chanName, bool self)
124 {
125     ui.highlightTable->setRowCount(ui.highlightTable->rowCount()+1);
126
127     QTableWidgetItem *enableItem = new QTableWidgetItem("");
128     if (enable)
129         enableItem->setCheckState(Qt::Checked);
130     else
131         enableItem->setCheckState(Qt::Unchecked);
132     enableItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
133
134     QTableWidgetItem *nameItem = new QTableWidgetItem(name);
135
136     QTableWidgetItem *regexItem = new QTableWidgetItem("");
137     if (regex)
138         regexItem->setCheckState(Qt::Checked);
139     else
140         regexItem->setCheckState(Qt::Unchecked);
141     regexItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
142
143     QTableWidgetItem *csItem = new QTableWidgetItem("");
144     if (cs)
145         csItem->setCheckState(Qt::Checked);
146     else
147         csItem->setCheckState(Qt::Unchecked);
148     csItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
149
150     QTableWidgetItem *chanNameItem = new QTableWidgetItem(chanName);
151
152     enableItem->setToolTip(tr("Enable/disable this rule"));
153     nameItem->setToolTip(tr("Phrase to match"));
154     regexItem->setToolTip(
155                 tr("<b>RegEx</b>: This option determines if the highlight rule should be "
156                    "interpreted as a <b>regular expression</b> or just as a keyword."));
157     csItem->setToolTip(
158                 tr("<b>CS</b>: This option determines if the highlight rule should be interpreted "
159                    "<b>case sensitive</b>."));
160     chanNameItem->setToolTip(
161                 tr("<p><b>Channel</b>: Semicolon separated list of channel names.</p>"
162                    "<p><i>Example:</i><br />"
163                    "<i>#quassel*; #foobar; !#quasseldroid</i><br />"
164                    "would match on #foobar and on any channel starting with <i>#quassel</i> except "
165                    "for <i>#quasseldroid</i><br />"
166                    "<p>If only inverted names are specified, it will match anything except for "
167                    "what's specified (implicit wildcard).</p>"
168                    "<p><i>Example:</i><br />"
169                    "<i>!#quassel*; !#foobar</i><br />"
170                    "would match anything except for #foobar or any channel starting with "
171                    "<i>#quassel</i></p>"));
172
173     int lastRow = ui.highlightTable->rowCount()-1;
174     ui.highlightTable->setItem(lastRow, HighlightSettingsPage::EnableColumn, enableItem);
175     ui.highlightTable->setItem(lastRow, HighlightSettingsPage::NameColumn, nameItem);
176     ui.highlightTable->setItem(lastRow, HighlightSettingsPage::RegExColumn, regexItem);
177     ui.highlightTable->setItem(lastRow, HighlightSettingsPage::CsColumn, csItem);
178     ui.highlightTable->setItem(lastRow, HighlightSettingsPage::ChanColumn, chanNameItem);
179
180     if (!self)
181         ui.highlightTable->setCurrentItem(nameItem);
182
183     QVariantMap highlightRule;
184     highlightRule["Name"] = name;
185     highlightRule["RegEx"] = regex;
186     highlightRule["CS"] = cs;
187     highlightRule["Enable"] = enable;
188     highlightRule["Channel"] = chanName;
189
190     highlightList.append(highlightRule);
191 }
192
193
194 void HighlightSettingsPage::removeSelectedRows()
195 {
196     QList<int> selectedRows;
197     QList<QTableWidgetItem *> selectedItemList = ui.highlightTable->selectedItems();
198     foreach(QTableWidgetItem *selectedItem, selectedItemList) {
199         selectedRows.append(selectedItem->row());
200     }
201     qSort(selectedRows.begin(), selectedRows.end(), qGreater<int>());
202     int lastRow = -1;
203     foreach(int row, selectedRows) {
204         if (row != lastRow) {
205             ui.highlightTable->removeRow(row);
206             highlightList.removeAt(row);
207         }
208         lastRow = row;
209     }
210 }
211
212
213 void HighlightSettingsPage::selectRow(QTableWidgetItem *item)
214 {
215     int row = item->row();
216     bool selected = item->isSelected();
217     ui.highlightTable->setRangeSelected(QTableWidgetSelectionRange(row, 0, row, HighlightSettingsPage::ColumnCount-1), selected);
218 }
219
220
221 void HighlightSettingsPage::emptyTable()
222 {
223     // ui.highlight and highlightList should have the same size, but just to make sure.
224     if (ui.highlightTable->rowCount() != highlightList.size()) {
225         qDebug() << "something is wrong: ui.highlight and highlightList don't have the same size!";
226     }
227     while (ui.highlightTable->rowCount()) {
228         ui.highlightTable->removeRow(0);
229     }
230     while (highlightList.size()) {
231         highlightList.removeLast();
232     }
233 }
234
235
236 void HighlightSettingsPage::tableChanged(QTableWidgetItem *item)
237 {
238     if (item->row()+1 > highlightList.size())
239         return;
240
241     QVariantMap highlightRule = highlightList.value(item->row()).toMap();
242
243     switch (item->column())
244     {
245     case HighlightSettingsPage::EnableColumn:
246         highlightRule["Enable"] = (item->checkState() == Qt::Checked);
247         break;
248     case HighlightSettingsPage::NameColumn:
249         if (item->text() == "")
250             item->setText(tr("this shouldn't be empty"));
251         highlightRule["Name"] = item->text();
252         break;
253     case HighlightSettingsPage::RegExColumn:
254         highlightRule["RegEx"] = (item->checkState() == Qt::Checked);
255         break;
256     case HighlightSettingsPage::CsColumn:
257         highlightRule["CS"] = (item->checkState() == Qt::Checked);
258         break;
259     case HighlightSettingsPage::ChanColumn:
260         if (!item->text().isEmpty() && item->text().trimmed().isEmpty())
261             item->setText("");
262         highlightRule["Channel"] = item->text();
263         break;
264     }
265     highlightList[item->row()] = highlightRule;
266     emit widgetHasChanged();
267 }
268
269
270 void HighlightSettingsPage::on_localHighlightsDetails_clicked()
271 {
272     // Re-use translations of "Remote Highlights" as this is a word-for-word reference, forcing all
273     // spaces to non-breaking
274     const QString remoteHighlightsName = tr("Remote Highlights").replace(" ", "&nbsp;");
275     QString localHighlightsMsgText;
276
277     // Set up client/monolithic local highlights information
278     if (Quassel::runMode() == Quassel::Monolithic) {
279         // We're running in Monolithic mode, core/client version in total sync.  Discourage the use
280         // of local highlights as it's identical to setting remote highlights.
281         localHighlightsMsgText =
282                 QString("<p><b>%1</b></p></br><p>%2</p></br><p>%3</p>"
283                         ).arg(tr("Local Highlights are replaced by Remote Highlights"),
284                               tr("These highlights will keep working for now, but you should move "
285                                  "to the improved highlight rules when you can."),
286                               tr("Configure the new style of highlights in "
287                                  "<i>%1</i>.").arg(remoteHighlightsName));
288     } else {
289         // We're running in client/split mode, allow for splitting the details.
290         localHighlightsMsgText =
291                 QString("<p><b>%1</b></p></br><p>%2</p></br><p>%3</p>"
292                         ).arg(tr("Local Highlights apply to this device only"),
293                               tr("Highlights configured on this page only apply to your current "
294                                  "device."),
295                               tr("Configure highlights for all of your devices in "
296                                  "<i>%1</i>.").arg(remoteHighlightsName));
297     }
298
299     QMessageBox::information(this,
300                              tr("Local Highlights vs. Remote Highlights"),
301                              localHighlightsMsgText);
302 }
303
304
305 void HighlightSettingsPage::load()
306 {
307     NotificationSettings notificationSettings;
308
309     emptyTable();
310
311     foreach(QVariant highlight, notificationSettings.highlightList()) {
312         QVariantMap highlightRule = highlight.toMap();
313         QString name = highlightRule["Name"].toString();
314         bool regex = highlightRule["RegEx"].toBool();
315         bool cs = highlightRule["CS"].toBool();
316         bool enable = highlightRule["Enable"].toBool();
317         QString chanName = highlightRule["Channel"].toString();
318
319         addNewRow(name, regex, cs, enable, chanName, true);
320     }
321
322     switch (notificationSettings.highlightNick())
323     {
324     case NotificationSettings::NoNick:
325         ui.highlightNoNick->setChecked(true);
326         break;
327     case NotificationSettings::CurrentNick:
328         ui.highlightCurrentNick->setChecked(true);
329         break;
330     case NotificationSettings::AllNicks:
331         ui.highlightAllNicks->setChecked(true);
332         break;
333     }
334     ui.nicksCaseSensitive->setChecked(notificationSettings.nicksCaseSensitive());
335
336     setChangedState(false);
337 }
338
339
340 void HighlightSettingsPage::save()
341 {
342     NotificationSettings notificationSettings;
343     notificationSettings.setHighlightList(highlightList);
344
345     NotificationSettings::HighlightNickType highlightNickType = NotificationSettings::NoNick;
346     if (ui.highlightCurrentNick->isChecked())
347         highlightNickType = NotificationSettings::CurrentNick;
348     if (ui.highlightAllNicks->isChecked())
349         highlightNickType = NotificationSettings::AllNicks;
350
351     notificationSettings.setHighlightNick(highlightNickType);
352     notificationSettings.setNicksCaseSensitive(ui.nicksCaseSensitive->isChecked());
353
354     load();
355     setChangedState(false);
356 }
357
358
359 void HighlightSettingsPage::widgetHasChanged()
360 {
361     bool changed = testHasChanged();
362     if (changed != hasChanged()) setChangedState(changed);
363 }
364
365
366 bool HighlightSettingsPage::testHasChanged()
367 {
368     NotificationSettings notificationSettings;
369
370     NotificationSettings::HighlightNickType highlightNickType = NotificationSettings::NoNick;
371     if (ui.highlightCurrentNick->isChecked())
372         highlightNickType = NotificationSettings::CurrentNick;
373     if (ui.highlightAllNicks->isChecked())
374         highlightNickType = NotificationSettings::AllNicks;
375
376     if (notificationSettings.highlightNick() != highlightNickType)
377         return true;
378     if (notificationSettings.nicksCaseSensitive() != ui.nicksCaseSensitive->isChecked())
379         return true;
380     if (notificationSettings.highlightList() != highlightList)
381         return true;
382
383     return false;
384 }