Sync last message id per buffer
[quassel.git] / src / uisupport / toolbaractionprovider.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 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 "toolbaractionprovider.h"
22
23 #include <QMenu>
24 #include <QToolBar>
25
26 #include "icon.h"
27
28 ToolBarActionProvider::ToolBarActionProvider(QObject* parent)
29     : NetworkModelController(parent)
30 {
31     registerAction(NetworkConnectAllWithDropdown, icon::get("network-connect"), tr("Connect"))->setToolTip(tr("Connect to IRC"));
32     registerAction(NetworkDisconnectAllWithDropdown, icon::get("network-disconnect"), tr("Disconnect"))->setToolTip(tr("Disconnect from IRC"));
33     registerAction(NetworkConnectAll, tr("Connect to all"));
34     registerAction(NetworkDisconnectAll, tr("Disconnect from all"));
35
36     registerAction(BufferPart, icon::get("irc-close-channel"), tr("Part"))->setToolTip(tr("Leave currently selected channel"));
37     registerAction(JoinChannel, icon::get("irc-join-channel"), tr("Join"))->setToolTip(tr("Join a channel"));
38
39     registerAction(NickQuery, icon::get("mail-message-new"), tr("Query"))->setToolTip(tr("Start a private conversation"));  // fix icon
40     registerAction(NickWhois, icon::get("im-user"), tr("Whois"))->setToolTip(tr("Request user information"));               // fix icon
41
42     registerAction(NickOp, icon::get("irc-operator"), tr("Op"))->setToolTip(tr("Give operator privileges to user"));
43     registerAction(NickDeop, icon::get("irc-remove-operator"), tr("Deop"))->setToolTip(tr("Take operator privileges from user"));
44     registerAction(NickVoice, icon::get("irc-voice"), tr("Voice"))->setToolTip(tr("Give voice to user"));
45     registerAction(NickDevoice, icon::get("irc-unvoice"), tr("Devoice"))->setToolTip(tr("Take voice from user"));
46     registerAction(NickKick, icon::get("im-kick-user"), tr("Kick"))->setToolTip(tr("Remove user from channel"));
47     registerAction(NickBan, icon::get("im-ban-user"), tr("Ban"))->setToolTip(tr("Ban user from channel"));
48     registerAction(NickKickBan, icon::get("im-ban-kick-user"), tr("Kick/Ban"))->setToolTip(tr("Remove and ban user from channel"));
49
50     _networksConnectMenu = new QMenu();
51     _networksConnectMenu->setSeparatorsCollapsible(false);
52     _networksConnectMenu->addSeparator();
53     _networksConnectMenu->addAction(action(NetworkConnectAll));
54     action(NetworkConnectAllWithDropdown)->setMenu(_networksConnectMenu);
55     action(NetworkConnectAllWithDropdown)->setEnabled(false);
56
57     _networksDisconnectMenu = new QMenu();
58     _networksDisconnectMenu->setSeparatorsCollapsible(false);
59     _networksDisconnectMenu->addSeparator();
60     _networksDisconnectMenu->addAction(action(NetworkDisconnectAll));
61     action(NetworkDisconnectAllWithDropdown)->setMenu(_networksDisconnectMenu);
62     action(NetworkDisconnectAllWithDropdown)->setEnabled(false);
63
64     connect(Client::instance(), &Client::networkCreated, this, &ToolBarActionProvider::networkCreated);
65     connect(Client::instance(), &Client::networkRemoved, this, &ToolBarActionProvider::networkRemoved);
66
67     updateStates();
68 }
69
70 void ToolBarActionProvider::disconnectedFromCore()
71 {
72     _currentBuffer = QModelIndex();
73     updateStates();
74     NetworkModelController::disconnectedFromCore();
75 }
76
77 void ToolBarActionProvider::updateStates()
78 {
79     action(BufferPart)
80         ->setEnabled(_currentBuffer.isValid() && _currentBuffer.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer
81                      && _currentBuffer.data(NetworkModel::ItemActiveRole).toBool());
82 }
83
84 void ToolBarActionProvider::addActions(QToolBar* bar, ToolBarType type)
85 {
86     switch (type) {
87     case MainToolBar:
88         bar->addAction(action(NetworkConnectAllWithDropdown));
89         bar->addAction(action(NetworkDisconnectAllWithDropdown));
90         bar->addAction(action(JoinChannel));
91         bar->addAction(action(BufferPart));
92         break;
93     case NickToolBar:
94         bar->addAction(action(NickQuery));
95         bar->addAction(action(NickWhois));
96         bar->addSeparator();
97         bar->addAction(action(NickOp));
98         bar->addAction(action(NickDeop));
99         bar->addAction(action(NickVoice));
100         bar->addAction(action(NickDevoice));
101         bar->addAction(action(NickKick));
102         bar->addAction(action(NickBan));
103         bar->addAction(action(NickKickBan));
104         break;
105     default:
106         return;
107     }
108 }
109
110 void ToolBarActionProvider::onCurrentBufferChanged(const QModelIndex& index)
111 {
112     _currentBuffer = index;
113     updateStates();
114 }
115
116 void ToolBarActionProvider::onNickSelectionChanged(const QModelIndexList& indexList)
117 {
118     _selectedNicks = indexList;
119     updateStates();
120 }
121
122 // override those to set indexes right
123 void ToolBarActionProvider::handleNetworkAction(ActionType type, QAction* action)
124 {
125     setIndexList(_currentBuffer);
126     NetworkModelController::handleNetworkAction(type, action);
127 }
128
129 void ToolBarActionProvider::handleBufferAction(ActionType type, QAction* action)
130 {
131     setIndexList(_currentBuffer);
132     NetworkModelController::handleBufferAction(type, action);
133 }
134
135 void ToolBarActionProvider::handleNickAction(ActionType type, QAction* action)
136 {
137     setIndexList(_selectedNicks);
138     NetworkModelController::handleNickAction(type, action);
139 }
140
141 void ToolBarActionProvider::handleGeneralAction(ActionType type, QAction* action)
142 {
143     setIndexList(_currentBuffer);
144     NetworkModelController::handleGeneralAction(type, action);
145 }
146
147 void ToolBarActionProvider::networkCreated(NetworkId id)
148 {
149     const Network* net = Client::network(id);
150     Action* act = new Action(net->networkName(), this);
151     _networkActions[id] = act;
152     act->setObjectName(QString("NetworkAction-%1").arg(id.toInt()));
153     act->setData(QVariant::fromValue(id));
154     connect(net, &Network::updatedRemotely, this, [this]() { networkUpdated(); });
155     connect(act, &QAction::triggered, this, &ToolBarActionProvider::connectOrDisconnectNet);
156     networkUpdated(net);
157 }
158
159 void ToolBarActionProvider::networkRemoved(NetworkId id)
160 {
161     Action* act = _networkActions.take(id);
162     if (act)
163         act->deleteLater();
164 }
165
166 void ToolBarActionProvider::networkUpdated(const Network* net)
167 {
168     if (!net)
169         net = qobject_cast<const Network*>(sender());
170     if (!net)
171         return;
172     Action* act = _networkActions.value(net->networkId());
173     if (!act)
174         return;
175
176     _networksConnectMenu->removeAction(act);
177     _networksDisconnectMenu->removeAction(act);
178
179     QMenu* newMenu = net->connectionState() != Network::Disconnected ? _networksDisconnectMenu : _networksConnectMenu;
180     act->setText(net->networkName());
181
182     const int lastidx = newMenu->actions().count() - 2;
183     QAction* beforeAction = newMenu->actions().at(lastidx);
184     for (int i = 0; i < newMenu->actions().count() - 2; i++) {
185         QAction* action = newMenu->actions().at(i);
186         if (net->networkName().localeAwareCompare(action->text()) < 0) {
187             beforeAction = action;
188             break;
189         }
190     }
191     newMenu->insertAction(beforeAction, act);
192
193     action(NetworkConnectAllWithDropdown)->setEnabled(_networksConnectMenu->actions().count() > 2);
194     action(NetworkDisconnectAllWithDropdown)->setEnabled(_networksDisconnectMenu->actions().count() > 2);
195     action(JoinChannel)->setEnabled(_networksDisconnectMenu->actions().count() > 2);
196 }
197
198 void ToolBarActionProvider::connectOrDisconnectNet()
199 {
200     auto* act = qobject_cast<QAction*>(sender());
201     if (!act)
202         return;
203     const Network* net = Client::network(act->data().value<NetworkId>());
204     if (!net)
205         return;
206
207     if (net->connectionState() == Network::Disconnected)
208         net->requestConnect();
209     else
210         net->requestDisconnect();
211 }
212
213 // void ToolBarActionProvider::