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 | void performUpload() |
68 | { |
69 | if (m_data.isEmpty()) { |
70 | setError(1); |
71 | setErrorText(i18n("No information to send" )); |
72 | emitResult(); |
73 | return; |
74 | } |
75 | |
76 | // qCDebug(PLUGIN_PASTEBIN) << "exporting patch to pastebin" << source->file(); |
77 | QByteArray bytearray = |
78 | "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=" |
79 | + *apiKey + "&api_paste_code=" ; |
80 | bytearray += QUrl::toPercentEncoding(QString::fromUtf8(ba: m_data)); |
81 | |
82 | const QUrl url(QStringLiteral("https://pastebin.com/api/api_post.php" )); |
83 | |
84 | KIO::TransferJob *tf = KIO::http_post(url, postData: bytearray); |
85 | |
86 | tf->addMetaData(QStringLiteral("content-type" ), QStringLiteral("Content-Type: application/x-www-form-urlencoded" )); |
87 | connect(sender: tf, signal: &KIO::TransferJob::data, context: this, slot: [this](KIO::Job *, const QByteArray &data) { |
88 | m_resultData += data; |
89 | }); |
90 | connect(sender: tf, signal: &KJob::result, context: this, slot: &PastebinJob::textUploaded); |
91 | |
92 | m_resultData.clear(); |
93 | } |
94 | |
95 | void textUploaded(KJob *job) |
96 | { |
97 | if (job->error()) { |
98 | setError(error()); |
99 | setErrorText(job->errorText()); |
100 | } else if (!m_resultData.startsWith(bv: "http" )) { |
101 | setError(1); |
102 | setErrorText(QString::fromUtf8(ba: m_resultData)); |
103 | } else |
104 | setOutput({{QStringLiteral("url" ), QString::fromUtf8(ba: m_resultData)}}); |
105 | emitResult(); |
106 | } |
107 | |
108 | private: |
109 | int m_pendingJobs; |
110 | QByteArray m_data; |
111 | QByteArray m_resultData; |
112 | }; |
113 | |
114 | class PastebinPlugin : public Purpose::PluginBase |
115 | { |
116 | Q_OBJECT |
117 | public: |
118 | using PluginBase::PluginBase; |
119 | Purpose::Job *createJob() const override |
120 | { |
121 | return new PastebinJob(nullptr); |
122 | } |
123 | }; |
124 | |
125 | K_PLUGIN_CLASS_WITH_JSON(PastebinPlugin, "pastebinplugin.json" ) |
126 | |
127 | #include "pastebinplugin.moc" |
128 | |