LCOV - code coverage report
Current view: top level - lang/qt/src - qgpgmekeyformailboxjob.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 57 0.0 %
Date: 2016-09-12 13:07:23 Functions: 0 9 0.0 %

          Line data    Source code
       1             : /*
       2             :     qgpgmekeyformailboxjob.cpp
       3             : 
       4             :     This file is part of qgpgme, the Qt API binding for gpgme
       5             :     Copyright (c) 2016 Intevation GmbH
       6             : 
       7             :     QGpgME is free software; you can redistribute it and/or
       8             :     modify it under the terms of the GNU General Public License as
       9             :     published by the Free Software Foundation; either version 2 of the
      10             :     License, or (at your option) any later version.
      11             : 
      12             :     QGpgME is distributed in the hope that it will be useful,
      13             :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :     General Public License for more details.
      16             : 
      17             :     You should have received a copy of the GNU General Public License
      18             :     along with this program; if not, write to the Free Software
      19             :     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
      20             : 
      21             :     In addition, as a special exception, the copyright holders give
      22             :     permission to link the code of this program with any edition of
      23             :     the Qt library by Trolltech AS, Norway (or with modified versions
      24             :     of Qt that use the same license as Qt), and distribute linked
      25             :     combinations including the two.  You must obey the GNU General
      26             :     Public License in all respects for all of the code used other than
      27             :     Qt.  If you modify this file, you may extend this exception to
      28             :     your version of the file, but you are not obligated to do so.  If
      29             :     you do not wish to do so, delete this exception statement from
      30             :     your version.
      31             : */
      32             : 
      33             : #include "qgpgmekeyformailboxjob.h"
      34             : #include "qgpgmekeylistjob.h"
      35             : 
      36             : #include <QStringList>
      37             : 
      38             : #include <tuple>
      39             : 
      40             : using namespace GpgME;
      41             : using namespace QGpgME;
      42             : 
      43           0 : QGpgMEKeyForMailboxJob::QGpgMEKeyForMailboxJob(Context *context)
      44           0 :     : mixin_type(context)
      45             : {
      46           0 :     lateInitialization();
      47           0 : }
      48             : 
      49           0 : QGpgMEKeyForMailboxJob::~QGpgMEKeyForMailboxJob() {}
      50             : 
      51           0 : static bool keyIsOk(const Key k)
      52             : {
      53           0 :     return !k.isExpired() && !k.isRevoked() && !k.isInvalid() && !k.isDisabled();
      54             : }
      55             : 
      56           0 : static bool uidIsOk(const UserID uid)
      57             : {
      58           0 :     return keyIsOk(uid.parent()) && !uid.isRevoked() && !uid.isInvalid();
      59             : }
      60             : 
      61           0 : static bool subkeyIsOk(const Subkey s)
      62             : {
      63           0 :     return !s.isRevoked() && !s.isInvalid() && !s.isDisabled();
      64             : }
      65             : 
      66           0 : static QGpgMEKeyForMailboxJob::result_type do_work(Context *ctx, const QString &mailbox, bool canEncrypt)
      67             : {
      68             :     /* Do a Keylisting. */
      69           0 :     ctx->setKeyListMode(GpgME::Extern | GpgME::Local | GpgME::Signatures | GpgME::Validate);
      70           0 :     std::vector<Key> keys;
      71           0 :     QGpgMEKeyListJob *keylist = new QGpgMEKeyListJob(ctx);
      72             : 
      73           0 :     KeyListResult result = keylist->exec(QStringList() << mailbox, false, keys);
      74             : 
      75           0 :     if (result.error()) {
      76           0 :         return std::make_tuple(result, Key(), UserID(), QString(), Error());
      77             :     }
      78             : 
      79             :     // This should ideally be decided by GnuPG and this Job changed
      80             :     // to just call the according API in GpgME
      81             :     // See: https://bugs.gnupg.org/gnupg/issue2359
      82           0 :     Key keyC;
      83           0 :     UserID uidC;
      84           0 :     Q_FOREACH (const Key k, keys) {
      85           0 :         if (canEncrypt && !k.canEncrypt()) {
      86           0 :             continue;
      87             :         }
      88             :         /* First get the uid that matches the mailbox */
      89           0 :         Q_FOREACH (const UserID u, k.userIDs()) {
      90           0 :             if (QString::fromUtf8(u.email()).toLower() == mailbox.toLower()) {
      91           0 :                 if (uidC.isNull()) {
      92           0 :                     keyC = k;
      93           0 :                     uidC = u;
      94           0 :                 } else if ((!uidIsOk(uidC) && uidIsOk(u)) || uidC.validity() < u.validity()) {
      95             :                     /* Validity of the new key is better. */
      96           0 :                     uidC = u;
      97           0 :                     keyC = k;
      98           0 :                 } else if (uidC.validity() == u.validity() && uidIsOk(u)) {
      99             :                     /* Both are the same check which one is newer. */
     100           0 :                     time_t oldTime = 0;
     101           0 :                     Q_FOREACH (const Subkey s, keyC.subkeys()) {
     102           0 :                         if ((canEncrypt && s.canEncrypt()) && subkeyIsOk(s)) {
     103           0 :                             oldTime = s.creationTime();
     104             :                         }
     105           0 :                     }
     106           0 :                     time_t newTime = 0;
     107           0 :                     Q_FOREACH (const Subkey s, k.subkeys()) {
     108           0 :                         if ((canEncrypt && s.canEncrypt()) && subkeyIsOk(s)) {
     109           0 :                             newTime = s.creationTime();
     110             :                         }
     111           0 :                     }
     112           0 :                     if (newTime > oldTime) {
     113           0 :                         uidC = u;
     114           0 :                         keyC = k;
     115             :                     }
     116             :                 }
     117             :             }
     118           0 :         }
     119           0 :     }
     120           0 :     return std::make_tuple(result, keyC, uidC, QString(), Error());
     121             : }
     122             : 
     123           0 : Error QGpgMEKeyForMailboxJob::start(const QString &mailbox, bool canEncrypt)
     124             : {
     125           0 :     run(std::bind(&do_work, std::placeholders::_1, mailbox, canEncrypt));
     126           0 :     return Error();
     127             : }
     128             : 
     129           0 : KeyListResult QGpgMEKeyForMailboxJob::exec(const QString &mailbox, bool canEncrypt, Key &key, UserID &uid)
     130             : {
     131           0 :     const result_type r = do_work(context(), mailbox, canEncrypt);
     132           0 :     resultHook(r);
     133           0 :     key = std::get<1>(r);
     134           0 :     uid = std::get<2>(r);
     135           0 :     return std::get<0>(r);
     136             : }
     137             : 
     138             : #include "qgpgmekeyformailboxjob.moc"

Generated by: LCOV version 1.11