1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "tabcompleter.h"
23 #include "buffermodel.h"
25 #include "ircchannel.h"
27 #include "multilineedit.h"
29 #include "networkmodel.h"
30 #include "uisettings.h"
34 const Network *TabCompleter::_currentNetwork;
35 BufferId TabCompleter::_currentBufferId;
37 TabCompleter::TabCompleter(MultiLineEdit *_lineEdit)
43 _lineEdit->installEventFilter(this);
46 void TabCompleter::buildCompletionList() {
47 // ensure a safe state in case we return early.
48 _completionMap.clear();
49 _nextCompletion = _completionMap.begin();
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())
57 NetworkId networkId = currentIndex.data(NetworkModel::NetworkIdRole).value<NetworkId>();
58 QString bufferName = currentIndex.sibling(currentIndex.row(), 0).data().toString();
60 _currentNetwork = Client::network(networkId);
64 QString tabAbbrev = _lineEdit->text().left(_lineEdit->cursorPosition()).section(QRegExp("[^\\w\\d-_\\[\\]{}|`^.\\\\]"),-1,-1);
65 QRegExp regex(QString("^[-_\\[\\]{}|`^.\\\\]*").append(QRegExp::escape(tabAbbrev)), Qt::CaseInsensitive);
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);
73 foreach(IrcUser *ircUser, channel->ircUsers()) {
74 if(regex.indexIn(ircUser->nick()) > -1)
75 _completionMap[ircUser->nick().toLower()] = ircUser->nick();
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();
90 _nextCompletion = _completionMap.begin();
91 _lastCompletionLength = tabAbbrev.length();
94 void TabCompleter::complete() {
95 TabCompletionSettings s;
96 _nickSuffix = s.completionSuffix();
99 buildCompletionList();
103 if (_nextCompletion != _completionMap.end()) {
104 // clear previous completion
105 for (int i = 0; i < _lastCompletionLength; i++) {
106 _lineEdit->backspace();
110 _lineEdit->insert(*_nextCompletion);
112 // remember charcount to delete next time and advance to next completion
113 _lastCompletionLength = _nextCompletion->length();
116 // we're completing the first word of the line
117 if(_lineEdit->cursorPosition() == _lastCompletionLength) {
118 _lineEdit->insert(_nickSuffix);
119 _lastCompletionLength += _nickSuffix.length();
122 // we're at the end of the list -> start over again
124 if(!_completionMap.isEmpty()) {
125 _nextCompletion = _completionMap.begin();
131 void TabCompleter::reset() {
135 bool TabCompleter::eventFilter(QObject *obj, QEvent *event) {
136 if(obj != _lineEdit || event->type() != QEvent::KeyPress)
137 return QObject::eventFilter(obj, event);
139 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
141 if(keyEvent->key() == Qt::Key_Tab) {
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))
156 IrcUser *thatUser = _currentNetwork->ircUser(other.nick);
157 if(thatUser && _currentNetwork->isMe(thatUser))
160 if(!thisUser || !thatUser)
161 return QString::localeAwareCompare(this->nick, other.nick) < 0;
163 QDateTime thisSpokenTo = thisUser->lastSpokenTo(_currentBufferId);
164 QDateTime thatSpokenTo = thatUser->lastSpokenTo(_currentBufferId);
166 if(thisSpokenTo.isValid() || thatSpokenTo.isValid())
167 return thisSpokenTo > thatSpokenTo;
169 QDateTime thisTime = thisUser->lastChannelActivity(_currentBufferId);
170 QDateTime thatTime = thatUser->lastChannelActivity(_currentBufferId);
172 if(thisTime.isValid() || thatTime.isValid())
173 return thisTime > thatTime;
175 return QString::localeAwareCompare(this->nick, other.nick) < 0;