63c1ed09ba681e5d3e0fde01e9a66e0a9571e49e
[quassel.git] / src / uisupport / tabcompleter.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005/06 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 "tabcompleter.h"
22
23 #include "inputline.h"
24 #include "client.h"
25 #include "buffermodel.h"
26 #include "networkmodel.h"
27 #include "network.h"
28 #include "ircchannel.h"
29 #include "ircuser.h"
30
31 #include <QRegExp>
32
33 TabCompleter::TabCompleter(InputLine *inputLine_)
34   : QObject(inputLine_),
35     inputLine(inputLine_),
36     enabled(false),
37     nickSuffix(": ")
38 {
39   inputLine->installEventFilter(this);
40 }
41
42 void TabCompleter::buildCompletionList() {
43   completionList.clear();
44   nextCompletion = completionList.begin();
45   // this is the first time tab is pressed -> build up the completion list and it's iterator
46   QModelIndex currentIndex = Client::bufferModel()->currentIndex();
47   if(!currentIndex.data(NetworkModel::BufferIdRole).isValid())
48     return;
49   
50   NetworkId networkId = currentIndex.data(NetworkModel::NetworkIdRole).value<NetworkId>();
51   QString channelName = currentIndex.sibling(currentIndex.row(), 0).data().toString();
52
53   const Network *network = Client::network(networkId);
54   if(!network)
55     return;
56
57   IrcChannel *channel = network->ircChannel(channelName);
58   if(!channel)
59     return;
60
61   // FIXME commented for debugging
62   /*
63   disconnect(this, SLOT(ircUserJoinedOrParted(IrcUser *)));
64   connect(channel, SIGNAL(ircUserJoined(IrcUser *)),
65           this, SLOT(ircUserJoinedOrParted(IrcUser *)));
66   connect(channel, SIGNAL(ircUserParted(IrcUser *)),
67           this, SLOT(ircUserJoinedOrParted(IrcUser *)));
68   */
69
70   completionList.clear();
71   QString tabAbbrev = inputLine->text().left(inputLine->cursorPosition()).section(' ',-1,-1);
72   completionList.clear();
73   QRegExp regex(QString("^[^a-zA-Z]*").append(tabAbbrev), Qt::CaseInsensitive);
74   QMap<QString, QString> sortMap;
75
76   foreach(IrcUser *ircUser, channel->ircUsers()) {
77     if(regex.indexIn(ircUser->nick()) > -1) {
78       sortMap[ircUser->nick().toLower()] = ircUser->nick();
79     }
80   }
81   foreach (QString str, sortMap)
82     completionList << str;
83
84   nextCompletion = completionList.begin();
85   lastCompletionLength = tabAbbrev.length();
86 }
87
88 void TabCompleter::ircUserJoinedOrParted(IrcUser *ircUser) {
89   Q_UNUSED(ircUser)
90   buildCompletionList();
91 }
92
93 void TabCompleter::complete() {
94   if(!enabled) {
95     buildCompletionList();
96     enabled = true;
97   }
98   
99   if (nextCompletion != completionList.end()) {
100     // clear previous completion
101     for (int i = 0; i < lastCompletionLength; i++) {
102       inputLine->backspace();
103     }
104     
105     // insert completion
106     inputLine->insert(*nextCompletion);
107     
108     // remember charcount to delete next time and advance to next completion
109     lastCompletionLength = nextCompletion->length();
110     nextCompletion++;
111     
112     // we're completing the first word of the line
113     if(inputLine->text().length() == lastCompletionLength) {
114       inputLine->insert(nickSuffix);
115       lastCompletionLength += nickSuffix.length();
116     }
117
118   // we're at the end of the list -> start over again
119   } else {
120     nextCompletion = completionList.begin();
121   }
122   
123 }
124
125 void TabCompleter::reset() {
126   enabled = false;
127 }
128
129 bool TabCompleter::eventFilter(QObject *obj, QEvent *event) {
130   if(obj != inputLine || event->type() != QEvent::KeyPress)
131     return QObject::eventFilter(obj, event);
132
133   QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
134   
135   if(keyEvent->key() == Qt::Key_Tab) {
136     complete();
137     return true;
138   } else {
139     reset();
140     return false;
141   }
142 }
143