faba1bdade9f6e4f0908a7b176901d8ed3abbae3
[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, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
65     connect(ui.showOwnMessages, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
66     connect(ui.alwaysOwn, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
67     connect(ui.showBacklog, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
68     connect(ui.includeRead, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
69 }
70
71
72 bool ChatMonitorSettingsPage::hasDefaults() const
73 {
74     return true;
75 }
76
77
78 void ChatMonitorSettingsPage::defaults()
79 {
80     settings["OperationMode"] = ChatViewSettings::OptOut;
81     settings["ShowHighlights"] = false;
82     settings["ShowOwnMsgs"] = false;
83     settings["AlwaysOwn"] = false;
84     settings["Buffers"] = QVariant();
85     settings["Default"] = true;
86     settings["ShowBacklog"] = true;
87     settings["IncludeRead"] = false;
88     load();
89     widgetHasChanged();
90 }
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->initSetBufferList(bufferIdsFromConfig);
120     }
121     ui.activeBuffers->setFilteredModel(Client::bufferModel(), _configActive);
122
123     Client::networkModel()->sortBufferIds(allBufferIds);
124     _configAvailable->initSetBufferList(allBufferIds);
125     ui.availableBuffers->setFilteredModel(Client::bufferModel(), _configAvailable);
126
127     setChangedState(false);
128 }
129
130
131 void ChatMonitorSettingsPage::loadSettings()
132 {
133     ChatViewSettings chatViewSettings("ChatMonitor");
134     settings["OperationMode"] = (ChatViewSettings::OperationMode)chatViewSettings.value("OperationMode", ChatViewSettings::OptOut).toInt();
135
136     settings["ShowHighlights"] = chatViewSettings.value("ShowHighlights", false);
137     settings["ShowOwnMsgs"] = chatViewSettings.value("ShowOwnMsgs", false);
138     settings["AlwaysOwn"] = chatViewSettings.value("AlwaysOwn", false);
139     settings["Buffers"] = chatViewSettings.value("Buffers", QVariantList());
140     settings["ShowBacklog"] = chatViewSettings.value("ShowBacklog", true);
141     settings["IncludeRead"] = chatViewSettings.value("IncludeRead", true);
142 }
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
168 void ChatMonitorSettingsPage::widgetHasChanged()
169 {
170     bool changed = testHasChanged();
171     if (changed != hasChanged()) setChangedState(changed);
172 }
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 = _configActive->bufferList().toSet();
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
204 //TODO: - support drag 'n drop
205 //      - adding of complete networks(?)
206
207 /*
208   toggleBuffers takes each a bufferView and its config for "input" and "output".
209   Any selected item will be moved over from the input to the output bufferview.
210 */
211 void ChatMonitorSettingsPage::toggleBuffers(BufferView *inView, BufferViewConfig *inCfg, BufferView *outView, BufferViewConfig *outCfg)
212 {
213     // Fill QMap with selected items ordered by selection row
214     QMap<int, QList<BufferId> > selectedBuffers;
215     foreach(QModelIndex index, inView->selectionModel()->selectedIndexes()) {
216         BufferId inBufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
217         if (index.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
218             // TODO:
219             //  If item is a network: move over all children and skip other selected items of this node
220         }
221         else if (index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType) {
222             selectedBuffers[index.parent().row()] << inBufferId;
223         }
224     }
225
226     // clear selection to be able to remove the bufferIds without errors
227     inView->selectionModel()->clearSelection();
228
229     /*
230       Invalidate the BufferViewFilters' configs to get constant add/remove times
231       even for huge lists.
232       This can probably be removed whenever BufferViewConfig::bulkAdd or something
233       like that is available.
234     */
235     qobject_cast<BufferViewFilter *>(outView->model())->setConfig(0);
236     qobject_cast<BufferViewFilter *>(inView->model())->setConfig(0);
237
238     // actually move the ids
239     foreach(QList<BufferId> list, selectedBuffers) {
240         foreach(BufferId buffer, list) {
241             outCfg->addBuffer(buffer, 0);
242             inCfg->removeBuffer(buffer);
243         }
244     }
245
246     outView->setFilteredModel(Client::bufferModel(), outCfg);
247     inView->setFilteredModel(Client::bufferModel(), inCfg);
248
249     widgetHasChanged();
250 }
251
252
253 void ChatMonitorSettingsPage::on_activateBuffer_clicked()
254 {
255     if (ui.availableBuffers->currentIndex().isValid() && ui.availableBuffers->selectionModel()->hasSelection()) {
256         toggleBuffers(ui.availableBuffers, _configAvailable, ui.activeBuffers, _configActive);
257         widgetHasChanged();
258     }
259 }
260
261
262 void ChatMonitorSettingsPage::on_deactivateBuffer_clicked()
263 {
264     if (ui.activeBuffers->currentIndex().isValid() && ui.activeBuffers->selectionModel()->hasSelection()) {
265         toggleBuffers(ui.activeBuffers, _configActive, ui.availableBuffers, _configAvailable);
266         widgetHasChanged();
267     }
268 }
269
270
271 /*
272   switchOperationMode gets called on combobox signal currentIndexChanged.
273   modeIndex is the row id in combobox itemlist
274 */
275 void ChatMonitorSettingsPage::switchOperationMode(int idx)
276 {
277     ChatViewSettings::OperationMode mode = (ChatViewSettings::OperationMode)(idx + 1);
278     if (mode == ChatViewSettings::OptIn) {
279         ui.labelActiveBuffers->setText(tr("Show:"));
280     }
281     else if (mode == ChatViewSettings::OptOut) {
282         ui.labelActiveBuffers->setText(tr("Ignore:"));
283     }
284     widgetHasChanged();
285 }