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