1 /***************************************************************************
2 * Copyright (C) 2005-2015 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"
26 #include "networkmodel.h"
27 #include "bufferviewconfig.h"
28 #include "buffermodel.h"
29 #include "bufferview.h"
30 #include "bufferviewfilter.h"
31 #include "chatviewsettings.h"
35 ChatMonitorSettingsPage::ChatMonitorSettingsPage(QWidget *parent)
36 : SettingsPage(tr("Interface"), tr("Chat Monitor"), parent)
40 ui.activateBuffer->setIcon(QIcon::fromTheme("go-next"));
41 ui.deactivateBuffer->setIcon(QIcon::fromTheme("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, SIGNAL(currentIndexChanged(int)), SLOT(switchOperationMode(int)));
65 connect(ui.showHighlights, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
66 connect(ui.showOwnMessages, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
67 connect(ui.showBacklog, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
68 connect(ui.includeRead, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
72 bool ChatMonitorSettingsPage::hasDefaults() const
78 void ChatMonitorSettingsPage::defaults()
80 settings["OperationMode"] = ChatViewSettings::OptOut;
81 settings["ShowHighlights"] = false;
82 settings["ShowOwnMsgs"] = false;
83 settings["Buffers"] = QVariant();
84 settings["Default"] = true;
85 settings["ShowBacklog"] = true;
86 settings["IncludeRead"] = false;
92 void ChatMonitorSettingsPage::load()
94 if (settings.contains("Default"))
95 settings.remove("Default");
99 switchOperationMode(settings["OperationMode"].toInt() - 1);
100 ui.operationMode->setCurrentIndex(settings["OperationMode"].toInt() - 1);
101 ui.showHighlights->setChecked(settings["ShowHighlights"].toBool());
102 ui.showOwnMessages->setChecked(settings["ShowOwnMsgs"].toBool());
103 ui.showBacklog->setChecked(settings["ShowBacklog"].toBool());
104 ui.includeRead->setChecked(settings["IncludeRead"].toBool());
106 // get all available buffer Ids
107 QList<BufferId> allBufferIds = Client::networkModel()->allBufferIds();
109 if (!settings["Buffers"].toList().isEmpty()) {
110 QList<BufferId> bufferIdsFromConfig;
111 // remove all active buffers from the available config
112 foreach(QVariant v, settings["Buffers"].toList()) {
113 bufferIdsFromConfig << v.value<BufferId>();
114 allBufferIds.removeAll(v.value<BufferId>());
116 Client::networkModel()->sortBufferIds(bufferIdsFromConfig);
117 _configActive->initSetBufferList(bufferIdsFromConfig);
119 ui.activeBuffers->setFilteredModel(Client::bufferModel(), _configActive);
121 Client::networkModel()->sortBufferIds(allBufferIds);
122 _configAvailable->initSetBufferList(allBufferIds);
123 ui.availableBuffers->setFilteredModel(Client::bufferModel(), _configAvailable);
125 setChangedState(false);
129 void ChatMonitorSettingsPage::loadSettings()
131 ChatViewSettings chatViewSettings("ChatMonitor");
132 settings["OperationMode"] = (ChatViewSettings::OperationMode)chatViewSettings.value("OperationMode", ChatViewSettings::OptOut).toInt();
134 settings["ShowHighlights"] = chatViewSettings.value("ShowHighlights", false);
135 settings["ShowOwnMsgs"] = chatViewSettings.value("ShowOwnMsgs", false);
136 settings["Buffers"] = chatViewSettings.value("Buffers", QVariantList());
137 settings["ShowBacklog"] = chatViewSettings.value("ShowBacklog", true);
138 settings["IncludeRead"] = chatViewSettings.value("IncludeRead", true);
142 void ChatMonitorSettingsPage::save()
144 ChatViewSettings chatViewSettings("ChatMonitor");
145 // save operation mode
146 chatViewSettings.setValue("OperationMode", ui.operationMode->currentIndex() + 1);
147 chatViewSettings.setValue("ShowHighlights", ui.showHighlights->isChecked());
148 chatViewSettings.setValue("ShowOwnMsgs", ui.showOwnMessages->isChecked());
149 chatViewSettings.setValue("ShowBacklog", ui.showBacklog->isChecked());
150 chatViewSettings.setValue("IncludeRead", ui.includeRead->isChecked());
152 // save list of active buffers
153 QVariantList saveableBufferIdList;
154 foreach(BufferId id, _configActive->bufferList()) {
155 saveableBufferIdList << QVariant::fromValue<BufferId>(id);
158 chatViewSettings.setValue("Buffers", saveableBufferIdList);
160 setChangedState(false);
164 void ChatMonitorSettingsPage::widgetHasChanged()
166 bool changed = testHasChanged();
167 if (changed != hasChanged()) setChangedState(changed);
171 bool ChatMonitorSettingsPage::testHasChanged()
173 if (settings["OperationMode"].toInt() != ui.operationMode->currentIndex() + 1)
175 if (settings["ShowHighlights"].toBool() != ui.showHighlights->isChecked())
177 if (settings["ShowOwnMsgs"].toBool() != ui.showOwnMessages->isChecked())
179 if (settings["ShowBacklog"].toBool() != ui.showBacklog->isChecked())
181 if (settings["IncludeRead"].toBool() != ui.includeRead->isChecked())
184 if (_configActive->bufferList().count() != settings["Buffers"].toList().count())
187 QSet<BufferId> uiBufs = _configActive->bufferList().toSet();
188 QSet<BufferId> settingsBufs;
189 foreach(QVariant v, settings["Buffers"].toList())
190 settingsBufs << v.value<BufferId>();
191 if (uiBufs != settingsBufs)
198 //TODO: - support drag 'n drop
199 // - adding of complete networks(?)
202 toggleBuffers takes each a bufferView and its config for "input" and "output".
203 Any selected item will be moved over from the input to the output bufferview.
205 void ChatMonitorSettingsPage::toggleBuffers(BufferView *inView, BufferViewConfig *inCfg, BufferView *outView, BufferViewConfig *outCfg)
207 // Fill QMap with selected items ordered by selection row
208 QMap<int, QList<BufferId> > selectedBuffers;
209 foreach(QModelIndex index, inView->selectionModel()->selectedIndexes()) {
210 BufferId inBufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
211 if (index.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
213 // If item is a network: move over all children and skip other selected items of this node
215 else if (index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType) {
216 selectedBuffers[index.parent().row()] << inBufferId;
220 // clear selection to be able to remove the bufferIds without errors
221 inView->selectionModel()->clearSelection();
224 Invalidate the BufferViewFilters' configs to get constant add/remove times
226 This can probably be removed whenever BufferViewConfig::bulkAdd or something
227 like that is available.
229 qobject_cast<BufferViewFilter *>(outView->model())->setConfig(0);
230 qobject_cast<BufferViewFilter *>(inView->model())->setConfig(0);
232 // actually move the ids
233 foreach(QList<BufferId> list, selectedBuffers) {
234 foreach(BufferId buffer, list) {
235 outCfg->addBuffer(buffer, 0);
236 inCfg->removeBuffer(buffer);
240 outView->setFilteredModel(Client::bufferModel(), outCfg);
241 inView->setFilteredModel(Client::bufferModel(), inCfg);
247 void ChatMonitorSettingsPage::on_activateBuffer_clicked()
249 if (ui.availableBuffers->currentIndex().isValid() && ui.availableBuffers->selectionModel()->hasSelection()) {
250 toggleBuffers(ui.availableBuffers, _configAvailable, ui.activeBuffers, _configActive);
256 void ChatMonitorSettingsPage::on_deactivateBuffer_clicked()
258 if (ui.activeBuffers->currentIndex().isValid() && ui.activeBuffers->selectionModel()->hasSelection()) {
259 toggleBuffers(ui.activeBuffers, _configActive, ui.availableBuffers, _configAvailable);
266 switchOperationMode gets called on combobox signal currentIndexChanged.
267 modeIndex is the row id in combobox itemlist
269 void ChatMonitorSettingsPage::switchOperationMode(int idx)
271 ChatViewSettings::OperationMode mode = (ChatViewSettings::OperationMode)(idx + 1);
272 if (mode == ChatViewSettings::OptIn) {
273 ui.labelActiveBuffers->setText(tr("Show:"));
275 else if (mode == ChatViewSettings::OptOut) {
276 ui.labelActiveBuffers->setText(tr("Ignore:"));