54b5347d56ce227787aed29053d4c6110202880b
[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("Behaviour"), tr("ChatMonitor"), parent) {
36   ui.setupUi(this);
37
38   ui.activateBuffer->setIcon(SmallIcon("go-next"));
39   ui.deactivateBuffer->setIcon(SmallIcon("go-previous"));
40   // initialize pointers
41   configAvailable = 0;
42   configActive = 0;
43
44   // fill combobox with operation modes
45   ui.operationMode->addItem("Opt-In", ChatViewSettings::OptIn);
46   ui.operationMode->addItem("Opt-Out", ChatViewSettings::OptOut);
47
48   // connect slots
49   connect(ui.operationMode, SIGNAL(currentIndexChanged(int)), this, SLOT(switchOperationMode(int)));
50 }
51
52 bool ChatMonitorSettingsPage::hasDefaults() const {
53   return true;
54 }
55
56 void ChatMonitorSettingsPage::defaults() {
57   settings["OperationMode"] = ChatViewSettings::OptOut;
58   settings["HighlightAlways"] = false;
59   settings["Buffers"] = QVariant();
60   settings["Default"] = true;
61   load();
62   widgetHasChanged();
63 }
64
65 void ChatMonitorSettingsPage::load() {
66   delete configAvailable;
67   delete configActive;
68
69   if (settings.contains("Default"))
70     settings.remove("Default");
71   else
72     loadSettings();
73
74   ui.operationMode->setCurrentIndex(settings["OperationMode"].toInt() - 1);
75   ui.highlightAlways->setChecked(settings["HighlightAlways"].toBool());
76
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();
83
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();
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.removeOne(v.value<BufferId>());
100     }
101     configActive->initSetBufferList(bufferIdsFromConfig);
102   }
103   ui.activeBuffers->setFilteredModel(Client::bufferModel(), configActive);
104
105   configAvailable->initSetBufferList(allBufferIds);
106   ui.availableBuffers->setFilteredModel(Client::bufferModel(), configAvailable);
107
108   setChangedState(false);
109 }
110
111 void ChatMonitorSettingsPage::loadSettings() {
112   ChatViewSettings chatViewSettings("ChatMonitor");
113   settings["OperationMode"] = static_cast<ChatViewSettings::OperationMode>(chatViewSettings.value("OperationMode", QVariant()).toInt());
114
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;
119   }
120   settings["HighlightAlways"] = chatViewSettings.value("HighlightAlways", false);
121   settings["Buffers"] = chatViewSettings.value("Buffers", QVariantList());
122 }
123
124 void ChatMonitorSettingsPage::save() {
125   ChatViewSettings chatViewSettings("ChatMonitor");
126   // save operation mode
127   chatViewSettings.setValue("OperationMode", settings["OperationMode"]);
128   chatViewSettings.setValue("HighlightAlways", settings["HighlightAlways"]);
129
130   // save list of active buffers
131   QVariantList saveableBufferIdList; 
132   foreach(BufferId id, configActive->bufferList()) {
133     saveableBufferIdList << QVariant::fromValue<BufferId>(id);
134   }
135
136   chatViewSettings.setValue("Buffers", saveableBufferIdList);
137   load();
138   setChangedState(false);
139 }
140
141 void ChatMonitorSettingsPage::widgetHasChanged() {
142   bool changed = testHasChanged();
143   if(changed != hasChanged()) setChangedState(changed);
144 }
145
146 bool ChatMonitorSettingsPage::testHasChanged() {
147   if (configAvailable != configActive) return true;
148   return false;
149 }
150
151 //TODO: - support drag 'n drop
152 //      - adding of complete networks(?)
153
154 /*
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.
157 */
158 void ChatMonitorSettingsPage::toggleBuffers(BufferView *inView, BufferViewConfig *inCfg, BufferView *outView, BufferViewConfig *outCfg) {
159
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) {
165       // TODO: 
166       //  If item is a network: move over all children and skip other selected items of this node
167     }
168     else if (index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType) {
169       selectedBuffers[index.parent().row()] << inBufferId;
170     }
171   }
172
173   // clear selection to be able to remove the bufferIds without errors
174   inView->selectionModel()->clearSelection();
175
176   /*
177     Invalidate the BufferViewFilters' configs to get constant add/remove times
178     even for huge lists.
179     This can probably be removed whenever BufferViewConfig::bulkAdd or something
180     like that is available.
181   */
182   qobject_cast<BufferViewFilter *>(outView->model())->setConfig(0);
183   qobject_cast<BufferViewFilter *>(inView->model())->setConfig(0);
184
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);
190     }
191   }
192
193   outView->setFilteredModel(Client::bufferModel(), outCfg);
194   inView->setFilteredModel(Client::bufferModel(), inCfg);
195
196   widgetHasChanged();
197 }
198
199 void ChatMonitorSettingsPage::on_activateBuffer_clicked() {
200   if (ui.availableBuffers->currentIndex().isValid() && ui.availableBuffers->selectionModel()->hasSelection()) {
201     toggleBuffers(ui.availableBuffers, configAvailable, ui.activeBuffers, configActive);
202     widgetHasChanged();
203   }
204 }
205
206 void ChatMonitorSettingsPage::on_deactivateBuffer_clicked() {
207   if (ui.activeBuffers->currentIndex().isValid() && ui.activeBuffers->selectionModel()->hasSelection()) {
208     toggleBuffers(ui.activeBuffers, configActive, ui.availableBuffers, configAvailable);
209     widgetHasChanged();
210   }
211 }
212
213 void ChatMonitorSettingsPage::on_highlightAlways_toggled(bool state)
214 {
215   settings["HighlightAlways"] = state;
216   widgetHasChanged();
217 }
218
219 /*
220   switchOperationMode gets called on combobox signal currentIndexChanged.
221   modeIndex is the row id in combobox itemlist
222 */
223 void ChatMonitorSettingsPage::switchOperationMode(int modeIndex) {
224   ChatViewSettings::OperationMode newMode = static_cast<ChatViewSettings::OperationMode>(ui.operationMode->itemData(modeIndex).toInt());
225
226   if(newMode == ChatViewSettings::OptIn) {
227     ui.labelActiveBuffers->setText(tr("Show:"));
228   } 
229   else if(newMode == ChatViewSettings::OptOut) {
230     ui.labelActiveBuffers->setText(tr("Ignore:"));
231   }
232
233   if(settings["OperationMode"] != newMode) {
234     setChangedState(true);
235   }
236   settings["OperationMode"] = newMode;
237 }