diff --git a/src/gpgcardgui/gpgcard.cpp b/src/gpgcardgui/gpgcard.cpp index 8c023a7..3a41176 100644 --- a/src/gpgcardgui/gpgcard.cpp +++ b/src/gpgcardgui/gpgcard.cpp @@ -1,85 +1,83 @@ /* Copyright (C) 2020 by g10 Code GmbH * * This file is Free Software under the GNU GPL (v>=2) * and comes with ABSOLUTELY NO WARRANTY! * See LICENSE.txt for details. */ #include "gpgcard.h" #include #include #include #include #include GpgCard::GpgCard(QObject *parent): QObject(parent), mValid(false) { const QFileInfo gpgInfo(Kleo::gpgPath()); QStringList paths; paths << gpgInfo.absoluteDir().absolutePath(); QString gpgCardPath = QStandardPaths::findExecutable(QStringLiteral("gpg-card"), paths); if (gpgCardPath.isEmpty()) { qDebug() << "Failed to find gpg-card in: " << paths; } gpgCardPath = QStandardPaths::findExecutable(QStringLiteral("gpg-card")); if (gpgCardPath.isEmpty()) { qDebug() << "Failed to find gpg-card in system paths"; } else { qDebug() << "Using: " << gpgCardPath; } if (QFileInfo(gpgCardPath).isExecutable()) { mValid = true; } connect(&mProc, QOverload::of(&QProcess::finished), - [=] (int exitCode, QProcess::ExitStatus exitStatus) { + [this] (int exitCode, QProcess::ExitStatus exitStatus) { /* Guranteed to be utf-8 by using --with-colons */ - mStdout = QString::fromUtf8 (mProc.readAllStandardOutput()); - mStderr = QString::fromUtf8 (mProc.readAllStandardError()); + mStdout = QString::fromUtf8(mProc.readAllStandardOutput()); + mStderr = QString::fromUtf8(mProc.readAllStandardError()); - Q_EMIT finished (); + Q_EMIT finished(); }); - mProc.setProgram(gpgCardPath); } - void GpgCard::start(const QStringList &commands) { mStderr = QString(); mStdout = QString(); if (!mValid) { return; } QStringList args; args << QStringLiteral("--with-colons"); args += commands; mProc.setArguments(args); mProc.start(); } QString GpgCard::stdOut() const { return mStdout; } QString GpgCard::stdErr() const { return mStderr; } int GpgCard::exitCode() const { return mExitCode; } diff --git a/src/gpgcardgui/gpgcard.h b/src/gpgcardgui/gpgcard.h new file mode 100644 index 0000000..9ea1da9 --- /dev/null +++ b/src/gpgcardgui/gpgcard.h @@ -0,0 +1,42 @@ +/* Copyright (C) 2020 by g10 Code GmbH + * + * This file is Free Software under the GNU GPL (v>=2) + * and comes with ABSOLUTELY NO WARRANTY! + * See LICENSE.txt for details. + */ +#pragma once +#include +#include + +/** The GpgCard class to wrap a call to the gpg-card tool. */ +class GpgCard: public QObject +{ + Q_OBJECT; + +public: + explicit GpgCard(QObject *parent = nullptr); + + /* Start the command and signal finished with the result + * when done. */ + void start(const QStringList &commands); + + /* Get the stdOut from the call */ + QString stdOut () const; + + /* Get the stdErr from the call */ + QString stdErr () const; + + /* The exitCode of gpg-card. If the process + * did not exit normally -1 is returned. */ + int exitCode () const; + +Q_SIGNALS: + void finished(); + +private: + QProcess mProc; + QString mStdout; + QString mStderr; + bool mValid; + int mExitCode; +};