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"), tr("Local Highlights"), parent)
34 ui.highlightTable->verticalHeader()->hide();
35 ui.highlightTable->setShowGrid(false);
37 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::RegExColumn)->setToolTip(
38 tr("<b>RegEx</b>: This option determines if the highlight rule should be "
39 "interpreted as a <b>regular expression</b> or just as a keyword."));
40 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::RegExColumn)->setWhatsThis(
41 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::RegExColumn)->toolTip());
43 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::CsColumn)->setToolTip(
44 tr("<b>CS</b>: This option determines if the highlight rule should be interpreted "
45 "<b>case sensitive</b>."));
46 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::CsColumn)->setWhatsThis(
47 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::CsColumn)->toolTip());
49 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::ChanColumn)->setToolTip(
50 tr("<p><b>Channel</b>: Semicolon separated list of channel names.</p>"
51 "<p><i>Example:</i><br />"
52 "<i>#quassel*; #foobar; !#quasseldroid</i><br />"
53 "would match on #foobar and on any channel starting with <i>#quassel</i> except "
54 "for <i>#quasseldroid</i><br />"
55 "<p>If only inverted names are specified, it will match anything except for "
56 "what's specified (implicit wildcard).</p>"
57 "<p><i>Example:</i><br />"
58 "<i>!#quassel*; !#foobar</i><br />"
59 "would match anything except for #foobar or any channel starting with "
60 "<i>#quassel</i></p>"));
61 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::ChanColumn)->setWhatsThis(
62 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::ChanColumn)->toolTip());
64 #if QT_VERSION < 0x050000
65 ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::NameColumn, QHeaderView::Stretch);
66 ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::RegExColumn, QHeaderView::ResizeToContents);
67 ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::CsColumn, QHeaderView::ResizeToContents);
68 ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::EnableColumn, QHeaderView::ResizeToContents);
69 ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::ChanColumn, QHeaderView::ResizeToContents);
71 ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::NameColumn, QHeaderView::Stretch);
72 ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::RegExColumn, QHeaderView::ResizeToContents);
73 ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::CsColumn, QHeaderView::ResizeToContents);
74 ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::EnableColumn, QHeaderView::ResizeToContents);
75 ui.highlightTable->horizontalHeader()->setSectionResizeMode(HighlightSettingsPage::ChanColumn, QHeaderView::ResizeToContents);
79 ui.localHighlightsIcon->setPixmap(QIcon::fromTheme("dialog-information").pixmap(16));
81 // Set up client/monolithic local highlights information
82 if (Quassel::runMode() == Quassel::Monolithic) {
83 // We're running in Monolithic mode, core/client version in total sync. Discourage the use
84 // of local highlights as it's identical to setting remote highlights.
85 ui.localHighlightsLabel->setText(
86 tr("Local Highlights are replaced by Remote Highlights"));
88 // We're running in client/split mode, allow for splitting the details.
89 ui.localHighlightsLabel->setText(tr("Local Highlights apply to this device only"));
92 connect(ui.add, SIGNAL(clicked(bool)), this, SLOT(addNewRow()));
93 connect(ui.remove, SIGNAL(clicked(bool)), this, SLOT(removeSelectedRows()));
94 //TODO: search for a better signal (one that emits everytime a selection has been changed for one item)
95 connect(ui.highlightTable, SIGNAL(itemClicked(QTableWidgetItem *)), this, SLOT(selectRow(QTableWidgetItem *)));
97 connect(ui.highlightAllNicks, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
98 connect(ui.highlightCurrentNick, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
99 connect(ui.highlightNoNick, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
100 connect(ui.nicksCaseSensitive, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
101 connect(ui.add, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
102 connect(ui.remove, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
103 connect(ui.highlightTable, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(tableChanged(QTableWidgetItem *)));
107 bool HighlightSettingsPage::hasDefaults() const
113 void HighlightSettingsPage::defaults()
115 ui.highlightNoNick->setChecked(true);
116 ui.nicksCaseSensitive->setChecked(false);
123 void HighlightSettingsPage::addNewRow(QString name, bool regex, bool cs, bool enable, QString chanName, bool self)
125 ui.highlightTable->setRowCount(ui.highlightTable->rowCount()+1);
127 QTableWidgetItem *enableItem = new QTableWidgetItem("");
129 enableItem->setCheckState(Qt::Checked);
131 enableItem->setCheckState(Qt::Unchecked);
132 enableItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
134 QTableWidgetItem *nameItem = new QTableWidgetItem(name);
136 QTableWidgetItem *regexItem = new QTableWidgetItem("");
138 regexItem->setCheckState(Qt::Checked);
140 regexItem->setCheckState(Qt::Unchecked);
141 regexItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
143 QTableWidgetItem *csItem = new QTableWidgetItem("");
145 csItem->setCheckState(Qt::Checked);
147 csItem->setCheckState(Qt::Unchecked);
148 csItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
150 QTableWidgetItem *chanNameItem = new QTableWidgetItem(chanName);
152 enableItem->setToolTip(tr("Enable/disable this rule"));
153 nameItem->setToolTip(tr("Phrase to match"));
154 regexItem->setToolTip(
155 tr("<b>RegEx</b>: This option determines if the highlight rule should be "
156 "interpreted as a <b>regular expression</b> or just as a keyword."));
158 tr("<b>CS</b>: This option determines if the highlight rule should be interpreted "
159 "<b>case sensitive</b>."));
160 chanNameItem->setToolTip(
161 tr("<p><b>Channel</b>: Semicolon separated list of channel names.</p>"
162 "<p><i>Example:</i><br />"
163 "<i>#quassel*; #foobar; !#quasseldroid</i><br />"
164 "would match on #foobar and on any channel starting with <i>#quassel</i> except "
165 "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 #foobar 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);
194 void HighlightSettingsPage::removeSelectedRows()
196 QList<int> selectedRows;
197 QList<QTableWidgetItem *> selectedItemList = ui.highlightTable->selectedItems();
198 foreach(QTableWidgetItem *selectedItem, selectedItemList) {
199 selectedRows.append(selectedItem->row());
201 qSort(selectedRows.begin(), selectedRows.end(), qGreater<int>());
203 foreach(int row, selectedRows) {
204 if (row != lastRow) {
205 ui.highlightTable->removeRow(row);
206 highlightList.removeAt(row);
213 void HighlightSettingsPage::selectRow(QTableWidgetItem *item)
215 int row = item->row();
216 bool selected = item->isSelected();
217 ui.highlightTable->setRangeSelected(QTableWidgetSelectionRange(row, 0, row, HighlightSettingsPage::ColumnCount-1), selected);
221 void HighlightSettingsPage::emptyTable()
223 // ui.highlight and highlightList should have the same size, but just to make sure.
224 if (ui.highlightTable->rowCount() != highlightList.size()) {
225 qDebug() << "something is wrong: ui.highlight and highlightList don't have the same size!";
227 while (ui.highlightTable->rowCount()) {
228 ui.highlightTable->removeRow(0);
230 while (highlightList.size()) {
231 highlightList.removeLast();
236 void HighlightSettingsPage::tableChanged(QTableWidgetItem *item)
238 if (item->row()+1 > highlightList.size())
241 QVariantMap highlightRule = highlightList.value(item->row()).toMap();
243 switch (item->column())
245 case HighlightSettingsPage::EnableColumn:
246 highlightRule["Enable"] = (item->checkState() == Qt::Checked);
248 case HighlightSettingsPage::NameColumn:
249 if (item->text() == "")
250 item->setText(tr("this shouldn't be empty"));
251 highlightRule["Name"] = item->text();
253 case HighlightSettingsPage::RegExColumn:
254 highlightRule["RegEx"] = (item->checkState() == Qt::Checked);
256 case HighlightSettingsPage::CsColumn:
257 highlightRule["CS"] = (item->checkState() == Qt::Checked);
259 case HighlightSettingsPage::ChanColumn:
260 if (!item->text().isEmpty() && item->text().trimmed().isEmpty())
262 highlightRule["Channel"] = item->text();
265 highlightList[item->row()] = highlightRule;
266 emit widgetHasChanged();
270 void HighlightSettingsPage::on_localHighlightsDetails_clicked()
272 // Re-use translations of "Remote Highlights" as this is a word-for-word reference, forcing all
273 // spaces to non-breaking
274 const QString remoteHighlightsName = tr("Remote Highlights").replace(" ", " ");
275 QString localHighlightsMsgText;
277 // Set up client/monolithic local highlights information
278 if (Quassel::runMode() == Quassel::Monolithic) {
279 // We're running in Monolithic mode, core/client version in total sync. Discourage the use
280 // of local highlights as it's identical to setting remote highlights.
281 localHighlightsMsgText =
282 QString("<p><b>%1</b></p></br><p>%2</p></br><p>%3</p>"
283 ).arg(tr("Local Highlights are replaced by Remote Highlights"),
284 tr("These highlights will keep working for now, but you should move "
285 "to the improved highlight rules when you can."),
286 tr("Configure the new style of highlights in "
287 "<i>%1</i>.").arg(remoteHighlightsName));
289 // We're running in client/split mode, allow for splitting the details.
290 localHighlightsMsgText =
291 QString("<p><b>%1</b></p></br><p>%2</p></br><p>%3</p>"
292 ).arg(tr("Local Highlights apply to this device only"),
293 tr("Highlights configured on this page only apply to your current "
295 tr("Configure highlights for all of your devices in "
296 "<i>%1</i>.").arg(remoteHighlightsName));
299 QMessageBox::information(this,
300 tr("Local Highlights vs. Remote Highlights"),
301 localHighlightsMsgText);
305 void HighlightSettingsPage::load()
307 NotificationSettings notificationSettings;
311 foreach(QVariant highlight, notificationSettings.highlightList()) {
312 QVariantMap highlightRule = highlight.toMap();
313 QString name = highlightRule["Name"].toString();
314 bool regex = highlightRule["RegEx"].toBool();
315 bool cs = highlightRule["CS"].toBool();
316 bool enable = highlightRule["Enable"].toBool();
317 QString chanName = highlightRule["Channel"].toString();
319 addNewRow(name, regex, cs, enable, chanName, true);
322 switch (notificationSettings.highlightNick())
324 case NotificationSettings::NoNick:
325 ui.highlightNoNick->setChecked(true);
327 case NotificationSettings::CurrentNick:
328 ui.highlightCurrentNick->setChecked(true);
330 case NotificationSettings::AllNicks:
331 ui.highlightAllNicks->setChecked(true);
334 ui.nicksCaseSensitive->setChecked(notificationSettings.nicksCaseSensitive());
336 setChangedState(false);
340 void HighlightSettingsPage::save()
342 NotificationSettings notificationSettings;
343 notificationSettings.setHighlightList(highlightList);
345 NotificationSettings::HighlightNickType highlightNickType = NotificationSettings::NoNick;
346 if (ui.highlightCurrentNick->isChecked())
347 highlightNickType = NotificationSettings::CurrentNick;
348 if (ui.highlightAllNicks->isChecked())
349 highlightNickType = NotificationSettings::AllNicks;
351 notificationSettings.setHighlightNick(highlightNickType);
352 notificationSettings.setNicksCaseSensitive(ui.nicksCaseSensitive->isChecked());
355 setChangedState(false);
359 void HighlightSettingsPage::widgetHasChanged()
361 bool changed = testHasChanged();
362 if (changed != hasChanged()) setChangedState(changed);
366 bool HighlightSettingsPage::testHasChanged()
368 NotificationSettings notificationSettings;
370 NotificationSettings::HighlightNickType highlightNickType = NotificationSettings::NoNick;
371 if (ui.highlightCurrentNick->isChecked())
372 highlightNickType = NotificationSettings::CurrentNick;
373 if (ui.highlightAllNicks->isChecked())
374 highlightNickType = NotificationSettings::AllNicks;
376 if (notificationSettings.highlightNick() != highlightNickType)
378 if (notificationSettings.nicksCaseSensitive() != ui.nicksCaseSensitive->isChecked())
380 if (notificationSettings.highlightList() != highlightList)