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