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 "isgdshorturlengineinterface.h" 0008 #include "isgdshorturlengineplugin_debug.h" 0009 0010 #include <QJsonDocument> 0011 #include <QNetworkReply> 0012 #include <QNetworkRequest> 0013 0014 #include "../../shorturlengineplugin/shorturlengineplugin.h" 0015 0016 IsgdShortUrlEngineInterface::IsgdShortUrlEngineInterface(ShortUrlEnginePlugin *plugin, QObject *parent) 0017 : ShortUrlEngineInterface(plugin, parent) 0018 { 0019 connect(mNetworkAccessManager, &QNetworkAccessManager::finished, this, &IsgdShortUrlEngineInterface::slotShortUrlFinished); 0020 } 0021 0022 IsgdShortUrlEngineInterface::~IsgdShortUrlEngineInterface() = default; 0023 0024 QString IsgdShortUrlEngineInterface::engineName() const 0025 { 0026 return mEnginePlugin->engineName(); 0027 } 0028 0029 void IsgdShortUrlEngineInterface::generateShortUrl() 0030 { 0031 const QString requestUrl = QStringLiteral("https://is.gd/create.php?%1&url=%2").arg(QStringLiteral("format=json"), mOriginalUrl); 0032 QNetworkRequest request = QNetworkRequest(QUrl(requestUrl)); 0033 0034 request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json")); 0035 QNetworkReply *reply = mNetworkAccessManager->get(request); 0036 connect(reply, &QNetworkReply::errorOccurred, this, &IsgdShortUrlEngineInterface::slotErrorFound); 0037 } 0038 0039 void IsgdShortUrlEngineInterface::slotSslErrors(QNetworkReply *reply, const QList<QSslError> &error) 0040 { 0041 reply->ignoreSslErrors(error); 0042 } 0043 0044 void IsgdShortUrlEngineInterface::slotShortUrlFinished(QNetworkReply *reply) 0045 { 0046 if (mErrorFound) { 0047 return; 0048 } 0049 0050 const QByteArray data = reply->readAll(); 0051 QJsonParseError error; 0052 const QJsonDocument json = QJsonDocument::fromJson(data, &error); 0053 qCDebug(ISGDSHORTURLENGINEPLUGIN_LOG) << "void IsGdShortUrl::slotShortUrlFinished(QNetworkReply *reply) " << data; 0054 0055 reply->deleteLater(); 0056 0057 if (error.error != QJsonParseError::NoError || json.isNull()) { 0058 qCDebug(ISGDSHORTURLENGINEPLUGIN_LOG) << " Error during parsing" << error.errorString(); 0059 Q_EMIT shortUrlFailed(error.errorString()); 0060 return; 0061 } 0062 const QMap<QString, QVariant> map = json.toVariant().toMap(); 0063 0064 const QVariant var = map.value(QStringLiteral("shorturl")); 0065 if (var.isValid()) { 0066 mTextCursor.insertText(var.toString()); 0067 } 0068 } 0069 0070 #include "moc_isgdshorturlengineinterface.cpp"