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

Generated by: LCOV version 1.11