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