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