using configdir to store crash logs instead of cwd
[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   connect(this, SIGNAL(userInput(BufferInfo, QString)), Client::instance(), SIGNAL(sendInput(BufferInfo, QString)));
41   setFocusProxy(ui.inputEdit);
42
43   ui.ownNick->setSizeAdjustPolicy(QComboBox::AdjustToContents);
44   ui.ownNick->installEventFilter(new MouseWheelFilter(this));
45   ui.inputEdit->installEventFilter(new JumpKeyHandler(this));
46
47   QtUiSettings s;
48   bool useInputLineFont = s.value("UseInputLineFont", QVariant(false)).toBool();
49   if(useInputLineFont) {
50     ui.inputEdit->setFont(s.value("InputLineFont").value<QFont>());
51   }
52
53   ActionCollection *coll = QtUi::actionCollection();
54
55   Action *activateInputline = coll->add<Action>("FocusInputLine");
56   connect(activateInputline, SIGNAL(triggered()), SLOT(setFocus()));
57   activateInputline->setText(tr("Focus Input Line"));
58   activateInputline->setShortcut(tr("Ctrl+L"));
59 }
60
61 InputWidget::~InputWidget() {
62 }
63
64 void InputWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
65   Q_UNUSED(previous)
66   NetworkId networkId = current.data(NetworkModel::NetworkIdRole).value<NetworkId>();
67   if(networkId == _networkId)
68     return;
69
70   setNetwork(networkId);
71   updateNickSelector();
72   updateEnabledState();
73 }
74
75 void InputWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
76   QItemSelectionRange changedArea(topLeft, bottomRight);
77   if(changedArea.contains(selectionModel()->currentIndex())) {
78     updateEnabledState();
79   }
80 };
81
82 void InputWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
83   NetworkId networkId;
84   QModelIndex child;
85   for(int row = start; row <= end; row++) {
86     child = model()->index(row, 0, parent);
87     if(NetworkModel::NetworkItemType != child.data(NetworkModel::ItemTypeRole).toInt())
88       continue;
89     networkId = child.data(NetworkModel::NetworkIdRole).value<NetworkId>();
90     if(networkId == _networkId) {
91       setNetwork(0);
92       updateNickSelector();
93       return;
94     }
95   }
96 }
97
98 void InputWidget::updateEnabledState() {
99   QModelIndex currentIndex = selectionModel()->currentIndex();
100   
101   const Network *net = Client::networkModel()->networkByIndex(currentIndex);
102   bool enabled = false;
103   if(net) {
104     // disable inputline if it's a channelbuffer we parted from or...
105     enabled = (currentIndex.data(NetworkModel::ItemActiveRole).value<bool>() || (currentIndex.data(NetworkModel::BufferTypeRole).toInt() != BufferInfo::ChannelBuffer));
106     // ... if we're not connected to the network at all
107     enabled &= net->isConnected();
108   }
109   ui.inputEdit->setEnabled(enabled);
110 }
111
112 const Network *InputWidget::currentNetwork() const {
113   return Client::network(_networkId);
114 }
115
116 BufferInfo InputWidget::currentBufferInfo() const {
117   return selectionModel()->currentIndex().data(NetworkModel::BufferInfoRole).value<BufferInfo>();
118 };
119
120 void InputWidget::setNetwork(NetworkId networkId) {
121   if(_networkId == networkId)
122     return;
123
124   const Network *previousNet = Client::network(_networkId);
125   if(previousNet) {
126     disconnect(previousNet, 0, this, 0);
127     if(previousNet->me())
128       disconnect(previousNet->me(), 0, this, 0);
129   }
130
131   _networkId = networkId;
132
133   const Network *network = Client::network(networkId);
134   if(network) {
135     connect(network, SIGNAL(identitySet(IdentityId)), this, SLOT(setIdentity(IdentityId)));
136     connectMyIrcUser();
137     setIdentity(network->identity());
138   } else {
139     setIdentity(0);
140     _networkId = 0;
141   }
142 }
143
144 void InputWidget::connectMyIrcUser() {
145   const Network *network = currentNetwork();
146   if(network->me()) {
147     connect(network->me(), SIGNAL(nickSet(const QString &)), this, SLOT(updateNickSelector()));
148     connect(network->me(), SIGNAL(userModesSet(QString)), this, SLOT(updateNickSelector()));
149     connect(network->me(), SIGNAL(userModesAdded(QString)), this, SLOT(updateNickSelector()));
150     connect(network->me(), SIGNAL(userModesRemoved(QString)), this, SLOT(updateNickSelector()));
151     connect(network->me(), SIGNAL(awaySet(bool)), this, SLOT(updateNickSelector()));
152     disconnect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
153   } else {
154     connect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
155   }
156 }
157
158 void InputWidget::setIdentity(IdentityId identityId) {
159   if(_identityId == identityId)
160     return;
161
162   const Identity *previousIdentity = Client::identity(_identityId);
163   if(previousIdentity)
164     disconnect(previousIdentity, 0, this, 0);
165
166   _identityId = identityId;
167
168   const Identity *identity = Client::identity(identityId);
169   if(identity) {
170     connect(identity, SIGNAL(nicksSet(QStringList)),
171             this, SLOT(updateNickSelector()));
172   }
173   updateNickSelector();
174 }
175
176 void InputWidget::updateNickSelector() const {
177   ui.ownNick->clear();
178
179   const Network *net = currentNetwork();
180   if(!net)
181     return;
182
183   const Identity *identity = Client::identity(net->identity());
184   if(!identity) {
185     qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId();
186     return;
187   }
188
189   int nickIdx;
190   QStringList nicks = identity->nicks();
191   if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
192     nicks.prepend(net->myNick());
193     nickIdx = 0;
194   }
195
196   if(nicks.isEmpty())
197     return;
198
199   IrcUser *me = net->me();
200   if(me)
201     nicks[nickIdx] = net->myNick() + QString(" (+%1)").arg(me->userModes());
202       
203   ui.ownNick->addItems(nicks);
204
205   if(me && me->isAway())
206     ui.ownNick->setItemData(nickIdx, SmallIcon("user-away"), Qt::DecorationRole);
207
208   ui.ownNick->setCurrentIndex(nickIdx);
209 }
210
211 void InputWidget::changeNick(const QString &newNick) const {
212   const Network *net = currentNetwork();
213   if(!net || net->isMyNick(newNick))
214     return;
215   emit userInput(currentBufferInfo(), QString("/nick %1").arg(newNick));
216 }
217
218 void InputWidget::sendText(QString text) {
219   emit userInput(currentBufferInfo(), text);
220 }
221
222
223 // MOUSE WHEEL FILTER
224 MouseWheelFilter::MouseWheelFilter(QObject *parent)
225   : QObject(parent)
226 {
227 }
228
229 bool MouseWheelFilter::eventFilter(QObject *obj, QEvent *event) {
230   if(event->type() != QEvent::Wheel)
231     return QObject::eventFilter(obj, event);
232   else
233     return true;
234 }