LCOV - code coverage report
Current view: top level - lang/qt/src - qgpgmebackend.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 19 72 26.4 %
Date: 2016-09-12 13:07:23 Functions: 7 22 31.8 %

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

Generated by: LCOV version 1.11