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"
25 #include "uisettings.h"
27 #include <QHeaderView>
28 #include <QMessageBox>
30 HighlightSettingsPage::HighlightSettingsPage(QWidget *parent)
31 : SettingsPage(tr("Interface"),
32 // In Monolithic mode, local highlights are replaced by remote highlights
33 Quassel::runMode() == Quassel::Monolithic ?
34 tr("Legacy Highlights") : tr("Local Highlights"),
38 ui.highlightTable->verticalHeader()->hide();
39 ui.highlightTable->setShowGrid(false);
42 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::EnableColumn)->setToolTip(
43 tr("Enable/disable this rule"));
44 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::EnableColumn)->setWhatsThis(
45 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::EnableColumn)->toolTip());
47 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::NameColumn)->setToolTip(
48 tr("Phrase to match"));
49 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::NameColumn)->setWhatsThis(
50 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::NameColumn)->toolTip());
52 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::RegExColumn)->setToolTip(
53 tr("<b>RegEx</b>: This option determines if the highlight rule and <i>Channel</i> "
54 "should be interpreted as <b>regular expressions</b> or just as keywords."));
55 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::RegExColumn)->setWhatsThis(
56 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::RegExColumn)->toolTip());
58 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::CsColumn)->setToolTip(
59 tr("<b>CS</b>: This option determines if the highlight rule and <i>Channel</i> "
60 "should be interpreted <b>case sensitive</b>."));
61 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::CsColumn)->setWhatsThis(
62 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::CsColumn)->toolTip());
64 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::ChanColumn)->setToolTip(
65 tr("<p><b>Channel</b>: Semicolon separated list of channel names, leave blank to "
67 "<p><i>Example:</i><br />"
68 "<i>#quassel*; #foobar; !#quasseldroid</i><br />"
69 "would match on <i>#foobar</i> and any channel starting with <i>#quassel</i> "
70 "except for <i>#quasseldroid</i><br />"
71 "<p>If only inverted names are specified, it will match anything except for "
72 "what's specified (implicit wildcard).</p>"
73 "<p><i>Example:</i><br />"
74 "<i>!#quassel*; !#foobar</i><br />"
75 "would match anything except for <i>#foobar</i> or any channel starting with "
76 "<i>#quassel</i></p>"));
77 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::ChanColumn)->setWhatsThis(
78 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::ChanColumn)->toolTip());
80 #if QT_VERSION < 0x050000
81 ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::NameColumn, QHeaderView::Stretch);
82 ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::RegExColumn, QHeaderView::ResizeToContents);
83 ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::CsColumn, QHeaderView::ResizeToContents);
84 ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::EnableColumn, QHeaderView::ResizeToContents);
85 ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::ChanColumn, QHeaderView::ResizeToContents);
87 ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::NameColumn, QHeaderView::Stretch);
88 ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::RegExColumn, QHeaderView::ResizeToContents);
89 ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::CsColumn, QHeaderView::ResizeToContents);
90 ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::EnableColumn, QHeaderView::ResizeToContents);
91 ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::ChanColumn, QHeaderView::ResizeToContents);
95 ui.localHighlightsIcon->setPixmap(QIcon::fromTheme("dialog-information").pixmap(16));
97 // Set up client/monolithic local highlights information
98 if (Quassel::runMode() == Quassel::Monolithic) {
99 // We're running in Monolithic mode, core/client version in total sync. Discourage the use
100 // of local (legacy) highlights as it's identical to setting remote highlights.
101 ui.localHighlightsLabel->setText(
102 tr("Legacy Highlights are replaced by Highlights"));
104 // We're running in client/split mode, allow for splitting the details.
105 ui.localHighlightsLabel->setText(tr("Local Highlights apply to this device only"));
108 connect(ui.add, SIGNAL(clicked(bool)), this, SLOT(addNewRow()));
109 connect(ui.remove, SIGNAL(clicked(bool)), this, SLOT(removeSelectedRows()));
110 //TODO: search for a better signal (one that emits everytime a selection has been changed for one item)
111 connect(ui.highlightTable, SIGNAL(itemClicked(QTableWidgetItem *)), this, SLOT(selectRow(QTableWidgetItem *)));
113 connect(ui.highlightAllNicks, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
114 connect(ui.highlightCurrentNick, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
115 connect(ui.highlightNoNick, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
116 connect(ui.nicksCaseSensitive, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
117 connect(ui.add, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
118 connect(ui.remove, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
119 connect(ui.highlightTable, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(tableChanged(QTableWidgetItem *)));
123 bool HighlightSettingsPage::hasDefaults() const
129 void HighlightSettingsPage::defaults()
131 ui.highlightNoNick->setChecked(true);
132 ui.nicksCaseSensitive->setChecked(false);
139 void HighlightSettingsPage::addNewRow(QString name, bool regex, bool cs, bool enable, QString chanName, bool self)
141 ui.highlightTable->setRowCount(ui.highlightTable->rowCount()+1);
143 QTableWidgetItem *enableItem = new QTableWidgetItem("");
145 enableItem->setCheckState(Qt::Checked);
147 enableItem->setCheckState(Qt::Unchecked);
148 enableItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
150 QTableWidgetItem *nameItem = new QTableWidgetItem(name);
152 QTableWidgetItem *regexItem = new QTableWidgetItem("");
154 regexItem->setCheckState(Qt::Checked);
156 regexItem->setCheckState(Qt::Unchecked);
157 regexItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
159 QTableWidgetItem *csItem = new QTableWidgetItem("");
161 csItem->setCheckState(Qt::Checked);
163 csItem->setCheckState(Qt::Unchecked);
164 csItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
166 QTableWidgetItem *chanNameItem = new QTableWidgetItem(chanName);
168 enableItem->setToolTip(tr("Enable/disable this rule"));
169 nameItem->setToolTip(tr("Phrase to match"));
170 regexItem->setToolTip(
171 tr("<b>RegEx</b>: This option determines if the highlight rule and <i>Channel</i> "
172 "should be interpreted as <b>regular expressions</b> or just as keywords."));
174 tr("<b>CS</b>: This option determines if the highlight rule and <i>Channel</i> "
175 "should be interpreted <b>case sensitive</b>."));
176 chanNameItem->setToolTip(
177 tr("<p><b>Channel</b>: Semicolon separated list of channel names, leave blank to "
178 "match any name.</p>"
179 "<p><i>Example:</i><br />"
180 "<i>#quassel*; #foobar; !#quasseldroid</i><br />"
181 "would match on <i>#foobar</i> and any channel starting with <i>#quassel</i> "
182 "except for <i>#quasseldroid</i><br />"
183 "<p>If only inverted names are specified, it will match anything except for "
184 "what's specified (implicit wildcard).</p>"
185 "<p><i>Example:</i><br />"
186 "<i>!#quassel*; !#foobar</i><br />"
187 "would match anything except for <i>#foobar</i> or any channel starting with "
188 "<i>#quassel</i></p>"));
190 int lastRow = ui.highlightTable->rowCount()-1;
191 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::EnableColumn, enableItem);
192 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::NameColumn, nameItem);
193 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::RegExColumn, regexItem);
194 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::CsColumn, csItem);
195 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::ChanColumn, chanNameItem);
198 ui.highlightTable->setCurrentItem(nameItem);
200 QVariantMap highlightRule;
201 highlightRule["Name"] = name;
202 highlightRule["RegEx"] = regex;
203 highlightRule["CS"] = cs;
204 highlightRule["Enable"] = enable;
205 highlightRule["Channel"] = chanName;
207 highlightList.append(highlightRule);
211 void HighlightSettingsPage::removeSelectedRows()
213 QList<int> selectedRows;
214 QList<QTableWidgetItem *> selectedItemList = ui.highlightTable->selectedItems();
215 foreach(QTableWidgetItem *selectedItem, selectedItemList) {
216 selectedRows.append(selectedItem->row());
218 qSort(selectedRows.begin(), selectedRows.end(), qGreater<int>());
220 foreach(int row, selectedRows) {
221 if (row != lastRow) {
222 ui.highlightTable->removeRow(row);
223 highlightList.removeAt(row);
230 void HighlightSettingsPage::selectRow(QTableWidgetItem *item)
232 int row = item->row();
233 bool selected = item->isSelected();
234 ui.highlightTable->setRangeSelected(QTableWidgetSelectionRange(row, 0, row, HighlightSettingsPage::ColumnCount-1), selected);
238 void HighlightSettingsPage::emptyTable()
240 // ui.highlight and highlightList should have the same size, but just to make sure.
241 if (ui.highlightTable->rowCount() != highlightList.size()) {
242 qDebug() << "something is wrong: ui.highlight and highlightList don't have the same size!";
244 while (ui.highlightTable->rowCount()) {
245 ui.highlightTable->removeRow(0);
247 while (highlightList.size()) {
248 highlightList.removeLast();
253 void HighlightSettingsPage::tableChanged(QTableWidgetItem *item)
255 if (item->row()+1 > highlightList.size())
258 QVariantMap highlightRule = highlightList.value(item->row()).toMap();
260 switch (item->column())
262 case HighlightSettingsPage::EnableColumn:
263 highlightRule["Enable"] = (item->checkState() == Qt::Checked);
265 case HighlightSettingsPage::NameColumn:
266 if (item->text() == "")
267 item->setText(tr("this shouldn't be empty"));
268 highlightRule["Name"] = item->text();
270 case HighlightSettingsPage::RegExColumn:
271 highlightRule["RegEx"] = (item->checkState() == Qt::Checked);
273 case HighlightSettingsPage::CsColumn:
274 highlightRule["CS"] = (item->checkState() == Qt::Checked);
276 case HighlightSettingsPage::ChanColumn:
277 if (!item->text().isEmpty() && item->text().trimmed().isEmpty())
279 highlightRule["Channel"] = item->text();
282 highlightList[item->row()] = highlightRule;
283 emit widgetHasChanged();
287 void HighlightSettingsPage::on_localHighlightsDetails_clicked()
289 // Show information specific to client/monolithic differences
290 if (Quassel::runMode() == Quassel::Monolithic) {
291 // We're running in Monolithic mode, core/client version in total sync. Discourage the use
292 // of local (legacy) highlights as it's identical to setting remote highlights.
293 QMessageBox::information(
295 tr("Legacy Highlights vs. Highlights"),
296 QString("<p><b>%1</b></p></br><p>%2</p></br><p>%3</p>")
297 .arg(tr("Legacy Highlights are replaced by Highlights"),
298 tr("These highlights will keep working for now, but you should move to "
299 "the improved highlight rules when you can."),
300 tr("Configure the new style of highlights in "
301 "<i>%1</i>.").arg(tr("Highlights"))));
303 // We're running in client/split mode, allow for splitting the details.
304 QMessageBox::information(
306 tr("Local Highlights vs. Remote Highlights"),
307 QString("<p><b>%1</b></p></br><p>%2</p></br><p>%3</p>")
308 .arg(tr("Local Highlights apply to this device only"),
309 tr("Highlights configured on this page only apply to your current "
311 tr("Configure highlights for all of your devices in "
312 "<i>%1</i>.").arg(tr("Remote Highlights").replace(" ", " "))));
313 // Re-use translations of "Remote Highlights" as this is a word-for-word reference, forcing
314 // all spaces to be non-breaking
319 void HighlightSettingsPage::load()
321 NotificationSettings notificationSettings;
325 foreach(QVariant highlight, notificationSettings.highlightList()) {
326 QVariantMap highlightRule = highlight.toMap();
327 QString name = highlightRule["Name"].toString();
328 bool regex = highlightRule["RegEx"].toBool();
329 bool cs = highlightRule["CS"].toBool();
330 bool enable = highlightRule["Enable"].toBool();
331 QString chanName = highlightRule["Channel"].toString();
333 addNewRow(name, regex, cs, enable, chanName, true);
336 switch (notificationSettings.highlightNick())
338 case NotificationSettings::NoNick:
339 ui.highlightNoNick->setChecked(true);
341 case NotificationSettings::CurrentNick:
342 ui.highlightCurrentNick->setChecked(true);
344 case NotificationSettings::AllNicks:
345 ui.highlightAllNicks->setChecked(true);
348 ui.nicksCaseSensitive->setChecked(notificationSettings.nicksCaseSensitive());
350 setChangedState(false);
354 void HighlightSettingsPage::save()
356 NotificationSettings notificationSettings;
357 notificationSettings.setHighlightList(highlightList);
359 NotificationSettings::HighlightNickType highlightNickType = NotificationSettings::NoNick;
360 if (ui.highlightCurrentNick->isChecked())
361 highlightNickType = NotificationSettings::CurrentNick;
362 if (ui.highlightAllNicks->isChecked())
363 highlightNickType = NotificationSettings::AllNicks;
365 notificationSettings.setHighlightNick(highlightNickType);
366 notificationSettings.setNicksCaseSensitive(ui.nicksCaseSensitive->isChecked());
369 setChangedState(false);
373 void HighlightSettingsPage::widgetHasChanged()
375 bool changed = testHasChanged();
376 if (changed != hasChanged()) setChangedState(changed);
380 bool HighlightSettingsPage::testHasChanged()
382 NotificationSettings notificationSettings;
384 NotificationSettings::HighlightNickType highlightNickType = NotificationSettings::NoNick;
385 if (ui.highlightCurrentNick->isChecked())
386 highlightNickType = NotificationSettings::CurrentNick;
387 if (ui.highlightAllNicks->isChecked())
388 highlightNickType = NotificationSettings::AllNicks;
390 if (notificationSettings.highlightNick() != highlightNickType)
392 if (notificationSettings.nicksCaseSensitive() != ui.nicksCaseSensitive->isChecked())
394 if (notificationSettings.highlightList() != highlightList)