LCOV - code coverage report
Current view: top level - lang/qt/src - qgpgmewkspublishjob.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 27 77 35.1 %
Date: 2016-09-12 13:07:23 Functions: 8 14 57.1 %

          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             : #include "qgpgmewkspublishjob.h"
      32             : 
      33             : #include "context.h"
      34             : #include "key.h"
      35             : #include "util.h"
      36             : 
      37             : #include <QFileInfo>
      38             : #include <QDir>
      39             : #include <QProcess>
      40             : 
      41             : /* Timeout for the WKS Processes will be 5 Minutes as
      42             :  * they can involve pinentry questions. */
      43             : #define TIMEOUT_VALUE (5*60*1000)
      44             : 
      45             : using namespace QGpgME;
      46             : using namespace GpgME;
      47             : 
      48           1 : QGpgMEWKSPublishJob::QGpgMEWKSPublishJob(Context *context)
      49           1 :     : mixin_type(context)
      50             : {
      51           1 :     lateInitialization();
      52           1 : }
      53             : 
      54           2 : QGpgMEWKSPublishJob::~QGpgMEWKSPublishJob() {}
      55             : 
      56           1 : static QString getWKSClient()
      57             : {
      58           1 :     auto libexecdir = QString::fromLocal8Bit(dirInfo("libexecdir"));
      59           1 :     if (libexecdir.isEmpty()) {
      60           0 :         return QString();
      61             :     }
      62             : 
      63           3 :     const QFileInfo fi(QDir(libexecdir).absoluteFilePath(QStringLiteral("gpg-wks-client")));
      64           1 :     if (fi.exists() && fi.isExecutable()) {
      65           1 :         return fi.absoluteFilePath();
      66             :     }
      67           1 :     return QString();
      68             : }
      69             : 
      70           1 : static QGpgMEWKSPublishJob::result_type check_worker(const QString &mail)
      71             : {
      72           1 :     if (mail.isEmpty()) {
      73             :         return std::make_tuple (Error(make_error(GPG_ERR_INV_ARG)),
      74           0 :                                 QByteArray(), QByteArray(), QString(), Error());
      75             :     }
      76             : 
      77           1 :     const auto wksPath = getWKSClient();
      78           1 :     if (wksPath.isEmpty()) {
      79             :         return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
      80           0 :                                 QByteArray(), QByteArray(), QString(), Error());
      81             :     }
      82             : 
      83             :     /* QProcess instead of engine_spawn because engine_spawn does not communicate
      84             :      * the return value of the process and we are in qt anyway. */
      85           2 :     QProcess proc;
      86           1 :     proc.setProgram(wksPath);
      87           2 :     proc.setArguments(QStringList() << QStringLiteral("--supported") << mail);
      88           1 :     proc.start();
      89           1 :     if (!proc.waitForStarted()) {
      90             :         return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
      91           0 :                                 QByteArray(), QByteArray(), QString(), Error());
      92             :     }
      93           1 :     if (!proc.waitForFinished(TIMEOUT_VALUE)) {
      94             :         return std::make_tuple (Error(make_error(GPG_ERR_TIMEOUT)),
      95           0 :                                 QByteArray(), QByteArray(), QString(), Error());
      96             :     }
      97           1 :     if (proc.exitStatus() == QProcess::NormalExit && proc.exitCode() == 0) {
      98           0 :         return std::make_tuple (Error(), QByteArray(), QByteArray(), QString(), Error());
      99             :     }
     100             :     return std::make_tuple (Error(make_error(GPG_ERR_NOT_ENABLED)),
     101           2 :                             QByteArray(), QByteArray(), QString(), Error());
     102             : }
     103             : 
     104           0 : static QGpgMEWKSPublishJob::result_type create_worker(const char *fpr, const QString &mail)
     105             : {
     106           0 :     if (mail.isEmpty() || !fpr) {
     107             :         return std::make_tuple (Error(make_error(GPG_ERR_INV_ARG)),
     108           0 :                                 QByteArray(), QByteArray(), QString(), Error());
     109             :     }
     110             : 
     111           0 :     const auto wksPath = getWKSClient();
     112           0 :     if (wksPath.isEmpty()) {
     113             :         return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
     114           0 :                                 QByteArray(), QByteArray(), QString(), Error());
     115             :     }
     116             : 
     117           0 :     QProcess proc;
     118           0 :     proc.setProgram(wksPath);
     119           0 :     proc.setArguments(QStringList() << QStringLiteral("--create")
     120           0 :                                     << QLatin1String(fpr)
     121           0 :                                     << mail);
     122           0 :     proc.start();
     123           0 :     if (!proc.waitForStarted()) {
     124             :         return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
     125           0 :                                 QByteArray(), QByteArray(), QString(), Error());
     126             :     }
     127             : 
     128           0 :     if (!proc.waitForFinished(TIMEOUT_VALUE)) {
     129             :         return std::make_tuple (Error(make_error(GPG_ERR_TIMEOUT)),
     130           0 :                                 QByteArray(), QByteArray(), QString(), Error());
     131             :     }
     132           0 :     if (proc.exitStatus() == QProcess::NormalExit && proc.exitCode() == 0) {
     133             :         return std::make_tuple (Error(), proc.readAllStandardOutput(),
     134           0 :                                 proc.readAllStandardError(), QString(), Error());
     135             :     }
     136             :     return std::make_tuple (Error(make_error(GPG_ERR_GENERAL)),
     137           0 :                             proc.readAllStandardOutput(), proc.readAllStandardError(), QString(), Error());
     138             : }
     139             : 
     140           0 : static QGpgMEWKSPublishJob::result_type recieve_worker(const QByteArray &response)
     141             : {
     142           0 :     if (response.isEmpty()) {
     143             :         return std::make_tuple (Error(make_error(GPG_ERR_INV_ARG)),
     144           0 :                                 QByteArray(), QByteArray(), QString(), Error());
     145             :     }
     146             : 
     147           0 :     const auto wksPath = getWKSClient();
     148           0 :     if (wksPath.isEmpty()) {
     149             :         return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
     150           0 :                                 QByteArray(), QByteArray(), QString(), Error());
     151             :     }
     152             : 
     153           0 :     QProcess proc;
     154           0 :     proc.setProgram(wksPath);
     155           0 :     proc.setArguments(QStringList() << QStringLiteral("--receive"));
     156           0 :     proc.start();
     157           0 :     if (!proc.waitForStarted()) {
     158             :         return std::make_tuple (Error(make_error(GPG_ERR_NOT_SUPPORTED)),
     159           0 :                                 QByteArray(), QByteArray(), QString(), Error());
     160             :     }
     161           0 :     proc.write(response);
     162           0 :     proc.closeWriteChannel();
     163           0 :     if (!proc.waitForFinished(TIMEOUT_VALUE)) {
     164             :         return std::make_tuple (Error(make_error(GPG_ERR_TIMEOUT)),
     165           0 :                                 QByteArray(), QByteArray(), QString(), Error());
     166             :     }
     167           0 :     if (proc.exitStatus() == QProcess::NormalExit && proc.exitCode() == 0) {
     168             :         return std::make_tuple (Error(), proc.readAllStandardOutput(),
     169           0 :                                 proc.readAllStandardError(), QString(), Error());
     170             :     }
     171             :     return std::make_tuple (Error(make_error(GPG_ERR_GENERAL)),
     172           0 :                             proc.readAllStandardOutput(), proc.readAllStandardError(), QString(), Error());
     173             : }
     174             : 
     175           1 : void QGpgMEWKSPublishJob::startCheck(const QString &mailbox)
     176             : {
     177           1 :     run(std::bind(&check_worker, mailbox));
     178           1 : }
     179             : 
     180           0 : void QGpgMEWKSPublishJob::startCreate(const char *fpr, const QString &mailbox) {
     181           0 :     run(std::bind(&create_worker, fpr, mailbox));
     182           0 : }
     183             : 
     184           0 : void QGpgMEWKSPublishJob::startRecieve(const QByteArray &response)
     185             : {
     186           0 :     run(std::bind(&recieve_worker, response));
     187           0 : }
     188             : 
     189             : #include "qgpgmewkspublishjob.moc"

Generated by: LCOV version 1.11