src: Yearly copyright bump
[quassel.git] / src / qtui / settingspages / chatmonitorsettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "chatmonitorsettingspage.h"
22
23 #include <QVariant>
24
25 #include "buffermodel.h"
26 #include "bufferview.h"
27 #include "bufferviewconfig.h"
28 #include "bufferviewfilter.h"
29 #include "chatviewsettings.h"
30 #include "client.h"
31 #include "icon.h"
32 #include "networkmodel.h"
33
34 ChatMonitorSettingsPage::ChatMonitorSettingsPage(QWidget* parent)
35     : SettingsPage(tr("Interface"), tr("Chat Monitor"), parent)
36 {
37     ui.setupUi(this);
38
39     ui.activateBuffer->setIcon(icon::get("go-next"));
40     ui.deactivateBuffer->setIcon(icon::get("go-previous"));
41
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();
49
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();
57
58     // fill combobox with operation modes
59     ui.operationMode->addItem(tr("Opt In"), ChatViewSettings::OptIn);
60     ui.operationMode->addItem(tr("Opt Out"), ChatViewSettings::OptOut);
61
62     // connect slots
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);
69 }
70
71 bool ChatMonitorSettingsPage::hasDefaults() const
72 {
73     return true;
74 }
75
76 void ChatMonitorSettingsPage::defaults()
77 {
78     // NOTE: Whenever changing defaults here, also update ChatMonitorFilter::ChatMonitorFilter()
79     // and ChatMonitorSettingsPage::loadSettings() to match
80
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;
89     load();
90     widgetHasChanged();
91 }
92
93 void ChatMonitorSettingsPage::load()
94 {
95     if (settings.contains("Default"))
96         settings.remove("Default");
97     else
98         loadSettings();
99
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());
107
108     // get all available buffer Ids
109     QList<BufferId> allBufferIds = Client::networkModel()->allBufferIds();
110
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>());
117         }
118         Client::networkModel()->sortBufferIds(bufferIdsFromConfig);
119         _configActive->setBufferList(bufferIdsFromConfig);
120     }
121     ui.activeBuffers->setFilteredModel(Client::bufferModel(), _configActive);
122
123     Client::networkModel()->sortBufferIds(allBufferIds);
124     _configAvailable->setBufferList(allBufferIds);
125     ui.availableBuffers->setFilteredModel(Client::bufferModel(), _configAvailable);
126
127     setChangedState(false);
128 }
129
130 void ChatMonitorSettingsPage::loadSettings()
131 {
132     // NOTE: Whenever changing defaults here, also update ChatMonitorFilter::ChatMonitorFilter()
133     // and ChatMonitorSettingsPage::defaults() to match
134     ChatViewSettings chatViewSettings("ChatMonitor");
135
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);
143 }
144
145 void ChatMonitorSettingsPage::save()
146 {
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());
155
156     // save list of active buffers
157     QVariantList saveableBufferIdList;
158     foreach (BufferId id, _configActive->bufferList()) {
159         saveableBufferIdList << QVariant::fromValue<BufferId>(id);
160     }
161
162     chatViewSettings.setValue("Buffers", saveableBufferIdList);
163     load();
164     setChangedState(false);
165 }
166
167 void ChatMonitorSettingsPage::widgetHasChanged()
168 {
169     bool changed = testHasChanged();
170     if (changed != hasChanged())
171         setChangedState(changed);
172 }
173
174 bool ChatMonitorSettingsPage::testHasChanged()
175 {
176     if (settings["OperationMode"].toInt() != ui.operationMode->currentIndex() + 1)
177         return true;
178     if (settings["ShowHighlights"].toBool() != ui.showHighlights->isChecked())
179         return true;
180     if (settings["ShowOwnMsgs"].toBool() != ui.showOwnMessages->isChecked())
181         return true;
182     if (settings["AlwaysOwn"].toBool() != ui.alwaysOwn->isChecked())
183         return true;
184     if (settings["ShowBacklog"].toBool() != ui.showBacklog->isChecked())
185         return true;
186     if (settings["IncludeRead"].toBool() != ui.includeRead->isChecked())
187         return true;
188
189     if (_configActive->bufferList().count() != settings["Buffers"].toList().count())
190         return true;
191
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)
197         return true;
198
199     return false;
200 }
201
202 // TODO: - support drag 'n drop
203 //      - adding of complete networks(?)
204
205 /*
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.
208 */
209 void ChatMonitorSettingsPage::toggleBuffers(BufferView* inView, BufferViewConfig* inCfg, BufferView* outView, BufferViewConfig* outCfg)
210 {
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) {
216             // TODO:
217             //  If item is a network: move over all children and skip other selected items of this node
218         }
219         else if (index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType) {
220             selectedBuffers[index.parent().row()] << inBufferId;
221         }
222     }
223
224     // clear selection to be able to remove the bufferIds without errors
225     inView->selectionModel()->clearSelection();
226
227     /*
228       Invalidate the BufferViewFilters' configs to get constant add/remove times
229       even for huge lists.
230       This can probably be removed whenever BufferViewConfig::bulkAdd or something
231       like that is available.
232     */
233     qobject_cast<BufferViewFilter*>(outView->model())->setConfig(nullptr);
234     qobject_cast<BufferViewFilter*>(inView->model())->setConfig(nullptr);
235
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);
241         }
242     }
243
244     outView->setFilteredModel(Client::bufferModel(), outCfg);
245     inView->setFilteredModel(Client::bufferModel(), inCfg);
246
247     widgetHasChanged();
248 }
249
250 void ChatMonitorSettingsPage::on_activateBuffer_clicked()
251 {
252     if (ui.availableBuffers->currentIndex().isValid() && ui.availableBuffers->selectionModel()->hasSelection()) {
253         toggleBuffers(ui.availableBuffers, _configAvailable, ui.activeBuffers, _configActive);
254         widgetHasChanged();
255     }
256 }
257
258 void ChatMonitorSettingsPage::on_deactivateBuffer_clicked()
259 {
260     if (ui.activeBuffers->currentIndex().isValid() && ui.activeBuffers->selectionModel()->hasSelection()) {
261         toggleBuffers(ui.activeBuffers, _configActive, ui.availableBuffers, _configAvailable);
262         widgetHasChanged();
263     }
264 }
265
266 /*
267   switchOperationMode gets called on combobox signal currentIndexChanged.
268   modeIndex is the row id in combobox itemlist
269 */
270 void ChatMonitorSettingsPage::switchOperationMode(int idx)
271 {
272     auto mode = (ChatViewSettings::OperationMode)(idx + 1);
273     if (mode == ChatViewSettings::OptIn) {
274         ui.labelActiveBuffers->setText(tr("Show:"));
275     }
276     else if (mode == ChatViewSettings::OptOut) {
277         ui.labelActiveBuffers->setText(tr("Ignore:"));
278     }
279     widgetHasChanged();
280 }