File indexing completed on 2024-05-19 04:03:03

0001 /*
0002     SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include <KIO/StoredTransferJob>
0008 #include <KIO/TransferJob>
0009 #include <KJob>
0010 #include <KLocalizedString>
0011 #include <KPluginFactory>
0012 #include <QDebug>
0013 #include <QJsonArray>
0014 #include <QStandardPaths>
0015 #include <QUrl>
0016 #include <purpose/pluginbase.h>
0017 
0018 // Taken from "share" Data Engine
0019 // key associated with plasma-devel@kde.org
0020 // thanks to Alan Schaaf of Pastebin (alan@pastebin.com)
0021 Q_GLOBAL_STATIC_WITH_ARGS(QByteArray, apiKey, ("0c8b6add8e0f6d53f61fe5ce870a1afa"))
0022 
0023 class PastebinJob : public Purpose::Job
0024 {
0025     Q_OBJECT
0026 public:
0027     PastebinJob(QObject *parent)
0028         : Purpose::Job(parent)
0029         , m_pendingJobs(0)
0030     {
0031     }
0032 
0033     void start() override
0034     {
0035         const QJsonArray urls = data().value(QLatin1String("urls")).toArray();
0036 
0037         if (urls.isEmpty()) {
0038             qWarning() << "no urls to share" << urls << data();
0039             emitResult();
0040             return;
0041         }
0042 
0043         m_pendingJobs = 0;
0044         for (const QJsonValue &val : urls) {
0045             QString u = val.toString();
0046             KIO::StoredTransferJob *job = KIO::storedGet(QUrl(u), KIO::NoReload, KIO::HideProgressInfo);
0047             connect(job, &KJob::finished, this, &PastebinJob::fileFetched);
0048             m_pendingJobs++;
0049         }
0050         Q_ASSERT(m_pendingJobs > 0);
0051     }
0052 
0053     void fileFetched(KJob *j)
0054     {
0055         KIO::StoredTransferJob *job = qobject_cast<KIO::StoredTransferJob *>(j);
0056         m_data += job->data();
0057         --m_pendingJobs;
0058 
0059         if (job->error()) {
0060             setError(job->error());
0061             setErrorText(job->errorString());
0062             emitResult();
0063         } else if (m_pendingJobs == 0)
0064             performUpload();
0065     }
0066 
0067     void performUpload()
0068     {
0069         if (m_data.isEmpty()) {
0070             setError(1);
0071             setErrorText(i18n("No information to send"));
0072             emitResult();
0073             return;
0074         }
0075 
0076         //             qCDebug(PLUGIN_PASTEBIN) << "exporting patch to pastebin" << source->file();
0077         QByteArray bytearray =
0078             "api_option=paste&api_paste_private=1&api_paste_name=kde-purpose-pastebin-plugin&api_paste_expire_date=1D&api_paste_format=diff&api_dev_key="
0079             + *apiKey + "&api_paste_code=";
0080         bytearray += QUrl::toPercentEncoding(QString::fromUtf8(m_data));
0081 
0082         const QUrl url(QStringLiteral("https://pastebin.com/api/api_post.php"));
0083 
0084         KIO::TransferJob *tf = KIO::http_post(url, bytearray);
0085 
0086         tf->addMetaData(QStringLiteral("content-type"), QStringLiteral("Content-Type: application/x-www-form-urlencoded"));
0087         connect(tf, &KIO::TransferJob::data, this, [this](KIO::Job *, const QByteArray &data) {
0088             m_resultData += data;
0089         });
0090         connect(tf, &KJob::result, this, &PastebinJob::textUploaded);
0091 
0092         m_resultData.clear();
0093     }
0094 
0095     void textUploaded(KJob *job)
0096     {
0097         if (job->error()) {
0098             setError(error());
0099             setErrorText(job->errorText());
0100         } else if (!m_resultData.startsWith("http")) {
0101             setError(1);
0102             setErrorText(QString::fromUtf8(m_resultData));
0103         } else
0104             setOutput({{QStringLiteral("url"), QString::fromUtf8(m_resultData)}});
0105         emitResult();
0106     }
0107 
0108 private:
0109     int m_pendingJobs;
0110     QByteArray m_data;
0111     QByteArray m_resultData;
0112 };
0113 
0114 class PastebinPlugin : public Purpose::PluginBase
0115 {
0116     Q_OBJECT
0117 public:
0118     using PluginBase::PluginBase;
0119     Purpose::Job *createJob() const override
0120     {
0121         return new PastebinJob(nullptr);
0122     }
0123 };
0124 
0125 K_PLUGIN_CLASS_WITH_JSON(PastebinPlugin, "pastebinplugin.json")
0126 
0127 #include "pastebinplugin.moc"