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