1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "inputwidget.h"
24 #include "networkmodel.h"
25 #include "jumpkeyhandler.h"
27 InputWidget::InputWidget(QWidget *parent)
34 connect(ui.inputEdit, SIGNAL(sendText(QString)), this, SLOT(sendText(QString)));
35 connect(ui.ownNick, SIGNAL(activated(QString)), this, SLOT(changeNick(QString)));
36 connect(this, SIGNAL(userInput(BufferInfo, QString)), Client::instance(), SIGNAL(sendInput(BufferInfo, QString)));
37 setFocusProxy(ui.inputEdit);
39 ui.inputEdit->installEventFilter(new JumpKeyHandler(this));
42 InputWidget::~InputWidget() {
45 void InputWidget::setModel(BufferModel *bufferModel) {
46 _bufferModel = bufferModel;
49 void InputWidget::setSelectionModel(QItemSelectionModel *selectionModel) {
51 disconnect(_selectionModel, 0, this, 0);
53 _selectionModel = selectionModel;
54 connect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
55 this, SLOT(currentChanged(QModelIndex, QModelIndex)));
58 void InputWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
61 validBuffer = current.isValid();
67 variant = current.data(NetworkModel::BufferInfoRole);
68 if(!variant.isValid())
71 currentBufferInfo = current.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
72 setNetwork(Client::networkModel()->networkByIndex(current));
74 ui.inputEdit->setEnabled(current.data(NetworkModel::ItemActiveRole).value<bool>());
77 const Network *InputWidget::currentNetwork() const {
81 return Client::network(_networkId);
84 void InputWidget::setNetwork(const Network *network) {
85 if(_networkId == network->networkId())
88 const Network *previousNet = Client::network(_networkId);
90 disconnect(previousNet, 0, this, 0);
93 _networkId = network->networkId();
94 connect(network, SIGNAL(myNickSet(QString)),
95 this, SLOT(updateNickSelector()));
96 connect(network, SIGNAL(identitySet(IdentityId)),
97 this, SLOT(setIdentity(IdentityId)));
99 setIdentity(network->identity());
102 void InputWidget::setIdentity(const IdentityId &identityId) {
103 if(_identityId == identityId)
106 const Identity *previousIdentity = Client::identity(_identityId);
108 disconnect(previousIdentity, 0, this, 0);
110 const Identity *identity = Client::identity(identityId);
112 _identityId = identityId;
113 connect(identity, SIGNAL(nicksSet(QStringList)),
114 this, SLOT(updateNickSelector()));
116 updateNickSelector();
119 void InputWidget::updateNickSelector() const {
122 const Network *net = currentNetwork();
126 const Identity *identity = Client::identity(net->identity());
128 qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId();
133 QStringList nicks = identity->nicks();
134 if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
135 nicks.prepend(net->myNick());
139 ui.ownNick->addItems(nicks);
140 ui.ownNick->setCurrentIndex(nickIdx);
143 void InputWidget::changeNick(const QString &newNick) const {
144 const Network *net = currentNetwork();
145 if(!net || net->isMyNick(newNick))
147 emit userInput(currentBufferInfo, QString("/nick %1").arg(newNick));
150 void InputWidget::sendText(QString text) {
151 emit userInput(currentBufferInfo, text);