Temporarily disabling tabcompletion in order to find out if it's the cause for Quasse...
[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 TabCompleter::TabCompleter(InputLine *inputLine_)
32   : QObject(inputLine_),
33     inputLine(inputLine_),
34     enabled(false),
35     nickSuffix(": ")
36 {
37 }
38
39 void TabCompleter::buildCompletionList() {
40   completionList.clear();
41   nextCompletion = completionList.begin();
42   // this is the first time tab is pressed -> build up the completion list and it's iterator
43   QModelIndex currentIndex = Client::bufferModel()->currentIndex();
44   if(!currentIndex.data(NetworkModel::BufferIdRole).isValid())
45     return;
46   
47   NetworkId networkId = currentIndex.data(NetworkModel::NetworkIdRole).value<NetworkId>();
48   QString channelName = currentIndex.sibling(currentIndex.row(), 0).data().toString();
49
50   const Network *network = Client::network(networkId);
51   if(!network)
52     return;
53
54   IrcChannel *channel = network->ircChannel(channelName);
55   if(!channel)
56     return;
57
58   disconnect(this, SLOT(ircUserJoinedOrParted(IrcUser *)));
59   connect(channel, SIGNAL(ircUserJoined(IrcUser *)),
60           this, SLOT(ircUserJoinedOrParted(IrcUser *)));
61   connect(channel, SIGNAL(ircUserParted(IrcUser *)),
62           this, SLOT(ircUserJoinedOrParted(IrcUser *)));
63              
64   completionList.clear();
65   QString tabAbbrev = inputLine->text().left(inputLine->cursorPosition()).section(' ',-1,-1);
66   completionList.clear();
67   foreach(IrcUser *ircUser, channel->ircUsers()) {
68     if(ircUser->nick().toLower().startsWith(tabAbbrev.toLower())) {
69       completionList << ircUser->nick();
70     }
71   }
72   completionList.sort();
73   nextCompletion = completionList.begin();
74   lastCompletionLength = tabAbbrev.length();
75 }
76
77 void TabCompleter::ircUserJoinedOrParted(IrcUser *ircUser) {
78   Q_UNUSED(ircUser)
79   buildCompletionList();
80 }
81
82 void TabCompleter::complete() {
83   return; // FIXME
84   if(!enabled) {
85     buildCompletionList();
86     enabled = true;
87   }
88   
89   if (nextCompletion != completionList.end()) {
90     // clear previous completion
91     for (int i = 0; i < lastCompletionLength; i++) {
92       inputLine->backspace();
93     }
94     
95     // insert completion
96     inputLine->insert(*nextCompletion);
97     
98     // remember charcount to delete next time and advance to next completion
99     lastCompletionLength = nextCompletion->length();
100     nextCompletion++;
101     
102     // we're completing the first word of the line
103     if(inputLine->text().length() == lastCompletionLength) {
104       inputLine->insert(nickSuffix);
105       lastCompletionLength += nickSuffix.length();
106     }
107
108   // we're at the end of the list -> start over again
109   } else {
110     nextCompletion = completionList.begin();
111   }
112   
113 }
114
115 void TabCompleter::reset() {
116   enabled = false;
117 }
118