d-pointerize ExternalCommand class.

This commit is contained in:
Andrius Štikonas 2018-04-12 00:47:40 +03:00
parent 9e107c8136
commit 221d7aded6
4 changed files with 118 additions and 56 deletions

View File

@ -15,9 +15,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
*************************************************************************/
#if !defined(KPMCORE_CAPACITY_H)
#ifndef KPMCORE_CAPACITY_H
#define KPMCORE_CAPACITY_H
#include "util/libpartitionmanagerexport.h"
class Partition;
@ -40,7 +40,7 @@ public:
/** Type of capacity to print */
enum class Type { Used, Available, Total };
/** Flags for printing */
enum Flag { NoFlags = 0, AppendUnit = 1, AppendBytes = 2 };
enum class Flag { NoFlags = 0, AppendUnit = 1, AppendBytes = 2 };
Q_DECLARE_FLAGS(Flags, Flag)
public:

View File

@ -40,17 +40,31 @@
#include <KJob>
#include <KLocalizedString>
struct ExternalCommandPrivate
{
QVariantMap arguments;
Report *m_Report;
QString m_Command;
QStringList m_Args;
int m_ExitCode;
QByteArray m_Output;
QByteArray m_Input;
};
/** Creates a new ExternalCommand instance without Report.
@param cmd the command to run
@param args the arguments to pass to the command
*/
ExternalCommand::ExternalCommand(const QString& cmd, const QStringList& args, const QProcess::ProcessChannelMode processChannelMode) :
m_Report(nullptr),
m_Command(cmd),
m_Args(args),
m_ExitCode(-1),
m_Output()
d(std::make_unique<ExternalCommandPrivate>())
{
d->m_Report = nullptr;
d->m_Command = cmd;
d->m_Args = args;
d->m_ExitCode = -1;
d->m_Output = QByteArray();
setup(processChannelMode);
}
@ -60,19 +74,25 @@ ExternalCommand::ExternalCommand(const QString& cmd, const QStringList& args, co
@param args the arguments to pass to the command
*/
ExternalCommand::ExternalCommand(Report& report, const QString& cmd, const QStringList& args, const QProcess::ProcessChannelMode processChannelMode) :
m_Report(report.newChild()),
m_Command(cmd),
m_Args(args),
m_ExitCode(-1),
m_Output()
d(std::make_unique<ExternalCommandPrivate>())
{
d->m_Report = report.newChild();
d->m_Command = cmd;
d->m_Args = args;
d->m_ExitCode = -1;
d->m_Output = QByteArray();
setup(processChannelMode);
}
ExternalCommand::~ExternalCommand()
{
}
void ExternalCommand::setup(const QProcess::ProcessChannelMode processChannelMode)
{
arguments.insert(QStringLiteral("environment"), QStringList() << QStringLiteral("LC_ALL=C") << QStringLiteral("LVM_SUPPRESS_FD_WARNINGS=1"));
arguments.insert(QStringLiteral("processChannelMode"), processChannelMode);
d->arguments.insert(QStringLiteral("environment"), QStringList() << QStringLiteral("LC_ALL=C") << QStringLiteral("LVM_SUPPRESS_FD_WARNINGS=1"));
d->arguments.insert(QStringLiteral("processChannelMode"), processChannelMode);
// connect(this, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), this, &ExternalCommand::onFinished);
// connect(this, &ExternalCommand::readyReadStandardOutput, this, &ExternalCommand::onReadOutput);
@ -112,7 +132,7 @@ bool ExternalCommand::start(int timeout)
CoreBackendManager::self()->Uuid(),
cmd,
args(),
m_Input,
d->m_Input,
QStringList());
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this);
@ -127,7 +147,7 @@ bool ExternalCommand::start(int timeout)
else {
QDBusPendingReply<QVariantMap> reply = *watcher;
m_Output = reply.value()[QStringLiteral("output")].toByteArray();
d->m_Output = reply.value()[QStringLiteral("output")].toByteArray();
setExitCode(reply.value()[QStringLiteral("exitCode")].toInt());
rval = true;
}
@ -188,7 +208,7 @@ bool ExternalCommand::copyBlocks(CopySource& source, CopyTarget& target)
bool ExternalCommand::write(const QByteArray& input)
{
m_Input = input;
d->m_Input = input;
return true;
}
@ -241,3 +261,53 @@ void ExternalCommand::onFinished(int exitCode, QProcess::ExitStatus exitStatus)
Q_UNUSED(exitStatus)
setExitCode(exitCode);
}
void ExternalCommand::setCommand(const QString& cmd)
{
d->m_Command = cmd;
}
const QString& ExternalCommand::command() const
{
return d->m_Command;
}
const QStringList& ExternalCommand::args() const
{
return d->m_Args;
}
void ExternalCommand::addArg(const QString& s)
{
d->m_Args << s;
}
void ExternalCommand::setArgs(const QStringList& args)
{
d->m_Args = args;
}
int ExternalCommand::exitCode() const
{
return d->m_ExitCode;
}
const QString ExternalCommand::output() const
{
return QString::fromLocal8Bit(d->m_Output);
}
const QByteArray& ExternalCommand::rawOutput() const
{
return d->m_Output;
}
Report* ExternalCommand::report()
{
return d->m_Report;
}
void ExternalCommand::setExitCode(int i)
{
d->m_ExitCode = i;
}

View File

@ -16,8 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
*************************************************************************/
#if !defined(KPMCORE_EXTERNALCOMMAND_H)
#ifndef KPMCORE_EXTERNALCOMMAND_H
#define KPMCORE_EXTERNALCOMMAND_H
#include "util/libpartitionmanagerexport.h"
@ -31,8 +30,11 @@
#include <QtGlobal>
#include <QVariant>
#include <memory>
class KJob;
class Report;
struct ExternalCommandPrivate;
/** An external command.
@ -50,14 +52,24 @@ public:
explicit ExternalCommand(const QString& cmd = QString(), const QStringList& args = QStringList(), const QProcess::ProcessChannelMode processChannelMode = QProcess::MergedChannels);
explicit ExternalCommand(Report& report, const QString& cmd = QString(), const QStringList& args = QStringList(), const QProcess::ProcessChannelMode processChannelMode = QProcess::MergedChannels);
~ExternalCommand();
public:
bool copyBlocks(CopySource& source, CopyTarget& target);
void setCommand(const QString& cmd) { m_Command = cmd; } /**< @param cmd the command to run */
const QString& command() const { return m_Command; } /**< @return the command to run */
void addArg(const QString& s) { m_Args << s; } /**< @param s the argument to add */
const QStringList& args() const { return m_Args; } /**< @return the arguments */
void setArgs(const QStringList& args) { m_Args = args; } /**< @param args the new arguments */
/**< @param cmd the command to run */
void setCommand(const QString& cmd);
/**< @return the command to run */
const QString& command() const;
/**< @return the arguments */
const QStringList& args() const;
/**< @param s the argument to add */
void addArg(const QString& s);
/**< @param args the new arguments */
void setArgs(const QStringList& args);
bool write(const QByteArray& input); /**< @param input the input for the program */
bool startCopyBlocks();
@ -65,21 +77,16 @@ public:
bool waitFor(int timeout = 30000);
bool run(int timeout = 30000);
int exitCode() const {
return m_ExitCode; /**< @return the exit code */
}
/**< @return the exit code */
int exitCode() const;
const QString output() const {
return QString::fromLocal8Bit(m_Output); /**< @return the command output */
}
/**< @return the command output */
const QString output() const;
/**< @return the command output */
const QByteArray& rawOutput() const;
const QByteArray& rawOutput() const {
return m_Output; /**< @return the command output */
}
Report* report() {
return m_Report; /**< @return pointer to the Report or nullptr */
}
/**< @return pointer to the Report or nullptr */
Report* report();
void emitReport(const QVariantMap& report) { emit reportSignal(report); }
@ -91,23 +98,14 @@ public Q_SLOTS:
void emitProgress(KJob*, unsigned long percent) { emit progress(percent); };
protected:
void setExitCode(int i) {
m_ExitCode = i;
}
void setExitCode(int i);
void setup(const QProcess::ProcessChannelMode processChannelMode);
void onFinished(int exitCode, QProcess::ExitStatus exitStatus);
void onReadOutput();
private:
QVariantMap arguments;
Report *m_Report;
QString m_Command;
QStringList m_Args;
int m_ExitCode;
QByteArray m_Output;
QByteArray m_Input;
std::unique_ptr<ExternalCommandPrivate> d;
};
#endif

View File

@ -15,15 +15,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
*************************************************************************/
// #include "libpartitionmanager_export.h"
#if !defined(KPMCORE_LIBPARTITIONMANAGEREXPORT_H)
#define KPMCORE_LIBPARTITIONMANAGEREXPORT_H
#ifndef LIBKPMCORE_EXPORT
#include <QtGlobal>
#if !defined(LIBKPMCORE_EXPORT)
#define LIBKPMCORE_EXPORT Q_DECL_EXPORT
#endif
#endif