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