1 /***************************************************************************
2 * Copyright (C) 2005-2019 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "highlightsettingspage.h"
25 #include <QHeaderView>
26 #include <QMessageBox>
31 #include "uisettings.h"
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"),
40 ui.highlightTable->verticalHeader()->hide();
41 ui.highlightTable->setShowGrid(false);
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());
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());
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());
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());
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());
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);
86 ui.localHighlightsIcon->setPixmap(icon::get("dialog-information").pixmap(16));
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"));
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"));
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);
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);
113 bool HighlightSettingsPage::hasDefaults() const
118 void HighlightSettingsPage::defaults()
120 ui.highlightCurrentNick->setChecked(true);
121 ui.nicksCaseSensitive->setChecked(false);
127 void HighlightSettingsPage::addNewRow(QString name, bool regex, bool cs, bool enable, QString chanName, bool self)
129 ui.highlightTable->setRowCount(ui.highlightTable->rowCount() + 1);
131 QTableWidgetItem* enableItem = new QTableWidgetItem("");
133 enableItem->setCheckState(Qt::Checked);
135 enableItem->setCheckState(Qt::Unchecked);
136 enableItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
138 auto* nameItem = new QTableWidgetItem(name);
140 QTableWidgetItem* regexItem = new QTableWidgetItem("");
142 regexItem->setCheckState(Qt::Checked);
144 regexItem->setCheckState(Qt::Unchecked);
145 regexItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
147 QTableWidgetItem* csItem = new QTableWidgetItem("");
149 csItem->setCheckState(Qt::Checked);
151 csItem->setCheckState(Qt::Unchecked);
152 csItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
154 auto* chanNameItem = new QTableWidgetItem(chanName);
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>"));
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);
183 ui.highlightTable->setCurrentItem(nameItem);
185 QVariantMap highlightRule;
186 highlightRule["Name"] = name;
187 highlightRule["RegEx"] = regex;
188 highlightRule["CS"] = cs;
189 highlightRule["Enable"] = enable;
190 highlightRule["Channel"] = chanName;
192 highlightList.append(highlightRule);
195 void HighlightSettingsPage::removeSelectedRows()
197 QList<int> selectedRows;
198 QList<QTableWidgetItem*> selectedItemList = ui.highlightTable->selectedItems();
199 foreach (QTableWidgetItem* selectedItem, selectedItemList) {
200 selectedRows.append(selectedItem->row());
202 std::sort(selectedRows.begin(), selectedRows.end(), std::greater<>());
204 foreach (int row, selectedRows) {
205 if (row != lastRow) {
206 ui.highlightTable->removeRow(row);
207 highlightList.removeAt(row);
213 void HighlightSettingsPage::selectRow(QTableWidgetItem* item)
215 int row = item->row();
216 bool selected = item->isSelected();
217 ui.highlightTable->setRangeSelected(QTableWidgetSelectionRange(row, 0, row, HighlightSettingsPage::ColumnCount - 1), selected);
220 void HighlightSettingsPage::emptyTable()
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!";
226 while (ui.highlightTable->rowCount()) {
227 ui.highlightTable->removeRow(0);
229 while (highlightList.size()) {
230 highlightList.removeLast();
234 void HighlightSettingsPage::tableChanged(QTableWidgetItem* item)
236 if (item->row() + 1 > highlightList.size())
239 QVariantMap highlightRule = highlightList.value(item->row()).toMap();
241 switch (item->column()) {
242 case HighlightSettingsPage::EnableColumn:
243 highlightRule["Enable"] = (item->checkState() == Qt::Checked);
245 case HighlightSettingsPage::NameColumn:
246 if (item->text() == "")
247 item->setText(tr("this shouldn't be empty"));
248 highlightRule["Name"] = item->text();
250 case HighlightSettingsPage::RegExColumn:
251 highlightRule["RegEx"] = (item->checkState() == Qt::Checked);
253 case HighlightSettingsPage::CsColumn:
254 highlightRule["CS"] = (item->checkState() == Qt::Checked);
256 case HighlightSettingsPage::ChanColumn:
257 if (!item->text().isEmpty() && item->text().trimmed().isEmpty())
259 highlightRule["Channel"] = item->text();
262 highlightList[item->row()] = highlightRule;
263 emit widgetHasChanged();
266 void HighlightSettingsPage::on_localHighlightsDetails_clicked()
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 "
280 .arg(tr("Highlights"))));
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 "
290 tr("Configure highlights for all of your devices in "
292 .arg(tr("Remote Highlights").replace(" ", " "))));
293 // Re-use translations of "Remote Highlights" as this is a word-for-word reference, forcing
294 // all spaces to be non-breaking
298 void HighlightSettingsPage::load()
300 NotificationSettings notificationSettings;
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();
312 addNewRow(name, regex, cs, enable, chanName, true);
315 switch (notificationSettings.highlightNick()) {
316 case NotificationSettings::NoNick:
317 ui.highlightNoNick->setChecked(true);
319 case NotificationSettings::CurrentNick:
320 ui.highlightCurrentNick->setChecked(true);
322 case NotificationSettings::AllNicks:
323 ui.highlightAllNicks->setChecked(true);
326 ui.nicksCaseSensitive->setChecked(notificationSettings.nicksCaseSensitive());
328 setChangedState(false);
331 void HighlightSettingsPage::save()
333 NotificationSettings notificationSettings;
334 notificationSettings.setHighlightList(highlightList);
336 NotificationSettings::HighlightNickType highlightNickType = NotificationSettings::NoNick;
337 if (ui.highlightCurrentNick->isChecked())
338 highlightNickType = NotificationSettings::CurrentNick;
339 if (ui.highlightAllNicks->isChecked())
340 highlightNickType = NotificationSettings::AllNicks;
342 notificationSettings.setHighlightNick(highlightNickType);
343 notificationSettings.setNicksCaseSensitive(ui.nicksCaseSensitive->isChecked());
346 setChangedState(false);
349 void HighlightSettingsPage::widgetHasChanged()
351 bool changed = testHasChanged();
352 if (changed != hasChanged())
353 setChangedState(changed);
356 bool HighlightSettingsPage::testHasChanged()
358 NotificationSettings notificationSettings;
360 NotificationSettings::HighlightNickType highlightNickType = NotificationSettings::NoNick;
361 if (ui.highlightCurrentNick->isChecked())
362 highlightNickType = NotificationSettings::CurrentNick;
363 if (ui.highlightAllNicks->isChecked())
364 highlightNickType = NotificationSettings::AllNicks;
366 if (notificationSettings.highlightNick() != highlightNickType)
368 if (notificationSettings.nicksCaseSensitive() != ui.nicksCaseSensitive->isChecked())
370 if (notificationSettings.highlightList() != highlightList)