Yearly copyright bump :)
[quassel.git] / src / qtui / settingspages / chatmonitorsettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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 Blank Public License as published by    *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
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 Blank Public License for more details.                            *
14  *                                                                         *
15  *   You should have received a copy of the GNU Blank Public License       *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, 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("General"), tr("Chat Monitor"), parent) {
36   ui.setupUi(this);
37
38   ui.activateBuffer->setIcon(SmallIcon("go-next"));
39   ui.deactivateBuffer->setIcon(SmallIcon("go-previous"));
40
41   // setup available buffers config (for the bufferview on the left)
42   _configAvailable = new BufferViewConfig(-667, this);
43   _configAvailable->setBufferViewName("tmpChatMonitorAvailableBuffers");
44   _configAvailable->setSortAlphabetically(true);
45   _configAvailable->setDisableDecoration(true);
46   _configAvailable->setNetworkId(NetworkId());
47   _configAvailable->setInitialized();
48
49   // setup active buffers config (for the bufferview on the right)
50   _configActive = new BufferViewConfig(-666, this);
51   _configActive->setBufferViewName("tmpChatMonitorActiveBuffers");
52   _configActive->setSortAlphabetically(true);
53   _configActive->setDisableDecoration(true);
54   _configActive->setNetworkId(NetworkId());
55   _configActive->setInitialized();
56
57   // fill combobox with operation modes
58   ui.operationMode->addItem(tr("Opt In"), ChatViewSettings::OptIn);
59   ui.operationMode->addItem(tr("Opt Out"), ChatViewSettings::OptOut);
60
61   // connect slots
62   connect(ui.operationMode, SIGNAL(currentIndexChanged(int)), SLOT(switchOperationMode(int)));
63   connect(ui.showHighlights, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
64   connect(ui.showOwnMessages, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
65 }
66
67 bool ChatMonitorSettingsPage::hasDefaults() const {
68   return true;
69 }
70
71 void ChatMonitorSettingsPage::defaults() {
72   settings["OperationMode"] = ChatViewSettings::OptOut;
73   settings["ShowHighlights"] = false;
74   settings["ShowOwnMsgs"] = false;
75   settings["Buffers"] = QVariant();
76   settings["Default"] = true;
77   load();
78   widgetHasChanged();
79 }
80
81 void ChatMonitorSettingsPage::load() {
82   if(settings.contains("Default"))
83     settings.remove("Default");
84   else
85     loadSettings();
86
87   switchOperationMode(settings["OperationMode"].toInt() - 1);
88   ui.operationMode->setCurrentIndex(settings["OperationMode"].toInt() - 1);
89   ui.showHighlights->setChecked(settings["ShowHighlights"].toBool());
90   ui.showOwnMessages->setChecked(settings["ShowOwnMsgs"].toBool());
91
92   // get all available buffer Ids
93   QList<BufferId> allBufferIds = Client::networkModel()->allBufferIds();
94
95   if(!settings["Buffers"].toList().isEmpty()) {
96     QList<BufferId> bufferIdsFromConfig;
97     // remove all active buffers from the available config
98     foreach(QVariant v, settings["Buffers"].toList()) {
99       bufferIdsFromConfig << v.value<BufferId>();
100       allBufferIds.removeAll(v.value<BufferId>());
101     }
102     Client::networkModel()->sortBufferIds(bufferIdsFromConfig);
103     _configActive->initSetBufferList(bufferIdsFromConfig);
104   }
105   ui.activeBuffers->setFilteredModel(Client::bufferModel(), _configActive);
106
107   Client::networkModel()->sortBufferIds(allBufferIds);
108   _configAvailable->initSetBufferList(allBufferIds);
109   ui.availableBuffers->setFilteredModel(Client::bufferModel(), _configAvailable);
110
111   setChangedState(false);
112 }
113
114 void ChatMonitorSettingsPage::loadSettings() {
115   ChatViewSettings chatViewSettings("ChatMonitor");
116   settings["OperationMode"] = (ChatViewSettings::OperationMode)chatViewSettings.value("OperationMode", ChatViewSettings::OptOut).toInt();
117
118   settings["ShowHighlights"] = chatViewSettings.value("ShowHighlights", false);
119   settings["ShowOwnMsgs"] = chatViewSettings.value("ShowOwnMsgs", false);
120   settings["Buffers"] = chatViewSettings.value("Buffers", QVariantList());
121 }
122
123 void ChatMonitorSettingsPage::save() {
124   ChatViewSettings chatViewSettings("ChatMonitor");
125   // save operation mode
126   chatViewSettings.setValue("OperationMode", ui.operationMode->currentIndex() + 1);
127   chatViewSettings.setValue("ShowHighlights", ui.showHighlights->isChecked());
128   chatViewSettings.setValue("ShowOwnMsgs", ui.showOwnMessages->isChecked());
129
130   // save list of active buffers
131   QVariantList saveableBufferIdList;
132   foreach(BufferId id, _configActive->bufferList()) {
133     saveableBufferIdList << QVariant::fromValue<BufferId>(id);
134   }
135
136   chatViewSettings.setValue("Buffers", saveableBufferIdList);
137   load();
138   setChangedState(false);
139 }
140
141 void ChatMonitorSettingsPage::widgetHasChanged() {
142   bool changed = testHasChanged();
143   if(changed != hasChanged()) setChangedState(changed);
144 }
145
146 bool ChatMonitorSettingsPage::testHasChanged() {
147   if(settings["OperationMode"].toInt() != ui.operationMode->currentIndex() + 1)
148     return true;
149   if(settings["ShowHighlights"].toBool() != ui.showHighlights->isChecked())
150     return true;
151   if(settings["ShowOwnMsgs"].toBool() != ui.showOwnMessages->isChecked())
152     return true;
153
154   if(_configActive->bufferList().count() != settings["Buffers"].toList().count())
155     return true;
156
157   QSet<BufferId> uiBufs = _configActive->bufferList().toSet();
158   QSet<BufferId> settingsBufs;
159   foreach(QVariant v, settings["Buffers"].toList())
160     settingsBufs << v.value<BufferId>();
161   if(uiBufs != settingsBufs)
162     return true;
163
164   return false;
165 }
166
167 //TODO: - support drag 'n drop
168 //      - adding of complete networks(?)
169
170 /*
171   toggleBuffers takes each a bufferView and its config for "input" and "output".
172   Any selected item will be moved over from the input to the output bufferview.
173 */
174 void ChatMonitorSettingsPage::toggleBuffers(BufferView *inView, BufferViewConfig *inCfg, BufferView *outView, BufferViewConfig *outCfg) {
175
176   // Fill QMap with selected items ordered by selection row
177   QMap<int, QList<BufferId> > selectedBuffers;
178   foreach(QModelIndex index, inView->selectionModel()->selectedIndexes()) {
179     BufferId inBufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
180     if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
181       // TODO:
182       //  If item is a network: move over all children and skip other selected items of this node
183     }
184     else if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType) {
185       selectedBuffers[index.parent().row()] << inBufferId;
186     }
187   }
188
189   // clear selection to be able to remove the bufferIds without errors
190   inView->selectionModel()->clearSelection();
191
192   /*
193     Invalidate the BufferViewFilters' configs to get constant add/remove times
194     even for huge lists.
195     This can probably be removed whenever BufferViewConfig::bulkAdd or something
196     like that is available.
197   */
198   qobject_cast<BufferViewFilter *>(outView->model())->setConfig(0);
199   qobject_cast<BufferViewFilter *>(inView->model())->setConfig(0);
200
201   // actually move the ids
202   foreach (QList<BufferId> list, selectedBuffers) {
203     foreach (BufferId buffer, list) {
204       outCfg->addBuffer(buffer,0);
205       inCfg->removeBuffer(buffer);
206     }
207   }
208
209   outView->setFilteredModel(Client::bufferModel(), outCfg);
210   inView->setFilteredModel(Client::bufferModel(), inCfg);
211
212   widgetHasChanged();
213 }
214
215 void ChatMonitorSettingsPage::on_activateBuffer_clicked() {
216   if (ui.availableBuffers->currentIndex().isValid() && ui.availableBuffers->selectionModel()->hasSelection()) {
217     toggleBuffers(ui.availableBuffers, _configAvailable, ui.activeBuffers, _configActive);
218     widgetHasChanged();
219   }
220 }
221
222 void ChatMonitorSettingsPage::on_deactivateBuffer_clicked() {
223   if (ui.activeBuffers->currentIndex().isValid() && ui.activeBuffers->selectionModel()->hasSelection()) {
224     toggleBuffers(ui.activeBuffers, _configActive, ui.availableBuffers, _configAvailable);
225     widgetHasChanged();
226   }
227 }
228
229 /*
230   switchOperationMode gets called on combobox signal currentIndexChanged.
231   modeIndex is the row id in combobox itemlist
232 */
233 void ChatMonitorSettingsPage::switchOperationMode(int idx) {
234   ChatViewSettings::OperationMode mode = (ChatViewSettings::OperationMode)(idx + 1);
235   if(mode == ChatViewSettings::OptIn) {
236     ui.labelActiveBuffers->setText(tr("Show:"));
237   }
238   else if(mode == ChatViewSettings::OptOut) {
239     ui.labelActiveBuffers->setText(tr("Ignore:"));
240   }
241   widgetHasChanged();
242 }