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