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-11-29 15:07:43 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             : #ifdef HAVE_CONFIG_H
      35             :  #include "config.h"
      36             : #endif
      37             : 
      38             : #include "qgpgmebackend.h"
      39             : 
      40             : 
      41             : #include "error.h"
      42             : #include "engineinfo.h"
      43             : 
      44             : #include "protocol_p.h"
      45             : 
      46             : #include <QFile>
      47             : #include <QString>
      48             : 
      49             : const char QGpgME::QGpgMEBackend::OpenPGP[] = "OpenPGP";
      50             : const char QGpgME::QGpgMEBackend::SMIME[] = "SMIME";
      51             : 
      52             : 
      53           6 : QGpgME::QGpgMEBackend::QGpgMEBackend()
      54             :     : mCryptoConfig(0),
      55             :       mOpenPGPProtocol(0),
      56           6 :       mSMIMEProtocol(0)
      57             : {
      58           6 :     GpgME::initializeLibrary();
      59           6 : }
      60             : 
      61           0 : QGpgME::QGpgMEBackend::~QGpgMEBackend()
      62             : {
      63           0 :     delete mCryptoConfig; mCryptoConfig = 0;
      64           0 :     delete mOpenPGPProtocol; mOpenPGPProtocol = 0;
      65           0 :     delete mSMIMEProtocol; mSMIMEProtocol = 0;
      66           0 : }
      67             : 
      68           0 : QString QGpgME::QGpgMEBackend::name() const
      69             : {
      70           0 :     return QStringLiteral("gpgme");
      71             : }
      72             : 
      73           0 : QString QGpgME::QGpgMEBackend::displayName() const
      74             : {
      75           0 :     return QStringLiteral("GpgME");
      76             : }
      77             : 
      78           0 : QGpgME::CryptoConfig *QGpgME::QGpgMEBackend::config() const
      79             : {
      80           0 :     if (!mCryptoConfig) {
      81           0 :         if (GpgME::hasFeature(GpgME::GpgConfEngineFeature, 0)) {
      82           0 :             mCryptoConfig = new QGpgMENewCryptoConfig;
      83             :         }
      84             :     }
      85           0 :     return mCryptoConfig;
      86             : }
      87             : 
      88           6 : static bool check(GpgME::Protocol proto, QString *reason)
      89             : {
      90           6 :     if (!GpgME::checkEngine(proto)) {
      91           6 :         return true;
      92             :     }
      93           0 :     if (!reason) {
      94           0 :         return false;
      95             :     }
      96             :     // error, check why:
      97             : #if 0
      98             : Port away from localised string or delete.
      99             :     const GpgME::EngineInfo ei = GpgME::engineInfo(proto);
     100             :     if (ei.isNull()) {
     101             :         *reason = i18n("GPGME was compiled without support for %1.", proto == GpgME::CMS ? QLatin1String("S/MIME") : QLatin1String("OpenPGP"));
     102             :     } else if (ei.fileName() && !ei.version()) {
     103             :         *reason = i18n("Engine %1 is not installed properly.", QFile::decodeName(ei.fileName()));
     104             :     } else if (ei.fileName() && ei.version() && ei.requiredVersion())
     105             :         *reason = i18n("Engine %1 version %2 installed, "
     106             :                        "but at least version %3 is required.",
     107             :                        QFile::decodeName(ei.fileName()), QLatin1String(ei.version()), QLatin1String(ei.requiredVersion()));
     108             :     else {
     109             :         *reason = i18n("Unknown problem with engine for protocol %1.", proto == GpgME::CMS ? QLatin1String("S/MIME") : QLatin1String("OpenPGP"));
     110             :     }
     111             : #endif
     112           0 :     return false;
     113             : }
     114             : 
     115           6 : bool QGpgME::QGpgMEBackend::checkForOpenPGP(QString *reason) const
     116             : {
     117           6 :     return check(GpgME::OpenPGP, reason);
     118             : }
     119             : 
     120           0 : bool QGpgME::QGpgMEBackend::checkForSMIME(QString *reason) const
     121             : {
     122           0 :     return check(GpgME::CMS, reason);
     123             : }
     124             : 
     125           0 : bool QGpgME::QGpgMEBackend::checkForProtocol(const char *name, QString *reason) const
     126             : {
     127           0 :     if (qstricmp(name, OpenPGP) == 0) {
     128           0 :         return check(GpgME::OpenPGP, reason);
     129             :     }
     130           0 :     if (qstricmp(name, SMIME) == 0) {
     131           0 :         return check(GpgME::CMS, reason);
     132             :     }
     133           0 :     if (reason) {
     134           0 :         *reason = QStringLiteral("Unsupported protocol \"%1\"").arg(QLatin1String(name));
     135             :     }
     136           0 :     return false;
     137             : }
     138             : 
     139          27 : QGpgME::Protocol *QGpgME::QGpgMEBackend::openpgp() const
     140             : {
     141          27 :     if (!mOpenPGPProtocol)
     142           6 :         if (checkForOpenPGP()) {
     143           6 :             mOpenPGPProtocol = new ::Protocol(GpgME::OpenPGP);
     144             :         }
     145          27 :     return mOpenPGPProtocol;
     146             : }
     147             : 
     148           0 : QGpgME::Protocol *QGpgME::QGpgMEBackend::smime() const
     149             : {
     150           0 :     if (!mSMIMEProtocol)
     151           0 :         if (checkForSMIME()) {
     152           0 :             mSMIMEProtocol = new ::Protocol(GpgME::CMS);
     153             :         }
     154           0 :     return mSMIMEProtocol;
     155             : }
     156             : 
     157           0 : QGpgME::Protocol *QGpgME::QGpgMEBackend::protocol(const char *name) const
     158             : {
     159           0 :     if (qstricmp(name, OpenPGP) == 0) {
     160           0 :         return openpgp();
     161             :     }
     162           0 :     if (qstricmp(name, SMIME) == 0) {
     163           0 :         return smime();
     164             :     }
     165           0 :     return 0;
     166             : }
     167             : 
     168           0 : bool QGpgME::QGpgMEBackend::supportsProtocol(const char *name) const
     169             : {
     170           0 :     return qstricmp(name, OpenPGP) == 0 || qstricmp(name, SMIME) == 0;
     171             : }
     172             : 
     173           0 : const char *QGpgME::QGpgMEBackend::enumerateProtocols(int i) const
     174             : {
     175           0 :     switch (i) {
     176           0 :     case 0: return OpenPGP;
     177           0 :     case 1: return SMIME;
     178           0 :     default: return 0;
     179             :     }
     180             : }
     181             : 
     182             : static QGpgME::QGpgMEBackend *gpgmeBackend;
     183             : 
     184           0 : QGpgME::CryptoConfig *QGpgME::cryptoConfig()
     185             : {
     186           0 :     if (!gpgmeBackend) {
     187           0 :         gpgmeBackend = new QGpgME::QGpgMEBackend();
     188             :     }
     189           0 :     return gpgmeBackend->config();
     190             : 
     191             : }
     192             : 
     193          27 : QGpgME::Protocol *QGpgME::openpgp()
     194             : {
     195          27 :     if (!gpgmeBackend) {
     196           6 :         gpgmeBackend = new QGpgME::QGpgMEBackend();
     197             :     }
     198          27 :     return gpgmeBackend->openpgp();
     199             : }
     200             : 
     201           0 : QGpgME::Protocol *QGpgME::smime()
     202             : {
     203           0 :     if (!gpgmeBackend) {
     204           0 :         gpgmeBackend = new QGpgME::QGpgMEBackend();
     205             :     }
     206           0 :     return gpgmeBackend->smime();
     207          18 : }

Generated by: LCOV version 1.11