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