File: coreirclisthelper.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 (119 lines) | stat: -rw-r--r-- 4,420 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
/***************************************************************************
 *   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 "coreirclisthelper.h"

#include "corenetwork.h"
#include "coreuserinputhandler.h"

constexpr auto kTimeoutMs = 5000;

INIT_SYNCABLE_OBJECT(CoreIrcListHelper)
QVariantList CoreIrcListHelper::requestChannelList(const NetworkId &netId, const QStringList &channelFilters)
{
    if (_finishedChannelLists.contains(netId))
        return _finishedChannelLists.take(netId);

    if (_channelLists.contains(netId)) {
        _queuedQuery[netId] = channelFilters.join(",");
    }
    else {
        dispatchQuery(netId, channelFilters.join(","));
    }
    return QVariantList();
}


bool CoreIrcListHelper::addChannel(const NetworkId &netId, const QString &channelName, quint32 userCount, const QString &topic)
{
    if (!_channelLists.contains(netId))
        return false;

    _channelLists[netId] << ChannelDescription(channelName, userCount, topic);
    if (_queryTimeoutByNetId.contains(netId))
        _queryTimeoutByNetId[netId]->start(kTimeoutMs, this);

    return true;
}


bool CoreIrcListHelper::dispatchQuery(const NetworkId &netId, const QString &query)
{
    CoreNetwork *network = coreSession()->network(netId);
    if (network) {
        _channelLists[netId] = QList<ChannelDescription>();
        network->userInputHandler()->handleList(BufferInfo(), query);

        auto timer = std::make_shared<QBasicTimer>();
        timer->start(kTimeoutMs, this);
        _queryTimeoutByNetId[netId] = timer;
        _queryTimeoutByTimerId[timer->timerId()] = netId;

        return true;
    }
    else {
        return false;
    }
}


bool CoreIrcListHelper::endOfChannelList(const NetworkId &netId)
{
    if (_queryTimeoutByNetId.contains(netId)) {
        // If we recieved an actual RPL_LISTEND, remove the timer
        int timerId = _queryTimeoutByNetId.take(netId)->timerId();
        _queryTimeoutByTimerId.remove(timerId);
    }

    if (_queuedQuery.contains(netId)) {
        // we're no longer interessted in the current data. drop it and issue a new request.
        return dispatchQuery(netId, _queuedQuery.take(netId));
    }
    else if (_channelLists.contains(netId)) {
        QVariantList channelList;
        foreach(ChannelDescription channel, _channelLists[netId]) {
            QVariantList channelVariant;
            channelVariant << channel.channelName << channel.userCount << channel.topic;
            channelList << QVariant::fromValue<QVariant>(channelVariant);
        }
        _finishedChannelLists[netId] = channelList;
        _channelLists.remove(netId);
        reportFinishedList(netId);
        return true;
    }
    else {
        return false;
    }
}


void CoreIrcListHelper::timerEvent(QTimerEvent *event)
{
    if (!_queryTimeoutByTimerId.contains(event->timerId())) {
        IrcListHelper::timerEvent(event);
        return;
    }

    NetworkId netId = _queryTimeoutByTimerId.take(event->timerId());
    _queryTimeoutByNetId.remove(netId);

    event->accept();
    endOfChannelList(netId);
}