Simplify some External Command functions.
This commit is contained in:
parent
5b80c6045c
commit
2ed9953694
|
@ -259,7 +259,6 @@ static void writeEntry(QTextStream& s, const FstabEntry& entry)
|
||||||
|
|
||||||
bool writeMountpoints(const FstabEntryList& fstabEntries, const QString& filename)
|
bool writeMountpoints(const FstabEntryList& fstabEntries, const QString& filename)
|
||||||
{
|
{
|
||||||
Report report(nullptr);
|
|
||||||
QString fstabContents;
|
QString fstabContents;
|
||||||
QTextStream out(&fstabContents);
|
QTextStream out(&fstabContents);
|
||||||
|
|
||||||
|
@ -267,5 +266,5 @@ bool writeMountpoints(const FstabEntryList& fstabEntries, const QString& filenam
|
||||||
writeEntry(out, e);
|
writeEntry(out, e);
|
||||||
|
|
||||||
ExternalCommand cmd;
|
ExternalCommand cmd;
|
||||||
return cmd.createFile(report, fstabContents.toLocal8Bit(), filename);
|
return cmd.createFile(fstabContents.toLocal8Bit(), filename);
|
||||||
}
|
}
|
||||||
|
|
|
@ -124,11 +124,6 @@ bool ExternalCommand::start(int timeout)
|
||||||
if (command().isEmpty())
|
if (command().isEmpty())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (!QDBusConnection::systemBus().isConnected()) {
|
|
||||||
qWarning() << QDBusConnection::systemBus().lastError().message();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (report())
|
if (report())
|
||||||
report()->setCommand(xi18nc("@info:status", "Command: %1 %2", command(), args().join(QStringLiteral(" "))));
|
report()->setCommand(xi18nc("@info:status", "Command: %1 %2", command(), args().join(QStringLiteral(" "))));
|
||||||
|
|
||||||
|
@ -139,10 +134,9 @@ bool ExternalCommand::start(int timeout)
|
||||||
if (cmd.isEmpty())
|
if (cmd.isEmpty())
|
||||||
cmd = QStandardPaths::findExecutable(command(), { QStringLiteral("/sbin/"), QStringLiteral("/usr/sbin/"), QStringLiteral("/usr/local/sbin/") });
|
cmd = QStandardPaths::findExecutable(command(), { QStringLiteral("/sbin/"), QStringLiteral("/usr/sbin/"), QStringLiteral("/usr/local/sbin/") });
|
||||||
|
|
||||||
auto *interface = new org::kde::kpmcore::externalcommand(QStringLiteral("org.kde.kpmcore.externalcommand"),
|
auto interface = helperInterface();
|
||||||
QStringLiteral("/Helper"), QDBusConnection::systemBus(), this);
|
if (!interface)
|
||||||
|
return false;
|
||||||
interface->setTimeout(10 * 24 * 3600 * 1000); // 10 days
|
|
||||||
|
|
||||||
bool rval = false;
|
bool rval = false;
|
||||||
|
|
||||||
|
@ -176,18 +170,13 @@ bool ExternalCommand::copyBlocks(const CopySource& source, CopyTarget& target)
|
||||||
bool rval = true;
|
bool rval = true;
|
||||||
const qint64 blockSize = 10 * 1024 * 1024; // number of bytes per block to copy
|
const qint64 blockSize = 10 * 1024 * 1024; // number of bytes per block to copy
|
||||||
|
|
||||||
if (!QDBusConnection::systemBus().isConnected()) {
|
|
||||||
qWarning() << QDBusConnection::systemBus().lastError().message();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO KF6:Use new signal-slot syntax
|
// TODO KF6:Use new signal-slot syntax
|
||||||
connect(m_job, SIGNAL(percent(KJob*, unsigned long)), this, SLOT(emitProgress(KJob*, unsigned long)));
|
connect(m_job, SIGNAL(percent(KJob*, unsigned long)), this, SLOT(emitProgress(KJob*, unsigned long)));
|
||||||
connect(m_job, &KAuth::ExecuteJob::newData, this, &ExternalCommand::emitReport);
|
connect(m_job, &KAuth::ExecuteJob::newData, this, &ExternalCommand::emitReport);
|
||||||
|
|
||||||
auto *interface = new org::kde::kpmcore::externalcommand(QStringLiteral("org.kde.kpmcore.externalcommand"),
|
auto interface = helperInterface();
|
||||||
QStringLiteral("/Helper"), QDBusConnection::systemBus(), this);
|
if (!interface)
|
||||||
interface->setTimeout(10 * 24 * 3600 * 1000); // 10 days
|
return false;
|
||||||
|
|
||||||
QDBusPendingCall pcall = interface->copyblocks(source.path(), source.firstByte(), source.length(),
|
QDBusPendingCall pcall = interface->copyblocks(source.path(), source.firstByte(), source.length(),
|
||||||
target.path(), target.firstByte(), blockSize);
|
target.path(), target.firstByte(), blockSize);
|
||||||
|
@ -222,58 +211,40 @@ bool ExternalCommand::writeData(Report& commandReport, const QByteArray& buffer,
|
||||||
if (report())
|
if (report())
|
||||||
report()->setCommand(xi18nc("@info:status", "Command: %1 %2", command(), args().join(QStringLiteral(" "))));
|
report()->setCommand(xi18nc("@info:status", "Command: %1 %2", command(), args().join(QStringLiteral(" "))));
|
||||||
|
|
||||||
bool rval = true;
|
auto interface = helperInterface();
|
||||||
|
if (!interface)
|
||||||
if (!QDBusConnection::systemBus().isConnected()) {
|
|
||||||
qWarning() << QDBusConnection::systemBus().lastError().message();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
auto *interface = new org::kde::kpmcore::externalcommand(QStringLiteral("org.kde.kpmcore.externalcommand"),
|
|
||||||
QStringLiteral("/Helper"), QDBusConnection::systemBus(), this);
|
|
||||||
interface->setTimeout(10 * 24 * 3600 * 1000); // 10 days
|
|
||||||
|
|
||||||
QDBusPendingCall pcall = interface->writeData(buffer, deviceNode, firstByte);
|
QDBusPendingCall pcall = interface->writeData(buffer, deviceNode, firstByte);
|
||||||
|
return waitForDbusReply(pcall);
|
||||||
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this);
|
|
||||||
QEventLoop loop;
|
|
||||||
|
|
||||||
auto exitLoop = [&] (QDBusPendingCallWatcher *watcher) {
|
|
||||||
loop.exit();
|
|
||||||
if (watcher->isError())
|
|
||||||
qWarning() << watcher->error();
|
|
||||||
else {
|
|
||||||
QDBusPendingReply<bool> reply = *watcher;
|
|
||||||
rval = reply.argumentAt<0>();
|
|
||||||
}
|
|
||||||
setExitCode(!rval);
|
|
||||||
};
|
|
||||||
|
|
||||||
connect(watcher, &QDBusPendingCallWatcher::finished, exitLoop);
|
|
||||||
loop.exec();
|
|
||||||
|
|
||||||
return rval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ExternalCommand::createFile(Report& commandReport, const QByteArray& buffer, const QString& deviceNode)
|
bool ExternalCommand::createFile(const QByteArray& buffer, const QString& deviceNode)
|
||||||
{
|
{
|
||||||
d->m_Report = commandReport.newChild();
|
auto interface = helperInterface();
|
||||||
if (report())
|
if (!interface)
|
||||||
report()->setCommand(xi18nc("@info:status", "Command: %1 %2", command(), args().join(QStringLiteral(" "))));
|
return false;
|
||||||
|
|
||||||
bool rval = true;
|
QDBusPendingCall pcall = interface->createFile(buffer, deviceNode);
|
||||||
|
return waitForDbusReply(pcall);
|
||||||
|
}
|
||||||
|
|
||||||
|
OrgKdeKpmcoreExternalcommandInterface* ExternalCommand::helperInterface()
|
||||||
|
{
|
||||||
if (!QDBusConnection::systemBus().isConnected()) {
|
if (!QDBusConnection::systemBus().isConnected()) {
|
||||||
qWarning() << QDBusConnection::systemBus().lastError().message();
|
qWarning() << QDBusConnection::systemBus().lastError().message();
|
||||||
return false;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto *interface = new org::kde::kpmcore::externalcommand(QStringLiteral("org.kde.kpmcore.externalcommand"),
|
auto *interface = new org::kde::kpmcore::externalcommand(QStringLiteral("org.kde.kpmcore.externalcommand"),
|
||||||
QStringLiteral("/Helper"), QDBusConnection::systemBus(), this);
|
QStringLiteral("/Helper"), QDBusConnection::systemBus(), this);
|
||||||
interface->setTimeout(10 * 24 * 3600 * 1000); // 10 days
|
interface->setTimeout(10 * 24 * 3600 * 1000); // 10 days
|
||||||
|
return interface;
|
||||||
|
}
|
||||||
|
|
||||||
QDBusPendingCall pcall = interface->createFile(buffer, deviceNode);
|
bool ExternalCommand::waitForDbusReply(QDBusPendingCall &pcall)
|
||||||
|
{
|
||||||
|
bool rval = true;
|
||||||
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this);
|
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this);
|
||||||
QEventLoop loop;
|
QEventLoop loop;
|
||||||
|
|
||||||
|
@ -294,7 +265,6 @@ bool ExternalCommand::createFile(Report& commandReport, const QByteArray& buffer
|
||||||
return rval;
|
return rval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool ExternalCommand::write(const QByteArray& input)
|
bool ExternalCommand::write(const QByteArray& input)
|
||||||
{
|
{
|
||||||
if ( qEnvironmentVariableIsSet( "KPMCORE_DEBUG" ))
|
if ( qEnvironmentVariableIsSet( "KPMCORE_DEBUG" ))
|
||||||
|
|
|
@ -38,6 +38,8 @@ class Report;
|
||||||
class CopySource;
|
class CopySource;
|
||||||
class CopyTarget;
|
class CopyTarget;
|
||||||
class QDBusInterface;
|
class QDBusInterface;
|
||||||
|
class QDBusPendingCall;
|
||||||
|
class OrgKdeKpmcoreExternalcommandInterface;
|
||||||
|
|
||||||
struct ExternalCommandPrivate;
|
struct ExternalCommandPrivate;
|
||||||
|
|
||||||
|
@ -71,7 +73,7 @@ public:
|
||||||
public:
|
public:
|
||||||
bool copyBlocks(const CopySource& source, CopyTarget& target);
|
bool copyBlocks(const CopySource& source, CopyTarget& target);
|
||||||
bool writeData(Report& commandReport, const QByteArray& buffer, const QString& deviceNode, const quint64 firstByte); // same as copyBlocks but from QByteArray
|
bool writeData(Report& commandReport, const QByteArray& buffer, const QString& deviceNode, const quint64 firstByte); // same as copyBlocks but from QByteArray
|
||||||
bool createFile(Report& commandReport, const QByteArray& buffer, const QString& deviceNode); // same as writeData but creates a new file
|
bool createFile(const QByteArray& buffer, const QString& deviceNode); // similar to writeData but creates a new file
|
||||||
|
|
||||||
/**< @param cmd the command to run */
|
/**< @param cmd the command to run */
|
||||||
void setCommand(const QString& cmd);
|
void setCommand(const QString& cmd);
|
||||||
|
@ -129,6 +131,8 @@ public Q_SLOTS:
|
||||||
private:
|
private:
|
||||||
void setExitCode(int i);
|
void setExitCode(int i);
|
||||||
void onReadOutput();
|
void onReadOutput();
|
||||||
|
bool waitForDbusReply(QDBusPendingCall &pcall);
|
||||||
|
OrgKdeKpmcoreExternalcommandInterface* helperInterface();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<ExternalCommandPrivate> d;
|
std::unique_ptr<ExternalCommandPrivate> d;
|
||||||
|
|
Loading…
Reference in New Issue