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 "chatmonitorsettingspage.h"
25 #include "networkmodel.h"
26 #include "bufferviewconfig.h"
27 #include "buffermodel.h"
28 #include "bufferview.h"
29 #include "bufferviewfilter.h"
30 #include "chatviewsettings.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, SIGNAL(currentIndexChanged(int)), SLOT(switchOperationMode(int)));
64 connect(ui.showHighlights, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
65 connect(ui.showOwnMessages, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
66 connect(ui.showBacklog, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
67 connect(ui.includeRead, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
71 bool ChatMonitorSettingsPage::hasDefaults() const
77 void ChatMonitorSettingsPage::defaults()
79 settings["OperationMode"] = ChatViewSettings::OptOut;
80 settings["ShowHighlights"] = false;
81 settings["ShowOwnMsgs"] = false;
82 settings["Buffers"] = QVariant();
83 settings["Default"] = true;
84 settings["ShowBacklog"] = true;
85 settings["IncludeRead"] = false;
91 void ChatMonitorSettingsPage::load()
93 if (settings.contains("Default"))
94 settings.remove("Default");
98 switchOperationMode(settings["OperationMode"].toInt() - 1);
99 ui.operationMode->setCurrentIndex(settings["OperationMode"].toInt() - 1);
100 ui.showHighlights->setChecked(settings["ShowHighlights"].toBool());
101 ui.showOwnMessages->setChecked(settings["ShowOwnMsgs"].toBool());
102 ui.showBacklog->setChecked(settings["ShowBacklog"].toBool());
103 ui.includeRead->setChecked(settings["IncludeRead"].toBool());
105 // get all available buffer Ids
106 QList<BufferId> allBufferIds = Client::networkModel()->allBufferIds();
108 if (!settings["Buffers"].toList().isEmpty()) {
109 QList<BufferId> bufferIdsFromConfig;
110 // remove all active buffers from the available config
111 foreach(QVariant v, settings["Buffers"].toList()) {
112 bufferIdsFromConfig << v.value<BufferId>();
113 allBufferIds.removeAll(v.value<BufferId>());
115 Client::networkModel()->sortBufferIds(bufferIdsFromConfig);
116 _configActive->initSetBufferList(bufferIdsFromConfig);
118 ui.activeBuffers->setFilteredModel(Client::bufferModel(), _configActive);
120 Client::networkModel()->sortBufferIds(allBufferIds);
121 _configAvailable->initSetBufferList(allBufferIds);
122 ui.availableBuffers->setFilteredModel(Client::bufferModel(), _configAvailable);
124 setChangedState(false);
128 void ChatMonitorSettingsPage::loadSettings()
130 ChatViewSettings chatViewSettings("ChatMonitor");
131 settings["OperationMode"] = (ChatViewSettings::OperationMode)chatViewSettings.value("OperationMode", ChatViewSettings::OptOut).toInt();
133 settings["ShowHighlights"] = chatViewSettings.value("ShowHighlights", false);
134 settings["ShowOwnMsgs"] = chatViewSettings.value("ShowOwnMsgs", false);
135 settings["Buffers"] = chatViewSettings.value("Buffers", QVariantList());
136 settings["ShowBacklog"] = chatViewSettings.value("ShowBacklog", true);
137 settings["IncludeRead"] = chatViewSettings.value("IncludeRead", true);
141 void ChatMonitorSettingsPage::save()
143 ChatViewSettings chatViewSettings("ChatMonitor");
144 // save operation mode
145 chatViewSettings.setValue("OperationMode", ui.operationMode->currentIndex() + 1);
146 chatViewSettings.setValue("ShowHighlights", ui.showHighlights->isChecked());
147 chatViewSettings.setValue("ShowOwnMsgs", ui.showOwnMessages->isChecked());
148 chatViewSettings.setValue("ShowBacklog", ui.showBacklog->isChecked());
149 chatViewSettings.setValue("IncludeRead", ui.includeRead->isChecked());
151 // save list of active buffers
152 QVariantList saveableBufferIdList;
153 foreach(BufferId id, _configActive->bufferList()) {
154 saveableBufferIdList << QVariant::fromValue<BufferId>(id);
157 chatViewSettings.setValue("Buffers", saveableBufferIdList);
159 setChangedState(false);
163 void ChatMonitorSettingsPage::widgetHasChanged()
165 bool changed = testHasChanged();
166 if (changed != hasChanged()) setChangedState(changed);
170 bool ChatMonitorSettingsPage::testHasChanged()
172 if (settings["OperationMode"].toInt() != ui.operationMode->currentIndex() + 1)
174 if (settings["ShowHighlights"].toBool() != ui.showHighlights->isChecked())
176 if (settings["ShowOwnMsgs"].toBool() != ui.showOwnMessages->isChecked())
178 if (settings["ShowBacklog"].toBool() != ui.showBacklog->isChecked())
180 if (settings["IncludeRead"].toBool() != ui.includeRead->isChecked())
183 if (_configActive->bufferList().count() != settings["Buffers"].toList().count())
186 QSet<BufferId> uiBufs = _configActive->bufferList().toSet();
187 QSet<BufferId> settingsBufs;
188 foreach(QVariant v, settings["Buffers"].toList())
189 settingsBufs << v.value<BufferId>();
190 if (uiBufs != settingsBufs)
197 //TODO: - support drag 'n drop
198 // - adding of complete networks(?)
201 toggleBuffers takes each a bufferView and its config for "input" and "output".
202 Any selected item will be moved over from the input to the output bufferview.
204 void ChatMonitorSettingsPage::toggleBuffers(BufferView *inView, BufferViewConfig *inCfg, BufferView *outView, BufferViewConfig *outCfg)
206 // Fill QMap with selected items ordered by selection row
207 QMap<int, QList<BufferId> > selectedBuffers;
208 foreach(QModelIndex index, inView->selectionModel()->selectedIndexes()) {
209 BufferId inBufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
210 if (index.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
212 // If item is a network: move over all children and skip other selected items of this node
214 else if (index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType) {
215 selectedBuffers[index.parent().row()] << inBufferId;
219 // clear selection to be able to remove the bufferIds without errors
220 inView->selectionModel()->clearSelection();
223 Invalidate the BufferViewFilters' configs to get constant add/remove times
225 This can probably be removed whenever BufferViewConfig::bulkAdd or something
226 like that is available.
228 qobject_cast<BufferViewFilter *>(outView->model())->setConfig(0);
229 qobject_cast<BufferViewFilter *>(inView->model())->setConfig(0);
231 // actually move the ids
232 foreach(QList<BufferId> list, selectedBuffers) {
233 foreach(BufferId buffer, list) {
234 outCfg->addBuffer(buffer, 0);
235 inCfg->removeBuffer(buffer);
239 outView->setFilteredModel(Client::bufferModel(), outCfg);
240 inView->setFilteredModel(Client::bufferModel(), inCfg);
246 void ChatMonitorSettingsPage::on_activateBuffer_clicked()
248 if (ui.availableBuffers->currentIndex().isValid() && ui.availableBuffers->selectionModel()->hasSelection()) {
249 toggleBuffers(ui.availableBuffers, _configAvailable, ui.activeBuffers, _configActive);
255 void ChatMonitorSettingsPage::on_deactivateBuffer_clicked()
257 if (ui.activeBuffers->currentIndex().isValid() && ui.activeBuffers->selectionModel()->hasSelection()) {
258 toggleBuffers(ui.activeBuffers, _configActive, ui.availableBuffers, _configAvailable);
265 switchOperationMode gets called on combobox signal currentIndexChanged.
266 modeIndex is the row id in combobox itemlist
268 void ChatMonitorSettingsPage::switchOperationMode(int idx)
270 ChatViewSettings::OperationMode mode = (ChatViewSettings::OperationMode)(idx + 1);
271 if (mode == ChatViewSettings::OptIn) {
272 ui.labelActiveBuffers->setText(tr("Show:"));
274 else if (mode == ChatViewSettings::OptOut) {
275 ui.labelActiveBuffers->setText(tr("Ignore:"));