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