460c872d3a77641d7ef333a057bb6cec0b2fd735
[quassel.git] / src / qtui / settingspages / chatmonitorsettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 "client.h"
24 #include "icon.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"
31
32 #include <QVariant>
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, SIGNAL(currentIndexChanged(int)), SLOT(switchOperationMode(int)));
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
72 bool ChatMonitorSettingsPage::hasDefaults() const
73 {
74     return true;
75 }
76
77
78 void ChatMonitorSettingsPage::defaults()
79 {
80     // NOTE: Whenever changing defaults here, also update ChatMonitorFilter::ChatMonitorFilter()
81     // and ChatMonitorSettingsPage::loadSettings() to match
82
83     settings["OperationMode"] = ChatViewSettings::OptOut;
84     settings["ShowHighlights"] = false;
85     settings["ShowOwnMsgs"] = true;
86     settings["AlwaysOwn"] = false;
87     settings["Buffers"] = QVariant();
88     settings["Default"] = true;
89     settings["ShowBacklog"] = true;
90     settings["IncludeRead"] = false;
91     load();
92     widgetHasChanged();
93 }
94
95
96 void ChatMonitorSettingsPage::load()
97 {
98     if (settings.contains("Default"))
99         settings.remove("Default");
100     else
101         loadSettings();
102
103     switchOperationMode(settings["OperationMode"].toInt() - 1);
104     ui.operationMode->setCurrentIndex(settings["OperationMode"].toInt() - 1);
105     ui.showHighlights->setChecked(settings["ShowHighlights"].toBool());
106     ui.showOwnMessages->setChecked(settings["ShowOwnMsgs"].toBool());
107     ui.alwaysOwn->setChecked(settings["AlwaysOwn"].toBool());
108     ui.showBacklog->setChecked(settings["ShowBacklog"].toBool());
109     ui.includeRead->setChecked(settings["IncludeRead"].toBool());
110
111     // get all available buffer Ids
112     QList<BufferId> allBufferIds = Client::networkModel()->allBufferIds();
113
114     if (!settings["Buffers"].toList().isEmpty()) {
115         QList<BufferId> bufferIdsFromConfig;
116         // remove all active buffers from the available config
117         foreach(QVariant v, settings["Buffers"].toList()) {
118             bufferIdsFromConfig << v.value<BufferId>();
119             allBufferIds.removeAll(v.value<BufferId>());
120         }
121         Client::networkModel()->sortBufferIds(bufferIdsFromConfig);
122         _configActive->initSetBufferList(bufferIdsFromConfig);
123     }
124     ui.activeBuffers->setFilteredModel(Client::bufferModel(), _configActive);
125
126     Client::networkModel()->sortBufferIds(allBufferIds);
127     _configAvailable->initSetBufferList(allBufferIds);
128     ui.availableBuffers->setFilteredModel(Client::bufferModel(), _configAvailable);
129
130     setChangedState(false);
131 }
132
133
134 void ChatMonitorSettingsPage::loadSettings()
135 {
136     // NOTE: Whenever changing defaults here, also update ChatMonitorFilter::ChatMonitorFilter()
137     // and ChatMonitorSettingsPage::defaults() to match
138     ChatViewSettings chatViewSettings("ChatMonitor");
139
140     settings["OperationMode"] = (ChatViewSettings::OperationMode)
141             chatViewSettings.value("OperationMode", ChatViewSettings::OptOut).toInt();
142     settings["ShowHighlights"] = chatViewSettings.value("ShowHighlights", false);
143     settings["ShowOwnMsgs"] = chatViewSettings.value("ShowOwnMsgs", true);
144     settings["AlwaysOwn"] = chatViewSettings.value("AlwaysOwn", false);
145     settings["Buffers"] = chatViewSettings.value("Buffers", QVariantList());
146     settings["ShowBacklog"] = chatViewSettings.value("ShowBacklog", true);
147     settings["IncludeRead"] = chatViewSettings.value("IncludeRead", false);
148 }
149
150
151 void ChatMonitorSettingsPage::save()
152 {
153     ChatViewSettings chatViewSettings("ChatMonitor");
154     // save operation mode
155     chatViewSettings.setValue("OperationMode", ui.operationMode->currentIndex() + 1);
156     chatViewSettings.setValue("ShowHighlights", ui.showHighlights->isChecked());
157     chatViewSettings.setValue("ShowOwnMsgs", ui.showOwnMessages->isChecked());
158     chatViewSettings.setValue("AlwaysOwn", ui.alwaysOwn->isChecked());
159     chatViewSettings.setValue("ShowBacklog", ui.showBacklog->isChecked());
160     chatViewSettings.setValue("IncludeRead", ui.includeRead->isChecked());
161
162     // save list of active buffers
163     QVariantList saveableBufferIdList;
164     foreach(BufferId id, _configActive->bufferList()) {
165         saveableBufferIdList << QVariant::fromValue<BufferId>(id);
166     }
167
168     chatViewSettings.setValue("Buffers", saveableBufferIdList);
169     load();
170     setChangedState(false);
171 }
172
173
174 void ChatMonitorSettingsPage::widgetHasChanged()
175 {
176     bool changed = testHasChanged();
177     if (changed != hasChanged()) setChangedState(changed);
178 }
179
180
181 bool ChatMonitorSettingsPage::testHasChanged()
182 {
183     if (settings["OperationMode"].toInt() != ui.operationMode->currentIndex() + 1)
184         return true;
185     if (settings["ShowHighlights"].toBool() != ui.showHighlights->isChecked())
186         return true;
187     if (settings["ShowOwnMsgs"].toBool() != ui.showOwnMessages->isChecked())
188         return true;
189     if (settings["AlwaysOwn"].toBool() != ui.alwaysOwn->isChecked())
190         return true;
191     if (settings["ShowBacklog"].toBool() != ui.showBacklog->isChecked())
192         return true;
193     if (settings["IncludeRead"].toBool() != ui.includeRead->isChecked())
194         return true;
195
196     if (_configActive->bufferList().count() != settings["Buffers"].toList().count())
197         return true;
198
199     QSet<BufferId> uiBufs = _configActive->bufferList().toSet();
200     QSet<BufferId> settingsBufs;
201     foreach(QVariant v, settings["Buffers"].toList())
202     settingsBufs << v.value<BufferId>();
203     if (uiBufs != settingsBufs)
204         return true;
205
206     return false;
207 }
208
209
210 //TODO: - support drag 'n drop
211 //      - adding of complete networks(?)
212
213 /*
214   toggleBuffers takes each a bufferView and its config for "input" and "output".
215   Any selected item will be moved over from the input to the output bufferview.
216 */
217 void ChatMonitorSettingsPage::toggleBuffers(BufferView *inView, BufferViewConfig *inCfg, BufferView *outView, BufferViewConfig *outCfg)
218 {
219     // Fill QMap with selected items ordered by selection row
220     QMap<int, QList<BufferId> > selectedBuffers;
221     foreach(QModelIndex index, inView->selectionModel()->selectedIndexes()) {
222         BufferId inBufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
223         if (index.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
224             // TODO:
225             //  If item is a network: move over all children and skip other selected items of this node
226         }
227         else if (index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType) {
228             selectedBuffers[index.parent().row()] << inBufferId;
229         }
230     }
231
232     // clear selection to be able to remove the bufferIds without errors
233     inView->selectionModel()->clearSelection();
234
235     /*
236       Invalidate the BufferViewFilters' configs to get constant add/remove times
237       even for huge lists.
238       This can probably be removed whenever BufferViewConfig::bulkAdd or something
239       like that is available.
240     */
241     qobject_cast<BufferViewFilter *>(outView->model())->setConfig(nullptr);
242     qobject_cast<BufferViewFilter *>(inView->model())->setConfig(nullptr);
243
244     // actually move the ids
245     foreach(QList<BufferId> list, selectedBuffers) {
246         foreach(BufferId buffer, list) {
247             outCfg->addBuffer(buffer, 0);
248             inCfg->removeBuffer(buffer);
249         }
250     }
251
252     outView->setFilteredModel(Client::bufferModel(), outCfg);
253     inView->setFilteredModel(Client::bufferModel(), inCfg);
254
255     widgetHasChanged();
256 }
257
258
259 void ChatMonitorSettingsPage::on_activateBuffer_clicked()
260 {
261     if (ui.availableBuffers->currentIndex().isValid() && ui.availableBuffers->selectionModel()->hasSelection()) {
262         toggleBuffers(ui.availableBuffers, _configAvailable, ui.activeBuffers, _configActive);
263         widgetHasChanged();
264     }
265 }
266
267
268 void ChatMonitorSettingsPage::on_deactivateBuffer_clicked()
269 {
270     if (ui.activeBuffers->currentIndex().isValid() && ui.activeBuffers->selectionModel()->hasSelection()) {
271         toggleBuffers(ui.activeBuffers, _configActive, ui.availableBuffers, _configAvailable);
272         widgetHasChanged();
273     }
274 }
275
276
277 /*
278   switchOperationMode gets called on combobox signal currentIndexChanged.
279   modeIndex is the row id in combobox itemlist
280 */
281 void ChatMonitorSettingsPage::switchOperationMode(int idx)
282 {
283     auto mode = (ChatViewSettings::OperationMode)(idx + 1);
284     if (mode == ChatViewSettings::OptIn) {
285         ui.labelActiveBuffers->setText(tr("Show:"));
286     }
287     else if (mode == ChatViewSettings::OptOut) {
288         ui.labelActiveBuffers->setText(tr("Ignore:"));
289     }
290     widgetHasChanged();
291 }