File indexing completed on 2024-04-14 05:44:09

0001 /*
0002     SPDX-FileCopyrightText: 2007-2008 Robert Knight <robertknight@gmail.com>
0003     SPDX-FileCopyrightText: 1997, 1998 Lars Doelle <lars.doelle@on-line.de>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "EscapeSequenceUrlExtractor.h"
0009 #include "Screen.h"
0010 
0011 #include <QUrl>
0012 
0013 namespace Konsole
0014 {
0015 EscapeSequenceUrlExtractor::EscapeSequenceUrlExtractor() = default;
0016 
0017 void Konsole::EscapeSequenceUrlExtractor::setScreen(Konsole::Screen *screen)
0018 {
0019     _screen = screen;
0020     clear();
0021 }
0022 
0023 void EscapeSequenceUrlExtractor::beginUrlInput()
0024 {
0025     _reading = true;
0026 }
0027 
0028 void EscapeSequenceUrlExtractor::appendUrlText_impl(QChar c)
0029 {
0030     if (_currentUrl.text.isEmpty()) {
0031         // We need to  on getCursorX because we want the position of the
0032         // last printed character, not the cursor.
0033         const int realCcolumn = _screen->getCursorY() + _screen->getHistLines();
0034         _currentUrl.begin = Coordinate{realCcolumn, _screen->getCursorX() - 1};
0035     }
0036     _currentUrl.text += c;
0037 }
0038 
0039 void EscapeSequenceUrlExtractor::setUrl(const QString &url)
0040 {
0041     if (_allowedUriSchemas.contains(QUrl(url).scheme() + QLatin1String("://"))) {
0042         _currentUrl.url = url;
0043     } else {
0044         abortUrlInput();
0045     }
0046 }
0047 
0048 void EscapeSequenceUrlExtractor::abortUrlInput()
0049 {
0050     _reading = false;
0051     _currentUrl = ExtractedUrl{};
0052     _ignoreNextUrlInput = true;
0053 }
0054 
0055 void EscapeSequenceUrlExtractor::endUrlInput()
0056 {
0057     Q_ASSERT(reading());
0058     _reading = false;
0059 
0060     const int realCcolumn = _screen->getCursorY() + _screen->getHistLines();
0061     const auto currentPos = Coordinate{realCcolumn, _screen->getCursorX()};
0062     _currentUrl.end = currentPos;
0063     _history.append(_currentUrl);
0064 
0065     _currentUrl = ExtractedUrl{};
0066 }
0067 
0068 void EscapeSequenceUrlExtractor::clear()
0069 {
0070     _history.clear();
0071 }
0072 
0073 void EscapeSequenceUrlExtractor::setAllowedLinkSchema(const QStringList &schema)
0074 {
0075     _allowedUriSchemas = schema;
0076 }
0077 
0078 void EscapeSequenceUrlExtractor::historyLinesRemoved(int lines)
0079 {
0080     for (auto &url : _history) {
0081         url.begin.row -= lines;
0082         url.end.row -= lines;
0083     }
0084     _history.erase(std::remove_if(std::begin(_history),
0085                                   std::end(_history),
0086                                   [](const ExtractedUrl &url) {
0087                                       const bool toRemove = url.begin.row < 0;
0088                                       return toRemove;
0089                                   }),
0090                    std::end(_history));
0091 }
0092 
0093 QVector<ExtractedUrl> EscapeSequenceUrlExtractor::history() const
0094 {
0095     return _history;
0096 }
0097 
0098 void Konsole::EscapeSequenceUrlExtractor::toggleUrlInput()
0099 {
0100     if (_ignoreNextUrlInput) {
0101         _ignoreNextUrlInput = false;
0102         return;
0103     }
0104 
0105     if (_reading) {
0106         endUrlInput();
0107     } else {
0108         beginUrlInput();
0109     }
0110 }
0111 
0112 }
0113 
0114 #include "moc_EscapeSequenceUrlExtractor.cpp"