fixes #580 - nick-dropdown is updated even if nickchange fails
[quassel.git] / src / qtui / inputwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "inputwidget.h"
22
23 #include "action.h"
24 #include "actioncollection.h"
25 #include "client.h"
26 #include "iconloader.h"
27 #include "ircuser.h"
28 #include "jumpkeyhandler.h"
29 #include "networkmodel.h"
30 #include "qtui.h"
31 #include "qtuisettings.h"
32
33 InputWidget::InputWidget(QWidget *parent)
34   : AbstractItemView(parent),
35     _networkId(0)
36 {
37   ui.setupUi(this);
38   connect(ui.inputEdit, SIGNAL(sendText(QString)), this, SLOT(sendText(QString)));
39   connect(ui.ownNick, SIGNAL(activated(QString)), this, SLOT(changeNick(QString)));
40   setFocusProxy(ui.inputEdit);
41
42   ui.ownNick->setSizeAdjustPolicy(QComboBox::AdjustToContents);
43   ui.ownNick->installEventFilter(new MouseWheelFilter(this));
44   ui.inputEdit->installEventFilter(new JumpKeyHandler(this));
45
46   QtUiStyleSettings s("Fonts");
47   s.notify("InputLine", this, SLOT(setCustomFont(QVariant)));
48   setCustomFont(s.value("InputLine", QFont()));
49
50   ActionCollection *coll = QtUi::actionCollection();
51
52   Action *activateInputline = coll->add<Action>("FocusInputLine");
53   connect(activateInputline, SIGNAL(triggered()), SLOT(setFocus()));
54   activateInputline->setText(tr("Focus Input Line"));
55   activateInputline->setShortcut(tr("Ctrl+L"));
56 }
57
58 InputWidget::~InputWidget() {
59 }
60
61 void InputWidget::setCustomFont(const QVariant &v) {
62   QFont font = v.value<QFont>();
63   if(font.family().isEmpty())
64     font = QApplication::font();
65   ui.inputEdit->setFont(font);
66 }
67
68 void InputWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
69   Q_UNUSED(previous)
70   NetworkId networkId = current.data(NetworkModel::NetworkIdRole).value<NetworkId>();
71   if(networkId == _networkId)
72     return;
73
74   setNetwork(networkId);
75   updateNickSelector();
76   updateEnabledState();
77 }
78
79 void InputWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
80   QItemSelectionRange changedArea(topLeft, bottomRight);
81   if(changedArea.contains(selectionModel()->currentIndex())) {
82     updateEnabledState();
83   }
84 };
85
86 void InputWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
87   NetworkId networkId;
88   QModelIndex child;
89   for(int row = start; row <= end; row++) {
90     child = model()->index(row, 0, parent);
91     if(NetworkModel::NetworkItemType != child.data(NetworkModel::ItemTypeRole).toInt())
92       continue;
93     networkId = child.data(NetworkModel::NetworkIdRole).value<NetworkId>();
94     if(networkId == _networkId) {
95       setNetwork(0);
96       updateNickSelector();
97       return;
98     }
99   }
100 }
101
102 void InputWidget::updateEnabledState() {
103   QModelIndex currentIndex = selectionModel()->currentIndex();
104   
105   const Network *net = Client::networkModel()->networkByIndex(currentIndex);
106   bool enabled = false;
107   if(net) {
108     // disable inputline if it's a channelbuffer we parted from or...
109     enabled = (currentIndex.data(NetworkModel::ItemActiveRole).value<bool>() || (currentIndex.data(NetworkModel::BufferTypeRole).toInt() != BufferInfo::ChannelBuffer));
110     // ... if we're not connected to the network at all
111     enabled &= net->isConnected();
112   }
113   ui.inputEdit->setEnabled(enabled);
114 }
115
116 const Network *InputWidget::currentNetwork() const {
117   return Client::network(_networkId);
118 }
119
120 BufferInfo InputWidget::currentBufferInfo() const {
121   return selectionModel()->currentIndex().data(NetworkModel::BufferInfoRole).value<BufferInfo>();
122 };
123
124 void InputWidget::setNetwork(NetworkId networkId) {
125   if(_networkId == networkId)
126     return;
127
128   const Network *previousNet = Client::network(_networkId);
129   if(previousNet) {
130     disconnect(previousNet, 0, this, 0);
131     if(previousNet->me())
132       disconnect(previousNet->me(), 0, this, 0);
133   }
134
135   _networkId = networkId;
136
137   const Network *network = Client::network(networkId);
138   if(network) {
139     connect(network, SIGNAL(identitySet(IdentityId)), this, SLOT(setIdentity(IdentityId)));
140     connectMyIrcUser();
141     setIdentity(network->identity());
142   } else {
143     setIdentity(0);
144     _networkId = 0;
145   }
146 }
147
148 void InputWidget::connectMyIrcUser() {
149   const Network *network = currentNetwork();
150   if(network->me()) {
151     connect(network->me(), SIGNAL(nickSet(const QString &)), this, SLOT(updateNickSelector()));
152     connect(network->me(), SIGNAL(userModesSet(QString)), this, SLOT(updateNickSelector()));
153     connect(network->me(), SIGNAL(userModesAdded(QString)), this, SLOT(updateNickSelector()));
154     connect(network->me(), SIGNAL(userModesRemoved(QString)), this, SLOT(updateNickSelector()));
155     connect(network->me(), SIGNAL(awaySet(bool)), this, SLOT(updateNickSelector()));
156     disconnect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
157     updateNickSelector();
158   } else {
159     connect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
160   }
161 }
162
163 void InputWidget::setIdentity(IdentityId identityId) {
164   if(_identityId == identityId)
165     return;
166
167   const Identity *previousIdentity = Client::identity(_identityId);
168   if(previousIdentity)
169     disconnect(previousIdentity, 0, this, 0);
170
171   _identityId = identityId;
172
173   const Identity *identity = Client::identity(identityId);
174   if(identity) {
175     connect(identity, SIGNAL(nicksSet(QStringList)), this, SLOT(updateNickSelector()));
176   } else {
177     _identityId = 0;
178   }
179   updateNickSelector();
180 }
181
182 void InputWidget::updateNickSelector() const {
183   ui.ownNick->clear();
184
185   const Network *net = currentNetwork();
186   if(!net)
187     return;
188
189   const Identity *identity = Client::identity(net->identity());
190   if(!identity) {
191     qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId() << "IdentityId:" << net->identity();
192     return;
193   }
194
195   int nickIdx;
196   QStringList nicks = identity->nicks();
197   if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
198     nicks.prepend(net->myNick());
199     nickIdx = 0;
200   }
201
202   if(nicks.isEmpty())
203     return;
204
205   IrcUser *me = net->me();
206   if(me) {
207     nicks[nickIdx] = net->myNick();
208     if(!me->userModes().isEmpty())
209       nicks[nickIdx] += QString(" (+%1)").arg(me->userModes());
210   }
211       
212   ui.ownNick->addItems(nicks);
213
214   if(me && me->isAway())
215     ui.ownNick->setItemData(nickIdx, SmallIcon("user-away"), Qt::DecorationRole);
216
217   ui.ownNick->setCurrentIndex(nickIdx);
218 }
219
220 void InputWidget::changeNick(const QString &newNick) const {
221   const Network *net = currentNetwork();
222   if(!net || net->isMyNick(newNick))
223     return;
224
225   // we reset the nick selecter as we have no confirmation yet, that this will succeed.
226   // if the action succeeds it will be properly updated anyways.
227   updateNickSelector();
228   Client::userInput(BufferInfo::fakeStatusBuffer(net->networkId()), QString("/NICK %1").arg(newNick));
229 }
230
231 void InputWidget::sendText(const QString &text) const {
232   Client::userInput(currentBufferInfo(), text);
233 }
234
235
236 // MOUSE WHEEL FILTER
237 MouseWheelFilter::MouseWheelFilter(QObject *parent)
238   : QObject(parent)
239 {
240 }
241
242 bool MouseWheelFilter::eventFilter(QObject *obj, QEvent *event) {
243   if(event->type() != QEvent::Wheel)
244     return QObject::eventFilter(obj, event);
245   else
246     return true;
247 }