1 /***************************************************************************
2 * Copyright (C) 2005-2019 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"
25 #include "buffermodel.h"
26 #include "bufferview.h"
27 #include "bufferviewconfig.h"
28 #include "bufferviewfilter.h"
29 #include "chatviewsettings.h"
32 #include "networkmodel.h"
34 ChatMonitorSettingsPage::ChatMonitorSettingsPage(QWidget* parent)
35 : SettingsPage(tr("Interface"), tr("Chat Monitor"), parent)
39 ui.activateBuffer->setIcon(icon::get("go-next"));
40 ui.deactivateBuffer->setIcon(icon::get("go-previous"));
42 // setup available buffers config (for the bufferview on the left)
43 _configAvailable = new BufferViewConfig(-667, this);
44 _configAvailable->setBufferViewName("tmpChatMonitorAvailableBuffers");
45 _configAvailable->setSortAlphabetically(true);
46 _configAvailable->setDisableDecoration(true);
47 _configAvailable->setNetworkId(NetworkId());
48 _configAvailable->setInitialized();
50 // setup active buffers config (for the bufferview on the right)
51 _configActive = new BufferViewConfig(-666, this);
52 _configActive->setBufferViewName("tmpChatMonitorActiveBuffers");
53 _configActive->setSortAlphabetically(true);
54 _configActive->setDisableDecoration(true);
55 _configActive->setNetworkId(NetworkId());
56 _configActive->setInitialized();
58 // fill combobox with operation modes
59 ui.operationMode->addItem(tr("Opt In"), ChatViewSettings::OptIn);
60 ui.operationMode->addItem(tr("Opt Out"), ChatViewSettings::OptOut);
63 connect(ui.operationMode, selectOverload<int>(&QComboBox::currentIndexChanged), this, &ChatMonitorSettingsPage::switchOperationMode);
64 connect(ui.showHighlights, &QAbstractButton::toggled, this, &ChatMonitorSettingsPage::widgetHasChanged);
65 connect(ui.showOwnMessages, &QAbstractButton::toggled, this, &ChatMonitorSettingsPage::widgetHasChanged);
66 connect(ui.alwaysOwn, &QAbstractButton::toggled, this, &ChatMonitorSettingsPage::widgetHasChanged);
67 connect(ui.showBacklog, &QAbstractButton::toggled, this, &ChatMonitorSettingsPage::widgetHasChanged);
68 connect(ui.includeRead, &QAbstractButton::toggled, this, &ChatMonitorSettingsPage::widgetHasChanged);
71 bool ChatMonitorSettingsPage::hasDefaults() const
76 void ChatMonitorSettingsPage::defaults()
78 // NOTE: Whenever changing defaults here, also update ChatMonitorFilter::ChatMonitorFilter()
79 // and ChatMonitorSettingsPage::loadSettings() to match
81 settings["OperationMode"] = ChatViewSettings::OptOut;
82 settings["ShowHighlights"] = false;
83 settings["ShowOwnMsgs"] = true;
84 settings["AlwaysOwn"] = false;
85 settings["Buffers"] = QVariant();
86 settings["Default"] = true;
87 settings["ShowBacklog"] = true;
88 settings["IncludeRead"] = false;
93 void ChatMonitorSettingsPage::load()
95 if (settings.contains("Default"))
96 settings.remove("Default");
100 switchOperationMode(settings["OperationMode"].toInt() - 1);
101 ui.operationMode->setCurrentIndex(settings["OperationMode"].toInt() - 1);
102 ui.showHighlights->setChecked(settings["ShowHighlights"].toBool());
103 ui.showOwnMessages->setChecked(settings["ShowOwnMsgs"].toBool());
104 ui.alwaysOwn->setChecked(settings["AlwaysOwn"].toBool());
105 ui.showBacklog->setChecked(settings["ShowBacklog"].toBool());
106 ui.includeRead->setChecked(settings["IncludeRead"].toBool());
108 // get all available buffer Ids
109 QList<BufferId> allBufferIds = Client::networkModel()->allBufferIds();
111 if (!settings["Buffers"].toList().isEmpty()) {
112 QList<BufferId> bufferIdsFromConfig;
113 // remove all active buffers from the available config
114 foreach (QVariant v, settings["Buffers"].toList()) {
115 bufferIdsFromConfig << v.value<BufferId>();
116 allBufferIds.removeAll(v.value<BufferId>());
118 Client::networkModel()->sortBufferIds(bufferIdsFromConfig);
119 _configActive->setBufferList(bufferIdsFromConfig);
121 ui.activeBuffers->setFilteredModel(Client::bufferModel(), _configActive);
123 Client::networkModel()->sortBufferIds(allBufferIds);
124 _configAvailable->setBufferList(allBufferIds);
125 ui.availableBuffers->setFilteredModel(Client::bufferModel(), _configAvailable);
127 setChangedState(false);
130 void ChatMonitorSettingsPage::loadSettings()
132 // NOTE: Whenever changing defaults here, also update ChatMonitorFilter::ChatMonitorFilter()
133 // and ChatMonitorSettingsPage::defaults() to match
134 ChatViewSettings chatViewSettings("ChatMonitor");
136 settings["OperationMode"] = (ChatViewSettings::OperationMode)chatViewSettings.value("OperationMode", ChatViewSettings::OptOut).toInt();
137 settings["ShowHighlights"] = chatViewSettings.value("ShowHighlights", false);
138 settings["ShowOwnMsgs"] = chatViewSettings.value("ShowOwnMsgs", true);
139 settings["AlwaysOwn"] = chatViewSettings.value("AlwaysOwn", false);
140 settings["Buffers"] = chatViewSettings.value("Buffers", QVariantList());
141 settings["ShowBacklog"] = chatViewSettings.value("ShowBacklog", true);
142 settings["IncludeRead"] = chatViewSettings.value("IncludeRead", false);
145 void ChatMonitorSettingsPage::save()
147 ChatViewSettings chatViewSettings("ChatMonitor");
148 // save operation mode
149 chatViewSettings.setValue("OperationMode", ui.operationMode->currentIndex() + 1);
150 chatViewSettings.setValue("ShowHighlights", ui.showHighlights->isChecked());
151 chatViewSettings.setValue("ShowOwnMsgs", ui.showOwnMessages->isChecked());
152 chatViewSettings.setValue("AlwaysOwn", ui.alwaysOwn->isChecked());
153 chatViewSettings.setValue("ShowBacklog", ui.showBacklog->isChecked());
154 chatViewSettings.setValue("IncludeRead", ui.includeRead->isChecked());
156 // save list of active buffers
157 QVariantList saveableBufferIdList;
158 foreach (BufferId id, _configActive->bufferList()) {
159 saveableBufferIdList << QVariant::fromValue(id);
162 chatViewSettings.setValue("Buffers", saveableBufferIdList);
164 setChangedState(false);
167 void ChatMonitorSettingsPage::widgetHasChanged()
169 bool changed = testHasChanged();
170 if (changed != hasChanged())
171 setChangedState(changed);
174 bool ChatMonitorSettingsPage::testHasChanged()
176 if (settings["OperationMode"].toInt() != ui.operationMode->currentIndex() + 1)
178 if (settings["ShowHighlights"].toBool() != ui.showHighlights->isChecked())
180 if (settings["ShowOwnMsgs"].toBool() != ui.showOwnMessages->isChecked())
182 if (settings["AlwaysOwn"].toBool() != ui.alwaysOwn->isChecked())
184 if (settings["ShowBacklog"].toBool() != ui.showBacklog->isChecked())
186 if (settings["IncludeRead"].toBool() != ui.includeRead->isChecked())
189 if (_configActive->bufferList().count() != settings["Buffers"].toList().count())
192 QSet<BufferId> uiBufs = _configActive->bufferList().toSet();
193 QSet<BufferId> settingsBufs;
194 foreach (QVariant v, settings["Buffers"].toList())
195 settingsBufs << v.value<BufferId>();
196 if (uiBufs != settingsBufs)
202 // TODO: - support drag 'n drop
203 // - adding of complete networks(?)
206 toggleBuffers takes each a bufferView and its config for "input" and "output".
207 Any selected item will be moved over from the input to the output bufferview.
209 void ChatMonitorSettingsPage::toggleBuffers(BufferView* inView, BufferViewConfig* inCfg, BufferView* outView, BufferViewConfig* outCfg)
211 // Fill QMap with selected items ordered by selection row
212 QMap<int, QList<BufferId>> selectedBuffers;
213 foreach (QModelIndex index, inView->selectionModel()->selectedIndexes()) {
214 BufferId inBufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
215 if (index.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
217 // If item is a network: move over all children and skip other selected items of this node
219 else if (index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType) {
220 selectedBuffers[index.parent().row()] << inBufferId;
224 // clear selection to be able to remove the bufferIds without errors
225 inView->selectionModel()->clearSelection();
228 Invalidate the BufferViewFilters' configs to get constant add/remove times
230 This can probably be removed whenever BufferViewConfig::bulkAdd or something
231 like that is available.
233 qobject_cast<BufferViewFilter*>(outView->model())->setConfig(nullptr);
234 qobject_cast<BufferViewFilter*>(inView->model())->setConfig(nullptr);
236 // actually move the ids
237 foreach (QList<BufferId> list, selectedBuffers) {
238 foreach (BufferId buffer, list) {
239 outCfg->addBuffer(buffer, 0);
240 inCfg->removeBuffer(buffer);
244 outView->setFilteredModel(Client::bufferModel(), outCfg);
245 inView->setFilteredModel(Client::bufferModel(), inCfg);
250 void ChatMonitorSettingsPage::on_activateBuffer_clicked()
252 if (ui.availableBuffers->currentIndex().isValid() && ui.availableBuffers->selectionModel()->hasSelection()) {
253 toggleBuffers(ui.availableBuffers, _configAvailable, ui.activeBuffers, _configActive);
258 void ChatMonitorSettingsPage::on_deactivateBuffer_clicked()
260 if (ui.activeBuffers->currentIndex().isValid() && ui.activeBuffers->selectionModel()->hasSelection()) {
261 toggleBuffers(ui.activeBuffers, _configActive, ui.availableBuffers, _configAvailable);
267 switchOperationMode gets called on combobox signal currentIndexChanged.
268 modeIndex is the row id in combobox itemlist
270 void ChatMonitorSettingsPage::switchOperationMode(int idx)
272 auto mode = (ChatViewSettings::OperationMode)(idx + 1);
273 if (mode == ChatViewSettings::OptIn) {
274 ui.labelActiveBuffers->setText(tr("Show:"));
276 else if (mode == ChatViewSettings::OptOut) {
277 ui.labelActiveBuffers->setText(tr("Ignore:"));