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