diff --git a/src/gpgcardgui/CMakeLists.txt b/src/gpgcardgui/CMakeLists.txt index 961b2d2..1c31b4b 100644 --- a/src/gpgcardgui/CMakeLists.txt +++ b/src/gpgcardgui/CMakeLists.txt @@ -1,29 +1,34 @@ # Copyright (C) 2018 Intevation GmbH # # This file is Free Software under the GNU GPL (v>=2) # and comes with ABSOLUTELY NO WARRANTY! # See LICENSE.txt for details. set(EXECUTABLE_NAME "gpgcardgui") set(EXECUTABLE_SRC main.cpp + gpgcard.cpp + gpgcardgui.cpp + mainwindow.cpp + ${CMAKE_SOURCE_DIR}/src/util/strhelp.c + ${CMAKE_SOURCE_DIR}/src/util/w32-gettext.c ) add_executable(${EXECUTABLE_NAME} ${_add_executable_params} ${EXECUTABLE_SRC} ) target_link_libraries(${EXECUTABLE_NAME} Qt5::Widgets KF5::Libkleo Gpgmepp QGpgme ) if (WIN32) set_target_properties(${EXECUTABLE_NAME} PROPERTIES LINK_FLAGS "-municode") endif(WIN32) install(TARGETS ${EXECUTABLE_NAME} DESTINATION bin) diff --git a/src/gpgcardgui/GPGAbstracrCard.cpp b/src/gpgcardgui/GPGAbstracrCard.cpp deleted file mode 100644 index 31b7e12..0000000 --- a/src/gpgcardgui/GPGAbstracrCard.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "GPGAbstractCard.h" - -GPGCard::GPGAbstractCard::GPGAbstractCard() -{ - QObject *parent; - QString program = "/usr/bin/gpg-card"; // FIXME add autodiscoverw - QStringList arguments; - arguments << "list"; - - QProcess *myProcess = new QProcess(parent); - myProcess->setReadChannel(QProcess::StandardOutput); - QObject::connect(myProcess, &QProcess::readyReadStandardOutput, readStdoutLine); -} - -GPGCard::GPGAbstractCard::~GPGAbstractCard() -{ - -} - -void GPGCard::GPGAbstractCard::RecieveStdout(QString StdOut) -{ - qInfo() << StdOut; -} diff --git a/src/gpgcardgui/GPGAbstractCard.h b/src/gpgcardgui/GPGAbstractCard.h deleted file mode 100644 index 6370ac8..0000000 --- a/src/gpgcardgui/GPGAbstractCard.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once -#include -#include - -class GpgCard; -class GPGAbstractCard { -public: - GPGAbstractCard(); - - // set, getter -private: - gpgcard_process *QProcess; -private slots: - void RecieveStdOut(QString StdOut); -} diff --git a/src/gpgcardgui/gpgcard.cpp b/src/gpgcardgui/gpgcard.cpp new file mode 100644 index 0000000..8809e6a --- /dev/null +++ b/src/gpgcardgui/gpgcard.cpp @@ -0,0 +1,85 @@ +/* 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) { + /* Guranteed to be utf-8 by using --with-colons */ + mStdout = QString::fromUtf8 (mProc.readAllStandardOutput()); + mStderr = QString::fromUtf8 (mProc.readAllStandardError()); + + 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/gpgcardgui.cpp b/src/gpgcardgui/gpgcardgui.cpp new file mode 100644 index 0000000..8cf96bd --- /dev/null +++ b/src/gpgcardgui/gpgcardgui.cpp @@ -0,0 +1,24 @@ +/* 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 "gpgcardgui.h" +#include "mainwindow.h" + +GpgCardGUI::GpgCardGUI(int &argc, char *argv[]): + QApplication(argc, argv) +{ + mMainWin = new MainWindow(); +} + +GpgCardGUI::~GpgCardGUI() +{ + delete mMainWin; +} + +void GpgCardGUI::showWindow() +{ + mMainWin->show(); +} diff --git a/src/gpgcardgui/gpgcardgui.h b/src/gpgcardgui/gpgcardgui.h index 83f83dd..d64fa2d 100644 --- a/src/gpgcardgui/gpgcardgui.h +++ b/src/gpgcardgui/gpgcardgui.h @@ -1,13 +1,34 @@ +/* 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 -class GpgCard; -class BasicCard { +class MainWindow; + + +/** @brief Resolver Application entry point. + * + * The GpgCardGUI exists to serve as a frontend + * for gpgcard. It is written in a way that the + * widgets are reusable in other applications. With + * the goal to move most of the code to libkleo. + * + * This is the global qApp singleton. */ +class GpgCardGUI: public QApplication +{ + Q_OBJECT + public: - BasicCard(); + explicit GpgCardGUI(int &argc, char *argv[]); + ~GpgCardGUI(); + + void showWindow(); - // set, getter -private slots: - void RecieveStdOut(QString StdOut); -} +private: + MainWindow *mMainWin; +}; diff --git a/src/gpgcardgui/main.cpp b/src/gpgcardgui/main.cpp index 38e147d..626ea0e 100644 --- a/src/gpgcardgui/main.cpp +++ b/src/gpgcardgui/main.cpp @@ -1,81 +1,81 @@ /* Copyright (C) 2018 by Intevation GmbH * * This file is Free Software under the GNU GPL (v>=2) * and comes with ABSOLUTELY NO WARRANTY! * See LICENSE.txt for details. */ /** @file Main entry point for the application. */ #include "strhelp.h" #include "gpgcardgui.h" #include #include #include #include #include #include #include #include #ifndef APPNAME -#define APPNAME "Resolver" +#define APPNAME "GpgCardGUI" #endif #ifndef VERSION #define VERSION "0.0" #endif +#include "gpgcardgui-options.h" + +bool g_debug = false; + +QtMessageHandler g_default_msg_handler = NULL; + +void filterDebugOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) +{ + if (!g_debug && type == QtDebugMsg) { + return; + } + + if (g_default_msg_handler) { + (*g_default_msg_handler)(type, context, msg); + } +} + /** @brief The real entry point to the application. * * @param [in] argc the count of the arguments. * @param [in] argv On GNU/Linux this function expects argv to be in the * native system encoding. On Windows the arguments * shall be UTF-8 * * @returns 0 on success an error code otherwise. */ - - - -void readStdoutLine(QString line) -{ - qInfo() << line; -} - - int main(int argc, char **argv) { /* QApplication setup */ - QApplication app(argc, argv); + GpgCardGUI app(argc, argv); QApplication::setOrganizationName(QStringLiteral(APPNAME)); QApplication::setApplicationName(QStringLiteral(APPNAME)); QApplication::setApplicationVersion(QStringLiteral(VERSION)); - /* Parse the command line */ QCommandLineParser parser; - + options(parser); + parser.process (app); const auto lang = parser.value("lang"); + g_debug = parser.isSet("debug"); + g_default_msg_handler = qInstallMessageHandler(filterDebugOutput); if (!lang.isEmpty()) { qputenv("LANG", lang.toUtf8()); } + app.showWindow(); - QObject *parent; - QString program = "/usr/bin/gpg-card"; - QStringList arguments; - arguments << "list"; - - QProcess *myProcess = new QProcess(parent); - myProcess->setReadChannel(QProcess::StandardOutput); - QObject::connect(myProcess, &QProcess::readyReadStandardOutput, readStdoutLine); - - myProcess->start(program, arguments); - - - return EXIT_SUCCESS; + /* Start the main event loop */ + return app.exec(); } diff --git a/src/gpgcardgui/mainwindow.cpp b/src/gpgcardgui/mainwindow.cpp new file mode 100644 index 0000000..37d094c --- /dev/null +++ b/src/gpgcardgui/mainwindow.cpp @@ -0,0 +1,43 @@ +/* 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 "mainwindow.h" +#include "gpgcard.h" + +#include +#include +#include + +#include "w32-gettext.h" + +MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags): + QMainWindow(parent, flags) +{ + mEdit = new QTextEdit (this); + mEdit->setReadOnly(true); + + setCentralWidget(mEdit); + + // Setup a new command + auto cmd = new GpgCard; + + connect (cmd, &GpgCard::finished, this, [this, cmd] () { + if (cmd->exitCode() != 0) { + qDebug() << "Process exited with: " << cmd->exitCode(); + const auto errMsg = QString::fromUtf8(_("Error")) + + QStringLiteral("%1\n%2").arg(cmd->exitCode()).arg(cmd->stdErr()); + mEdit->setText(errMsg); + } else { + // Here the output should be parsed. + mEdit->setText(cmd->stdOut()); + } + cmd->deleteLater(); + }); + + cmd->start (QStringList() << QStringLiteral("--") + << QStringLiteral("list")); +} diff --git a/src/gpgcardgui/mainwindow.h b/src/gpgcardgui/mainwindow.h new file mode 100644 index 0000000..e374fff --- /dev/null +++ b/src/gpgcardgui/mainwindow.h @@ -0,0 +1,23 @@ +/* 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 + +class QTextEdit; + +/** MainWindow of the application. */ +class MainWindow: public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = nullptr, + Qt::WindowFlags flags = Qt::WindowFlags()); +private: + QTextEdit *mEdit; +};