Purpose

pastebinplugin.cpp
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)
21Q_GLOBAL_STATIC_WITH_ARGS(QByteArray, apiKey, ("0c8b6add8e0f6d53f61fe5ce870a1afa"))
22
23class PastebinJob : public Purpose::Job
24{
25 Q_OBJECT
26public:
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(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();
47 connect(job, &KJob::finished, this, &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 *>(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(m_data));
81
82 const QUrl url(QStringLiteral("https://pastebin.com/api/api_post.php"));
83
84 KIO::TransferJob *tf = KIO::http_post(url, bytearray);
85
86 tf->addMetaData(QStringLiteral("content-type"), QStringLiteral("Content-Type: application/x-www-form-urlencoded"));
87 connect(tf, &KIO::TransferJob::data, this, [this](KIO::Job *, const QByteArray &data) {
88 m_resultData += data;
89 });
90 connect(tf, &KJob::result, this, &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("http")) {
101 setError(1);
102 setErrorText(QString::fromUtf8(m_resultData));
103 } else
104 setOutput({{QStringLiteral("url"), QString::fromUtf8(m_resultData)}});
105 emitResult();
106 }
107
108private:
109 int m_pendingJobs;
110 QByteArray m_data;
111 QByteArray m_resultData;
112};
113
114class PastebinPlugin : public Purpose::PluginBase
115{
117public:
118 using PluginBase::PluginBase;
119 Purpose::Job *createJob() const override
120 {
121 return new PastebinJob(nullptr);
122 }
123};
124
125K_PLUGIN_CLASS_WITH_JSON(PastebinPlugin, "pastebinplugin.json")
126
127#include "pastebinplugin.moc"
QString errorString() const override
void addMetaData(const QMap< QString, QString > &values)
QByteArray data() const
void data(KIO::Job *job, const QByteArray &data)
int error() const
void result(KJob *job)
void finished(KJob *job)
QString errorText() const
#define K_PLUGIN_CLASS_WITH_JSON(classname, jsonFile)
Job that will actually perform the sharing.
Definition job.h:34
Base class to implement by plugins.
Definition pluginbase.h:29
Q_SCRIPTABLE Q_NOREPLY void start()
QString i18n(const char *text, const TYPE &arg...)
KIOCORE_EXPORT StoredTransferJob * storedGet(const QUrl &url, LoadType reload=NoReload, JobFlags flags=DefaultFlags)
KIOCORE_EXPORT TransferJob * http_post(const QUrl &url, const QByteArray &postData, JobFlags flags=DefaultFlags)
HideProgressInfo
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
bool isEmpty() const const
Q_OBJECTQ_OBJECT
QString fromUtf8(QByteArrayView str)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QByteArray toPercentEncoding(const QString &input, const QByteArray &exclude, const QByteArray &include)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:14:05 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.