1 /***************************************************************************
2 * Copyright (C) 2005-2020 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"), tr("Legacy Highlights"),
38 ui.highlightTable->verticalHeader()->hide();
39 ui.highlightTable->setShowGrid(false);
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());
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());
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());
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());
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());
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);
84 ui.localHighlightsIcon->setPixmap(icon::get({"emblem-information", "dialog-information"}).pixmap(16));
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 everytime a selection has been changed for one item)
89 connect(ui.highlightTable, &QTableWidget::itemClicked, this, &HighlightSettingsPage::selectRow);
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);
100 bool HighlightSettingsPage::hasDefaults() const
105 void HighlightSettingsPage::defaults()
107 ui.highlightNoNick->setChecked(true);
108 ui.nicksCaseSensitive->setChecked(false);
114 void HighlightSettingsPage::addNewRow(QString name, bool regex, bool cs, bool enable, QString chanName, bool self)
116 ui.highlightTable->setRowCount(ui.highlightTable->rowCount() + 1);
118 QTableWidgetItem* enableItem = new QTableWidgetItem("");
120 enableItem->setCheckState(Qt::Checked);
122 enableItem->setCheckState(Qt::Unchecked);
123 enableItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
125 auto* nameItem = new QTableWidgetItem(name);
127 QTableWidgetItem* regexItem = new QTableWidgetItem("");
129 regexItem->setCheckState(Qt::Checked);
131 regexItem->setCheckState(Qt::Unchecked);
132 regexItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
134 QTableWidgetItem* csItem = new QTableWidgetItem("");
136 csItem->setCheckState(Qt::Checked);
138 csItem->setCheckState(Qt::Unchecked);
139 csItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
141 auto* chanNameItem = new QTableWidgetItem(chanName);
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>"));
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);
170 ui.highlightTable->setCurrentItem(nameItem);
172 QVariantMap highlightRule;
173 highlightRule["Name"] = name;
174 highlightRule["RegEx"] = regex;
175 highlightRule["CS"] = cs;
176 highlightRule["Enable"] = enable;
177 highlightRule["Channel"] = chanName;
179 highlightList.append(highlightRule);
182 void HighlightSettingsPage::removeSelectedRows()
184 QList<int> selectedRows;
185 QList<QTableWidgetItem*> selectedItemList = ui.highlightTable->selectedItems();
186 foreach (QTableWidgetItem* selectedItem, selectedItemList) {
187 selectedRows.append(selectedItem->row());
189 std::sort(selectedRows.begin(), selectedRows.end(), std::greater<>());
191 foreach (int row, selectedRows) {
192 if (row != lastRow) {
193 ui.highlightTable->removeRow(row);
194 highlightList.removeAt(row);
200 void HighlightSettingsPage::selectRow(QTableWidgetItem* item)
202 int row = item->row();
203 bool selected = item->isSelected();
204 ui.highlightTable->setRangeSelected(QTableWidgetSelectionRange(row, 0, row, HighlightSettingsPage::ColumnCount - 1), selected);
207 void HighlightSettingsPage::emptyTable()
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!";
213 while (ui.highlightTable->rowCount()) {
214 ui.highlightTable->removeRow(0);
216 while (highlightList.size()) {
217 highlightList.removeLast();
221 void HighlightSettingsPage::tableChanged(QTableWidgetItem* item)
223 if (item->row() + 1 > highlightList.size())
226 QVariantMap highlightRule = highlightList.value(item->row()).toMap();
228 switch (item->column()) {
229 case HighlightSettingsPage::EnableColumn:
230 highlightRule["Enable"] = (item->checkState() == Qt::Checked);
232 case HighlightSettingsPage::NameColumn:
233 if (item->text() == "")
234 item->setText(tr("this shouldn't be empty"));
235 highlightRule["Name"] = item->text();
237 case HighlightSettingsPage::RegExColumn:
238 highlightRule["RegEx"] = (item->checkState() == Qt::Checked);
240 case HighlightSettingsPage::CsColumn:
241 highlightRule["CS"] = (item->checkState() == Qt::Checked);
243 case HighlightSettingsPage::ChanColumn:
244 if (!item->text().isEmpty() && item->text().trimmed().isEmpty())
246 highlightRule["Channel"] = item->text();
249 highlightList[item->row()] = highlightRule;
250 emit widgetHasChanged();
253 void HighlightSettingsPage::on_localHighlightsDetails_clicked()
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"))));
267 void HighlightSettingsPage::load()
269 NotificationSettings notificationSettings;
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();
281 addNewRow(name, regex, cs, enable, chanName, true);
284 switch (notificationSettings.highlightNick()) {
285 case NotificationSettings::NoNick:
286 ui.highlightNoNick->setChecked(true);
288 case NotificationSettings::CurrentNick:
289 ui.highlightCurrentNick->setChecked(true);
291 case NotificationSettings::AllNicks:
292 ui.highlightAllNicks->setChecked(true);
295 ui.nicksCaseSensitive->setChecked(notificationSettings.nicksCaseSensitive());
297 setChangedState(false);
300 void HighlightSettingsPage::save()
302 NotificationSettings notificationSettings;
303 notificationSettings.setHighlightList(highlightList);
305 NotificationSettings::HighlightNickType highlightNickType = NotificationSettings::NoNick;
306 if (ui.highlightCurrentNick->isChecked())
307 highlightNickType = NotificationSettings::CurrentNick;
308 if (ui.highlightAllNicks->isChecked())
309 highlightNickType = NotificationSettings::AllNicks;
311 notificationSettings.setHighlightNick(highlightNickType);
312 notificationSettings.setNicksCaseSensitive(ui.nicksCaseSensitive->isChecked());
315 setChangedState(false);
318 void HighlightSettingsPage::widgetHasChanged()
320 bool changed = testHasChanged();
321 if (changed != hasChanged())
322 setChangedState(changed);
325 bool HighlightSettingsPage::testHasChanged()
327 NotificationSettings notificationSettings;
329 NotificationSettings::HighlightNickType highlightNickType = NotificationSettings::NoNick;
330 if (ui.highlightCurrentNick->isChecked())
331 highlightNickType = NotificationSettings::CurrentNick;
332 if (ui.highlightAllNicks->isChecked())
333 highlightNickType = NotificationSettings::AllNicks;
335 if (notificationSettings.highlightNick() != highlightNickType)
337 if (notificationSettings.nicksCaseSensitive() != ui.nicksCaseSensitive->isChecked())
339 if (notificationSettings.highlightList() != highlightList)