Fix typos
[quassel.git] / src / qtui / settingspages / highlightsettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2022 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 <algorithm>
24
25 #include <QHeaderView>
26 #include <QMessageBox>
27
28 #include "client.h"
29 #include "icon.h"
30 #include "qtui.h"
31 #include "uisettings.h"
32
33 HighlightSettingsPage::HighlightSettingsPage(QWidget* parent)
34     : SettingsPage(tr("Interface"), tr("Legacy 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({"emblem-information", "dialog-information"}).pixmap(16));
85
86     connect(ui.add, &QAbstractButton::clicked, this, [this]() { addNewRow(); });
87     connect(ui.remove, &QAbstractButton::clicked, this, &HighlightSettingsPage::removeSelectedRows);
88     // TODO: search for a better signal (one that emits every time a selection has been changed for one item)
89     connect(ui.highlightTable, &QTableWidget::itemClicked, this, &HighlightSettingsPage::selectRow);
90
91     connect(ui.highlightAllNicks, &QAbstractButton::clicked, this, &HighlightSettingsPage::widgetHasChanged);
92     connect(ui.highlightCurrentNick, &QAbstractButton::clicked, this, &HighlightSettingsPage::widgetHasChanged);
93     connect(ui.highlightNoNick, &QAbstractButton::clicked, this, &HighlightSettingsPage::widgetHasChanged);
94     connect(ui.nicksCaseSensitive, &QAbstractButton::clicked, this, &HighlightSettingsPage::widgetHasChanged);
95     connect(ui.add, &QAbstractButton::clicked, this, &HighlightSettingsPage::widgetHasChanged);
96     connect(ui.remove, &QAbstractButton::clicked, this, &HighlightSettingsPage::widgetHasChanged);
97     connect(ui.highlightTable, &QTableWidget::itemChanged, this, &HighlightSettingsPage::tableChanged);
98 }
99
100 bool HighlightSettingsPage::hasDefaults() const
101 {
102     return true;
103 }
104
105 void HighlightSettingsPage::defaults()
106 {
107     ui.highlightNoNick->setChecked(true);
108     ui.nicksCaseSensitive->setChecked(false);
109     emptyTable();
110
111     widgetHasChanged();
112 }
113
114 void HighlightSettingsPage::addNewRow(QString name, bool regex, bool cs, bool enable, QString chanName, bool self)
115 {
116     ui.highlightTable->setRowCount(ui.highlightTable->rowCount() + 1);
117
118     QTableWidgetItem* enableItem = new QTableWidgetItem("");
119     if (enable)
120         enableItem->setCheckState(Qt::Checked);
121     else
122         enableItem->setCheckState(Qt::Unchecked);
123     enableItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
124
125     auto* nameItem = new QTableWidgetItem(name);
126
127     QTableWidgetItem* regexItem = new QTableWidgetItem("");
128     if (regex)
129         regexItem->setCheckState(Qt::Checked);
130     else
131         regexItem->setCheckState(Qt::Unchecked);
132     regexItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
133
134     QTableWidgetItem* csItem = new QTableWidgetItem("");
135     if (cs)
136         csItem->setCheckState(Qt::Checked);
137     else
138         csItem->setCheckState(Qt::Unchecked);
139     csItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
140
141     auto* chanNameItem = new QTableWidgetItem(chanName);
142
143     enableItem->setToolTip(tr("Enable/disable this rule"));
144     nameItem->setToolTip(tr("Phrase to match"));
145     regexItem->setToolTip(tr("<b>RegEx</b>: This option determines if the highlight rule and <i>Channel</i> "
146                              "should be interpreted as <b>regular expressions</b> or just as keywords."));
147     csItem->setToolTip(tr("<b>CS</b>: This option determines if the highlight rule and <i>Channel</i> "
148                           "should be interpreted <b>case sensitive</b>."));
149     chanNameItem->setToolTip(tr("<p><b>Channel</b>: Semicolon separated list of channel/query names, leave "
150                                 "blank to match any name.</p>"
151                                 "<p><i>Example:</i><br />"
152                                 "<i>#quassel*; #foobar; !#quasseldroid</i><br />"
153                                 "would match on <i>#foobar</i> and any channel starting with <i>#quassel</i> "
154                                 "except for <i>#quasseldroid</i><br />"
155                                 "<p>If only inverted names are specified, it will match anything except for "
156                                 "what's specified (implicit wildcard).</p>"
157                                 "<p><i>Example:</i><br />"
158                                 "<i>!#quassel*; !#foobar</i><br />"
159                                 "would match anything except for <i>#foobar</i> or any channel starting with "
160                                 "<i>#quassel</i></p>"));
161
162     int lastRow = ui.highlightTable->rowCount() - 1;
163     ui.highlightTable->setItem(lastRow, HighlightSettingsPage::EnableColumn, enableItem);
164     ui.highlightTable->setItem(lastRow, HighlightSettingsPage::NameColumn, nameItem);
165     ui.highlightTable->setItem(lastRow, HighlightSettingsPage::RegExColumn, regexItem);
166     ui.highlightTable->setItem(lastRow, HighlightSettingsPage::CsColumn, csItem);
167     ui.highlightTable->setItem(lastRow, HighlightSettingsPage::ChanColumn, chanNameItem);
168
169     if (!self)
170         ui.highlightTable->setCurrentItem(nameItem);
171
172     QVariantMap highlightRule;
173     highlightRule["Name"] = name;
174     highlightRule["RegEx"] = regex;
175     highlightRule["CS"] = cs;
176     highlightRule["Enable"] = enable;
177     highlightRule["Channel"] = chanName;
178
179     highlightList.append(highlightRule);
180 }
181
182 void HighlightSettingsPage::removeSelectedRows()
183 {
184     QList<int> selectedRows;
185     QList<QTableWidgetItem*> selectedItemList = ui.highlightTable->selectedItems();
186     foreach (QTableWidgetItem* selectedItem, selectedItemList) {
187         selectedRows.append(selectedItem->row());
188     }
189     std::sort(selectedRows.begin(), selectedRows.end(), std::greater<>());
190     int lastRow = -1;
191     foreach (int row, selectedRows) {
192         if (row != lastRow) {
193             ui.highlightTable->removeRow(row);
194             highlightList.removeAt(row);
195         }
196         lastRow = row;
197     }
198 }
199
200 void HighlightSettingsPage::selectRow(QTableWidgetItem* item)
201 {
202     int row = item->row();
203     bool selected = item->isSelected();
204     ui.highlightTable->setRangeSelected(QTableWidgetSelectionRange(row, 0, row, HighlightSettingsPage::ColumnCount - 1), selected);
205 }
206
207 void HighlightSettingsPage::emptyTable()
208 {
209     // ui.highlight and highlightList should have the same size, but just to make sure.
210     if (ui.highlightTable->rowCount() != highlightList.size()) {
211         qDebug() << "something is wrong: ui.highlight and highlightList don't have the same size!";
212     }
213     while (ui.highlightTable->rowCount()) {
214         ui.highlightTable->removeRow(0);
215     }
216     while (highlightList.size()) {
217         highlightList.removeLast();
218     }
219 }
220
221 void HighlightSettingsPage::tableChanged(QTableWidgetItem* item)
222 {
223     if (item->row() + 1 > highlightList.size())
224         return;
225
226     QVariantMap highlightRule = highlightList.value(item->row()).toMap();
227
228     switch (item->column()) {
229     case HighlightSettingsPage::EnableColumn:
230         highlightRule["Enable"] = (item->checkState() == Qt::Checked);
231         break;
232     case HighlightSettingsPage::NameColumn:
233         if (item->text() == "")
234             item->setText(tr("this shouldn't be empty"));
235         highlightRule["Name"] = item->text();
236         break;
237     case HighlightSettingsPage::RegExColumn:
238         highlightRule["RegEx"] = (item->checkState() == Qt::Checked);
239         break;
240     case HighlightSettingsPage::CsColumn:
241         highlightRule["CS"] = (item->checkState() == Qt::Checked);
242         break;
243     case HighlightSettingsPage::ChanColumn:
244         if (!item->text().isEmpty() && item->text().trimmed().isEmpty())
245             item->setText("");
246         highlightRule["Channel"] = item->text();
247         break;
248     }
249     highlightList[item->row()] = highlightRule;
250     emit widgetHasChanged();
251 }
252
253 void HighlightSettingsPage::on_localHighlightsDetails_clicked()
254 {
255     // Discourage the use of local (legacy) highlights.  When not running in Monolithic mode, they
256     // need to be kept around for pre-0.13 cores.
257     QMessageBox::information(this,
258                              tr("Legacy Highlights vs. Highlights"),
259                              QString("<p><b>%1</b></p></br><p>%2</p></br><p>%3</p>")
260                              .arg(tr("Legacy Highlights are replaced by Highlights"),
261                                   tr("These highlights will keep working for now, but you should move to "
262                                      "the improved highlight rules when you can."),
263                                   tr("Configure the new style of highlights in <i>%1</i>.")
264                                   .arg(tr("Highlights"))));
265 }
266
267 void HighlightSettingsPage::load()
268 {
269     NotificationSettings notificationSettings;
270
271     emptyTable();
272
273     foreach (QVariant highlight, notificationSettings.highlightList()) {
274         QVariantMap highlightRule = highlight.toMap();
275         QString name = highlightRule["Name"].toString();
276         bool regex = highlightRule["RegEx"].toBool();
277         bool cs = highlightRule["CS"].toBool();
278         bool enable = highlightRule["Enable"].toBool();
279         QString chanName = highlightRule["Channel"].toString();
280
281         addNewRow(name, regex, cs, enable, chanName, true);
282     }
283
284     switch (notificationSettings.highlightNick()) {
285     case NotificationSettings::NoNick:
286         ui.highlightNoNick->setChecked(true);
287         break;
288     case NotificationSettings::CurrentNick:
289         ui.highlightCurrentNick->setChecked(true);
290         break;
291     case NotificationSettings::AllNicks:
292         ui.highlightAllNicks->setChecked(true);
293         break;
294     }
295     ui.nicksCaseSensitive->setChecked(notificationSettings.nicksCaseSensitive());
296
297     setChangedState(false);
298 }
299
300 void HighlightSettingsPage::save()
301 {
302     NotificationSettings notificationSettings;
303     notificationSettings.setHighlightList(highlightList);
304
305     NotificationSettings::HighlightNickType highlightNickType = NotificationSettings::NoNick;
306     if (ui.highlightCurrentNick->isChecked())
307         highlightNickType = NotificationSettings::CurrentNick;
308     if (ui.highlightAllNicks->isChecked())
309         highlightNickType = NotificationSettings::AllNicks;
310
311     notificationSettings.setHighlightNick(highlightNickType);
312     notificationSettings.setNicksCaseSensitive(ui.nicksCaseSensitive->isChecked());
313
314     load();
315     setChangedState(false);
316 }
317
318 void HighlightSettingsPage::widgetHasChanged()
319 {
320     bool changed = testHasChanged();
321     if (changed != hasChanged())
322         setChangedState(changed);
323 }
324
325 bool HighlightSettingsPage::testHasChanged()
326 {
327     NotificationSettings notificationSettings;
328
329     NotificationSettings::HighlightNickType highlightNickType = NotificationSettings::NoNick;
330     if (ui.highlightCurrentNick->isChecked())
331         highlightNickType = NotificationSettings::CurrentNick;
332     if (ui.highlightAllNicks->isChecked())
333         highlightNickType = NotificationSettings::AllNicks;
334
335     if (notificationSettings.highlightNick() != highlightNickType)
336         return true;
337     if (notificationSettings.nicksCaseSensitive() != ui.nicksCaseSensitive->isChecked())
338         return true;
339     if (notificationSettings.highlightList() != highlightList)
340         return true;
341
342     return false;
343 }