diff --git a/src/util/externalcommandhelper.h b/src/util/externalcommandhelper.h --- a/src/util/externalcommandhelper.h +++ b/src/util/externalcommandhelper.h @@ -1,5 +1,6 @@ /************************************************************************* * Copyright (C) 2017-2018 by Andrius Štikonas * + * Copyright (C) 2019 by Shubham * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * @@ -24,8 +25,12 @@ #include #include -#include #include +#include +#include + +#define HELPER_MAIN() \ + int main(int argc, char **argv) { ExternalCommandHelper helper; return helper.helperMain(argc, argv); } using namespace KAuth; @@ -41,20 +46,20 @@ public: bool readData(const QString& sourceDevice, QByteArray& buffer, const qint64 offset, const qint64 size); bool writeData(const QString& targetDevice, const QByteArray& buffer, const qint64 offset); + int helperMain(int argc, char **argv); public Q_SLOTS: - ActionReply init(const QVariantMap& args); Q_SCRIPTABLE QVariantMap start(const QString& command, const QStringList& arguments, const QByteArray& input, const int processChannelMode); Q_SCRIPTABLE QVariantMap copyblocks(const QString& sourceDevice, const qint64 sourceFirstByte, const qint64 sourceLength, const QString& targetDevice, const qint64 targetFirstByte, const qint64 blockSize); Q_SCRIPTABLE bool writeData(const QByteArray& buffer, const QString& targetDevice, const qint64 targetFirstByte); Q_SCRIPTABLE void exit(); private: - void onReadOutput(); - std::unique_ptr m_loop; QProcess m_cmd; + // QByteArray output; +// void onReadOutput(); }; -#endif +#endif // KPMCORE_EXTERNALCOMMANDHELPER_H diff --git a/src/util/externalcommandhelper.cpp b/src/util/externalcommandhelper.cpp --- a/src/util/externalcommandhelper.cpp +++ b/src/util/externalcommandhelper.cpp @@ -1,5 +1,6 @@ /************************************************************************* * Copyright (C) 2017-2018 by Andrius Štikonas * + * Copyright (C) 2019 by Shubham * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * @@ -42,51 +43,54 @@ * command execution requests from the application that started the helper. * */ -ActionReply ExternalCommandHelper::init(const QVariantMap& args) + +/** Reads the given number of bytes from the sourceDevice into the given buffer. + @param argc argument count + @param argv argument vector + @return zero on success, non-zero on failure +*/ +int ExternalCommandHelper::helperMain(int argc, char **argv) { - Q_UNUSED(args) + QCoreApplication app(argc, argv); - ActionReply reply; - - if (!QDBusConnection::systemBus().isConnected() || !QDBusConnection::systemBus().registerService(QStringLiteral("org.kde.kpmcore.helperinterface")) || - !QDBusConnection::systemBus().registerObject(QStringLiteral("/Helper"), this, QDBusConnection::ExportAllSlots)) { + if (!QDBusConnection::systemBus().isConnected() || + !QDBusConnection::systemBus().registerObject(QStringLiteral("/Helper"), this, QDBusConnection::ExportAllSlots) || + !QDBusConnection::systemBus().registerService(QStringLiteral("org.kde.kpmcore.helperinterface")) || + !QDBusConnection::systemBus().registerObject(QStringLiteral("/Application"), this, QDBusConnection::ExportAllSlots) || + !QDBusConnection::systemBus().registerService(QStringLiteral("org.kde.kpmcore.applicationinterface"))) { + qDebug() << "Failed to initialize the Helper"; qWarning() << QDBusConnection::systemBus().lastError().message(); - reply.addData(QStringLiteral("success"), false); - - // Also end the application loop started by KAuth's main() code. Our loop - // exits when our client disappears. Without client we have no reason to - // live. + + // We have no reason to live when Main GUI app has expired qApp->quit(); - - return reply; + + return app.exec(); } m_loop = std::make_unique(); - HelperSupport::progressStep(QVariantMap()); - - // End the loop and return only once the client is done using us. - auto serviceWatcher = - new QDBusServiceWatcher(QStringLiteral("org.kde.kpmcore.applicationinterface"), - QDBusConnection::systemBus(), - QDBusServiceWatcher::WatchForUnregistration, - this); + + // We send zero percent new data on initial start-up + //sendProgress(0); + + // QDBus Service watcher which keeps an eye on the client (Main GUI app) + // End the loop and return only once the client has unregistered over the QDBus. + auto serviceWatcher = new QDBusServiceWatcher(QStringLiteral("org.kde.kpmcore.applicationinterface"), + QDBusConnection::systemBus(), + QDBusServiceWatcher::WatchForUnregistration, + this); + connect(serviceWatcher, &QDBusServiceWatcher::serviceUnregistered, [this]() { m_loop->exit(); }); m_loop->exec(); - reply.addData(QStringLiteral("success"), true); - - // Also end the application loop started by KAuth's main() code. Our loop - // exits when our client disappears. Without client we have no reason to - // live. + qApp->quit(); - - return reply; + + return app.exec(); } - /** Reads the given number of bytes from the sourceDevice into the given buffer. @param sourceDevice device or file to read from @param buffer buffer to store the bytes read in @@ -289,20 +293,20 @@ QDBusConnection::systemBus().unregisterService(QStringLiteral("org.kde.kpmcore.helperinterface")); } -void ExternalCommandHelper::onReadOutput() +/*void ExternalCommandHelper::onReadOutput() { -/* const QByteArray s = cmd.readAllStandardOutput(); + const QByteArray s = cmd.readAllStandardOutput(); - if(output.length() > 10*1024*1024) { // prevent memory overflow for badly corrupted file systems + if(output.length() > 10*1024*1024) { // prevent memory overflow for badly corrupted file systems if (report()) report()->line() << xi18nc("@info:status", "(Command is printing too much output)"); return; } output += s; if (report()) - *report() << QString::fromLocal8Bit(s);*/ -} + *report() << QString::fromLocal8Bit(s); +}*/ KAUTH_HELPER_MAIN("org.kde.kpmcore.externalcommand", ExternalCommandHelper)