Don't leak BufferViewConfigs, plus some syntax/naming convention fixes
[quassel.git] / src / qtui / settingspages / chatmonitorsettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
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->sortAlphabetically();
45   _configAvailable->setNetworkId(NetworkId());
46   _configAvailable->setInitialized();
47
48   // setup active buffers config (for the bufferview on the right)
49   _configActive = new BufferViewConfig(-666, this);
50   _configActive->setBufferViewName("tmpChatMonitorActiveBuffers");
51   _configActive->setSortAlphabetically(true);
52   _configActive->setNetworkId(NetworkId());
53   _configActive->setInitialized();
54
55   // fill combobox with operation modes
56   ui.operationMode->addItem(tr("Opt In"), ChatViewSettings::OptIn);
57   ui.operationMode->addItem(tr("Opt Out"), ChatViewSettings::OptOut);
58
59   // connect slots
60   connect(ui.operationMode, SIGNAL(currentIndexChanged(int)), this, SLOT(switchOperationMode(int)));
61 }
62
63 bool ChatMonitorSettingsPage::hasDefaults() const {
64   return true;
65 }
66
67 void ChatMonitorSettingsPage::defaults() {
68   settings["OperationMode"] = ChatViewSettings::OptOut;
69   settings["ShowHighlights"] = false;
70   settings["Buffers"] = QVariant();
71   settings["Default"] = true;
72   load();
73   widgetHasChanged();
74 }
75
76 void ChatMonitorSettingsPage::load() {
77   if(settings.contains("Default"))
78     settings.remove("Default");
79   else
80     loadSettings();
81
82   ui.operationMode->setCurrentIndex(settings["OperationMode"].toInt() - 1);
83   ui.showHighlights->setChecked(settings["ShowHighlights"].toBool());
84
85   // get all available buffer Ids
86   QList<BufferId> allBufferIds = Client::networkModel()->allBufferIds();
87
88   if(!settings["Buffers"].toList().isEmpty()) {
89     QList<BufferId> bufferIdsFromConfig;
90     // remove all active buffers from the available config
91     foreach(QVariant v, settings["Buffers"].toList()) {
92       bufferIdsFromConfig << v.value<BufferId>();
93       allBufferIds.removeAll(v.value<BufferId>());
94     }
95     _configActive->initSetBufferList(bufferIdsFromConfig);
96   }
97   ui.activeBuffers->setFilteredModel(Client::bufferModel(), _configActive);
98
99   _configAvailable->initSetBufferList(allBufferIds);
100   ui.availableBuffers->setFilteredModel(Client::bufferModel(), _configAvailable);
101
102   setChangedState(false);
103 }
104
105 void ChatMonitorSettingsPage::loadSettings() {
106   ChatViewSettings chatViewSettings("ChatMonitor");
107   settings["OperationMode"] = static_cast<ChatViewSettings::OperationMode>(chatViewSettings.value("OperationMode", QVariant()).toInt());
108
109   // Load default behavior if no or invalid settings found
110   if(settings["OperationMode"] == ChatViewSettings::InvalidMode) {
111     switchOperationMode(ui.operationMode->findData(ChatViewSettings::OptOut));
112     settings["OperationMode"] == ChatViewSettings::OptOut;
113   }
114   settings["ShowHighlights"] = chatViewSettings.value("ShowHighlights", false);
115   settings["Buffers"] = chatViewSettings.value("Buffers", QVariantList());
116 }
117
118 void ChatMonitorSettingsPage::save() {
119   ChatViewSettings chatViewSettings("ChatMonitor");
120   // save operation mode
121   chatViewSettings.setValue("OperationMode", settings["OperationMode"]);
122   chatViewSettings.setValue("ShowHighlights", settings["ShowHighlights"]);
123
124   // save list of active buffers
125   QVariantList saveableBufferIdList;
126   foreach(BufferId id, _configActive->bufferList()) {
127     saveableBufferIdList << QVariant::fromValue<BufferId>(id);
128   }
129
130   chatViewSettings.setValue("Buffers", saveableBufferIdList);
131   load();
132   setChangedState(false);
133 }
134
135 void ChatMonitorSettingsPage::widgetHasChanged() {
136   bool changed = testHasChanged();
137   if(changed != hasChanged()) setChangedState(changed);
138 }
139
140 bool ChatMonitorSettingsPage::testHasChanged() {
141   if (_configAvailable != _configActive) return true;
142   return false;
143 }
144
145 //TODO: - support drag 'n drop
146 //      - adding of complete networks(?)
147
148 /*
149   toggleBuffers takes each a bufferView and its config for "input" and "output".
150   Any selected item will be moved over from the input to the output bufferview.
151 */
152 void ChatMonitorSettingsPage::toggleBuffers(BufferView *inView, BufferViewConfig *inCfg, BufferView *outView, BufferViewConfig *outCfg) {
153
154   // Fill QMap with selected items ordered by selection row
155   QMap<int, QList<BufferId> > selectedBuffers;
156   foreach(QModelIndex index, inView->selectionModel()->selectedIndexes()) {
157     BufferId inBufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
158     if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
159       // TODO:
160       //  If item is a network: move over all children and skip other selected items of this node
161     }
162     else if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType) {
163       selectedBuffers[index.parent().row()] << inBufferId;
164     }
165   }
166
167   // clear selection to be able to remove the bufferIds without errors
168   inView->selectionModel()->clearSelection();
169
170   /*
171     Invalidate the BufferViewFilters' configs to get constant add/remove times
172     even for huge lists.
173     This can probably be removed whenever BufferViewConfig::bulkAdd or something
174     like that is available.
175   */
176   qobject_cast<BufferViewFilter *>(outView->model())->setConfig(0);
177   qobject_cast<BufferViewFilter *>(inView->model())->setConfig(0);
178
179   // actually move the ids
180   foreach (QList<BufferId> list, selectedBuffers) {
181     foreach (BufferId buffer, list) {
182       outCfg->addBuffer(buffer,0);
183       inCfg->removeBuffer(buffer);
184     }
185   }
186
187   outView->setFilteredModel(Client::bufferModel(), outCfg);
188   inView->setFilteredModel(Client::bufferModel(), inCfg);
189
190   widgetHasChanged();
191 }
192
193 void ChatMonitorSettingsPage::on_activateBuffer_clicked() {
194   if (ui.availableBuffers->currentIndex().isValid() && ui.availableBuffers->selectionModel()->hasSelection()) {
195     toggleBuffers(ui.availableBuffers, _configAvailable, ui.activeBuffers, _configActive);
196     widgetHasChanged();
197   }
198 }
199
200 void ChatMonitorSettingsPage::on_deactivateBuffer_clicked() {
201   if (ui.activeBuffers->currentIndex().isValid() && ui.activeBuffers->selectionModel()->hasSelection()) {
202     toggleBuffers(ui.activeBuffers, _configActive, ui.availableBuffers, _configAvailable);
203     widgetHasChanged();
204   }
205 }
206
207 void ChatMonitorSettingsPage::on_showHighlights_toggled(bool state)
208 {
209   settings["ShowHighlights"] = state;
210   widgetHasChanged();
211 }
212
213 /*
214   switchOperationMode gets called on combobox signal currentIndexChanged.
215   modeIndex is the row id in combobox itemlist
216 */
217 void ChatMonitorSettingsPage::switchOperationMode(int modeIndex) {
218   ChatViewSettings::OperationMode newMode = static_cast<ChatViewSettings::OperationMode>(ui.operationMode->itemData(modeIndex).toInt());
219
220   if(newMode == ChatViewSettings::OptIn) {
221     ui.labelActiveBuffers->setText(tr("Show:"));
222   }
223   else if(newMode == ChatViewSettings::OptOut) {
224     ui.labelActiveBuffers->setText(tr("Ignore:"));
225   }
226
227   if(settings["OperationMode"] != newMode) {
228     setChangedState(true);
229   }
230   settings["OperationMode"] = newMode;
231 }