1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel IRC Team *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "chatmonitorsettingspage.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"
34 ChatMonitorSettingsPage::ChatMonitorSettingsPage(QWidget *parent)
35 : SettingsPage(tr("General"), tr("Chat Monitor"), parent) {
38 ui.activateBuffer->setIcon(SmallIcon("go-next"));
39 ui.deactivateBuffer->setIcon(SmallIcon("go-previous"));
40 // initialize pointers
44 // fill combobox with operation modes
45 ui.operationMode->addItem(tr("Opt In"), ChatViewSettings::OptIn);
46 ui.operationMode->addItem(tr("Opt Out"), ChatViewSettings::OptOut);
49 connect(ui.operationMode, SIGNAL(currentIndexChanged(int)), this, SLOT(switchOperationMode(int)));
52 bool ChatMonitorSettingsPage::hasDefaults() const {
56 void ChatMonitorSettingsPage::defaults() {
57 settings["OperationMode"] = ChatViewSettings::OptOut;
58 settings["ShowHighlights"] = false;
59 settings["Buffers"] = QVariant();
60 settings["Default"] = true;
65 void ChatMonitorSettingsPage::load() {
66 delete configAvailable;
69 if (settings.contains("Default"))
70 settings.remove("Default");
74 ui.operationMode->setCurrentIndex(settings["OperationMode"].toInt() - 1);
75 ui.showHighlights->setChecked(settings["ShowHighlights"].toBool());
77 // setup available buffers config (for the bufferview on the left)
78 configAvailable = new BufferViewConfig(-667);
79 configAvailable->setBufferViewName("tmpChatMonitorAvailableBuffers");
80 configAvailable->sortAlphabetically();
81 configAvailable->setNetworkId(NetworkId());
82 configAvailable->setInitialized();
84 // setup active buffers config (for the bufferview on the right)
85 configActive = new BufferViewConfig(-666);
86 configActive->setBufferViewName("tmpChatMonitorActiveBuffers");
87 configActive->setSortAlphabetically(true);
88 configActive->setNetworkId(NetworkId());
89 configActive->setInitialized();
91 // get all available buffer Ids
92 QList<BufferId> allBufferIds = Client::networkModel()->allBufferIds();
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.removeOne(v.value<BufferId>());
101 configActive->initSetBufferList(bufferIdsFromConfig);
103 ui.activeBuffers->setFilteredModel(Client::bufferModel(), configActive);
105 configAvailable->initSetBufferList(allBufferIds);
106 ui.availableBuffers->setFilteredModel(Client::bufferModel(), configAvailable);
108 setChangedState(false);
111 void ChatMonitorSettingsPage::loadSettings() {
112 ChatViewSettings chatViewSettings("ChatMonitor");
113 settings["OperationMode"] = static_cast<ChatViewSettings::OperationMode>(chatViewSettings.value("OperationMode", QVariant()).toInt());
115 // Load default behavior if no or invalid settings found
116 if (settings["OperationMode"] == ChatViewSettings::InvalidMode) {
117 switchOperationMode(ui.operationMode->findData(ChatViewSettings::OptOut));
118 settings["OperationMode"] == ChatViewSettings::OptOut;
120 settings["ShowHighlights"] = chatViewSettings.value("ShowHighlights", false);
121 settings["Buffers"] = chatViewSettings.value("Buffers", QVariantList());
124 void ChatMonitorSettingsPage::save() {
125 ChatViewSettings chatViewSettings("ChatMonitor");
126 // save operation mode
127 chatViewSettings.setValue("OperationMode", settings["OperationMode"]);
128 chatViewSettings.setValue("ShowHighlights", settings["ShowHighlights"]);
130 // save list of active buffers
131 QVariantList saveableBufferIdList;
132 foreach(BufferId id, configActive->bufferList()) {
133 saveableBufferIdList << QVariant::fromValue<BufferId>(id);
136 chatViewSettings.setValue("Buffers", saveableBufferIdList);
138 setChangedState(false);
141 void ChatMonitorSettingsPage::widgetHasChanged() {
142 bool changed = testHasChanged();
143 if(changed != hasChanged()) setChangedState(changed);
146 bool ChatMonitorSettingsPage::testHasChanged() {
147 if (configAvailable != configActive) return true;
151 //TODO: - support drag 'n drop
152 // - adding of complete networks(?)
155 toggleBuffers takes each a bufferView and its config for "input" and "output".
156 Any selected item will be moved over from the input to the output bufferview.
158 void ChatMonitorSettingsPage::toggleBuffers(BufferView *inView, BufferViewConfig *inCfg, BufferView *outView, BufferViewConfig *outCfg) {
160 // Fill QMap with selected items ordered by selection row
161 QMap<int, QList<BufferId> > selectedBuffers;
162 foreach (QModelIndex index, inView->selectionModel()->selectedIndexes()) {
163 BufferId inBufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
164 if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
166 // If item is a network: move over all children and skip other selected items of this node
168 else if (index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType) {
169 selectedBuffers[index.parent().row()] << inBufferId;
173 // clear selection to be able to remove the bufferIds without errors
174 inView->selectionModel()->clearSelection();
177 Invalidate the BufferViewFilters' configs to get constant add/remove times
179 This can probably be removed whenever BufferViewConfig::bulkAdd or something
180 like that is available.
182 qobject_cast<BufferViewFilter *>(outView->model())->setConfig(0);
183 qobject_cast<BufferViewFilter *>(inView->model())->setConfig(0);
185 // actually move the ids
186 foreach (QList<BufferId> list, selectedBuffers) {
187 foreach (BufferId buffer, list) {
188 outCfg->addBuffer(buffer,0);
189 inCfg->removeBuffer(buffer);
193 outView->setFilteredModel(Client::bufferModel(), outCfg);
194 inView->setFilteredModel(Client::bufferModel(), inCfg);
199 void ChatMonitorSettingsPage::on_activateBuffer_clicked() {
200 if (ui.availableBuffers->currentIndex().isValid() && ui.availableBuffers->selectionModel()->hasSelection()) {
201 toggleBuffers(ui.availableBuffers, configAvailable, ui.activeBuffers, configActive);
206 void ChatMonitorSettingsPage::on_deactivateBuffer_clicked() {
207 if (ui.activeBuffers->currentIndex().isValid() && ui.activeBuffers->selectionModel()->hasSelection()) {
208 toggleBuffers(ui.activeBuffers, configActive, ui.availableBuffers, configAvailable);
213 void ChatMonitorSettingsPage::on_showHighlights_toggled(bool state)
215 settings["ShowHighlights"] = state;
220 switchOperationMode gets called on combobox signal currentIndexChanged.
221 modeIndex is the row id in combobox itemlist
223 void ChatMonitorSettingsPage::switchOperationMode(int modeIndex) {
224 ChatViewSettings::OperationMode newMode = static_cast<ChatViewSettings::OperationMode>(ui.operationMode->itemData(modeIndex).toInt());
226 if(newMode == ChatViewSettings::OptIn) {
227 ui.labelActiveBuffers->setText(tr("Show:"));
229 else if(newMode == ChatViewSettings::OptOut) {
230 ui.labelActiveBuffers->setText(tr("Ignore:"));
233 if(settings["OperationMode"] != newMode) {
234 setChangedState(true);
236 settings["OperationMode"] = newMode;