1 /***************************************************************************
2 * Copyright (C) 2005-2022 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 "chatmonitorsettingspage.h"
23 #include <QMessageBox>
26 #include "backlogrequester.h"
27 #include "backlogsettings.h"
28 #include "buffermodel.h"
29 #include "bufferview.h"
30 #include "bufferviewconfig.h"
31 #include "bufferviewfilter.h"
32 #include "chatviewsettings.h"
35 #include "networkmodel.h"
38 ChatMonitorSettingsPage::ChatMonitorSettingsPage(QWidget* parent)
39 : SettingsPage(tr("Interface"), tr("Chat Monitor"), parent)
43 ui.activateBuffer->setIcon(icon::get("go-next"));
44 ui.deactivateBuffer->setIcon(icon::get("go-previous"));
46 // setup available buffers config (for the bufferview on the left)
47 _configAvailable = new BufferViewConfig(-667, this);
48 _configAvailable->setBufferViewName("tmpChatMonitorAvailableBuffers");
49 _configAvailable->setSortAlphabetically(true);
50 _configAvailable->setDisableDecoration(true);
51 _configAvailable->setNetworkId(NetworkId());
52 _configAvailable->setInitialized();
54 // setup active buffers config (for the bufferview on the right)
55 _configActive = new BufferViewConfig(-666, this);
56 _configActive->setBufferViewName("tmpChatMonitorActiveBuffers");
57 _configActive->setSortAlphabetically(true);
58 _configActive->setDisableDecoration(true);
59 _configActive->setNetworkId(NetworkId());
60 _configActive->setInitialized();
62 // fill combobox with operation modes
63 ui.operationMode->addItem(tr("Opt In"), ChatViewSettings::OptIn);
64 ui.operationMode->addItem(tr("Opt Out"), ChatViewSettings::OptOut);
67 connect(ui.operationMode, selectOverload<int>(&QComboBox::currentIndexChanged), this, &ChatMonitorSettingsPage::switchOperationMode);
68 connect(ui.showHighlights, &QAbstractButton::toggled, this, &ChatMonitorSettingsPage::widgetHasChanged);
69 connect(ui.showOwnMessages, &QAbstractButton::toggled, this, &ChatMonitorSettingsPage::widgetHasChanged);
70 connect(ui.alwaysOwn, &QAbstractButton::toggled, this, &ChatMonitorSettingsPage::widgetHasChanged);
71 connect(ui.showBacklog, &QAbstractButton::toggled, this, &ChatMonitorSettingsPage::widgetHasChanged);
72 connect(ui.includeRead, &QAbstractButton::toggled, this, &ChatMonitorSettingsPage::widgetHasChanged);
74 // AsNeededBacklogRequester conflicts with showing backlog in Chat Monitor
75 BacklogSettings backlogSettings;
76 backlogSettings.initAndNotify("RequesterType", this, &ChatMonitorSettingsPage::setRequesterType, BacklogRequester::AsNeeded);
79 bool ChatMonitorSettingsPage::hasDefaults() const
84 void ChatMonitorSettingsPage::defaults()
86 // NOTE: Whenever changing defaults here, also update ChatMonitorFilter::ChatMonitorFilter()
87 // and ChatMonitorSettingsPage::loadSettings() to match
89 settings["OperationMode"] = ChatViewSettings::OptOut;
90 settings["ShowHighlights"] = false;
91 settings["ShowOwnMsgs"] = true;
92 settings["AlwaysOwn"] = false;
93 settings["Buffers"] = QVariant();
94 settings["Default"] = true;
95 settings["ShowBacklog"] = true;
96 settings["IncludeRead"] = false;
101 void ChatMonitorSettingsPage::load()
103 if (settings.contains("Default"))
104 settings.remove("Default");
108 switchOperationMode(settings["OperationMode"].toInt() - 1);
109 ui.operationMode->setCurrentIndex(settings["OperationMode"].toInt() - 1);
110 ui.showHighlights->setChecked(settings["ShowHighlights"].toBool());
111 ui.showOwnMessages->setChecked(settings["ShowOwnMsgs"].toBool());
112 ui.alwaysOwn->setChecked(settings["AlwaysOwn"].toBool());
113 ui.showBacklog->setChecked(settings["ShowBacklog"].toBool());
114 ui.includeRead->setChecked(settings["IncludeRead"].toBool());
116 // get all available buffer Ids
117 QList<BufferId> allBufferIds = Client::networkModel()->allBufferIds();
119 if (!settings["Buffers"].toList().isEmpty()) {
120 QList<BufferId> bufferIdsFromConfig;
121 // remove all active buffers from the available config
122 foreach (QVariant v, settings["Buffers"].toList()) {
123 bufferIdsFromConfig << v.value<BufferId>();
124 allBufferIds.removeAll(v.value<BufferId>());
126 Client::networkModel()->sortBufferIds(bufferIdsFromConfig);
127 _configActive->setBufferList(bufferIdsFromConfig);
129 ui.activeBuffers->setFilteredModel(Client::bufferModel(), _configActive);
131 Client::networkModel()->sortBufferIds(allBufferIds);
132 _configAvailable->setBufferList(allBufferIds);
133 ui.availableBuffers->setFilteredModel(Client::bufferModel(), _configAvailable);
135 setChangedState(false);
138 void ChatMonitorSettingsPage::loadSettings()
140 // NOTE: Whenever changing defaults here, also update ChatMonitorFilter::ChatMonitorFilter()
141 // and ChatMonitorSettingsPage::defaults() to match
142 ChatViewSettings chatViewSettings("ChatMonitor");
144 settings["OperationMode"] = (ChatViewSettings::OperationMode)chatViewSettings.value("OperationMode", ChatViewSettings::OptOut).toInt();
145 settings["ShowHighlights"] = chatViewSettings.value("ShowHighlights", false);
146 settings["ShowOwnMsgs"] = chatViewSettings.value("ShowOwnMsgs", true);
147 settings["AlwaysOwn"] = chatViewSettings.value("AlwaysOwn", false);
148 settings["Buffers"] = chatViewSettings.value("Buffers", QVariantList());
149 settings["ShowBacklog"] = chatViewSettings.value("ShowBacklog", true);
150 settings["IncludeRead"] = chatViewSettings.value("IncludeRead", false);
153 void ChatMonitorSettingsPage::save()
155 ChatViewSettings chatViewSettings("ChatMonitor");
156 // save operation mode
157 chatViewSettings.setValue("OperationMode", ui.operationMode->currentIndex() + 1);
158 chatViewSettings.setValue("ShowHighlights", ui.showHighlights->isChecked());
159 chatViewSettings.setValue("ShowOwnMsgs", ui.showOwnMessages->isChecked());
160 chatViewSettings.setValue("AlwaysOwn", ui.alwaysOwn->isChecked());
161 chatViewSettings.setValue("ShowBacklog", ui.showBacklog->isChecked());
162 chatViewSettings.setValue("IncludeRead", ui.includeRead->isChecked());
164 // save list of active buffers
165 QVariantList saveableBufferIdList;
166 foreach (BufferId id, _configActive->bufferList()) {
167 saveableBufferIdList << QVariant::fromValue(id);
170 chatViewSettings.setValue("Buffers", saveableBufferIdList);
172 setChangedState(false);
175 void ChatMonitorSettingsPage::widgetHasChanged()
177 bool changed = testHasChanged();
178 if (changed != hasChanged())
179 setChangedState(changed);
182 bool ChatMonitorSettingsPage::testHasChanged()
184 if (settings["OperationMode"].toInt() != ui.operationMode->currentIndex() + 1)
186 if (settings["ShowHighlights"].toBool() != ui.showHighlights->isChecked())
188 if (settings["ShowOwnMsgs"].toBool() != ui.showOwnMessages->isChecked())
190 if (settings["AlwaysOwn"].toBool() != ui.alwaysOwn->isChecked())
192 if (settings["ShowBacklog"].toBool() != ui.showBacklog->isChecked())
194 if (settings["IncludeRead"].toBool() != ui.includeRead->isChecked())
197 if (_configActive->bufferList().count() != settings["Buffers"].toList().count())
200 QSet<BufferId> uiBufs = toQSet(_configActive->bufferList());
201 QSet<BufferId> settingsBufs;
202 foreach (QVariant v, settings["Buffers"].toList())
203 settingsBufs << v.value<BufferId>();
204 if (uiBufs != settingsBufs)
210 // TODO: - support drag 'n drop
211 // - adding of complete networks(?)
214 toggleBuffers takes each a bufferView and its config for "input" and "output".
215 Any selected item will be moved over from the input to the output bufferview.
217 void ChatMonitorSettingsPage::toggleBuffers(BufferView* inView, BufferViewConfig* inCfg, BufferView* outView, BufferViewConfig* outCfg)
219 // Fill QMap with selected items ordered by selection row
220 QMap<int, QList<BufferId>> selectedBuffers;
221 foreach (QModelIndex index, inView->selectionModel()->selectedIndexes()) {
222 BufferId inBufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
223 if (index.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
225 // If item is a network: move over all children and skip other selected items of this node
227 else if (index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType) {
228 selectedBuffers[index.parent().row()] << inBufferId;
232 // clear selection to be able to remove the bufferIds without errors
233 inView->selectionModel()->clearSelection();
236 Invalidate the BufferViewFilters' configs to get constant add/remove times
238 This can probably be removed whenever BufferViewConfig::bulkAdd or something
239 like that is available.
241 qobject_cast<BufferViewFilter*>(outView->model())->setConfig(nullptr);
242 qobject_cast<BufferViewFilter*>(inView->model())->setConfig(nullptr);
244 // actually move the ids
245 foreach (QList<BufferId> list, selectedBuffers) {
246 foreach (BufferId buffer, list) {
247 outCfg->addBuffer(buffer, 0);
248 inCfg->removeBuffer(buffer);
252 outView->setFilteredModel(Client::bufferModel(), outCfg);
253 inView->setFilteredModel(Client::bufferModel(), inCfg);
258 void ChatMonitorSettingsPage::on_activateBuffer_clicked()
260 if (ui.availableBuffers->currentIndex().isValid() && ui.availableBuffers->selectionModel()->hasSelection()) {
261 toggleBuffers(ui.availableBuffers, _configAvailable, ui.activeBuffers, _configActive);
266 void ChatMonitorSettingsPage::on_deactivateBuffer_clicked()
268 if (ui.activeBuffers->currentIndex().isValid() && ui.activeBuffers->selectionModel()->hasSelection()) {
269 toggleBuffers(ui.activeBuffers, _configActive, ui.availableBuffers, _configAvailable);
275 switchOperationMode gets called on combobox signal currentIndexChanged.
276 modeIndex is the row id in combobox itemlist
278 void ChatMonitorSettingsPage::switchOperationMode(int idx)
280 auto mode = (ChatViewSettings::OperationMode)(idx + 1);
281 if (mode == ChatViewSettings::OptIn) {
282 ui.labelActiveBuffers->setText(tr("Show:"));
284 else if (mode == ChatViewSettings::OptOut) {
285 ui.labelActiveBuffers->setText(tr("Ignore:"));
290 void ChatMonitorSettingsPage::setRequesterType(const QVariant& v)
292 bool usingAsNeededRequester = (v.toInt() == BacklogRequester::AsNeeded);
293 ui.showBacklogUnavailableDetails->setVisible(usingAsNeededRequester);
294 if (usingAsNeededRequester) {
295 ui.showBacklog->setText(tr("Show messages from backlog (not available)"));
298 ui.showBacklog->setText(tr("Show messages from backlog"));
302 void ChatMonitorSettingsPage::on_showBacklogUnavailableDetails_clicked()
304 // Explain that backlog fetching is disabled, so backlog messages won't show up
306 // Technically, backlog messages *will* show up once fetched, e.g. after clicking on a buffer.
307 // This might be too trivial of a detail to warrant explaining, though.
308 QMessageBox::information(this,
309 tr("Messages from backlog are not fetched"),
310 QString("<p>%1</p><p>%2</p>")
311 .arg(tr("No initial backlog will be fetched when using the backlog request method of <i>%1</i>.")
312 .arg(tr("Only fetch when needed").replace(" ", " ")),
313 tr("Configure this in the <i>%1</i> settings page.")
314 .arg(tr("Backlog Fetching").replace(" ", " "))
317 // Re-use translations of "Only fetch when needed" and "Backlog Fetching" as this is a
318 // word-for-word reference, forcing all spaces to be non-breaking