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