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