| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 | */ |
| 6 | |
| 7 | #include <KIO/StoredTransferJob> |
| 8 | #include <KIO/TransferJob> |
| 9 | #include <KJob> |
| 10 | #include <KLocalizedString> |
| 11 | #include <KPluginFactory> |
| 12 | #include <QDebug> |
| 13 | #include <QJsonArray> |
| 14 | #include <QStandardPaths> |
| 15 | #include <QUrl> |
| 16 | #include <purpose/pluginbase.h> |
| 17 | |
| 18 | // Taken from "share" Data Engine |
| 19 | // key associated with plasma-devel@kde.org |
| 20 | // thanks to Alan Schaaf of Pastebin (alan@pastebin.com) |
| 21 | Q_GLOBAL_STATIC_WITH_ARGS(QByteArray, apiKey, ("0c8b6add8e0f6d53f61fe5ce870a1afa" )) |
| 22 | |
| 23 | class PastebinJob : public Purpose::Job |
| 24 | { |
| 25 | Q_OBJECT |
| 26 | public: |
| 27 | PastebinJob(QObject *parent) |
| 28 | : Purpose::Job(parent) |
| 29 | , m_pendingJobs(0) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | void start() override |
| 34 | { |
| 35 | const QJsonArray urls = data().value(key: QLatin1String("urls" )).toArray(); |
| 36 | |
| 37 | if (urls.isEmpty()) { |
| 38 | qWarning() << "no urls to share" << urls << data(); |
| 39 | emitResult(); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | m_pendingJobs = 0; |
| 44 | for (const QJsonValue &val : urls) { |
| 45 | QString u = val.toString(); |
| 46 | KIO::StoredTransferJob *job = KIO::storedGet(url: QUrl(u), reload: KIO::NoReload, flags: KIO::HideProgressInfo); |
| 47 | connect(sender: job, signal: &KJob::finished, context: this, slot: &PastebinJob::fileFetched); |
| 48 | m_pendingJobs++; |
| 49 | } |
| 50 | Q_ASSERT(m_pendingJobs > 0); |
| 51 | } |
| 52 | |
| 53 | void fileFetched(KJob *j) |
| 54 | { |
| 55 | KIO::StoredTransferJob *job = qobject_cast<KIO::StoredTransferJob *>(object: j); |
| 56 | m_data += job->data(); |
| 57 | --m_pendingJobs; |
| 58 | |
| 59 | if (job->error()) { |
| 60 | setError(job->error()); |
| 61 | setErrorText(job->errorString()); |
| 62 | emitResult(); |
| 63 | } else if (m_pendingJobs == 0) { |
| 64 | performUpload(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void performUpload() |
| 69 | { |
| 70 | if (m_data.isEmpty()) { |
| 71 | setError(1); |
| 72 | setErrorText(i18n("No information to send" )); |
| 73 | emitResult(); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | // qCDebug(PLUGIN_PASTEBIN) << "exporting patch to pastebin" << source->file(); |
| 78 | QByteArray bytearray = |
| 79 | "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=" |
| 80 | + *apiKey + "&api_paste_code=" ; |
| 81 | bytearray += QUrl::toPercentEncoding(QString::fromUtf8(ba: m_data)); |
| 82 | |
| 83 | const QUrl url(QStringLiteral("https://pastebin.com/api/api_post.php" )); |
| 84 | |
| 85 | KIO::TransferJob *tf = KIO::http_post(url, postData: bytearray); |
| 86 | |
| 87 | tf->addMetaData(QStringLiteral("content-type" ), QStringLiteral("Content-Type: application/x-www-form-urlencoded" )); |
| 88 | connect(sender: tf, signal: &KIO::TransferJob::data, context: this, slot: [this](KIO::Job *, const QByteArray &data) { |
| 89 | m_resultData += data; |
| 90 | }); |
| 91 | connect(sender: tf, signal: &KJob::result, context: this, slot: &PastebinJob::textUploaded); |
| 92 | |
| 93 | m_resultData.clear(); |
| 94 | } |
| 95 | |
| 96 | void textUploaded(KJob *job) |
| 97 | { |
| 98 | if (job->error()) { |
| 99 | setError(error()); |
| 100 | setErrorText(job->errorText()); |
| 101 | } else if (!m_resultData.startsWith(bv: "http" )) { |
| 102 | setError(1); |
| 103 | setErrorText(QString::fromUtf8(ba: m_resultData)); |
| 104 | } else { |
| 105 | setOutput({{QStringLiteral("url" ), QString::fromUtf8(ba: m_resultData)}}); |
| 106 | } |
| 107 | emitResult(); |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | int m_pendingJobs; |
| 112 | QByteArray m_data; |
| 113 | QByteArray m_resultData; |
| 114 | }; |
| 115 | |
| 116 | class PastebinPlugin : public Purpose::PluginBase |
| 117 | { |
| 118 | Q_OBJECT |
| 119 | public: |
| 120 | using PluginBase::PluginBase; |
| 121 | Purpose::Job *createJob() const override |
| 122 | { |
| 123 | return new PastebinJob(nullptr); |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | K_PLUGIN_CLASS_WITH_JSON(PastebinPlugin, "pastebinplugin.json" ) |
| 128 | |
| 129 | #include "pastebinplugin.moc" |
| 130 | |