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 ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::NameColumn, QHeaderView::Stretch);
83 ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::RegExColumn, QHeaderView::ResizeToContents);
84 ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::CsColumn, QHeaderView::ResizeToContents);
85 ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::EnableColumn, QHeaderView::ResizeToContents);
86 ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::ChanColumn, QHeaderView::ResizeToContents);
89 ui.localHighlightsIcon->setPixmap(icon::get("dialog-information").pixmap(16));
91 // Set up client/monolithic local highlights information
92 if (Quassel::runMode() == Quassel::Monolithic) {
93 // We're running in Monolithic mode, core/client version in total sync. Discourage the use
94 // of local (legacy) highlights as it's identical to setting remote highlights.
95 ui.localHighlightsLabel->setText(
96 tr("Legacy Highlights are replaced by Highlights"));
98 // We're running in client/split mode, allow for splitting the details.
99 ui.localHighlightsLabel->setText(tr("Local Highlights apply to this device only"));
102 connect(ui.add, SIGNAL(clicked(bool)), this, SLOT(addNewRow()));
103 connect(ui.remove, SIGNAL(clicked(bool)), this, SLOT(removeSelectedRows()));
104 //TODO: search for a better signal (one that emits everytime a selection has been changed for one item)
105 connect(ui.highlightTable, SIGNAL(itemClicked(QTableWidgetItem *)), this, SLOT(selectRow(QTableWidgetItem *)));
107 connect(ui.highlightAllNicks, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
108 connect(ui.highlightCurrentNick, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
109 connect(ui.highlightNoNick, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
110 connect(ui.nicksCaseSensitive, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
111 connect(ui.add, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
112 connect(ui.remove, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
113 connect(ui.highlightTable, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(tableChanged(QTableWidgetItem *)));
117 bool HighlightSettingsPage::hasDefaults() const
123 void HighlightSettingsPage::defaults()
125 ui.highlightCurrentNick->setChecked(true);
126 ui.nicksCaseSensitive->setChecked(false);
133 void HighlightSettingsPage::addNewRow(QString name, bool regex, bool cs, bool enable, QString chanName, bool self)
135 ui.highlightTable->setRowCount(ui.highlightTable->rowCount()+1);
137 QTableWidgetItem *enableItem = new QTableWidgetItem("");
139 enableItem->setCheckState(Qt::Checked);
141 enableItem->setCheckState(Qt::Unchecked);
142 enableItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
144 QTableWidgetItem *nameItem = new QTableWidgetItem(name);
146 QTableWidgetItem *regexItem = new QTableWidgetItem("");
148 regexItem->setCheckState(Qt::Checked);
150 regexItem->setCheckState(Qt::Unchecked);
151 regexItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
153 QTableWidgetItem *csItem = new QTableWidgetItem("");
155 csItem->setCheckState(Qt::Checked);
157 csItem->setCheckState(Qt::Unchecked);
158 csItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
160 QTableWidgetItem *chanNameItem = new QTableWidgetItem(chanName);
162 enableItem->setToolTip(tr("Enable/disable this rule"));
163 nameItem->setToolTip(tr("Phrase to match"));
164 regexItem->setToolTip(
165 tr("<b>RegEx</b>: This option determines if the highlight rule and <i>Channel</i> "
166 "should be interpreted as <b>regular expressions</b> or just as keywords."));
168 tr("<b>CS</b>: This option determines if the highlight rule and <i>Channel</i> "
169 "should be interpreted <b>case sensitive</b>."));
170 chanNameItem->setToolTip(
171 tr("<p><b>Channel</b>: Semicolon separated list of channel/query names, leave "
172 "blank to match any name.</p>"
173 "<p><i>Example:</i><br />"
174 "<i>#quassel*; #foobar; !#quasseldroid</i><br />"
175 "would match on <i>#foobar</i> and any channel starting with <i>#quassel</i> "
176 "except for <i>#quasseldroid</i><br />"
177 "<p>If only inverted names are specified, it will match anything except for "
178 "what's specified (implicit wildcard).</p>"
179 "<p><i>Example:</i><br />"
180 "<i>!#quassel*; !#foobar</i><br />"
181 "would match anything except for <i>#foobar</i> or any channel starting with "
182 "<i>#quassel</i></p>"));
184 int lastRow = ui.highlightTable->rowCount()-1;
185 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::EnableColumn, enableItem);
186 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::NameColumn, nameItem);
187 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::RegExColumn, regexItem);
188 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::CsColumn, csItem);
189 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::ChanColumn, chanNameItem);
192 ui.highlightTable->setCurrentItem(nameItem);
194 QVariantMap highlightRule;
195 highlightRule["Name"] = name;
196 highlightRule["RegEx"] = regex;
197 highlightRule["CS"] = cs;
198 highlightRule["Enable"] = enable;
199 highlightRule["Channel"] = chanName;
201 highlightList.append(highlightRule);
205 void HighlightSettingsPage::removeSelectedRows()
207 QList<int> selectedRows;
208 QList<QTableWidgetItem *> selectedItemList = ui.highlightTable->selectedItems();
209 foreach(QTableWidgetItem *selectedItem, selectedItemList) {
210 selectedRows.append(selectedItem->row());
212 qSort(selectedRows.begin(), selectedRows.end(), qGreater<int>());
214 foreach(int row, selectedRows) {
215 if (row != lastRow) {
216 ui.highlightTable->removeRow(row);
217 highlightList.removeAt(row);
224 void HighlightSettingsPage::selectRow(QTableWidgetItem *item)
226 int row = item->row();
227 bool selected = item->isSelected();
228 ui.highlightTable->setRangeSelected(QTableWidgetSelectionRange(row, 0, row, HighlightSettingsPage::ColumnCount-1), selected);
232 void HighlightSettingsPage::emptyTable()
234 // ui.highlight and highlightList should have the same size, but just to make sure.
235 if (ui.highlightTable->rowCount() != highlightList.size()) {
236 qDebug() << "something is wrong: ui.highlight and highlightList don't have the same size!";
238 while (ui.highlightTable->rowCount()) {
239 ui.highlightTable->removeRow(0);
241 while (highlightList.size()) {
242 highlightList.removeLast();
247 void HighlightSettingsPage::tableChanged(QTableWidgetItem *item)
249 if (item->row()+1 > highlightList.size())
252 QVariantMap highlightRule = highlightList.value(item->row()).toMap();
254 switch (item->column())
256 case HighlightSettingsPage::EnableColumn:
257 highlightRule["Enable"] = (item->checkState() == Qt::Checked);
259 case HighlightSettingsPage::NameColumn:
260 if (item->text() == "")
261 item->setText(tr("this shouldn't be empty"));
262 highlightRule["Name"] = item->text();
264 case HighlightSettingsPage::RegExColumn:
265 highlightRule["RegEx"] = (item->checkState() == Qt::Checked);
267 case HighlightSettingsPage::CsColumn:
268 highlightRule["CS"] = (item->checkState() == Qt::Checked);
270 case HighlightSettingsPage::ChanColumn:
271 if (!item->text().isEmpty() && item->text().trimmed().isEmpty())
273 highlightRule["Channel"] = item->text();
276 highlightList[item->row()] = highlightRule;
277 emit widgetHasChanged();
281 void HighlightSettingsPage::on_localHighlightsDetails_clicked()
283 // Show information specific to client/monolithic differences
284 if (Quassel::runMode() == Quassel::Monolithic) {
285 // We're running in Monolithic mode, core/client version in total sync. Discourage the use
286 // of local (legacy) highlights as it's identical to setting remote highlights.
287 QMessageBox::information(
289 tr("Legacy Highlights vs. Highlights"),
290 QString("<p><b>%1</b></p></br><p>%2</p></br><p>%3</p>")
291 .arg(tr("Legacy Highlights are replaced by Highlights"),
292 tr("These highlights will keep working for now, but you should move to "
293 "the improved highlight rules when you can."),
294 tr("Configure the new style of highlights in "
295 "<i>%1</i>.").arg(tr("Highlights"))));
297 // We're running in client/split mode, allow for splitting the details.
298 QMessageBox::information(
300 tr("Local Highlights vs. Remote Highlights"),
301 QString("<p><b>%1</b></p></br><p>%2</p></br><p>%3</p>")
302 .arg(tr("Local Highlights apply to this device only"),
303 tr("Highlights configured on this page only apply to your current "
305 tr("Configure highlights for all of your devices in "
306 "<i>%1</i>.").arg(tr("Remote Highlights").replace(" ", " "))));
307 // Re-use translations of "Remote Highlights" as this is a word-for-word reference, forcing
308 // all spaces to be non-breaking
313 void HighlightSettingsPage::load()
315 NotificationSettings notificationSettings;
319 foreach(QVariant highlight, notificationSettings.highlightList()) {
320 QVariantMap highlightRule = highlight.toMap();
321 QString name = highlightRule["Name"].toString();
322 bool regex = highlightRule["RegEx"].toBool();
323 bool cs = highlightRule["CS"].toBool();
324 bool enable = highlightRule["Enable"].toBool();
325 QString chanName = highlightRule["Channel"].toString();
327 addNewRow(name, regex, cs, enable, chanName, true);
330 switch (notificationSettings.highlightNick())
332 case NotificationSettings::NoNick:
333 ui.highlightNoNick->setChecked(true);
335 case NotificationSettings::CurrentNick:
336 ui.highlightCurrentNick->setChecked(true);
338 case NotificationSettings::AllNicks:
339 ui.highlightAllNicks->setChecked(true);
342 ui.nicksCaseSensitive->setChecked(notificationSettings.nicksCaseSensitive());
344 setChangedState(false);
348 void HighlightSettingsPage::save()
350 NotificationSettings notificationSettings;
351 notificationSettings.setHighlightList(highlightList);
353 NotificationSettings::HighlightNickType highlightNickType = NotificationSettings::NoNick;
354 if (ui.highlightCurrentNick->isChecked())
355 highlightNickType = NotificationSettings::CurrentNick;
356 if (ui.highlightAllNicks->isChecked())
357 highlightNickType = NotificationSettings::AllNicks;
359 notificationSettings.setHighlightNick(highlightNickType);
360 notificationSettings.setNicksCaseSensitive(ui.nicksCaseSensitive->isChecked());
363 setChangedState(false);
367 void HighlightSettingsPage::widgetHasChanged()
369 bool changed = testHasChanged();
370 if (changed != hasChanged()) setChangedState(changed);
374 bool HighlightSettingsPage::testHasChanged()
376 NotificationSettings notificationSettings;
378 NotificationSettings::HighlightNickType highlightNickType = NotificationSettings::NoNick;
379 if (ui.highlightCurrentNick->isChecked())
380 highlightNickType = NotificationSettings::CurrentNick;
381 if (ui.highlightAllNicks->isChecked())
382 highlightNickType = NotificationSettings::AllNicks;
384 if (notificationSettings.highlightNick() != highlightNickType)
386 if (notificationSettings.nicksCaseSensitive() != ui.nicksCaseSensitive->isChecked())
388 if (notificationSettings.highlightList() != highlightList)