Purpose

phabricatorplugin.cpp
1/*
2 SPDX-FileCopyrightText: 2017 René J.V. Bertin <rjvbertin@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include <QFileInfo>
8#include <QJsonArray>
9#include <QStandardPaths>
10#include <QUrl>
11
12#include <KLocalizedString>
13#include <KPluginFactory>
14
15#include "debug.h"
16#include "phabricatorjobs.h"
17
18#include "purpose/job.h"
19#include "purpose/pluginbase.h"
20
21class PhabricatorJob : public Purpose::Job
22{
24public:
25 PhabricatorJob(QObject *object = nullptr)
26 : Purpose::Job(object)
27 {
28 }
29
30 void start() override
31 {
32 const QString localBaseDir(data().value(QLatin1String("localBaseDir")).toString());
33 const QUrl sourceFile(data().value(QLatin1String("urls")).toArray().first().toString());
34 const QString updateDR = data().value(QLatin1String("updateDR")).toString();
35 const bool doBrowse = data().value(QLatin1String("doBrowse")).toBool();
36
37 const QString baseDir = QUrl(localBaseDir).toLocalFile();
38
39 if (QFileInfo(sourceFile.toLocalFile()).size() <= 0) {
40 setError(KJob::UserDefinedError + 1);
41 setErrorText(i18n("Phabricator refuses empty patchfiles"));
43 qCCritical(PLUGIN_PHABRICATOR) << errorString();
44 emitResult();
45 return;
46 } else if (updateDR.localeAwareCompare(i18n("unknown")) == 0) {
47 setError(KJob::UserDefinedError + 1);
48 setErrorText(i18n("Please choose between creating a new revision or updating an existing one"));
50 qCCritical(PLUGIN_PHABRICATOR) << errorString();
51 emitResult();
52 return;
53 }
54
55 m_drTitle = data().value(QLatin1String("drTitle")).toString();
56
57 KJob *job;
58 if (!updateDR.isEmpty()) {
59 const QString updateComment = data().value(QLatin1String("updateComment")).toString();
60 job = new Phabricator::UpdateDiffRev(sourceFile, baseDir, updateDR, updateComment, doBrowse, this);
61 connect(job, &KJob::finished, this, &PhabricatorJob::diffUpdated);
62 } else {
63 job = new Phabricator::NewDiffRev(sourceFile, baseDir, true, this);
64 connect(job, &KJob::finished, this, &PhabricatorJob::diffCreated);
65 }
66 job->start();
67 Q_EMIT PhabricatorJob::infoMessage(this, QStringLiteral("upload job started"));
68 }
69
70 void diffCreatedOrUpdated(KJob *j, bool created)
71 {
72 if (j->error() != 0) {
73 setError(j->error());
76 qCCritical(PLUGIN_PHABRICATOR) << "Could not upload the patch" << j->errorString();
77 emitResult();
78 return;
79 }
80
81 if (created) {
82 Phabricator::NewDiffRev const *job = qobject_cast<Phabricator::NewDiffRev *>(j);
83 qCWarning(PLUGIN_PHABRICATOR) << "new diff:" << job->diffURI();
84 setOutput({{QStringLiteral("url"), job->diffURI()}});
85 } else {
86 Phabricator::UpdateDiffRev const *job = qobject_cast<Phabricator::UpdateDiffRev *>(j);
87 qCWarning(PLUGIN_PHABRICATOR) << "updated diff" << job->requestId() << ":" << job->diffURI();
88 setOutput({{QStringLiteral("url"), job->diffURI()}});
89 Q_EMIT PhabricatorJob::infoMessage(this, QStringLiteral("updated diff %1: %2").arg(job->requestId()).arg(job->diffURI()));
90 }
91 emitResult();
92 }
93
94 void diffCreated(KJob *j)
95 {
96 diffCreatedOrUpdated(j, true);
97 }
98
99 void diffUpdated(KJob *j)
100 {
101 diffCreatedOrUpdated(j, false);
102 }
103
104 QString m_drTitle;
105};
106
107class PhabricatorPlugin : public Purpose::PluginBase
108{
110public:
111 using PluginBase::PluginBase;
112 Purpose::Job *createJob() const override
113 {
114 return new PhabricatorJob;
115 }
116};
117
118K_PLUGIN_CLASS_WITH_JSON(PhabricatorPlugin, "phabricatorplugin.json")
119
120#include "phabricatorplugin.moc"
void setErrorText(const QString &errorText)
virtual QString errorString() const
void emitResult()
int error() const
void finished(KJob *job)
void warning(KJob *job, const QString &message)
void setError(int errorCode)
void infoMessage(KJob *job, const QString &message)
virtual Q_SCRIPTABLE void start()=0
#define K_PLUGIN_CLASS_WITH_JSON(classname, jsonFile)
Job that will actually perform the sharing.
Definition job.h:34
QJsonObject data
Represents the data the job will have available to perform its task.
Definition job.h:39
Base class to implement by plugins.
Definition pluginbase.h:29
QString i18n(const char *text, const TYPE &arg...)
char * toString(const EngineQuery &query)
qint64 size() const const
QJsonValue value(QLatin1StringView key) const const
bool toBool(bool defaultValue) const const
QString toString() const const
Q_EMITQ_EMIT
Q_OBJECTQ_OBJECT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T qobject_cast(QObject *object)
bool isEmpty() const const
int localeAwareCompare(QStringView s1, QStringView s2)
QString toLocalFile() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:08:47 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.