the nickselector should now react properly to any relevant changes (aka nickchanges...
[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
26 InputWidget::InputWidget(QWidget *parent)
27   : QWidget(parent),
28     validBuffer(false),
29     _bufferModel(0),
30     _selectionModel(0)
31 {
32   ui.setupUi(this);
33   connect(ui.inputEdit, SIGNAL(returnPressed()), this, SLOT(enterPressed()));
34   connect(ui.ownNick, SIGNAL(activated(QString)), this, SLOT(changeNick(QString)));
35   connect(this, SIGNAL(userInput(BufferInfo, QString)), Client::instance(), SIGNAL(sendInput(BufferInfo, QString)));
36   setFocusProxy(ui.inputEdit);
37 }
38
39 InputWidget::~InputWidget() {
40 }
41
42 void InputWidget::setModel(BufferModel *bufferModel) {
43   _bufferModel = bufferModel;
44 }
45
46 void InputWidget::setSelectionModel(QItemSelectionModel *selectionModel) {
47   if(_selectionModel) {
48     disconnect(_selectionModel, 0, this, 0);
49   }
50   _selectionModel = selectionModel;
51   connect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
52           this, SLOT(currentChanged(QModelIndex, QModelIndex)));
53 }
54
55 void InputWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
56   Q_UNUSED(previous);
57
58   validBuffer = current.isValid();
59
60   if(!validBuffer)
61     return;
62   
63   QVariant variant;
64   variant = current.data(NetworkModel::BufferInfoRole);
65   if(!variant.isValid())
66     return;
67
68   currentBufferInfo  = current.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
69   setNetwork(Client::networkModel()->networkByIndex(current));
70   updateNickSelector();
71   ui.inputEdit->setEnabled(current.data(NetworkModel::ItemActiveRole).value<bool>());
72 }
73
74 const Network *InputWidget::currentNetwork() const {
75   if(!validBuffer)
76     return 0;
77
78   return Client::network(_networkId);
79 }
80
81 void InputWidget::setNetwork(const Network *network) {
82   if(_networkId == network->networkId())
83     return;
84
85   const Network *previousNet = Client::network(_networkId);
86   if(previousNet)
87     disconnect(previousNet, 0, this, 0);
88
89   if(network) {
90     _networkId = network->networkId();
91     connect(network, SIGNAL(myNickSet(QString)),
92             this, SLOT(updateNickSelector()));
93     connect(network, SIGNAL(identitySet(IdentityId)),
94             this, SLOT(setIdentity(IdentityId)));
95   }
96   setIdentity(network->identity());
97 }
98
99 void InputWidget::setIdentity(const IdentityId &identityId) {
100   if(_identityId == identityId)
101     return;
102
103   const Identity *previousIdentity = Client::identity(_identityId);
104   if(previousIdentity)
105     disconnect(previousIdentity, 0, this, 0);
106
107   const Identity *identity = Client::identity(identityId);
108   if(identity) {
109     _identityId = identityId;
110     connect(identity, SIGNAL(nicksSet(QStringList)),
111             this, SLOT(updateNickSelector()));
112   }
113   updateNickSelector();
114 }
115
116 void InputWidget::updateNickSelector() const {
117   ui.ownNick->clear();
118
119   const Network *net = currentNetwork();
120   if(!net)
121     return;
122
123   const Identity *identity = Client::identity(net->identity());
124   if(!identity) {
125     qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId();
126     return;
127   }
128
129   int nickIdx;
130   QStringList nicks = identity->nicks();
131   if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
132     nicks.prepend(net->myNick());
133     nickIdx = 0;
134   }
135   
136   ui.ownNick->addItems(nicks);
137   ui.ownNick->setCurrentIndex(nickIdx);
138 }
139
140 void InputWidget::changeNick(const QString &newNick) const {
141   const Network *net = currentNetwork();
142   if(!net || net->isMyNick(newNick))
143     return;
144   emit userInput(currentBufferInfo, QString("/nick %1").arg(newNick));
145 }
146
147 void InputWidget::enterPressed() {
148   QStringList lines = ui.inputEdit->text().split('\n', QString::SkipEmptyParts);
149   foreach(QString msg, lines) {
150     if(msg.isEmpty()) continue;
151     emit userInput(currentBufferInfo, msg);
152   }
153   ui.inputEdit->clear();
154 }
155