File indexing completed on 2025-01-19 04:46:39
0001 /* 0002 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "triopabshorturlengineinterface.h" 0008 #include "../shorturlengineplugin.h" 0009 0010 #include <QNetworkReply> 0011 #include <QNetworkRequest> 0012 0013 TripAbShortUrlEngineInterface::TripAbShortUrlEngineInterface(ShortUrlEnginePlugin *plugin, QObject *parent) 0014 : ShortUrlEngineInterface(plugin, parent) 0015 { 0016 connect(mNetworkAccessManager, &QNetworkAccessManager::finished, this, &TripAbShortUrlEngineInterface::slotShortUrlFinished); 0017 } 0018 0019 TripAbShortUrlEngineInterface::~TripAbShortUrlEngineInterface() = default; 0020 0021 QString TripAbShortUrlEngineInterface::engineName() const 0022 { 0023 return mEnginePlugin->engineName(); 0024 } 0025 0026 void TripAbShortUrlEngineInterface::generateShortUrl() 0027 { 0028 const QString requestUrl = QStringLiteral("https://to.ly/api.php?longurl=%1").arg(mOriginalUrl); 0029 QNetworkReply *reply = mNetworkAccessManager->get(QNetworkRequest(QUrl(requestUrl))); 0030 connect(reply, &QNetworkReply::errorOccurred, this, &TripAbShortUrlEngineInterface::slotErrorFound); 0031 } 0032 0033 void TripAbShortUrlEngineInterface::slotShortUrlFinished(QNetworkReply *reply) 0034 { 0035 if (!mErrorFound) { 0036 const QString data = QString::fromUtf8(reply->readAll()); 0037 if (!data.isEmpty()) { 0038 mTextCursor.insertText(data); 0039 } 0040 } 0041 reply->deleteLater(); 0042 } 0043 0044 #include "moc_triopabshorturlengineinterface.cpp"