icons: Warn on missing icons
[quassel.git] / src / qtui / settingspages / highlightsettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "highlightsettingspage.h"
22
23 #include <QHeaderView>
24 #include <QMessageBox>
25
26 #include "client.h"
27 #include "icon.h"
28 #include "qtui.h"
29 #include "uisettings.h"
30
31
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"),
37                    parent)
38 {
39     ui.setupUi(this);
40     ui.highlightTable->verticalHeader()->hide();
41     ui.highlightTable->setShowGrid(false);
42
43
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());
48
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());
53
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());
59
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());
65
66     ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::ChanColumn)->setToolTip(
67                 tr("<p><b>Channel</b>: Semicolon separated list of channel names, leave blank to "
68                    "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());
81
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);
88 #else
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);
94 #endif
95
96     // Information icon
97     ui.localHighlightsIcon->setPixmap(icon::get("dialog-information").pixmap(16));
98
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"));
105     } else {
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"));
108     }
109
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 *)));
114
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 *)));
122 }
123
124
125 bool HighlightSettingsPage::hasDefaults() const
126 {
127     return true;
128 }
129
130
131 void HighlightSettingsPage::defaults()
132 {
133     ui.highlightNoNick->setChecked(true);
134     ui.nicksCaseSensitive->setChecked(false);
135     emptyTable();
136
137     widgetHasChanged();
138 }
139
140
141 void HighlightSettingsPage::addNewRow(QString name, bool regex, bool cs, bool enable, QString chanName, bool self)
142 {
143     ui.highlightTable->setRowCount(ui.highlightTable->rowCount()+1);
144
145     QTableWidgetItem *enableItem = new QTableWidgetItem("");
146     if (enable)
147         enableItem->setCheckState(Qt::Checked);
148     else
149         enableItem->setCheckState(Qt::Unchecked);
150     enableItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
151
152     QTableWidgetItem *nameItem = new QTableWidgetItem(name);
153
154     QTableWidgetItem *regexItem = new QTableWidgetItem("");
155     if (regex)
156         regexItem->setCheckState(Qt::Checked);
157     else
158         regexItem->setCheckState(Qt::Unchecked);
159     regexItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
160
161     QTableWidgetItem *csItem = new QTableWidgetItem("");
162     if (cs)
163         csItem->setCheckState(Qt::Checked);
164     else
165         csItem->setCheckState(Qt::Unchecked);
166     csItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
167
168     QTableWidgetItem *chanNameItem = new QTableWidgetItem(chanName);
169
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."));
175     csItem->setToolTip(
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 names, leave blank to "
180                    "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>"));
191
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);
198
199     if (!self)
200         ui.highlightTable->setCurrentItem(nameItem);
201
202     QVariantMap highlightRule;
203     highlightRule["Name"] = name;
204     highlightRule["RegEx"] = regex;
205     highlightRule["CS"] = cs;
206     highlightRule["Enable"] = enable;
207     highlightRule["Channel"] = chanName;
208
209     highlightList.append(highlightRule);
210 }
211
212
213 void HighlightSettingsPage::removeSelectedRows()
214 {
215     QList<int> selectedRows;
216     QList<QTableWidgetItem *> selectedItemList = ui.highlightTable->selectedItems();
217     foreach(QTableWidgetItem *selectedItem, selectedItemList) {
218         selectedRows.append(selectedItem->row());
219     }
220     qSort(selectedRows.begin(), selectedRows.end(), qGreater<int>());
221     int lastRow = -1;
222     foreach(int row, selectedRows) {
223         if (row != lastRow) {
224             ui.highlightTable->removeRow(row);
225             highlightList.removeAt(row);
226         }
227         lastRow = row;
228     }
229 }
230
231
232 void HighlightSettingsPage::selectRow(QTableWidgetItem *item)
233 {
234     int row = item->row();
235     bool selected = item->isSelected();
236     ui.highlightTable->setRangeSelected(QTableWidgetSelectionRange(row, 0, row, HighlightSettingsPage::ColumnCount-1), selected);
237 }
238
239
240 void HighlightSettingsPage::emptyTable()
241 {
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!";
245     }
246     while (ui.highlightTable->rowCount()) {
247         ui.highlightTable->removeRow(0);
248     }
249     while (highlightList.size()) {
250         highlightList.removeLast();
251     }
252 }
253
254
255 void HighlightSettingsPage::tableChanged(QTableWidgetItem *item)
256 {
257     if (item->row()+1 > highlightList.size())
258         return;
259
260     QVariantMap highlightRule = highlightList.value(item->row()).toMap();
261
262     switch (item->column())
263     {
264     case HighlightSettingsPage::EnableColumn:
265         highlightRule["Enable"] = (item->checkState() == Qt::Checked);
266         break;
267     case HighlightSettingsPage::NameColumn:
268         if (item->text() == "")
269             item->setText(tr("this shouldn't be empty"));
270         highlightRule["Name"] = item->text();
271         break;
272     case HighlightSettingsPage::RegExColumn:
273         highlightRule["RegEx"] = (item->checkState() == Qt::Checked);
274         break;
275     case HighlightSettingsPage::CsColumn:
276         highlightRule["CS"] = (item->checkState() == Qt::Checked);
277         break;
278     case HighlightSettingsPage::ChanColumn:
279         if (!item->text().isEmpty() && item->text().trimmed().isEmpty())
280             item->setText("");
281         highlightRule["Channel"] = item->text();
282         break;
283     }
284     highlightList[item->row()] = highlightRule;
285     emit widgetHasChanged();
286 }
287
288
289 void HighlightSettingsPage::on_localHighlightsDetails_clicked()
290 {
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(
296                     this,
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"))));
304     } else {
305         // We're running in client/split mode, allow for splitting the details.
306         QMessageBox::information(
307                     this,
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 "
312                             "device."),
313                          tr("Configure highlights for all of your devices in "
314                             "<i>%1</i>.").arg(tr("Remote Highlights").replace(" ", "&nbsp;"))));
315         // Re-use translations of "Remote Highlights" as this is a word-for-word reference, forcing
316         // all spaces to be non-breaking
317     }
318 }
319
320
321 void HighlightSettingsPage::load()
322 {
323     NotificationSettings notificationSettings;
324
325     emptyTable();
326
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();
334
335         addNewRow(name, regex, cs, enable, chanName, true);
336     }
337
338     switch (notificationSettings.highlightNick())
339     {
340     case NotificationSettings::NoNick:
341         ui.highlightNoNick->setChecked(true);
342         break;
343     case NotificationSettings::CurrentNick:
344         ui.highlightCurrentNick->setChecked(true);
345         break;
346     case NotificationSettings::AllNicks:
347         ui.highlightAllNicks->setChecked(true);
348         break;
349     }
350     ui.nicksCaseSensitive->setChecked(notificationSettings.nicksCaseSensitive());
351
352     setChangedState(false);
353 }
354
355
356 void HighlightSettingsPage::save()
357 {
358     NotificationSettings notificationSettings;
359     notificationSettings.setHighlightList(highlightList);
360
361     NotificationSettings::HighlightNickType highlightNickType = NotificationSettings::NoNick;
362     if (ui.highlightCurrentNick->isChecked())
363         highlightNickType = NotificationSettings::CurrentNick;
364     if (ui.highlightAllNicks->isChecked())
365         highlightNickType = NotificationSettings::AllNicks;
366
367     notificationSettings.setHighlightNick(highlightNickType);
368     notificationSettings.setNicksCaseSensitive(ui.nicksCaseSensitive->isChecked());
369
370     load();
371     setChangedState(false);
372 }
373
374
375 void HighlightSettingsPage::widgetHasChanged()
376 {
377     bool changed = testHasChanged();
378     if (changed != hasChanged()) setChangedState(changed);
379 }
380
381
382 bool HighlightSettingsPage::testHasChanged()
383 {
384     NotificationSettings notificationSettings;
385
386     NotificationSettings::HighlightNickType highlightNickType = NotificationSettings::NoNick;
387     if (ui.highlightCurrentNick->isChecked())
388         highlightNickType = NotificationSettings::CurrentNick;
389     if (ui.highlightAllNicks->isChecked())
390         highlightNickType = NotificationSettings::AllNicks;
391
392     if (notificationSettings.highlightNick() != highlightNickType)
393         return true;
394     if (notificationSettings.nicksCaseSensitive() != ui.nicksCaseSensitive->isChecked())
395         return true;
396     if (notificationSettings.highlightList() != highlightList)
397         return true;
398
399     return false;
400 }