enabled query in user context menu - now that the query command is working (thanks...
[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 "network.h"
26 #include "identity.h"
27
28 InputWidget::InputWidget(QWidget *parent)
29   : QWidget(parent),
30     validBuffer(false),
31     _bufferModel(0),
32     _selectionModel(0)
33 {
34   ui.setupUi(this);
35   connect(ui.inputEdit, SIGNAL(returnPressed()), this, SLOT(enterPressed()));
36   connect(ui.ownNick, SIGNAL(activated(QString)), this, SLOT(changeNick(QString)));
37   connect(this, SIGNAL(userInput(BufferInfo, QString)), Client::instance(), SIGNAL(sendInput(BufferInfo, QString)));
38   setFocusProxy(ui.inputEdit);
39 }
40
41 InputWidget::~InputWidget() {
42 }
43
44 void InputWidget::setModel(BufferModel *bufferModel) {
45   _bufferModel = bufferModel;
46 }
47
48 void InputWidget::setSelectionModel(QItemSelectionModel *selectionModel) {
49   if(_selectionModel) {
50     disconnect(_selectionModel, 0, this, 0);
51   }
52   _selectionModel = selectionModel;
53   connect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
54           this, SLOT(currentChanged(QModelIndex, QModelIndex)));
55 }
56
57 void InputWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
58   Q_UNUSED(previous);
59
60   validBuffer = current.isValid();
61
62   if(!validBuffer)
63     return;
64   
65   QVariant variant;
66   variant = current.data(NetworkModel::BufferInfoRole);
67   if(!variant.isValid())
68     return;
69
70   currentBufferInfo  = current.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
71   updateNickSelector();
72
73   ui.inputEdit->setEnabled(current.data(NetworkModel::ItemActiveRole).value<bool>());
74 }
75
76 const Network *InputWidget::currentNetwork() const {
77   if(!validBuffer)
78     return 0;
79
80   return Client::network(currentBufferInfo.networkId());
81 }
82
83 void InputWidget::updateNickSelector() const {
84   const Network *net = currentNetwork();
85   if(!net)
86     return;
87
88   const Identity *identity = Client::identity(net->identity());
89   if(!identity) {
90     qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId();
91     return;
92   }
93
94   int nickIdx;
95   QStringList nicks = identity->nicks();
96   if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
97     nicks.prepend(net->myNick());
98     nickIdx = 0;
99   }
100   
101   ui.ownNick->clear();
102   ui.ownNick->addItems(nicks);
103   ui.ownNick->setCurrentIndex(nickIdx);
104 }
105
106 void InputWidget::changeNick(const QString &newNick) const {
107   const Network *net = currentNetwork();
108   if(!net || net->isMyNick(newNick))
109     return;
110   emit userInput(currentBufferInfo, QString("/nick %1").arg(newNick));
111 }
112
113 void InputWidget::enterPressed() {
114   QStringList lines = ui.inputEdit->text().split('\n', QString::SkipEmptyParts);
115   foreach(QString msg, lines) {
116     if(msg.isEmpty()) continue;
117     emit userInput(currentBufferInfo, msg);
118   }
119   ui.inputEdit->clear();
120 }
121