File: toolbaractionprovider.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 (230 lines) | stat: -rw-r--r-- 8,922 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
/***************************************************************************
 *   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 "toolbaractionprovider.h"

#include <QMenu>
#include <QToolBar>

#include "icon.h"

ToolBarActionProvider::ToolBarActionProvider(QObject *parent)
    : NetworkModelController(parent)
{
    registerAction(NetworkConnectAllWithDropdown, icon::get("network-connect"), tr("Connect"))->setToolTip(tr("Connect to IRC"));
    registerAction(NetworkDisconnectAllWithDropdown, icon::get("network-disconnect"), tr("Disconnect"))->setToolTip(tr("Disconnect from IRC"));
    registerAction(NetworkConnectAll, tr("Connect to all"));
    registerAction(NetworkDisconnectAll, tr("Disconnect from all"));

    registerAction(BufferPart, icon::get("irc-close-channel"), tr("Part"))->setToolTip(tr("Leave currently selected channel"));
    registerAction(JoinChannel, icon::get("irc-join-channel"), tr("Join"))->setToolTip(tr("Join a channel"));

    registerAction(NickQuery, icon::get("mail-message-new"), tr("Query"))->setToolTip(tr("Start a private conversation")); // fix icon
    registerAction(NickWhois, icon::get("im-user"), tr("Whois"))->setToolTip(tr("Request user information")); // fix icon

    registerAction(NickOp, icon::get("irc-operator"), tr("Op"))->setToolTip(tr("Give operator privileges to user"));
    registerAction(NickDeop, icon::get("irc-remove-operator"), tr("Deop"))->setToolTip(tr("Take operator privileges from user"));
    registerAction(NickVoice, icon::get("irc-voice"), tr("Voice"))->setToolTip(tr("Give voice to user"));
    registerAction(NickDevoice, icon::get("irc-unvoice"), tr("Devoice"))->setToolTip(tr("Take voice from user"));
    registerAction(NickKick, icon::get("im-kick-user"), tr("Kick"))->setToolTip(tr("Remove user from channel"));
    registerAction(NickBan, icon::get("im-ban-user"), tr("Ban"))->setToolTip(tr("Ban user from channel"));
    registerAction(NickKickBan, icon::get("im-ban-kick-user"), tr("Kick/Ban"))->setToolTip(tr("Remove and ban user from channel"));

    _networksConnectMenu = new QMenu();
    _networksConnectMenu->setSeparatorsCollapsible(false);
    _networksConnectMenu->addSeparator();
    _networksConnectMenu->addAction(action(NetworkConnectAll));
    action(NetworkConnectAllWithDropdown)->setMenu(_networksConnectMenu);
    action(NetworkConnectAllWithDropdown)->setEnabled(false);

    _networksDisconnectMenu = new QMenu();
    _networksDisconnectMenu->setSeparatorsCollapsible(false);
    _networksDisconnectMenu->addSeparator();
    _networksDisconnectMenu->addAction(action(NetworkDisconnectAll));
    action(NetworkDisconnectAllWithDropdown)->setMenu(_networksDisconnectMenu);
    action(NetworkDisconnectAllWithDropdown)->setEnabled(false);

    connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), SLOT(networkCreated(NetworkId)));
    connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), SLOT(networkRemoved(NetworkId)));

    updateStates();
}


ToolBarActionProvider::~ToolBarActionProvider()
{
}


void ToolBarActionProvider::disconnectedFromCore()
{
    _currentBuffer = QModelIndex();
    updateStates();
    NetworkModelController::disconnectedFromCore();
}


void ToolBarActionProvider::updateStates()
{
    action(BufferPart)->setEnabled(_currentBuffer.isValid()
        && _currentBuffer.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer
        && _currentBuffer.data(NetworkModel::ItemActiveRole).toBool());
}


void ToolBarActionProvider::addActions(QToolBar *bar, ToolBarType type)
{
    switch (type) {
    case MainToolBar:
        bar->addAction(action(NetworkConnectAllWithDropdown));
        bar->addAction(action(NetworkDisconnectAllWithDropdown));
        bar->addAction(action(JoinChannel));
        bar->addAction(action(BufferPart));
        break;
    case NickToolBar:
        bar->addAction(action(NickQuery));
        bar->addAction(action(NickWhois));
        bar->addSeparator();
        bar->addAction(action(NickOp));
        bar->addAction(action(NickDeop));
        bar->addAction(action(NickVoice));
        bar->addAction(action(NickDevoice));
        bar->addAction(action(NickKick));
        bar->addAction(action(NickBan));
        bar->addAction(action(NickKickBan));
        break;
    default:
        return;
    }
}


void ToolBarActionProvider::currentBufferChanged(const QModelIndex &index)
{
    _currentBuffer = index;
    updateStates();
}


void ToolBarActionProvider::nickSelectionChanged(const QModelIndexList &indexList)
{
    _selectedNicks = indexList;
    updateStates();
}


// override those to set indexes right
void ToolBarActionProvider::handleNetworkAction(ActionType type, QAction *action)
{
    setIndexList(_currentBuffer);
    NetworkModelController::handleNetworkAction(type, action);
}


void ToolBarActionProvider::handleBufferAction(ActionType type, QAction *action)
{
    setIndexList(_currentBuffer);
    NetworkModelController::handleBufferAction(type, action);
}


void ToolBarActionProvider::handleNickAction(ActionType type, QAction *action)
{
    setIndexList(_selectedNicks);
    NetworkModelController::handleNickAction(type, action);
}


void ToolBarActionProvider::handleGeneralAction(ActionType type, QAction *action)
{
    setIndexList(_currentBuffer);
    NetworkModelController::handleGeneralAction(type, action);
}


void ToolBarActionProvider::networkCreated(NetworkId id)
{
    const Network *net = Client::network(id);
    Action *act = new Action(net->networkName(), this);
    _networkActions[id] = act;
    act->setObjectName(QString("NetworkAction-%1").arg(id.toInt()));
    act->setData(QVariant::fromValue(id));
    connect(net, SIGNAL(updatedRemotely()), SLOT(networkUpdated()));
    connect(act, SIGNAL(triggered()), SLOT(connectOrDisconnectNet()));
    networkUpdated(net);
}


void ToolBarActionProvider::networkRemoved(NetworkId id)
{
    Action *act = _networkActions.take(id);
    if (act)
        act->deleteLater();
}


void ToolBarActionProvider::networkUpdated(const Network *net)
{
    if (!net)
        net = qobject_cast<const Network *>(sender());
    if (!net)
        return;
    Action *act = _networkActions.value(net->networkId());
    if (!act)
        return;

    _networksConnectMenu->removeAction(act);
    _networksDisconnectMenu->removeAction(act);

    QMenu *newMenu = net->connectionState() != Network::Disconnected ? _networksDisconnectMenu : _networksConnectMenu;
    act->setText(net->networkName());

    const int lastidx = newMenu->actions().count() - 2;
    QAction *beforeAction = newMenu->actions().at(lastidx);
    for (int i = 0; i < newMenu->actions().count() - 2; i++) {
        QAction *action = newMenu->actions().at(i);
        if (net->networkName().localeAwareCompare(action->text()) < 0) {
            beforeAction = action;
            break;
        }
    }
    newMenu->insertAction(beforeAction, act);

    action(NetworkConnectAllWithDropdown)->setEnabled(_networksConnectMenu->actions().count() > 2);
    action(NetworkDisconnectAllWithDropdown)->setEnabled(_networksDisconnectMenu->actions().count() > 2);
    action(JoinChannel)->setEnabled(_networksDisconnectMenu->actions().count() > 2);
}


void ToolBarActionProvider::connectOrDisconnectNet()
{
    QAction *act = qobject_cast<QAction *>(sender());
    if (!act)
        return;
    const Network *net = Client::network(act->data().value<NetworkId>());
    if (!net)
        return;

    if (net->connectionState() == Network::Disconnected) net->requestConnect();
    else net->requestDisconnect();
}


//void ToolBarActionProvider::