File: chatmonitorsettingspage.cpp

package info (click to toggle)
quassel 1:0.13.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 25,640 kB
  • sloc: cpp: 66,953; perl: 15,837; ansic: 4,418; sql: 1,225; sh: 328; xml: 263; python: 224; makefile: 24
file content (292 lines) | stat: -rw-r--r-- 11,595 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/***************************************************************************
 *   Copyright (C) 2005-2019 by the Quassel Project                        *
 *   devel@quassel-irc.org                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) version 3.                                           *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/

#include "chatmonitorsettingspage.h"

#include "client.h"
#include "icon.h"
#include "networkmodel.h"
#include "bufferviewconfig.h"
#include "buffermodel.h"
#include "bufferview.h"
#include "bufferviewfilter.h"
#include "chatviewsettings.h"
#include "util.h"

#include <QVariant>

ChatMonitorSettingsPage::ChatMonitorSettingsPage(QWidget *parent)
    : SettingsPage(tr("Interface"), tr("Chat Monitor"), parent)
{
    ui.setupUi(this);

    ui.activateBuffer->setIcon(icon::get("go-next"));
    ui.deactivateBuffer->setIcon(icon::get("go-previous"));

    // setup available buffers config (for the bufferview on the left)
    _configAvailable = new BufferViewConfig(-667, this);
    _configAvailable->setBufferViewName("tmpChatMonitorAvailableBuffers");
    _configAvailable->setSortAlphabetically(true);
    _configAvailable->setDisableDecoration(true);
    _configAvailable->setNetworkId(NetworkId());
    _configAvailable->setInitialized();

    // setup active buffers config (for the bufferview on the right)
    _configActive = new BufferViewConfig(-666, this);
    _configActive->setBufferViewName("tmpChatMonitorActiveBuffers");
    _configActive->setSortAlphabetically(true);
    _configActive->setDisableDecoration(true);
    _configActive->setNetworkId(NetworkId());
    _configActive->setInitialized();

    // fill combobox with operation modes
    ui.operationMode->addItem(tr("Opt In"), ChatViewSettings::OptIn);
    ui.operationMode->addItem(tr("Opt Out"), ChatViewSettings::OptOut);

    // connect slots
    connect(ui.operationMode, SIGNAL(currentIndexChanged(int)), SLOT(switchOperationMode(int)));
    connect(ui.showHighlights, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
    connect(ui.showOwnMessages, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
    connect(ui.alwaysOwn, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
    connect(ui.showBacklog, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
    connect(ui.includeRead, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
}


bool ChatMonitorSettingsPage::hasDefaults() const
{
    return true;
}


void ChatMonitorSettingsPage::defaults()
{
    // NOTE: Whenever changing defaults here, also update ChatMonitorFilter::ChatMonitorFilter()
    // and ChatMonitorSettingsPage::loadSettings() to match

    settings["OperationMode"] = ChatViewSettings::OptOut;
    settings["ShowHighlights"] = false;
    settings["ShowOwnMsgs"] = true;
    settings["AlwaysOwn"] = false;
    settings["Buffers"] = QVariant();
    settings["Default"] = true;
    settings["ShowBacklog"] = true;
    settings["IncludeRead"] = false;
    load();
    widgetHasChanged();
}


void ChatMonitorSettingsPage::load()
{
    if (settings.contains("Default"))
        settings.remove("Default");
    else
        loadSettings();

    switchOperationMode(settings["OperationMode"].toInt() - 1);
    ui.operationMode->setCurrentIndex(settings["OperationMode"].toInt() - 1);
    ui.showHighlights->setChecked(settings["ShowHighlights"].toBool());
    ui.showOwnMessages->setChecked(settings["ShowOwnMsgs"].toBool());
    ui.alwaysOwn->setChecked(settings["AlwaysOwn"].toBool());
    ui.showBacklog->setChecked(settings["ShowBacklog"].toBool());
    ui.includeRead->setChecked(settings["IncludeRead"].toBool());

    // get all available buffer Ids
    QList<BufferId> allBufferIds = Client::networkModel()->allBufferIds();

    if (!settings["Buffers"].toList().isEmpty()) {
        QList<BufferId> bufferIdsFromConfig;
        // remove all active buffers from the available config
        foreach(QVariant v, settings["Buffers"].toList()) {
            bufferIdsFromConfig << v.value<BufferId>();
            allBufferIds.removeAll(v.value<BufferId>());
        }
        Client::networkModel()->sortBufferIds(bufferIdsFromConfig);
        _configActive->initSetBufferList(bufferIdsFromConfig);
    }
    ui.activeBuffers->setFilteredModel(Client::bufferModel(), _configActive);

    Client::networkModel()->sortBufferIds(allBufferIds);
    _configAvailable->initSetBufferList(allBufferIds);
    ui.availableBuffers->setFilteredModel(Client::bufferModel(), _configAvailable);

    setChangedState(false);
}


void ChatMonitorSettingsPage::loadSettings()
{
    // NOTE: Whenever changing defaults here, also update ChatMonitorFilter::ChatMonitorFilter()
    // and ChatMonitorSettingsPage::defaults() to match
    ChatViewSettings chatViewSettings("ChatMonitor");

    settings["OperationMode"] = (ChatViewSettings::OperationMode)
            chatViewSettings.value("OperationMode", ChatViewSettings::OptOut).toInt();
    settings["ShowHighlights"] = chatViewSettings.value("ShowHighlights", false);
    settings["ShowOwnMsgs"] = chatViewSettings.value("ShowOwnMsgs", true);
    settings["AlwaysOwn"] = chatViewSettings.value("AlwaysOwn", false);
    settings["Buffers"] = chatViewSettings.value("Buffers", QVariantList());
    settings["ShowBacklog"] = chatViewSettings.value("ShowBacklog", true);
    settings["IncludeRead"] = chatViewSettings.value("IncludeRead", false);
}


void ChatMonitorSettingsPage::save()
{
    ChatViewSettings chatViewSettings("ChatMonitor");
    // save operation mode
    chatViewSettings.setValue("OperationMode", ui.operationMode->currentIndex() + 1);
    chatViewSettings.setValue("ShowHighlights", ui.showHighlights->isChecked());
    chatViewSettings.setValue("ShowOwnMsgs", ui.showOwnMessages->isChecked());
    chatViewSettings.setValue("AlwaysOwn", ui.alwaysOwn->isChecked());
    chatViewSettings.setValue("ShowBacklog", ui.showBacklog->isChecked());
    chatViewSettings.setValue("IncludeRead", ui.includeRead->isChecked());

    // save list of active buffers
    QVariantList saveableBufferIdList;
    foreach (BufferId id, _configActive->bufferList()) {
        saveableBufferIdList << QVariant::fromValue(id);
    }

    chatViewSettings.setValue("Buffers", saveableBufferIdList);
    load();
    setChangedState(false);
}


void ChatMonitorSettingsPage::widgetHasChanged()
{
    bool changed = testHasChanged();
    if (changed != hasChanged()) setChangedState(changed);
}


bool ChatMonitorSettingsPage::testHasChanged()
{
    if (settings["OperationMode"].toInt() != ui.operationMode->currentIndex() + 1)
        return true;
    if (settings["ShowHighlights"].toBool() != ui.showHighlights->isChecked())
        return true;
    if (settings["ShowOwnMsgs"].toBool() != ui.showOwnMessages->isChecked())
        return true;
    if (settings["AlwaysOwn"].toBool() != ui.alwaysOwn->isChecked())
        return true;
    if (settings["ShowBacklog"].toBool() != ui.showBacklog->isChecked())
        return true;
    if (settings["IncludeRead"].toBool() != ui.includeRead->isChecked())
        return true;

    if (_configActive->bufferList().count() != settings["Buffers"].toList().count())
        return true;

    QSet<BufferId> uiBufs = toQSet(_configActive->bufferList());
    QSet<BufferId> settingsBufs;
    foreach(QVariant v, settings["Buffers"].toList())
    settingsBufs << v.value<BufferId>();
    if (uiBufs != settingsBufs)
        return true;

    return false;
}


//TODO: - support drag 'n drop
//      - adding of complete networks(?)

/*
  toggleBuffers takes each a bufferView and its config for "input" and "output".
  Any selected item will be moved over from the input to the output bufferview.
*/
void ChatMonitorSettingsPage::toggleBuffers(BufferView *inView, BufferViewConfig *inCfg, BufferView *outView, BufferViewConfig *outCfg)
{
    // Fill QMap with selected items ordered by selection row
    QMap<int, QList<BufferId> > selectedBuffers;
    foreach(QModelIndex index, inView->selectionModel()->selectedIndexes()) {
        BufferId inBufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
        if (index.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
            // TODO:
            //  If item is a network: move over all children and skip other selected items of this node
        }
        else if (index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType) {
            selectedBuffers[index.parent().row()] << inBufferId;
        }
    }

    // clear selection to be able to remove the bufferIds without errors
    inView->selectionModel()->clearSelection();

    /*
      Invalidate the BufferViewFilters' configs to get constant add/remove times
      even for huge lists.
      This can probably be removed whenever BufferViewConfig::bulkAdd or something
      like that is available.
    */
    qobject_cast<BufferViewFilter *>(outView->model())->setConfig(0);
    qobject_cast<BufferViewFilter *>(inView->model())->setConfig(0);

    // actually move the ids
    foreach(QList<BufferId> list, selectedBuffers) {
        foreach(BufferId buffer, list) {
            outCfg->addBuffer(buffer, 0);
            inCfg->removeBuffer(buffer);
        }
    }

    outView->setFilteredModel(Client::bufferModel(), outCfg);
    inView->setFilteredModel(Client::bufferModel(), inCfg);

    widgetHasChanged();
}


void ChatMonitorSettingsPage::on_activateBuffer_clicked()
{
    if (ui.availableBuffers->currentIndex().isValid() && ui.availableBuffers->selectionModel()->hasSelection()) {
        toggleBuffers(ui.availableBuffers, _configAvailable, ui.activeBuffers, _configActive);
        widgetHasChanged();
    }
}


void ChatMonitorSettingsPage::on_deactivateBuffer_clicked()
{
    if (ui.activeBuffers->currentIndex().isValid() && ui.activeBuffers->selectionModel()->hasSelection()) {
        toggleBuffers(ui.activeBuffers, _configActive, ui.availableBuffers, _configAvailable);
        widgetHasChanged();
    }
}


/*
  switchOperationMode gets called on combobox signal currentIndexChanged.
  modeIndex is the row id in combobox itemlist
*/
void ChatMonitorSettingsPage::switchOperationMode(int idx)
{
    ChatViewSettings::OperationMode mode = (ChatViewSettings::OperationMode)(idx + 1);
    if (mode == ChatViewSettings::OptIn) {
        ui.labelActiveBuffers->setText(tr("Show:"));
    }
    else if (mode == ChatViewSettings::OptOut) {
        ui.labelActiveBuffers->setText(tr("Ignore:"));
    }
    widgetHasChanged();
}