0f4a7ee42d081ab55f91e630c4a7141e9da822f6
[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   if(_bufferModel) {
47     disconnect(_bufferModel, 0, this, 0);
48   }
49   _bufferModel = bufferModel;
50   connect(bufferModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
51           this, SLOT(dataChanged(QModelIndex, QModelIndex)));
52 }
53
54 void InputWidget::setSelectionModel(QItemSelectionModel *selectionModel) {
55   if(_selectionModel) {
56     disconnect(_selectionModel, 0, this, 0);
57   }
58   _selectionModel = selectionModel;
59   connect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
60           this, SLOT(currentChanged(QModelIndex, QModelIndex)));
61 }
62
63 void InputWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
64   Q_UNUSED(previous);
65
66   validBuffer = current.isValid();
67
68   if(!validBuffer)
69     return;
70   
71   QVariant variant;
72   variant = current.data(NetworkModel::BufferInfoRole);
73   if(!variant.isValid())
74     return;
75
76   currentBufferInfo  = current.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
77   setNetwork(Client::networkModel()->networkByIndex(current));
78   updateNickSelector();
79   ui.inputEdit->setEnabled(current.data(NetworkModel::ItemActiveRole).value<bool>());
80 }
81
82 void InputWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
83   QItemSelectionRange changedArea(topLeft, bottomRight);
84   QModelIndex currentIndex = Client::bufferModel()->currentIndex();
85   if(changedArea.contains(currentIndex)) {
86     ui.inputEdit->setEnabled(currentIndex.data(NetworkModel::ItemActiveRole).value<bool>());
87   }
88 };
89
90
91 const Network *InputWidget::currentNetwork() const {
92   if(!validBuffer)
93     return 0;
94
95   return Client::network(_networkId);
96 }
97
98 void InputWidget::setNetwork(const Network *network) {
99   if(_networkId == network->networkId())
100     return;
101
102   const Network *previousNet = Client::network(_networkId);
103   if(previousNet)
104     disconnect(previousNet, 0, this, 0);
105
106   if(network) {
107     _networkId = network->networkId();
108     connect(network, SIGNAL(myNickSet(QString)),
109             this, SLOT(updateNickSelector()));
110     connect(network, SIGNAL(identitySet(IdentityId)),
111             this, SLOT(setIdentity(IdentityId)));
112   }
113   setIdentity(network->identity());
114 }
115
116 void InputWidget::setIdentity(const IdentityId &identityId) {
117   if(_identityId == identityId)
118     return;
119
120   const Identity *previousIdentity = Client::identity(_identityId);
121   if(previousIdentity)
122     disconnect(previousIdentity, 0, this, 0);
123
124   const Identity *identity = Client::identity(identityId);
125   if(identity) {
126     _identityId = identityId;
127     connect(identity, SIGNAL(nicksSet(QStringList)),
128             this, SLOT(updateNickSelector()));
129   }
130   updateNickSelector();
131 }
132
133 void InputWidget::updateNickSelector() const {
134   ui.ownNick->clear();
135
136   const Network *net = currentNetwork();
137   if(!net)
138     return;
139
140   const Identity *identity = Client::identity(net->identity());
141   if(!identity) {
142     qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId();
143     return;
144   }
145
146   int nickIdx;
147   QStringList nicks = identity->nicks();
148   if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
149     nicks.prepend(net->myNick());
150     nickIdx = 0;
151   }
152   
153   ui.ownNick->addItems(nicks);
154   ui.ownNick->setCurrentIndex(nickIdx);
155 }
156
157 void InputWidget::changeNick(const QString &newNick) const {
158   const Network *net = currentNetwork();
159   if(!net || net->isMyNick(newNick))
160     return;
161   emit userInput(currentBufferInfo, QString("/nick %1").arg(newNick));
162 }
163
164 void InputWidget::sendText(QString text) {
165   emit userInput(currentBufferInfo, text);
166 }