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