File indexing completed on 2025-01-05 04:49:28
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 "ur1cashorturlengineinterface.h" 0008 #include "../shorturlengineplugin.h" 0009 #include "ur1cashorturlengineplugin_debug.h" 0010 0011 #include <QNetworkReply> 0012 #include <QNetworkRequest> 0013 #include <QUrlQuery> 0014 0015 Ur1CaShortUrlEngineInterface::Ur1CaShortUrlEngineInterface(ShortUrlEnginePlugin *plugin, QObject *parent) 0016 : ShortUrlEngineInterface(plugin, parent) 0017 { 0018 connect(mNetworkAccessManager, &QNetworkAccessManager::sslErrors, this, &Ur1CaShortUrlEngineInterface::slotSslErrors); 0019 connect(mNetworkAccessManager, &QNetworkAccessManager::finished, this, &Ur1CaShortUrlEngineInterface::slotShortUrlFinished); 0020 } 0021 0022 Ur1CaShortUrlEngineInterface::~Ur1CaShortUrlEngineInterface() 0023 { 0024 } 0025 0026 QString Ur1CaShortUrlEngineInterface::engineName() const 0027 { 0028 return mEnginePlugin->engineName(); 0029 } 0030 0031 void Ur1CaShortUrlEngineInterface::generateShortUrl() 0032 { 0033 QUrl url(QStringLiteral("http://ur1.ca/")); 0034 QUrlQuery query; 0035 query.addQueryItem(QStringLiteral("longurl"), mOriginalUrl); 0036 0037 url.setQuery(query); 0038 QByteArray postData; 0039 0040 postData = query.query().toLatin1(); 0041 QNetworkRequest request(url); 0042 request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("text/plain")); 0043 0044 QNetworkReply *reply = mNetworkAccessManager->post(request, postData); 0045 connect(reply, qOverload<QNetworkReply::NetworkError>(&QNetworkReply::error), this, &Ur1CaShortUrlEngineInterface::slotErrorFound); 0046 } 0047 0048 void Ur1CaShortUrlEngineInterface::slotSslErrors(QNetworkReply *reply, const QList<QSslError> &error) 0049 { 0050 reply->ignoreSslErrors(error); 0051 } 0052 0053 void Ur1CaShortUrlEngineInterface::slotShortUrlFinished(QNetworkReply *reply) 0054 { 0055 if (!mErrorFound) { 0056 QString output = QLatin1StringView(reply->readAll()); 0057 qCDebug(UR1CASHORTURLENGINEPLUGIN_LOG) << "void Ur1CaShortUrl::slotShortUrlFinished(QNetworkReply *reply) " << output; 0058 QRegExp rx(QStringLiteral("<p class=[\'\"]success[\'\"]>(.*)</p>")); 0059 rx.setMinimal(true); 0060 output = rx.cap(1); 0061 rx.setPattern(QStringLiteral("href=[\'\"](.*)[\'\"]")); 0062 rx.indexIn(output); 0063 output = rx.cap(1); 0064 qCDebug(UR1CASHORTURLENGINEPLUGIN_LOG) << "Short url is: " << output; 0065 if (!output.isEmpty()) { 0066 mTextCursor.insertText(output); 0067 } else { 0068 Q_EMIT shortUrlFailed(QString()); 0069 } 0070 } 0071 reply->deleteLater(); 0072 } 0073 0074 #include "moc_ur1cashorturlengineinterface.cpp"