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"
23 #include <QHeaderView>
24 #include <QMessageBox>
29 #include "uisettings.h"
31 HighlightSettingsPage::HighlightSettingsPage(QWidget* parent)
32 : SettingsPage(tr("Interface"),
33 // In Monolithic mode, local highlights are replaced by remote highlights
34 Quassel::runMode() == Quassel::Monolithic ? tr("Legacy Highlights") : tr("Local 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("dialog-information").pixmap(16));
86 // Set up client/monolithic local highlights information
87 if (Quassel::runMode() == Quassel::Monolithic) {
88 // We're running in Monolithic mode, core/client version in total sync. Discourage the use
89 // of local (legacy) highlights as it's identical to setting remote highlights.
90 ui.localHighlightsLabel->setText(tr("Legacy Highlights are replaced by Highlights"));
93 // We're running in client/split mode, allow for splitting the details.
94 ui.localHighlightsLabel->setText(tr("Local Highlights apply to this device only"));
97 connect(ui.add, &QAbstractButton::clicked, this, [this]() { addNewRow(); });
98 connect(ui.remove, &QAbstractButton::clicked, this, &HighlightSettingsPage::removeSelectedRows);
99 // TODO: search for a better signal (one that emits everytime a selection has been changed for one item)
100 connect(ui.highlightTable, &QTableWidget::itemClicked, this, &HighlightSettingsPage::selectRow);
102 connect(ui.highlightAllNicks, &QAbstractButton::clicked, this, &HighlightSettingsPage::widgetHasChanged);
103 connect(ui.highlightCurrentNick, &QAbstractButton::clicked, this, &HighlightSettingsPage::widgetHasChanged);
104 connect(ui.highlightNoNick, &QAbstractButton::clicked, this, &HighlightSettingsPage::widgetHasChanged);
105 connect(ui.nicksCaseSensitive, &QAbstractButton::clicked, this, &HighlightSettingsPage::widgetHasChanged);
106 connect(ui.add, &QAbstractButton::clicked, this, &HighlightSettingsPage::widgetHasChanged);
107 connect(ui.remove, &QAbstractButton::clicked, this, &HighlightSettingsPage::widgetHasChanged);
108 connect(ui.highlightTable, &QTableWidget::itemChanged, this, &HighlightSettingsPage::tableChanged);
111 bool HighlightSettingsPage::hasDefaults() const
116 void HighlightSettingsPage::defaults()
118 ui.highlightCurrentNick->setChecked(true);
119 ui.nicksCaseSensitive->setChecked(false);
125 void HighlightSettingsPage::addNewRow(QString name, bool regex, bool cs, bool enable, QString chanName, bool self)
127 ui.highlightTable->setRowCount(ui.highlightTable->rowCount() + 1);
129 QTableWidgetItem* enableItem = new QTableWidgetItem("");
131 enableItem->setCheckState(Qt::Checked);
133 enableItem->setCheckState(Qt::Unchecked);
134 enableItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
136 auto* nameItem = new QTableWidgetItem(name);
138 QTableWidgetItem* regexItem = new QTableWidgetItem("");
140 regexItem->setCheckState(Qt::Checked);
142 regexItem->setCheckState(Qt::Unchecked);
143 regexItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
145 QTableWidgetItem* csItem = new QTableWidgetItem("");
147 csItem->setCheckState(Qt::Checked);
149 csItem->setCheckState(Qt::Unchecked);
150 csItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
152 auto* chanNameItem = new QTableWidgetItem(chanName);
154 enableItem->setToolTip(tr("Enable/disable this rule"));
155 nameItem->setToolTip(tr("Phrase to match"));
156 regexItem->setToolTip(tr("<b>RegEx</b>: This option determines if the highlight rule and <i>Channel</i> "
157 "should be interpreted as <b>regular expressions</b> or just as keywords."));
158 csItem->setToolTip(tr("<b>CS</b>: This option determines if the highlight rule and <i>Channel</i> "
159 "should be interpreted <b>case sensitive</b>."));
160 chanNameItem->setToolTip(tr("<p><b>Channel</b>: Semicolon separated list of channel/query names, leave "
161 "blank to match any name.</p>"
162 "<p><i>Example:</i><br />"
163 "<i>#quassel*; #foobar; !#quasseldroid</i><br />"
164 "would match on <i>#foobar</i> and any channel starting with <i>#quassel</i> "
165 "except for <i>#quasseldroid</i><br />"
166 "<p>If only inverted names are specified, it will match anything except for "
167 "what's specified (implicit wildcard).</p>"
168 "<p><i>Example:</i><br />"
169 "<i>!#quassel*; !#foobar</i><br />"
170 "would match anything except for <i>#foobar</i> or any channel starting with "
171 "<i>#quassel</i></p>"));
173 int lastRow = ui.highlightTable->rowCount() - 1;
174 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::EnableColumn, enableItem);
175 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::NameColumn, nameItem);
176 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::RegExColumn, regexItem);
177 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::CsColumn, csItem);
178 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::ChanColumn, chanNameItem);
181 ui.highlightTable->setCurrentItem(nameItem);
183 QVariantMap highlightRule;
184 highlightRule["Name"] = name;
185 highlightRule["RegEx"] = regex;
186 highlightRule["CS"] = cs;
187 highlightRule["Enable"] = enable;
188 highlightRule["Channel"] = chanName;
190 highlightList.append(highlightRule);
193 void HighlightSettingsPage::removeSelectedRows()
195 QList<int> selectedRows;
196 QList<QTableWidgetItem*> selectedItemList = ui.highlightTable->selectedItems();
197 foreach (QTableWidgetItem* selectedItem, selectedItemList) {
198 selectedRows.append(selectedItem->row());
200 qSort(selectedRows.begin(), selectedRows.end(), qGreater<int>());
202 foreach (int row, selectedRows) {
203 if (row != lastRow) {
204 ui.highlightTable->removeRow(row);
205 highlightList.removeAt(row);
211 void HighlightSettingsPage::selectRow(QTableWidgetItem* item)
213 int row = item->row();
214 bool selected = item->isSelected();
215 ui.highlightTable->setRangeSelected(QTableWidgetSelectionRange(row, 0, row, HighlightSettingsPage::ColumnCount - 1), selected);
218 void HighlightSettingsPage::emptyTable()
220 // ui.highlight and highlightList should have the same size, but just to make sure.
221 if (ui.highlightTable->rowCount() != highlightList.size()) {
222 qDebug() << "something is wrong: ui.highlight and highlightList don't have the same size!";
224 while (ui.highlightTable->rowCount()) {
225 ui.highlightTable->removeRow(0);
227 while (highlightList.size()) {
228 highlightList.removeLast();
232 void HighlightSettingsPage::tableChanged(QTableWidgetItem* item)
234 if (item->row() + 1 > highlightList.size())
237 QVariantMap highlightRule = highlightList.value(item->row()).toMap();
239 switch (item->column()) {
240 case HighlightSettingsPage::EnableColumn:
241 highlightRule["Enable"] = (item->checkState() == Qt::Checked);
243 case HighlightSettingsPage::NameColumn:
244 if (item->text() == "")
245 item->setText(tr("this shouldn't be empty"));
246 highlightRule["Name"] = item->text();
248 case HighlightSettingsPage::RegExColumn:
249 highlightRule["RegEx"] = (item->checkState() == Qt::Checked);
251 case HighlightSettingsPage::CsColumn:
252 highlightRule["CS"] = (item->checkState() == Qt::Checked);
254 case HighlightSettingsPage::ChanColumn:
255 if (!item->text().isEmpty() && item->text().trimmed().isEmpty())
257 highlightRule["Channel"] = item->text();
260 highlightList[item->row()] = highlightRule;
261 emit widgetHasChanged();
264 void HighlightSettingsPage::on_localHighlightsDetails_clicked()
266 // Show information specific to client/monolithic differences
267 if (Quassel::runMode() == Quassel::Monolithic) {
268 // We're running in Monolithic mode, core/client version in total sync. Discourage the use
269 // of local (legacy) highlights as it's identical to setting remote highlights.
270 QMessageBox::information(this,
271 tr("Legacy Highlights vs. Highlights"),
272 QString("<p><b>%1</b></p></br><p>%2</p></br><p>%3</p>")
273 .arg(tr("Legacy Highlights are replaced by Highlights"),
274 tr("These highlights will keep working for now, but you should move to "
275 "the improved highlight rules when you can."),
276 tr("Configure the new style of highlights in "
278 .arg(tr("Highlights"))));
281 // We're running in client/split mode, allow for splitting the details.
282 QMessageBox::information(this,
283 tr("Local Highlights vs. Remote Highlights"),
284 QString("<p><b>%1</b></p></br><p>%2</p></br><p>%3</p>")
285 .arg(tr("Local Highlights apply to this device only"),
286 tr("Highlights configured on this page only apply to your current "
288 tr("Configure highlights for all of your devices in "
290 .arg(tr("Remote Highlights").replace(" ", " "))));
291 // Re-use translations of "Remote Highlights" as this is a word-for-word reference, forcing
292 // all spaces to be non-breaking
296 void HighlightSettingsPage::load()
298 NotificationSettings notificationSettings;
302 foreach (QVariant highlight, notificationSettings.highlightList()) {
303 QVariantMap highlightRule = highlight.toMap();
304 QString name = highlightRule["Name"].toString();
305 bool regex = highlightRule["RegEx"].toBool();
306 bool cs = highlightRule["CS"].toBool();
307 bool enable = highlightRule["Enable"].toBool();
308 QString chanName = highlightRule["Channel"].toString();
310 addNewRow(name, regex, cs, enable, chanName, true);
313 switch (notificationSettings.highlightNick()) {
314 case NotificationSettings::NoNick:
315 ui.highlightNoNick->setChecked(true);
317 case NotificationSettings::CurrentNick:
318 ui.highlightCurrentNick->setChecked(true);
320 case NotificationSettings::AllNicks:
321 ui.highlightAllNicks->setChecked(true);
324 ui.nicksCaseSensitive->setChecked(notificationSettings.nicksCaseSensitive());
326 setChangedState(false);
329 void HighlightSettingsPage::save()
331 NotificationSettings notificationSettings;
332 notificationSettings.setHighlightList(highlightList);
334 NotificationSettings::HighlightNickType highlightNickType = NotificationSettings::NoNick;
335 if (ui.highlightCurrentNick->isChecked())
336 highlightNickType = NotificationSettings::CurrentNick;
337 if (ui.highlightAllNicks->isChecked())
338 highlightNickType = NotificationSettings::AllNicks;
340 notificationSettings.setHighlightNick(highlightNickType);
341 notificationSettings.setNicksCaseSensitive(ui.nicksCaseSensitive->isChecked());
344 setChangedState(false);
347 void HighlightSettingsPage::widgetHasChanged()
349 bool changed = testHasChanged();
350 if (changed != hasChanged())
351 setChangedState(changed);
354 bool HighlightSettingsPage::testHasChanged()
356 NotificationSettings notificationSettings;
358 NotificationSettings::HighlightNickType highlightNickType = NotificationSettings::NoNick;
359 if (ui.highlightCurrentNick->isChecked())
360 highlightNickType = NotificationSettings::CurrentNick;
361 if (ui.highlightAllNicks->isChecked())
362 highlightNickType = NotificationSettings::AllNicks;
364 if (notificationSettings.highlightNick() != highlightNickType)
366 if (notificationSettings.nicksCaseSensitive() != ui.nicksCaseSensitive->isChecked())
368 if (notificationSettings.highlightList() != highlightList)