LCOV - code coverage report
Current view: top level - lang/qt/src - qgpgmewkspublishjob.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 77 0.0 %
Date: 2016-12-01 18:45:36 Functions: 0 14 0.0 %

          Line data    Source code
       1             : /* wkspublishjob.cpp
       2             : 
       3             :     Copyright (c) 2016 Intevation GmbH
       4             : 
       5             :     QGpgME is free software; you can redistribute it and/or
       6             :     modify it under the terms of the GNU General Public License as
       7             :     published by the Free Software Foundation; either version 2 of the
       8             :     License, or (at your option) any later version.
       9             : 
      10             :     QGpgME is distributed in the hope that it will be useful,
      11             :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13             :     General Public License for more details.
      14             : 
      15             :     You should have received a copy of the GNU General Public License along
      16             :     with this program; if not, write to the Free Software Foundation, Inc.,
      17             :     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
      18             : 
      19             :     In addition, as a special exception, the copyright holders give
      20             :     permission to link the code of this program with any edition of
      21             :     the Qt library by Trolltech AS, Norway (or with modified versions
      22             :     of Qt that use the same license as Qt), and distribute linked
      23             :     combinations including the two.  You must obey the GNU General
      24             :     Public License in all respects for all of the code used other than
      25             :     Qt.  If you modify this file, you may extend this exception to
      26             :     your version of the file, but you are not obligated to do so.  If
      27             :     you do not wish to do so, delete this exception statement from
      28             :     your version.
      29             : */
      30             : 
      31             : #ifdef HAVE_CONFIG_H
      32             :  #include "config.h"
      33             : #endif
      34             : 
      35             : #include "qgpgmewkspublishjob.h"
      36             : 
      37             : #include "context.h"
      38             : #include "key.h"
      39             : #include "util.h"
      40             : 
      41             : #include <QFileInfo>
      42             : #include <QDir>
      43             : #include <QProcess>
      44             : 
      45             : /* Timeout for the WKS Processes will be 5 Minutes as
      46             :  * they can involve pinentry questions. */
      47             : #define TIMEOUT_VALUE (5*60*1000)
      48             : 
      49             : using namespace QGpgME;
      50             : using namespace GpgME;
      51             : 
      52           0 : QGpgMEWKSPublishJob::QGpgMEWKSPublishJob(Context *context)
      53           0 :     : mixin_type(context)
      54             : {
      55           0 :     lateInitialization();
      56           0 : }
      57             : 
      58           0 : QGpgMEWKSPublishJob::~QGpgMEWKSPublishJob() {}
      59             : 
      60           0 : static QString getWKSClient()
      61             : {
      62           0 :     auto libexecdir = QString::fromLocal8Bit(dirInfo("libexecdir"));
      63           0 :     if (libexecdir.isEmpty()) {
      64           0 :         return QString();
      65             :     }
      66             : 
      67           0 :     const QFileInfo fi(QDir(libexecdir).absoluteFilePath(QStringLiteral("gpg-wks-client")));
      68           0 :     if (fi.exists() && fi.isExecutable()) {
      69           0 :         return fi.absoluteFilePath();
      70             :     }
      71           0 :     return QString();
      72             : }
      73             : 
      74           0 : static QGpgMEWKSPublishJob::result_type check_worker(const QString &mail)
      75             : {
      76           0 :     if (mail.isEmpty()) {
      77             :         return std::make_tuple (Error(make_error(GPG_ERR_INV_ARG)),
      78           0 :                                 QByteArray(), QByteArray(), QString(), Error());
      79             :     }
      80             : 
      81           0 :     const auto wksPath = getWKSClient();
      82           0 :     if (wksPath.isEmpty()) {
      83             :         return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
      84           0 :                                 QByteArray(), QByteArray(), QString(), Error());
      85             :     }
      86             : 
      87             :     /* QProcess instead of engine_spawn because engine_spawn does not communicate
      88             :      * the return value of the process and we are in qt anyway. */
      89           0 :     QProcess proc;
      90           0 :     proc.setProgram(wksPath);
      91           0 :     proc.setArguments(QStringList() << QStringLiteral("--supported") << mail);
      92           0 :     proc.start();
      93           0 :     if (!proc.waitForStarted()) {
      94             :         return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
      95           0 :                                 QByteArray(), QByteArray(), QString(), Error());
      96             :     }
      97           0 :     if (!proc.waitForFinished(TIMEOUT_VALUE)) {
      98             :         return std::make_tuple (Error(make_error(GPG_ERR_TIMEOUT)),
      99           0 :                                 QByteArray(), QByteArray(), QString(), Error());
     100             :     }
     101           0 :     if (proc.exitStatus() == QProcess::NormalExit && proc.exitCode() == 0) {
     102           0 :         return std::make_tuple (Error(), QByteArray(), QByteArray(), QString(), Error());
     103             :     }
     104             :     return std::make_tuple (Error(make_error(GPG_ERR_NOT_ENABLED)),
     105           0 :                             QByteArray(), QByteArray(), QString(), Error());
     106             : }
     107             : 
     108           0 : static QGpgMEWKSPublishJob::result_type create_worker(const char *fpr, const QString &mail)
     109             : {
     110           0 :     if (mail.isEmpty() || !fpr) {
     111             :         return std::make_tuple (Error(make_error(GPG_ERR_INV_ARG)),
     112           0 :                                 QByteArray(), QByteArray(), QString(), Error());
     113             :     }
     114             : 
     115           0 :     const auto wksPath = getWKSClient();
     116           0 :     if (wksPath.isEmpty()) {
     117             :         return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
     118           0 :                                 QByteArray(), QByteArray(), QString(), Error());
     119             :     }
     120             : 
     121           0 :     QProcess proc;
     122           0 :     proc.setProgram(wksPath);
     123           0 :     proc.setArguments(QStringList() << QStringLiteral("--create")
     124           0 :                                     << QLatin1String(fpr)
     125           0 :                                     << mail);
     126           0 :     proc.start();
     127           0 :     if (!proc.waitForStarted()) {
     128             :         return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
     129           0 :                                 QByteArray(), QByteArray(), QString(), Error());
     130             :     }
     131             : 
     132           0 :     if (!proc.waitForFinished(TIMEOUT_VALUE)) {
     133             :         return std::make_tuple (Error(make_error(GPG_ERR_TIMEOUT)),
     134           0 :                                 QByteArray(), QByteArray(), QString(), Error());
     135             :     }
     136           0 :     if (proc.exitStatus() == QProcess::NormalExit && proc.exitCode() == 0) {
     137             :         return std::make_tuple (Error(), proc.readAllStandardOutput(),
     138           0 :                                 proc.readAllStandardError(), QString(), Error());
     139             :     }
     140             :     return std::make_tuple (Error(make_error(GPG_ERR_GENERAL)),
     141           0 :                             proc.readAllStandardOutput(), proc.readAllStandardError(), QString(), Error());
     142             : }
     143             : 
     144           0 : static QGpgMEWKSPublishJob::result_type receive_worker(const QByteArray &response)
     145             : {
     146           0 :     if (response.isEmpty()) {
     147             :         return std::make_tuple (Error(make_error(GPG_ERR_INV_ARG)),
     148           0 :                                 QByteArray(), QByteArray(), QString(), Error());
     149             :     }
     150             : 
     151           0 :     const auto wksPath = getWKSClient();
     152           0 :     if (wksPath.isEmpty()) {
     153             :         return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
     154           0 :                                 QByteArray(), QByteArray(), QString(), Error());
     155             :     }
     156             : 
     157           0 :     QProcess proc;
     158           0 :     proc.setProgram(wksPath);
     159           0 :     proc.setArguments(QStringList() << QStringLiteral("--receive"));
     160           0 :     proc.start();
     161           0 :     if (!proc.waitForStarted()) {
     162             :         return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
     163           0 :                                 QByteArray(), QByteArray(), QString(), Error());
     164             :     }
     165           0 :     proc.write(response);
     166           0 :     proc.closeWriteChannel();
     167           0 :     if (!proc.waitForFinished(TIMEOUT_VALUE)) {
     168             :         return std::make_tuple (Error(make_error(GPG_ERR_TIMEOUT)),
     169           0 :                                 QByteArray(), QByteArray(), QString(), Error());
     170             :     }
     171           0 :     if (proc.exitStatus() == QProcess::NormalExit && proc.exitCode() == 0) {
     172             :         return std::make_tuple (Error(), proc.readAllStandardOutput(),
     173           0 :                                 proc.readAllStandardError(), QString(), Error());
     174             :     }
     175             :     return std::make_tuple (Error(make_error(GPG_ERR_GENERAL)),
     176           0 :                             proc.readAllStandardOutput(), proc.readAllStandardError(), QString(), Error());
     177             : }
     178             : 
     179           0 : void QGpgMEWKSPublishJob::startCheck(const QString &mailbox)
     180             : {
     181           0 :     run(std::bind(&check_worker, mailbox));
     182           0 : }
     183             : 
     184           0 : void QGpgMEWKSPublishJob::startCreate(const char *fpr, const QString &mailbox) {
     185           0 :     run(std::bind(&create_worker, fpr, mailbox));
     186           0 : }
     187             : 
     188           0 : void QGpgMEWKSPublishJob::startReceive(const QByteArray &response)
     189             : {
     190           0 :     run(std::bind(&receive_worker, response));
     191           0 : }
     192             : 
     193             : #include "qgpgmewkspublishjob.moc"

Generated by: LCOV version 1.11