added missing queries
[quassel.git] / src / qtui / inputwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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 "client.h"
24 #include "networkmodel.h"
25 #include "jumpkeyhandler.h"
26
27 InputWidget::InputWidget(QWidget *parent)
28   : QWidget(parent),
29     validBuffer(false),
30     _bufferModel(0),
31     _selectionModel(0)
32 {
33   ui.setupUi(this);
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);
38
39   ui.inputEdit->installEventFilter(new JumpKeyHandler(this));  
40 }
41
42 InputWidget::~InputWidget() {
43 }
44
45 void InputWidget::setModel(BufferModel *bufferModel) {
46   _bufferModel = bufferModel;
47 }
48
49 void InputWidget::setSelectionModel(QItemSelectionModel *selectionModel) {
50   if(_selectionModel) {
51     disconnect(_selectionModel, 0, this, 0);
52   }
53   _selectionModel = selectionModel;
54   connect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
55           this, SLOT(currentChanged(QModelIndex, QModelIndex)));
56 }
57
58 void InputWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
59   Q_UNUSED(previous);
60
61   validBuffer = current.isValid();
62
63   if(!validBuffer)
64     return;
65   
66   QVariant variant;
67   variant = current.data(NetworkModel::BufferInfoRole);
68   if(!variant.isValid())
69     return;
70
71   currentBufferInfo  = current.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
72   setNetwork(Client::networkModel()->networkByIndex(current));
73   updateNickSelector();
74   ui.inputEdit->setEnabled(current.data(NetworkModel::ItemActiveRole).value<bool>());
75 }
76
77 const Network *InputWidget::currentNetwork() const {
78   if(!validBuffer)
79     return 0;
80
81   return Client::network(_networkId);
82 }
83
84 void InputWidget::setNetwork(const Network *network) {
85   if(_networkId == network->networkId())
86     return;
87
88   const Network *previousNet = Client::network(_networkId);
89   if(previousNet)
90     disconnect(previousNet, 0, this, 0);
91
92   if(network) {
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)));
98   }
99   setIdentity(network->identity());
100 }
101
102 void InputWidget::setIdentity(const IdentityId &identityId) {
103   if(_identityId == identityId)
104     return;
105
106   const Identity *previousIdentity = Client::identity(_identityId);
107   if(previousIdentity)
108     disconnect(previousIdentity, 0, this, 0);
109
110   const Identity *identity = Client::identity(identityId);
111   if(identity) {
112     _identityId = identityId;
113     connect(identity, SIGNAL(nicksSet(QStringList)),
114             this, SLOT(updateNickSelector()));
115   }
116   updateNickSelector();
117 }
118
119 void InputWidget::updateNickSelector() const {
120   ui.ownNick->clear();
121
122   const Network *net = currentNetwork();
123   if(!net)
124     return;
125
126   const Identity *identity = Client::identity(net->identity());
127   if(!identity) {
128     qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId();
129     return;
130   }
131
132   int nickIdx;
133   QStringList nicks = identity->nicks();
134   if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
135     nicks.prepend(net->myNick());
136     nickIdx = 0;
137   }
138   
139   ui.ownNick->addItems(nicks);
140   ui.ownNick->setCurrentIndex(nickIdx);
141 }
142
143 void InputWidget::changeNick(const QString &newNick) const {
144   const Network *net = currentNetwork();
145   if(!net || net->isMyNick(newNick))
146     return;
147   emit userInput(currentBufferInfo, QString("/nick %1").arg(newNick));
148 }
149
150 void InputWidget::sendText(QString text) {
151   emit userInput(currentBufferInfo, text);
152 }