1 /***************************************************************************
2 * Copyright (C) 2005-2020 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"
35 ChatMonitorSettingsPage::ChatMonitorSettingsPage(QWidget* parent)
36 : SettingsPage(tr("Interface"), tr("Chat Monitor"), parent)
40 ui.activateBuffer->setIcon(icon::get("go-next"));
41 ui.deactivateBuffer->setIcon(icon::get("go-previous"));
43 // setup available buffers config (for the bufferview on the left)
44 _configAvailable = new BufferViewConfig(-667, this);
45 _configAvailable->setBufferViewName("tmpChatMonitorAvailableBuffers");
46 _configAvailable->setSortAlphabetically(true);
47 _configAvailable->setDisableDecoration(true);
48 _configAvailable->setNetworkId(NetworkId());
49 _configAvailable->setInitialized();
51 // setup active buffers config (for the bufferview on the right)
52 _configActive = new BufferViewConfig(-666, this);
53 _configActive->setBufferViewName("tmpChatMonitorActiveBuffers");
54 _configActive->setSortAlphabetically(true);
55 _configActive->setDisableDecoration(true);
56 _configActive->setNetworkId(NetworkId());
57 _configActive->setInitialized();
59 // fill combobox with operation modes
60 ui.operationMode->addItem(tr("Opt In"), ChatViewSettings::OptIn);
61 ui.operationMode->addItem(tr("Opt Out"), ChatViewSettings::OptOut);
64 connect(ui.operationMode, selectOverload<int>(&QComboBox::currentIndexChanged), this, &ChatMonitorSettingsPage::switchOperationMode);
65 connect(ui.showHighlights, &QAbstractButton::toggled, this, &ChatMonitorSettingsPage::widgetHasChanged);
66 connect(ui.showOwnMessages, &QAbstractButton::toggled, this, &ChatMonitorSettingsPage::widgetHasChanged);
67 connect(ui.alwaysOwn, &QAbstractButton::toggled, this, &ChatMonitorSettingsPage::widgetHasChanged);
68 connect(ui.showBacklog, &QAbstractButton::toggled, this, &ChatMonitorSettingsPage::widgetHasChanged);
69 connect(ui.includeRead, &QAbstractButton::toggled, this, &ChatMonitorSettingsPage::widgetHasChanged);
72 bool ChatMonitorSettingsPage::hasDefaults() const
77 void ChatMonitorSettingsPage::defaults()
79 // NOTE: Whenever changing defaults here, also update ChatMonitorFilter::ChatMonitorFilter()
80 // and ChatMonitorSettingsPage::loadSettings() to match
82 settings["OperationMode"] = ChatViewSettings::OptOut;
83 settings["ShowHighlights"] = false;
84 settings["ShowOwnMsgs"] = true;
85 settings["AlwaysOwn"] = false;
86 settings["Buffers"] = QVariant();
87 settings["Default"] = true;
88 settings["ShowBacklog"] = true;
89 settings["IncludeRead"] = false;
94 void ChatMonitorSettingsPage::load()
96 if (settings.contains("Default"))
97 settings.remove("Default");
101 switchOperationMode(settings["OperationMode"].toInt() - 1);
102 ui.operationMode->setCurrentIndex(settings["OperationMode"].toInt() - 1);
103 ui.showHighlights->setChecked(settings["ShowHighlights"].toBool());
104 ui.showOwnMessages->setChecked(settings["ShowOwnMsgs"].toBool());
105 ui.alwaysOwn->setChecked(settings["AlwaysOwn"].toBool());
106 ui.showBacklog->setChecked(settings["ShowBacklog"].toBool());
107 ui.includeRead->setChecked(settings["IncludeRead"].toBool());
109 // get all available buffer Ids
110 QList<BufferId> allBufferIds = Client::networkModel()->allBufferIds();
112 if (!settings["Buffers"].toList().isEmpty()) {
113 QList<BufferId> bufferIdsFromConfig;
114 // remove all active buffers from the available config
115 foreach (QVariant v, settings["Buffers"].toList()) {
116 bufferIdsFromConfig << v.value<BufferId>();
117 allBufferIds.removeAll(v.value<BufferId>());
119 Client::networkModel()->sortBufferIds(bufferIdsFromConfig);
120 _configActive->setBufferList(bufferIdsFromConfig);
122 ui.activeBuffers->setFilteredModel(Client::bufferModel(), _configActive);
124 Client::networkModel()->sortBufferIds(allBufferIds);
125 _configAvailable->setBufferList(allBufferIds);
126 ui.availableBuffers->setFilteredModel(Client::bufferModel(), _configAvailable);
128 setChangedState(false);
131 void ChatMonitorSettingsPage::loadSettings()
133 // NOTE: Whenever changing defaults here, also update ChatMonitorFilter::ChatMonitorFilter()
134 // and ChatMonitorSettingsPage::defaults() to match
135 ChatViewSettings chatViewSettings("ChatMonitor");
137 settings["OperationMode"] = (ChatViewSettings::OperationMode)chatViewSettings.value("OperationMode", ChatViewSettings::OptOut).toInt();
138 settings["ShowHighlights"] = chatViewSettings.value("ShowHighlights", false);
139 settings["ShowOwnMsgs"] = chatViewSettings.value("ShowOwnMsgs", true);
140 settings["AlwaysOwn"] = chatViewSettings.value("AlwaysOwn", false);
141 settings["Buffers"] = chatViewSettings.value("Buffers", QVariantList());
142 settings["ShowBacklog"] = chatViewSettings.value("ShowBacklog", true);
143 settings["IncludeRead"] = chatViewSettings.value("IncludeRead", false);
146 void ChatMonitorSettingsPage::save()
148 ChatViewSettings chatViewSettings("ChatMonitor");
149 // save operation mode
150 chatViewSettings.setValue("OperationMode", ui.operationMode->currentIndex() + 1);
151 chatViewSettings.setValue("ShowHighlights", ui.showHighlights->isChecked());
152 chatViewSettings.setValue("ShowOwnMsgs", ui.showOwnMessages->isChecked());
153 chatViewSettings.setValue("AlwaysOwn", ui.alwaysOwn->isChecked());
154 chatViewSettings.setValue("ShowBacklog", ui.showBacklog->isChecked());
155 chatViewSettings.setValue("IncludeRead", ui.includeRead->isChecked());
157 // save list of active buffers
158 QVariantList saveableBufferIdList;
159 foreach (BufferId id, _configActive->bufferList()) {
160 saveableBufferIdList << QVariant::fromValue(id);
163 chatViewSettings.setValue("Buffers", saveableBufferIdList);
165 setChangedState(false);
168 void ChatMonitorSettingsPage::widgetHasChanged()
170 bool changed = testHasChanged();
171 if (changed != hasChanged())
172 setChangedState(changed);
175 bool ChatMonitorSettingsPage::testHasChanged()
177 if (settings["OperationMode"].toInt() != ui.operationMode->currentIndex() + 1)
179 if (settings["ShowHighlights"].toBool() != ui.showHighlights->isChecked())
181 if (settings["ShowOwnMsgs"].toBool() != ui.showOwnMessages->isChecked())
183 if (settings["AlwaysOwn"].toBool() != ui.alwaysOwn->isChecked())
185 if (settings["ShowBacklog"].toBool() != ui.showBacklog->isChecked())
187 if (settings["IncludeRead"].toBool() != ui.includeRead->isChecked())
190 if (_configActive->bufferList().count() != settings["Buffers"].toList().count())
193 QSet<BufferId> uiBufs = toQSet(_configActive->bufferList());
194 QSet<BufferId> settingsBufs;
195 foreach (QVariant v, settings["Buffers"].toList())
196 settingsBufs << v.value<BufferId>();
197 if (uiBufs != settingsBufs)
203 // TODO: - support drag 'n drop
204 // - adding of complete networks(?)
207 toggleBuffers takes each a bufferView and its config for "input" and "output".
208 Any selected item will be moved over from the input to the output bufferview.
210 void ChatMonitorSettingsPage::toggleBuffers(BufferView* inView, BufferViewConfig* inCfg, BufferView* outView, BufferViewConfig* outCfg)
212 // Fill QMap with selected items ordered by selection row
213 QMap<int, QList<BufferId>> selectedBuffers;
214 foreach (QModelIndex index, inView->selectionModel()->selectedIndexes()) {
215 BufferId inBufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
216 if (index.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
218 // If item is a network: move over all children and skip other selected items of this node
220 else if (index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType) {
221 selectedBuffers[index.parent().row()] << inBufferId;
225 // clear selection to be able to remove the bufferIds without errors
226 inView->selectionModel()->clearSelection();
229 Invalidate the BufferViewFilters' configs to get constant add/remove times
231 This can probably be removed whenever BufferViewConfig::bulkAdd or something
232 like that is available.
234 qobject_cast<BufferViewFilter*>(outView->model())->setConfig(nullptr);
235 qobject_cast<BufferViewFilter*>(inView->model())->setConfig(nullptr);
237 // actually move the ids
238 foreach (QList<BufferId> list, selectedBuffers) {
239 foreach (BufferId buffer, list) {
240 outCfg->addBuffer(buffer, 0);
241 inCfg->removeBuffer(buffer);
245 outView->setFilteredModel(Client::bufferModel(), outCfg);
246 inView->setFilteredModel(Client::bufferModel(), inCfg);
251 void ChatMonitorSettingsPage::on_activateBuffer_clicked()
253 if (ui.availableBuffers->currentIndex().isValid() && ui.availableBuffers->selectionModel()->hasSelection()) {
254 toggleBuffers(ui.availableBuffers, _configAvailable, ui.activeBuffers, _configActive);
259 void ChatMonitorSettingsPage::on_deactivateBuffer_clicked()
261 if (ui.activeBuffers->currentIndex().isValid() && ui.activeBuffers->selectionModel()->hasSelection()) {
262 toggleBuffers(ui.activeBuffers, _configActive, ui.availableBuffers, _configAvailable);
268 switchOperationMode gets called on combobox signal currentIndexChanged.
269 modeIndex is the row id in combobox itemlist
271 void ChatMonitorSettingsPage::switchOperationMode(int idx)
273 auto mode = (ChatViewSettings::OperationMode)(idx + 1);
274 if (mode == ChatViewSettings::OptIn) {
275 ui.labelActiveBuffers->setText(tr("Show:"));
277 else if (mode == ChatViewSettings::OptOut) {
278 ui.labelActiveBuffers->setText(tr("Ignore:"));