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