695923fdb1fbdc77e2cd6748dd3c712c75676d1a
[quassel.git] / src / uisupport / tabcompleter.cpp
1 /***************************************************************************
2 *   Copyright (C) 2005-09 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 const Network *TabCompleter::_currentNetwork;
35 BufferId TabCompleter::_currentBufferId;
36
37 TabCompleter::TabCompleter(InputLine *inputLine_)
38   : QObject(inputLine_),
39     inputLine(inputLine_),
40     enabled(false),
41     nickSuffix(": ")
42 {
43   inputLine->installEventFilter(this);
44 }
45
46 void TabCompleter::buildCompletionList() {
47   // ensure a safe state in case we return early.
48   completionMap.clear();
49   nextCompletion = completionMap.begin();
50
51   // this is the first time tab is pressed -> build up the completion list and it's iterator
52   QModelIndex currentIndex = Client::bufferModel()->currentIndex();
53   _currentBufferId = currentIndex.data(NetworkModel::BufferIdRole).value<BufferId>();
54   if(!_currentBufferId.isValid())
55     return;
56
57   NetworkId networkId = currentIndex.data(NetworkModel::NetworkIdRole).value<NetworkId>();
58   QString bufferName = currentIndex.sibling(currentIndex.row(), 0).data().toString();
59
60   _currentNetwork = Client::network(networkId);
61   if(!_currentNetwork)
62     return;
63
64   QString tabAbbrev = inputLine->text().left(inputLine->cursorPosition()).section(' ',-1,-1);
65   QRegExp regex(QString("^[^a-zA-Z]*").append(QRegExp::escape(tabAbbrev)), Qt::CaseInsensitive);
66
67   switch(static_cast<BufferInfo::Type>(currentIndex.data(NetworkModel::BufferTypeRole).toInt())) {
68   case BufferInfo::ChannelBuffer:
69     { // scope is needed for local var declaration
70       IrcChannel *channel = _currentNetwork->ircChannel(bufferName);
71       if(!channel)
72         return;
73       foreach(IrcUser *ircUser, channel->ircUsers()) {
74         if(regex.indexIn(ircUser->nick()) > -1)
75           completionMap[ircUser->nick().toLower()] = ircUser->nick();
76       }
77     }
78     break;
79   case BufferInfo::QueryBuffer:
80     if(regex.indexIn(bufferName) > -1)
81       completionMap[bufferName.toLower()] = bufferName;
82   case BufferInfo::StatusBuffer:
83     if(!_currentNetwork->myNick().isEmpty() && regex.indexIn(_currentNetwork->myNick()) > -1)
84       completionMap[_currentNetwork->myNick().toLower()] = _currentNetwork->myNick();
85     break;
86   default:
87     return;
88   }
89
90   nextCompletion = completionMap.begin();
91   lastCompletionLength = tabAbbrev.length();
92 }
93
94 void TabCompleter::complete() {
95   NickCompletionSettings s;
96   nickSuffix = s.completionSuffix();
97
98   if(!enabled) {
99     buildCompletionList();
100     enabled = true;
101   }
102
103   if (nextCompletion != completionMap.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->cursorPosition() == 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     if(!completionMap.isEmpty()) {
125       nextCompletion = completionMap.begin();
126       complete();
127     }
128   }
129 }
130
131 void TabCompleter::reset() {
132   enabled = false;
133 }
134
135 bool TabCompleter::eventFilter(QObject *obj, QEvent *event) {
136   if(obj != inputLine || event->type() != QEvent::KeyPress)
137     return QObject::eventFilter(obj, event);
138
139   QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
140
141   if(keyEvent->key() == Qt::Key_Tab) {
142     complete();
143     return true;
144   } else {
145     reset();
146     return false;
147   }
148 }
149
150 // this determines the sort order
151 bool TabCompleter::CompletionKey::operator<(const CompletionKey &other) const {
152   IrcUser *thisUser = _currentNetwork->ircUser(this->nick);
153   if(thisUser && _currentNetwork->isMe(thisUser))
154     return false;
155
156   IrcUser *thatUser = _currentNetwork->ircUser(other.nick);
157   if(thatUser && _currentNetwork->isMe(thatUser))
158     return true;
159
160   if(!thisUser || !thatUser)
161     return QString::localeAwareCompare(this->nick, other.nick) < 0;
162
163   QDateTime thisSpokenTo = thisUser->lastSpokenTo(_currentBufferId);
164   QDateTime thatSpokenTo = thatUser->lastSpokenTo(_currentBufferId);
165
166   if(thisSpokenTo.isValid() || thatSpokenTo.isValid())
167     return thisSpokenTo > thatSpokenTo;
168
169   QDateTime thisTime = thisUser->lastChannelActivity(_currentBufferId);
170   QDateTime thatTime = thatUser->lastChannelActivity(_currentBufferId);
171
172   if(thisTime.isValid() || thatTime.isValid())
173     return thisTime > thatTime;
174
175   return QString::localeAwareCompare(this->nick, other.nick) < 0;
176 }