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