1 /***************************************************************************
2 * Copyright (C) 2005/06 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "tabcompleter.h"
23 #include "inputline.h"
25 #include "buffermodel.h"
26 #include "networkmodel.h"
28 #include "ircchannel.h"
30 #include "uisettings.h"
34 TabCompleter::TabCompleter(InputLine *inputLine_)
35 : QObject(inputLine_),
36 inputLine(inputLine_),
40 inputLine->installEventFilter(this);
43 void TabCompleter::buildCompletionList() {
44 completionMap.clear();
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 nextCompletion = completionMap.begin();
52 NetworkId networkId = currentIndex.data(NetworkModel::NetworkIdRole).value<NetworkId>();
53 QString channelName = currentIndex.sibling(currentIndex.row(), 0).data().toString();
55 const Network *network = Client::network(networkId);
59 IrcChannel *channel = network->ircChannel(channelName);
63 // FIXME commented for debugging
65 disconnect(this, SLOT(ircUserJoinedOrParted(IrcUser *)));
66 connect(channel, SIGNAL(ircUserJoined(IrcUser *)),
67 this, SLOT(ircUserJoinedOrParted(IrcUser *)));
68 connect(channel, SIGNAL(ircUserParted(IrcUser *)),
69 this, SLOT(ircUserJoinedOrParted(IrcUser *)));
72 QString tabAbbrev = inputLine->text().left(inputLine->cursorPosition()).section(' ',-1,-1);
73 QRegExp regex(QString("^[^a-zA-Z]*").append(QRegExp::escape(tabAbbrev)), Qt::CaseInsensitive);
75 foreach(IrcUser *ircUser, channel->ircUsers()) {
76 if(regex.indexIn(ircUser->nick()) > -1) {
77 completionMap[ircUser->nick().toLower()] = ircUser->nick();
81 nextCompletion = completionMap.begin();
82 lastCompletionLength = tabAbbrev.length();
85 void TabCompleter::ircUserJoinedOrParted(IrcUser *ircUser) {
87 buildCompletionList();
90 void TabCompleter::complete() {
91 UiSettings uiSettings;
92 nickSuffix = uiSettings.value("CompletionSuffix", QString(": ")).toString();
95 buildCompletionList();
99 if (nextCompletion != completionMap.end()) {
100 // clear previous completion
101 for (int i = 0; i < lastCompletionLength; i++) {
102 inputLine->backspace();
106 inputLine->insert(*nextCompletion);
108 // remember charcount to delete next time and advance to next completion
109 lastCompletionLength = nextCompletion->length();
112 // we're completing the first word of the line
113 if(inputLine->text().length() == lastCompletionLength) {
114 inputLine->insert(nickSuffix);
115 lastCompletionLength += nickSuffix.length();
118 // we're at the end of the list -> start over again
120 if(!completionMap.isEmpty()) {
121 nextCompletion = completionMap.begin();
128 void TabCompleter::reset() {
132 bool TabCompleter::eventFilter(QObject *obj, QEvent *event) {
133 if(obj != inputLine || event->type() != QEvent::KeyPress)
134 return QObject::eventFilter(obj, event);
136 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
138 if(keyEvent->key() == Qt::Key_Tab) {