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
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(m_data));
82
83 const QUrl url(QStringLiteral("https://pastebin.com/api/api_post.php"));
84
85 KIO::TransferJob *tf = KIO::http_post(url, bytearray);
86
87 tf->addMetaData(QStringLiteral("content-type"), QStringLiteral("Content-Type: application/x-www-form-urlencoded"));
88 connect(tf, &KIO::TransferJob::data, this, [this](KIO::Job *, const QByteArray &data) {
89 m_resultData += data;
90 });
91 connect(tf, &KJob::result, this, &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("http")) {
102 setError(1);
103 setErrorText(QString::fromUtf8(m_resultData));
104 } else {
105 setOutput({{QStringLiteral("url"), QString::fromUtf8(m_resultData)}});
106 }
107 emitResult();
108 }
109
110private:
111 int m_pendingJobs;
112 QByteArray m_data;
113 QByteArray m_resultData;
114};
115
116class PastebinPlugin : public Purpose::PluginBase
117{
119public:
120 using PluginBase::PluginBase;
121 Purpose::Job *createJob() const override
122 {
123 return new PastebinJob(nullptr);
124 }
125};
126
127K_PLUGIN_CLASS_WITH_JSON(PastebinPlugin, "pastebinplugin.json")
128
129#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 Sat Dec 21 2024 17:03:06 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.