Add option for chat monitor to look in the backlog
[quassel.git] / src / qtui / settingspages / chatmonitorsettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2014 by the Quassel Project                        *
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 General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
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 General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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("Interface"), tr("Chat Monitor"), parent)
36 {
37     ui.setupUi(this);
38
39     ui.activateBuffer->setIcon(SmallIcon("go-next"));
40     ui.deactivateBuffer->setIcon(SmallIcon("go-previous"));
41
42     // setup available buffers config (for the bufferview on the left)
43     _configAvailable = new BufferViewConfig(-667, this);
44     _configAvailable->setBufferViewName("tmpChatMonitorAvailableBuffers");
45     _configAvailable->setSortAlphabetically(true);
46     _configAvailable->setDisableDecoration(true);
47     _configAvailable->setNetworkId(NetworkId());
48     _configAvailable->setInitialized();
49
50     // setup active buffers config (for the bufferview on the right)
51     _configActive = new BufferViewConfig(-666, this);
52     _configActive->setBufferViewName("tmpChatMonitorActiveBuffers");
53     _configActive->setSortAlphabetically(true);
54     _configActive->setDisableDecoration(true);
55     _configActive->setNetworkId(NetworkId());
56     _configActive->setInitialized();
57
58     // fill combobox with operation modes
59     ui.operationMode->addItem(tr("Opt In"), ChatViewSettings::OptIn);
60     ui.operationMode->addItem(tr("Opt Out"), ChatViewSettings::OptOut);
61
62     // connect slots
63     connect(ui.operationMode, SIGNAL(currentIndexChanged(int)), SLOT(switchOperationMode(int)));
64     connect(ui.showHighlights, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
65     connect(ui.showOwnMessages, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
66     connect(ui.showBacklog, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
67 }
68
69
70 bool ChatMonitorSettingsPage::hasDefaults() const
71 {
72     return true;
73 }
74
75
76 void ChatMonitorSettingsPage::defaults()
77 {
78     settings["OperationMode"] = ChatViewSettings::OptOut;
79     settings["ShowHighlights"] = false;
80     settings["ShowOwnMsgs"] = false;
81     settings["Buffers"] = QVariant();
82     settings["Default"] = true;
83     settings["ShowBacklog"] = true;
84     load();
85     widgetHasChanged();
86 }
87
88
89 void ChatMonitorSettingsPage::load()
90 {
91     if (settings.contains("Default"))
92         settings.remove("Default");
93     else
94         loadSettings();
95
96     switchOperationMode(settings["OperationMode"].toInt() - 1);
97     ui.operationMode->setCurrentIndex(settings["OperationMode"].toInt() - 1);
98     ui.showHighlights->setChecked(settings["ShowHighlights"].toBool());
99     ui.showOwnMessages->setChecked(settings["ShowOwnMsgs"].toBool());
100     ui.showBacklog->setChecked(settings["ShowBacklog"].toBool());
101
102     // get all available buffer Ids
103     QList<BufferId> allBufferIds = Client::networkModel()->allBufferIds();
104
105     if (!settings["Buffers"].toList().isEmpty()) {
106         QList<BufferId> bufferIdsFromConfig;
107         // remove all active buffers from the available config
108         foreach(QVariant v, settings["Buffers"].toList()) {
109             bufferIdsFromConfig << v.value<BufferId>();
110             allBufferIds.removeAll(v.value<BufferId>());
111         }
112         Client::networkModel()->sortBufferIds(bufferIdsFromConfig);
113         _configActive->initSetBufferList(bufferIdsFromConfig);
114     }
115     ui.activeBuffers->setFilteredModel(Client::bufferModel(), _configActive);
116
117     Client::networkModel()->sortBufferIds(allBufferIds);
118     _configAvailable->initSetBufferList(allBufferIds);
119     ui.availableBuffers->setFilteredModel(Client::bufferModel(), _configAvailable);
120
121     setChangedState(false);
122 }
123
124
125 void ChatMonitorSettingsPage::loadSettings()
126 {
127     ChatViewSettings chatViewSettings("ChatMonitor");
128     settings["OperationMode"] = (ChatViewSettings::OperationMode)chatViewSettings.value("OperationMode", ChatViewSettings::OptOut).toInt();
129
130     settings["ShowHighlights"] = chatViewSettings.value("ShowHighlights", false);
131     settings["ShowOwnMsgs"] = chatViewSettings.value("ShowOwnMsgs", false);
132     settings["Buffers"] = chatViewSettings.value("Buffers", QVariantList());
133     settings["ShowBacklog"] = chatViewSettings.value("ShowBacklog", true);
134 }
135
136
137 void ChatMonitorSettingsPage::save()
138 {
139     ChatViewSettings chatViewSettings("ChatMonitor");
140     // save operation mode
141     chatViewSettings.setValue("OperationMode", ui.operationMode->currentIndex() + 1);
142     chatViewSettings.setValue("ShowHighlights", ui.showHighlights->isChecked());
143     chatViewSettings.setValue("ShowOwnMsgs", ui.showOwnMessages->isChecked());
144     chatViewSettings.setValue("ShowBacklog", ui.showBacklog->isChecked());
145
146     // save list of active buffers
147     QVariantList saveableBufferIdList;
148     foreach(BufferId id, _configActive->bufferList()) {
149         saveableBufferIdList << QVariant::fromValue<BufferId>(id);
150     }
151
152     chatViewSettings.setValue("Buffers", saveableBufferIdList);
153     load();
154     setChangedState(false);
155 }
156
157
158 void ChatMonitorSettingsPage::widgetHasChanged()
159 {
160     bool changed = testHasChanged();
161     if (changed != hasChanged()) setChangedState(changed);
162 }
163
164
165 bool ChatMonitorSettingsPage::testHasChanged()
166 {
167     if (settings["OperationMode"].toInt() != ui.operationMode->currentIndex() + 1)
168         return true;
169     if (settings["ShowHighlights"].toBool() != ui.showHighlights->isChecked())
170         return true;
171     if (settings["ShowOwnMsgs"].toBool() != ui.showOwnMessages->isChecked())
172         return true;
173
174     if (_configActive->bufferList().count() != settings["Buffers"].toList().count())
175         return true;
176
177     QSet<BufferId> uiBufs = _configActive->bufferList().toSet();
178     QSet<BufferId> settingsBufs;
179     foreach(QVariant v, settings["Buffers"].toList())
180     settingsBufs << v.value<BufferId>();
181     if (uiBufs != settingsBufs)
182         return true;
183
184     return false;
185 }
186
187
188 //TODO: - support drag 'n drop
189 //      - adding of complete networks(?)
190
191 /*
192   toggleBuffers takes each a bufferView and its config for "input" and "output".
193   Any selected item will be moved over from the input to the output bufferview.
194 */
195 void ChatMonitorSettingsPage::toggleBuffers(BufferView *inView, BufferViewConfig *inCfg, BufferView *outView, BufferViewConfig *outCfg)
196 {
197     // Fill QMap with selected items ordered by selection row
198     QMap<int, QList<BufferId> > selectedBuffers;
199     foreach(QModelIndex index, inView->selectionModel()->selectedIndexes()) {
200         BufferId inBufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
201         if (index.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
202             // TODO:
203             //  If item is a network: move over all children and skip other selected items of this node
204         }
205         else if (index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType) {
206             selectedBuffers[index.parent().row()] << inBufferId;
207         }
208     }
209
210     // clear selection to be able to remove the bufferIds without errors
211     inView->selectionModel()->clearSelection();
212
213     /*
214       Invalidate the BufferViewFilters' configs to get constant add/remove times
215       even for huge lists.
216       This can probably be removed whenever BufferViewConfig::bulkAdd or something
217       like that is available.
218     */
219     qobject_cast<BufferViewFilter *>(outView->model())->setConfig(0);
220     qobject_cast<BufferViewFilter *>(inView->model())->setConfig(0);
221
222     // actually move the ids
223     foreach(QList<BufferId> list, selectedBuffers) {
224         foreach(BufferId buffer, list) {
225             outCfg->addBuffer(buffer, 0);
226             inCfg->removeBuffer(buffer);
227         }
228     }
229
230     outView->setFilteredModel(Client::bufferModel(), outCfg);
231     inView->setFilteredModel(Client::bufferModel(), inCfg);
232
233     widgetHasChanged();
234 }
235
236
237 void ChatMonitorSettingsPage::on_activateBuffer_clicked()
238 {
239     if (ui.availableBuffers->currentIndex().isValid() && ui.availableBuffers->selectionModel()->hasSelection()) {
240         toggleBuffers(ui.availableBuffers, _configAvailable, ui.activeBuffers, _configActive);
241         widgetHasChanged();
242     }
243 }
244
245
246 void ChatMonitorSettingsPage::on_deactivateBuffer_clicked()
247 {
248     if (ui.activeBuffers->currentIndex().isValid() && ui.activeBuffers->selectionModel()->hasSelection()) {
249         toggleBuffers(ui.activeBuffers, _configActive, ui.availableBuffers, _configAvailable);
250         widgetHasChanged();
251     }
252 }
253
254
255 /*
256   switchOperationMode gets called on combobox signal currentIndexChanged.
257   modeIndex is the row id in combobox itemlist
258 */
259 void ChatMonitorSettingsPage::switchOperationMode(int idx)
260 {
261     ChatViewSettings::OperationMode mode = (ChatViewSettings::OperationMode)(idx + 1);
262     if (mode == ChatViewSettings::OptIn) {
263         ui.labelActiveBuffers->setText(tr("Show:"));
264     }
265     else if (mode == ChatViewSettings::OptOut) {
266         ui.labelActiveBuffers->setText(tr("Ignore:"));
267     }
268     widgetHasChanged();
269 }