diff --git a/src/util/capacity.h b/src/util/capacity.h index bd11bca..632e847 100644 --- a/src/util/capacity.h +++ b/src/util/capacity.h @@ -15,9 +15,9 @@ * along with this program. If not, see .* *************************************************************************/ -#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: diff --git a/src/util/externalcommand.cpp b/src/util/externalcommand.cpp index 02f60b8..8b67bb6 100644 --- a/src/util/externalcommand.cpp +++ b/src/util/externalcommand.cpp @@ -40,17 +40,31 @@ #include #include +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()) { + 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()) { + 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(&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 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; +} diff --git a/src/util/externalcommand.h b/src/util/externalcommand.h index 43b151c..f045f4e 100644 --- a/src/util/externalcommand.h +++ b/src/util/externalcommand.h @@ -16,8 +16,7 @@ * along with this program. If not, see .* *************************************************************************/ -#if !defined(KPMCORE_EXTERNALCOMMAND_H) - +#ifndef KPMCORE_EXTERNALCOMMAND_H #define KPMCORE_EXTERNALCOMMAND_H #include "util/libpartitionmanagerexport.h" @@ -31,8 +30,11 @@ #include #include +#include + 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 d; }; #endif diff --git a/src/util/libpartitionmanagerexport.h b/src/util/libpartitionmanagerexport.h index 457f0e8..9459e65 100644 --- a/src/util/libpartitionmanagerexport.h +++ b/src/util/libpartitionmanagerexport.h @@ -15,15 +15,9 @@ * along with this program. If not, see .* *************************************************************************/ -// #include "libpartitionmanager_export.h" - -#if !defined(KPMCORE_LIBPARTITIONMANAGEREXPORT_H) -#define KPMCORE_LIBPARTITIONMANAGEREXPORT_H +#ifndef LIBKPMCORE_EXPORT #include - -#if !defined(LIBKPMCORE_EXPORT) #define LIBKPMCORE_EXPORT Q_DECL_EXPORT -#endif #endif