af6d25a095abcce00f6b8771fa4f95496d0f874d
[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(), SLOT(userInput(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
74 const Network *InputWidget::currentNetwork() const {
75   if(!validBuffer)
76     return 0;
77
78   return Client::network(currentBufferInfo.networkId());
79 }
80
81 void InputWidget::updateNickSelector() const {
82   const Network *net = currentNetwork();
83   if(!net)
84     return;
85
86   const Identity *identity = Client::identity(net->identity());
87   if(!identity) {
88     qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId();
89     return;
90   }
91
92   int nickIdx;
93   QStringList nicks = identity->nicks();
94   if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
95     nicks.prepend(net->myNick());
96     nickIdx = 0;
97   }
98   
99   ui.ownNick->clear();
100   ui.ownNick->addItems(nicks);
101   ui.ownNick->setCurrentIndex(nickIdx);
102 }
103
104 void InputWidget::changeNick(const QString &newNick) const {
105   const Network *net = currentNetwork();
106   if(!net || net->isMyNick(newNick))
107     return;
108   emit userInput(currentBufferInfo, QString("/nick %1").arg(newNick));
109 }
110
111 void InputWidget::enterPressed() {
112   QStringList lines = ui.inputEdit->text().split('\n', QString::SkipEmptyParts);
113   foreach(QString msg, lines) {
114     if(msg.isEmpty()) continue;
115     emit userInput(currentBufferInfo, msg);
116   }
117   ui.inputEdit->clear();
118 }
119