LCOV - code coverage report
Current view: top level - lang/qt/src - qgpgmebackend.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 28 72 38.9 %
Date: 2018-11-14 16:53:58 Functions: 9 22 40.9 %

          Line data    Source code
       1             : /*
       2             :     qgpgmebackend.cpp
       3             : 
       4             :     This file is part of qgpgme, the Qt API binding for gpgme
       5             :     Copyright (c) 2004,2005 Klarälvdalens Datakonsult AB
       6             :     Copyright (c) 2016 by Bundesamt für Sicherheit in der Informationstechnik
       7             :     Software engineering by Intevation GmbH
       8             : 
       9             :     QGpgME is free software; you can redistribute it and/or
      10             :     modify it under the terms of the GNU General Public License as
      11             :     published by the Free Software Foundation; either version 2 of the
      12             :     License, or (at your option) any later version.
      13             : 
      14             :     QGpgME is distributed in the hope that it will be useful,
      15             :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      17             :     General Public License for more details.
      18             : 
      19             :     You should have received a copy of the GNU General Public License
      20             :     along with this program; if not, write to the Free Software
      21             :     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
      22             : 
      23             :     In addition, as a special exception, the copyright holders give
      24             :     permission to link the code of this program with any edition of
      25             :     the Qt library by Trolltech AS, Norway (or with modified versions
      26             :     of Qt that use the same license as Qt), and distribute linked
      27             :     combinations including the two.  You must obey the GNU General
      28             :     Public License in all respects for all of the code used other than
      29             :     Qt.  If you modify this file, you may extend this exception to
      30             :     your version of the file, but you are not obligated to do so.  If
      31             :     you do not wish to do so, delete this exception statement from
      32             :     your version.
      33             : */
      34             : 
      35             : #ifdef HAVE_CONFIG_H
      36             :  #include "config.h"
      37             : #endif
      38             : 
      39             : #include "qgpgmebackend.h"
      40             : 
      41             : 
      42             : #include "error.h"
      43             : #include "engineinfo.h"
      44             : 
      45             : #include "protocol_p.h"
      46             : 
      47             : #include <QFile>
      48             : #include <QString>
      49             : 
      50             : const char QGpgME::QGpgMEBackend::OpenPGP[] = "OpenPGP";
      51             : const char QGpgME::QGpgMEBackend::SMIME[] = "SMIME";
      52             : 
      53             : 
      54           8 : QGpgME::QGpgMEBackend::QGpgMEBackend()
      55             :     : mCryptoConfig(0),
      56             :       mOpenPGPProtocol(0),
      57           8 :       mSMIMEProtocol(0)
      58             : {
      59           8 :     GpgME::initializeLibrary();
      60           8 : }
      61             : 
      62           0 : QGpgME::QGpgMEBackend::~QGpgMEBackend()
      63             : {
      64           0 :     delete mCryptoConfig; mCryptoConfig = 0;
      65           0 :     delete mOpenPGPProtocol; mOpenPGPProtocol = 0;
      66           0 :     delete mSMIMEProtocol; mSMIMEProtocol = 0;
      67           0 : }
      68             : 
      69           0 : QString QGpgME::QGpgMEBackend::name() const
      70             : {
      71           0 :     return QStringLiteral("gpgme");
      72             : }
      73             : 
      74           0 : QString QGpgME::QGpgMEBackend::displayName() const
      75             : {
      76           0 :     return QStringLiteral("GpgME");
      77             : }
      78             : 
      79          11 : QGpgME::CryptoConfig *QGpgME::QGpgMEBackend::config() const
      80             : {
      81          11 :     if (!mCryptoConfig) {
      82           1 :         if (GpgME::hasFeature(GpgME::GpgConfEngineFeature, 0)) {
      83           1 :             mCryptoConfig = new QGpgMENewCryptoConfig;
      84             :         }
      85             :     }
      86          11 :     return mCryptoConfig;
      87             : }
      88             : 
      89           7 : static bool check(GpgME::Protocol proto, QString *reason)
      90             : {
      91           7 :     if (!GpgME::checkEngine(proto)) {
      92           7 :         return true;
      93             :     }
      94           0 :     if (!reason) {
      95           0 :         return false;
      96             :     }
      97             :     // error, check why:
      98             : #if 0
      99             : Port away from localised string or delete.
     100             :     const GpgME::EngineInfo ei = GpgME::engineInfo(proto);
     101             :     if (ei.isNull()) {
     102             :         *reason = i18n("GPGME was compiled without support for %1.", proto == GpgME::CMS ? QLatin1String("S/MIME") : QLatin1String("OpenPGP"));
     103             :     } else if (ei.fileName() && !ei.version()) {
     104             :         *reason = i18n("Engine %1 is not installed properly.", QFile::decodeName(ei.fileName()));
     105             :     } else if (ei.fileName() && ei.version() && ei.requiredVersion())
     106             :         *reason = i18n("Engine %1 version %2 installed, "
     107             :                        "but at least version %3 is required.",
     108             :                        QFile::decodeName(ei.fileName()), QLatin1String(ei.version()), QLatin1String(ei.requiredVersion()));
     109             :     else {
     110             :         *reason = i18n("Unknown problem with engine for protocol %1.", proto == GpgME::CMS ? QLatin1String("S/MIME") : QLatin1String("OpenPGP"));
     111             :     }
     112             : #endif
     113           0 :     return false;
     114             : }
     115             : 
     116           7 : bool QGpgME::QGpgMEBackend::checkForOpenPGP(QString *reason) const
     117             : {
     118           7 :     return check(GpgME::OpenPGP, reason);
     119             : }
     120             : 
     121           0 : bool QGpgME::QGpgMEBackend::checkForSMIME(QString *reason) const
     122             : {
     123           0 :     return check(GpgME::CMS, reason);
     124             : }
     125             : 
     126           0 : bool QGpgME::QGpgMEBackend::checkForProtocol(const char *name, QString *reason) const
     127             : {
     128           0 :     if (qstricmp(name, OpenPGP) == 0) {
     129           0 :         return check(GpgME::OpenPGP, reason);
     130             :     }
     131           0 :     if (qstricmp(name, SMIME) == 0) {
     132           0 :         return check(GpgME::CMS, reason);
     133             :     }
     134           0 :     if (reason) {
     135           0 :         *reason = QStringLiteral("Unsupported protocol \"%1\"").arg(QLatin1String(name));
     136             :     }
     137           0 :     return false;
     138             : }
     139             : 
     140          46 : QGpgME::Protocol *QGpgME::QGpgMEBackend::openpgp() const
     141             : {
     142          46 :     if (!mOpenPGPProtocol)
     143           7 :         if (checkForOpenPGP()) {
     144           7 :             mOpenPGPProtocol = new ::Protocol(GpgME::OpenPGP);
     145             :         }
     146          46 :     return mOpenPGPProtocol;
     147             : }
     148             : 
     149           0 : QGpgME::Protocol *QGpgME::QGpgMEBackend::smime() const
     150             : {
     151           0 :     if (!mSMIMEProtocol)
     152           0 :         if (checkForSMIME()) {
     153           0 :             mSMIMEProtocol = new ::Protocol(GpgME::CMS);
     154             :         }
     155           0 :     return mSMIMEProtocol;
     156             : }
     157             : 
     158           0 : QGpgME::Protocol *QGpgME::QGpgMEBackend::protocol(const char *name) const
     159             : {
     160           0 :     if (qstricmp(name, OpenPGP) == 0) {
     161           0 :         return openpgp();
     162             :     }
     163           0 :     if (qstricmp(name, SMIME) == 0) {
     164           0 :         return smime();
     165             :     }
     166           0 :     return 0;
     167             : }
     168             : 
     169           0 : bool QGpgME::QGpgMEBackend::supportsProtocol(const char *name) const
     170             : {
     171           0 :     return qstricmp(name, OpenPGP) == 0 || qstricmp(name, SMIME) == 0;
     172             : }
     173             : 
     174           0 : const char *QGpgME::QGpgMEBackend::enumerateProtocols(int i) const
     175             : {
     176           0 :     switch (i) {
     177           0 :     case 0: return OpenPGP;
     178           0 :     case 1: return SMIME;
     179           0 :     default: return 0;
     180             :     }
     181             : }
     182             : 
     183             : static QGpgME::QGpgMEBackend *gpgmeBackend;
     184             : 
     185          11 : QGpgME::CryptoConfig *QGpgME::cryptoConfig()
     186             : {
     187          11 :     if (!gpgmeBackend) {
     188           1 :         gpgmeBackend = new QGpgME::QGpgMEBackend();
     189             :     }
     190          11 :     return gpgmeBackend->config();
     191             : 
     192             : }
     193             : 
     194          46 : QGpgME::Protocol *QGpgME::openpgp()
     195             : {
     196          46 :     if (!gpgmeBackend) {
     197           7 :         gpgmeBackend = new QGpgME::QGpgMEBackend();
     198             :     }
     199          46 :     return gpgmeBackend->openpgp();
     200             : }
     201             : 
     202           0 : QGpgME::Protocol *QGpgME::smime()
     203             : {
     204           0 :     if (!gpgmeBackend) {
     205           0 :         gpgmeBackend = new QGpgME::QGpgMEBackend();
     206             :     }
     207           0 :     return gpgmeBackend->smime();
     208          24 : }

Generated by: LCOV version 1.13