Cleanup allowing for tags to be available at later points, adds TAGMSG
[quassel.git] / src / uisupport / tabcompleter.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "tabcompleter.h"
22
23 #include <QRegExp>
24
25 #include "action.h"
26 #include "actioncollection.h"
27 #include "buffermodel.h"
28 #include "client.h"
29 #include "graphicalui.h"
30 #include "ircchannel.h"
31 #include "ircuser.h"
32 #include "multilineedit.h"
33 #include "network.h"
34 #include "networkmodel.h"
35 #include "uisettings.h"
36
37 const Network* TabCompleter::_currentNetwork;
38 BufferId TabCompleter::_currentBufferId;
39 QString TabCompleter::_currentBufferName;
40 TabCompleter::Type TabCompleter::_completionType;
41
42 TabCompleter::TabCompleter(MultiLineEdit* _lineEdit)
43     : QObject(_lineEdit)
44     , _lineEdit(_lineEdit)
45     , _enabled(false)
46     , _nickSuffix(": ")
47 {
48     // This Action just serves as a container for the custom shortcut and isn't actually handled;
49     // apparently, using tab as an Action shortcut in an input widget is unreliable on some platforms (e.g. OS/2)
50     _lineEdit->installEventFilter(this);
51     ActionCollection* coll = GraphicalUi::actionCollection("General");
52     QAction* a = coll->addAction("TabCompletionKey",
53                                  new Action(tr("Tab completion"), coll, this, &TabCompleter::onTabCompletionKey, QKeySequence(Qt::Key_Tab)));
54     a->setEnabled(false);  // avoid catching the shortcut
55 }
56
57 void TabCompleter::onTabCompletionKey()
58 {
59     // do nothing; we use the event filter instead
60 }
61
62 void TabCompleter::buildCompletionList()
63 {
64     // ensure a safe state in case we return early.
65     _completionMap.clear();
66     _nextCompletion = _completionMap.begin();
67
68     // this is the first time tab is pressed -> build up the completion list and it's iterator
69     QModelIndex currentIndex = Client::bufferModel()->currentIndex();
70     _currentBufferId = currentIndex.data(NetworkModel::BufferIdRole).value<BufferId>();
71     if (!_currentBufferId.isValid())
72         return;
73
74     NetworkId networkId = currentIndex.data(NetworkModel::NetworkIdRole).value<NetworkId>();
75     _currentBufferName = currentIndex.sibling(currentIndex.row(), 0).data().toString();
76
77     _currentNetwork = Client::network(networkId);
78     if (!_currentNetwork)
79         return;
80
81     QString tabAbbrev = _lineEdit->text().left(_lineEdit->cursorPosition()).section(QRegExp(R"([^#\w\d-_\[\]{}|`^.\\])"), -1, -1);
82     QRegExp regex(QString(R"(^[-_\[\]{}|`^.\\]*)").append(QRegExp::escape(tabAbbrev)), Qt::CaseInsensitive);
83
84     // channel completion - add all channels of the current network to the map
85     if (tabAbbrev.startsWith('#')) {
86         _completionType = ChannelTab;
87         foreach (IrcChannel* ircChannel, _currentNetwork->ircChannels()) {
88             if (regex.indexIn(ircChannel->name()) > -1)
89                 _completionMap[ircChannel->name()] = ircChannel->name();
90         }
91     }
92     else {
93         // user completion
94         _completionType = UserTab;
95         switch (static_cast<BufferInfo::Type>(currentIndex.data(NetworkModel::BufferTypeRole).toInt())) {
96         case BufferInfo::ChannelBuffer: {  // scope is needed for local var declaration
97             IrcChannel* channel = _currentNetwork->ircChannel(_currentBufferName);
98             if (!channel)
99                 return;
100             foreach (IrcUser* ircUser, channel->ircUsers()) {
101                 if (regex.indexIn(ircUser->nick()) > -1)
102                     _completionMap[ircUser->nick().toLower()] = ircUser->nick();
103             }
104         } break;
105         case BufferInfo::QueryBuffer:
106             if (regex.indexIn(_currentBufferName) > -1)
107                 _completionMap[_currentBufferName.toLower()] = _currentBufferName;
108             // fallthrough
109         case BufferInfo::StatusBuffer:
110             if (!_currentNetwork->myNick().isEmpty() && regex.indexIn(_currentNetwork->myNick()) > -1)
111                 _completionMap[_currentNetwork->myNick().toLower()] = _currentNetwork->myNick();
112             break;
113         default:
114             return;
115         }
116     }
117
118     _nextCompletion = _completionMap.begin();
119     _lastCompletionLength = tabAbbrev.length();
120 }
121
122 void TabCompleter::complete()
123 {
124     TabCompletionSettings s;
125     _nickSuffix = s.completionSuffix();
126
127     if (!_enabled) {
128         buildCompletionList();
129         _enabled = true;
130     }
131
132     if (_nextCompletion != _completionMap.end()) {
133         // clear previous completion
134         for (int i = 0; i < _lastCompletionLength; i++) {
135             _lineEdit->backspace();
136         }
137
138         // insert completion
139         _lineEdit->insert(*_nextCompletion);
140
141         // remember charcount to delete next time and advance to next completion
142         _lastCompletionLength = _nextCompletion->length();
143         _nextCompletion++;
144
145         // we're completing the first word of the line
146         if (_completionType == UserTab && _lineEdit->cursorPosition() == _lastCompletionLength) {
147             _lineEdit->insert(_nickSuffix);
148             _lastCompletionLength += _nickSuffix.length();
149         }
150         else if (s.addSpaceMidSentence()) {
151             _lineEdit->addCompletionSpace();
152             _lastCompletionLength++;
153         }
154
155         // we're at the end of the list -> start over again
156     }
157     else {
158         if (!_completionMap.isEmpty()) {
159             _nextCompletion = _completionMap.begin();
160             complete();
161         }
162     }
163 }
164
165 void TabCompleter::reset()
166 {
167     _enabled = false;
168 }
169
170 bool TabCompleter::eventFilter(QObject* obj, QEvent* event)
171 {
172     if (obj != _lineEdit || event->type() != QEvent::KeyPress)
173         return QObject::eventFilter(obj, event);
174
175     auto* keyEvent = static_cast<QKeyEvent*>(event);
176
177     if (keyEvent->key() == GraphicalUi::actionCollection("General")->action("TabCompletionKey")->shortcut()[0])
178         complete();
179     else
180         reset();
181
182     return false;
183 }
184
185 // this determines the sort order
186 bool TabCompleter::CompletionKey::operator<(const CompletionKey& other) const
187 {
188     switch (_completionType) {
189     case UserTab: {
190         IrcUser* thisUser = _currentNetwork->ircUser(this->contents);
191         if (thisUser && _currentNetwork->isMe(thisUser))
192             return false;
193
194         IrcUser* thatUser = _currentNetwork->ircUser(other.contents);
195         if (thatUser && _currentNetwork->isMe(thatUser))
196             return true;
197
198         if (!thisUser || !thatUser)
199             return QString::localeAwareCompare(this->contents, other.contents) < 0;
200
201         QDateTime thisSpokenTo = thisUser->lastSpokenTo(_currentBufferId);
202         QDateTime thatSpokenTo = thatUser->lastSpokenTo(_currentBufferId);
203
204         if (thisSpokenTo.isValid() || thatSpokenTo.isValid())
205             return thisSpokenTo > thatSpokenTo;
206
207         QDateTime thisTime = thisUser->lastChannelActivity(_currentBufferId);
208         QDateTime thatTime = thatUser->lastChannelActivity(_currentBufferId);
209
210         if (thisTime.isValid() || thatTime.isValid())
211             return thisTime > thatTime;
212     } break;
213     case ChannelTab:
214         if (QString::compare(_currentBufferName, this->contents, Qt::CaseInsensitive) == 0)
215             return true;
216
217         if (QString::compare(_currentBufferName, other.contents, Qt::CaseInsensitive) == 0)
218             return false;
219         break;
220     default:
221         break;
222     }
223
224     return QString::localeAwareCompare(this->contents, other.contents) < 0;
225 }