LCOV - code coverage report
Current view: top level - lang/qt/src - qgpgmelistallkeysjob.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 57 0.0 %
Date: 2018-11-15 08:49:49 Functions: 0 10 0.0 %

          Line data    Source code
       1             : /*
       2             :     qgpgmelistallkeysjob.cpp
       3             : 
       4             :     This file is part of qgpgme, the Qt API binding for gpgme
       5             :     Copyright (c) 2004,2008 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 "qgpgmelistallkeysjob.h"
      40             : 
      41             : #include "key.h"
      42             : #include "context.h"
      43             : #include "keylistresult.h"
      44             : #include <gpg-error.h>
      45             : 
      46             : #include <algorithm>
      47             : 
      48             : #include <cstdlib>
      49             : #include <cstring>
      50             : #include <cassert>
      51             : 
      52             : using namespace QGpgME;
      53             : using namespace GpgME;
      54             : 
      55           0 : QGpgMEListAllKeysJob::QGpgMEListAllKeysJob(Context *context)
      56             :     : mixin_type(context),
      57           0 :       mResult()
      58             : {
      59           0 :     lateInitialization();
      60           0 : }
      61             : 
      62           0 : QGpgMEListAllKeysJob::~QGpgMEListAllKeysJob() {}
      63             : 
      64           0 : static KeyListResult do_list_keys(Context *ctx, std::vector<Key> &keys, bool secretOnly)
      65             : {
      66             : 
      67           0 :     const char **pat = 0;
      68           0 :     if (const Error err = ctx->startKeyListing(pat, secretOnly)) {
      69           0 :         return KeyListResult(0, err);
      70             :     }
      71             : 
      72           0 :     Error err;
      73           0 :     do {
      74           0 :         keys.push_back(ctx->nextKey(err));
      75           0 :     } while (!err);
      76             : 
      77           0 :     keys.pop_back();
      78             : 
      79           0 :     const KeyListResult result = ctx->endKeyListing();
      80           0 :     ctx->cancelPendingOperation();
      81           0 :     return result;
      82             : }
      83             : 
      84             : namespace
      85             : {
      86             : 
      87             : template <typename ForwardIterator, typename BinaryPredicate>
      88           0 : ForwardIterator unique_by_merge(ForwardIterator first, ForwardIterator last, BinaryPredicate pred)
      89             : {
      90           0 :     first = std::adjacent_find(first, last, pred);
      91           0 :     if (first == last) {
      92           0 :         return last;
      93             :     }
      94             : 
      95           0 :     ForwardIterator dest = first;
      96           0 :     dest->mergeWith(*++first);
      97           0 :     while (++first != last)
      98           0 :         if (pred(*dest, *first)) {
      99           0 :             dest->mergeWith(*first);
     100             :         } else {
     101           0 :             *++dest = *first;
     102             :         }
     103           0 :     return ++dest;
     104             : }
     105             : 
     106             : }
     107             : 
     108           0 : static void merge_keys(std::vector<Key> &merged, std::vector<Key> &pub, std::vector<Key> &sec)
     109             : {
     110           0 :     merged.reserve(pub.size() + sec.size());
     111             : 
     112             :     std::merge(pub.begin(), pub.end(),
     113             :                sec.begin(), sec.end(),
     114             :                std::back_inserter(merged),
     115           0 :                ByFingerprint<std::less>());
     116             : 
     117           0 :     merged.erase(unique_by_merge(merged.begin(), merged.end(), ByFingerprint<std::equal_to>()),
     118           0 :                  merged.end());
     119           0 : }
     120             : 
     121           0 : static QGpgMEListAllKeysJob::result_type list_keys(Context *ctx, bool mergeKeys)
     122             : {
     123           0 :     std::vector<Key> pub, sec, merged;
     124           0 :     KeyListResult r;
     125             : 
     126           0 :     r.mergeWith(do_list_keys(ctx, pub, false));
     127           0 :     std::sort(pub.begin(), pub.end(), ByFingerprint<std::less>());
     128             : 
     129           0 :     r.mergeWith(do_list_keys(ctx, sec, true));
     130           0 :     std::sort(sec.begin(), sec.end(), ByFingerprint<std::less>());
     131             : 
     132           0 :     if (mergeKeys) {
     133           0 :         merge_keys(merged, pub, sec);
     134             :     } else {
     135           0 :         merged.swap(pub);
     136             :     }
     137           0 :     return std::make_tuple(r, merged, sec, QString(), Error());
     138             : }
     139             : 
     140           0 : Error QGpgMEListAllKeysJob::start(bool mergeKeys)
     141             : {
     142           0 :     run(std::bind(&list_keys, std::placeholders::_1, mergeKeys));
     143           0 :     return Error();
     144             : }
     145             : 
     146           0 : KeyListResult QGpgMEListAllKeysJob::exec(std::vector<Key> &pub, std::vector<Key> &sec, bool mergeKeys)
     147             : {
     148           0 :     const result_type r = list_keys(context(), mergeKeys);
     149           0 :     resultHook(r);
     150           0 :     pub = std::get<1>(r);
     151           0 :     sec = std::get<2>(r);
     152           0 :     return std::get<0>(r);
     153             : }
     154             : 
     155           0 : void QGpgMEListAllKeysJob::resultHook(const result_type &tuple)
     156             : {
     157           0 :     mResult = std::get<0>(tuple);
     158           0 : }
     159             : 
     160             : #if 0
     161             : void QGpgMEListAllKeysJob::showErrorDialog(QWidget *parent, const QString &caption) const
     162             : {
     163             :     if (!mResult.error() || mResult.error().isCanceled()) {
     164             :         return;
     165             :     }
     166             :     const QString msg = i18n("<qt><p>An error occurred while fetching "
     167             :                              "the keys from the backend:</p>"
     168             :                              "<p><b>%1</b></p></qt>",
     169             :                              QString::fromLocal8Bit(mResult.error().asString()));
     170             :     KMessageBox::error(parent, msg, caption);
     171             : }
     172             : #endif
     173             : #include "qgpgmelistallkeysjob.moc"

Generated by: LCOV version 1.13