1 /***************************************************************************
2 * Copyright (C) 2005-2018 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"
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"),
40 ui.highlightTable->verticalHeader()->hide();
41 ui.highlightTable->setShowGrid(false);
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());
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());
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());
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());
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());
82 #if QT_VERSION < 0x050000
83 ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::NameColumn, QHeaderView::Stretch);
84 ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::RegExColumn, QHeaderView::ResizeToContents);
85 ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::CsColumn, QHeaderView::ResizeToContents);
86 ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::EnableColumn, QHeaderView::ResizeToContents);
87 ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::ChanColumn, QHeaderView::ResizeToContents);
89 ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::NameColumn, QHeaderView::Stretch);
90 ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::RegExColumn, QHeaderView::ResizeToContents);
91 ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::CsColumn, QHeaderView::ResizeToContents);
92 ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::EnableColumn, QHeaderView::ResizeToContents);
93 ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::ChanColumn, QHeaderView::ResizeToContents);
97 ui.localHighlightsIcon->setPixmap(icon::get("dialog-information").pixmap(16));
99 // Set up client/monolithic local highlights information
100 if (Quassel::runMode() == Quassel::Monolithic) {
101 // We're running in Monolithic mode, core/client version in total sync. Discourage the use
102 // of local (legacy) highlights as it's identical to setting remote highlights.
103 ui.localHighlightsLabel->setText(
104 tr("Legacy Highlights are replaced by Highlights"));
106 // We're running in client/split mode, allow for splitting the details.
107 ui.localHighlightsLabel->setText(tr("Local Highlights apply to this device only"));
110 connect(ui.add, SIGNAL(clicked(bool)), this, SLOT(addNewRow()));
111 connect(ui.remove, SIGNAL(clicked(bool)), this, SLOT(removeSelectedRows()));
112 //TODO: search for a better signal (one that emits everytime a selection has been changed for one item)
113 connect(ui.highlightTable, SIGNAL(itemClicked(QTableWidgetItem *)), this, SLOT(selectRow(QTableWidgetItem *)));
115 connect(ui.highlightAllNicks, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
116 connect(ui.highlightCurrentNick, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
117 connect(ui.highlightNoNick, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
118 connect(ui.nicksCaseSensitive, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
119 connect(ui.add, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
120 connect(ui.remove, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
121 connect(ui.highlightTable, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(tableChanged(QTableWidgetItem *)));
125 bool HighlightSettingsPage::hasDefaults() const
131 void HighlightSettingsPage::defaults()
133 ui.highlightNoNick->setChecked(true);
134 ui.nicksCaseSensitive->setChecked(false);
141 void HighlightSettingsPage::addNewRow(QString name, bool regex, bool cs, bool enable, QString chanName, bool self)
143 ui.highlightTable->setRowCount(ui.highlightTable->rowCount()+1);
145 QTableWidgetItem *enableItem = new QTableWidgetItem("");
147 enableItem->setCheckState(Qt::Checked);
149 enableItem->setCheckState(Qt::Unchecked);
150 enableItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
152 QTableWidgetItem *nameItem = new QTableWidgetItem(name);
154 QTableWidgetItem *regexItem = new QTableWidgetItem("");
156 regexItem->setCheckState(Qt::Checked);
158 regexItem->setCheckState(Qt::Unchecked);
159 regexItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
161 QTableWidgetItem *csItem = new QTableWidgetItem("");
163 csItem->setCheckState(Qt::Checked);
165 csItem->setCheckState(Qt::Unchecked);
166 csItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
168 QTableWidgetItem *chanNameItem = new QTableWidgetItem(chanName);
170 enableItem->setToolTip(tr("Enable/disable this rule"));
171 nameItem->setToolTip(tr("Phrase to match"));
172 regexItem->setToolTip(
173 tr("<b>RegEx</b>: This option determines if the highlight rule and <i>Channel</i> "
174 "should be interpreted as <b>regular expressions</b> or just as keywords."));
176 tr("<b>CS</b>: This option determines if the highlight rule and <i>Channel</i> "
177 "should be interpreted <b>case sensitive</b>."));
178 chanNameItem->setToolTip(
179 tr("<p><b>Channel</b>: Semicolon separated list of channel/query names, leave "
180 "blank to match any name.</p>"
181 "<p><i>Example:</i><br />"
182 "<i>#quassel*; #foobar; !#quasseldroid</i><br />"
183 "would match on <i>#foobar</i> and any channel starting with <i>#quassel</i> "
184 "except for <i>#quasseldroid</i><br />"
185 "<p>If only inverted names are specified, it will match anything except for "
186 "what's specified (implicit wildcard).</p>"
187 "<p><i>Example:</i><br />"
188 "<i>!#quassel*; !#foobar</i><br />"
189 "would match anything except for <i>#foobar</i> or any channel starting with "
190 "<i>#quassel</i></p>"));
192 int lastRow = ui.highlightTable->rowCount()-1;
193 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::EnableColumn, enableItem);
194 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::NameColumn, nameItem);
195 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::RegExColumn, regexItem);
196 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::CsColumn, csItem);
197 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::ChanColumn, chanNameItem);
200 ui.highlightTable->setCurrentItem(nameItem);
202 QVariantMap highlightRule;
203 highlightRule["Name"] = name;
204 highlightRule["RegEx"] = regex;
205 highlightRule["CS"] = cs;
206 highlightRule["Enable"] = enable;
207 highlightRule["Channel"] = chanName;
209 highlightList.append(highlightRule);
213 void HighlightSettingsPage::removeSelectedRows()
215 QList<int> selectedRows;
216 QList<QTableWidgetItem *> selectedItemList = ui.highlightTable->selectedItems();
217 foreach(QTableWidgetItem *selectedItem, selectedItemList) {
218 selectedRows.append(selectedItem->row());
220 qSort(selectedRows.begin(), selectedRows.end(), qGreater<int>());
222 foreach(int row, selectedRows) {
223 if (row != lastRow) {
224 ui.highlightTable->removeRow(row);
225 highlightList.removeAt(row);
232 void HighlightSettingsPage::selectRow(QTableWidgetItem *item)
234 int row = item->row();
235 bool selected = item->isSelected();
236 ui.highlightTable->setRangeSelected(QTableWidgetSelectionRange(row, 0, row, HighlightSettingsPage::ColumnCount-1), selected);
240 void HighlightSettingsPage::emptyTable()
242 // ui.highlight and highlightList should have the same size, but just to make sure.
243 if (ui.highlightTable->rowCount() != highlightList.size()) {
244 qDebug() << "something is wrong: ui.highlight and highlightList don't have the same size!";
246 while (ui.highlightTable->rowCount()) {
247 ui.highlightTable->removeRow(0);
249 while (highlightList.size()) {
250 highlightList.removeLast();
255 void HighlightSettingsPage::tableChanged(QTableWidgetItem *item)
257 if (item->row()+1 > highlightList.size())
260 QVariantMap highlightRule = highlightList.value(item->row()).toMap();
262 switch (item->column())
264 case HighlightSettingsPage::EnableColumn:
265 highlightRule["Enable"] = (item->checkState() == Qt::Checked);
267 case HighlightSettingsPage::NameColumn:
268 if (item->text() == "")
269 item->setText(tr("this shouldn't be empty"));
270 highlightRule["Name"] = item->text();
272 case HighlightSettingsPage::RegExColumn:
273 highlightRule["RegEx"] = (item->checkState() == Qt::Checked);
275 case HighlightSettingsPage::CsColumn:
276 highlightRule["CS"] = (item->checkState() == Qt::Checked);
278 case HighlightSettingsPage::ChanColumn:
279 if (!item->text().isEmpty() && item->text().trimmed().isEmpty())
281 highlightRule["Channel"] = item->text();
284 highlightList[item->row()] = highlightRule;
285 emit widgetHasChanged();
289 void HighlightSettingsPage::on_localHighlightsDetails_clicked()
291 // Show information specific to client/monolithic differences
292 if (Quassel::runMode() == Quassel::Monolithic) {
293 // We're running in Monolithic mode, core/client version in total sync. Discourage the use
294 // of local (legacy) highlights as it's identical to setting remote highlights.
295 QMessageBox::information(
297 tr("Legacy Highlights vs. Highlights"),
298 QString("<p><b>%1</b></p></br><p>%2</p></br><p>%3</p>")
299 .arg(tr("Legacy Highlights are replaced by Highlights"),
300 tr("These highlights will keep working for now, but you should move to "
301 "the improved highlight rules when you can."),
302 tr("Configure the new style of highlights in "
303 "<i>%1</i>.").arg(tr("Highlights"))));
305 // We're running in client/split mode, allow for splitting the details.
306 QMessageBox::information(
308 tr("Local Highlights vs. Remote Highlights"),
309 QString("<p><b>%1</b></p></br><p>%2</p></br><p>%3</p>")
310 .arg(tr("Local Highlights apply to this device only"),
311 tr("Highlights configured on this page only apply to your current "
313 tr("Configure highlights for all of your devices in "
314 "<i>%1</i>.").arg(tr("Remote Highlights").replace(" ", " "))));
315 // Re-use translations of "Remote Highlights" as this is a word-for-word reference, forcing
316 // all spaces to be non-breaking
321 void HighlightSettingsPage::load()
323 NotificationSettings notificationSettings;
327 foreach(QVariant highlight, notificationSettings.highlightList()) {
328 QVariantMap highlightRule = highlight.toMap();
329 QString name = highlightRule["Name"].toString();
330 bool regex = highlightRule["RegEx"].toBool();
331 bool cs = highlightRule["CS"].toBool();
332 bool enable = highlightRule["Enable"].toBool();
333 QString chanName = highlightRule["Channel"].toString();
335 addNewRow(name, regex, cs, enable, chanName, true);
338 switch (notificationSettings.highlightNick())
340 case NotificationSettings::NoNick:
341 ui.highlightNoNick->setChecked(true);
343 case NotificationSettings::CurrentNick:
344 ui.highlightCurrentNick->setChecked(true);
346 case NotificationSettings::AllNicks:
347 ui.highlightAllNicks->setChecked(true);
350 ui.nicksCaseSensitive->setChecked(notificationSettings.nicksCaseSensitive());
352 setChangedState(false);
356 void HighlightSettingsPage::save()
358 NotificationSettings notificationSettings;
359 notificationSettings.setHighlightList(highlightList);
361 NotificationSettings::HighlightNickType highlightNickType = NotificationSettings::NoNick;
362 if (ui.highlightCurrentNick->isChecked())
363 highlightNickType = NotificationSettings::CurrentNick;
364 if (ui.highlightAllNicks->isChecked())
365 highlightNickType = NotificationSettings::AllNicks;
367 notificationSettings.setHighlightNick(highlightNickType);
368 notificationSettings.setNicksCaseSensitive(ui.nicksCaseSensitive->isChecked());
371 setChangedState(false);
375 void HighlightSettingsPage::widgetHasChanged()
377 bool changed = testHasChanged();
378 if (changed != hasChanged()) setChangedState(changed);
382 bool HighlightSettingsPage::testHasChanged()
384 NotificationSettings notificationSettings;
386 NotificationSettings::HighlightNickType highlightNickType = NotificationSettings::NoNick;
387 if (ui.highlightCurrentNick->isChecked())
388 highlightNickType = NotificationSettings::CurrentNick;
389 if (ui.highlightAllNicks->isChecked())
390 highlightNickType = NotificationSettings::AllNicks;
392 if (notificationSettings.highlightNick() != highlightNickType)
394 if (notificationSettings.nicksCaseSensitive() != ui.nicksCaseSensitive->isChecked())
396 if (notificationSettings.highlightList() != highlightList)