summaryrefslogtreecommitdiffstats
path: root/certmanager/lib/backends/qgpgme/qgpgmejob.h
blob: 4a03d7d94f1708e770d5503e72fd55e4b0824c0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
    qgpgmejob.h

    This file is part of libkleopatra, the KDE keymanagement library
    Copyright (c) 2004 Klarälvdalens Datakonsult AB

    Libkleopatra is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.

    Libkleopatra is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    In addition, as a special exception, the copyright holders give
    permission to link the code of this program with any edition of
    the Qt library by Trolltech AS, Norway (or with modified versions
    of Qt that use the same license as Qt), and distribute linked
    combinations including the two.  You must obey the GNU General
    Public License in all respects for all of the code used other than
    Qt.  If you modify this file, you may extend this exception to
    your version of the file, but you are not obligated to do so.  If
    you do not wish to do so, delete this exception statement from
    your version.
*/

#ifndef __KLEO_QGPGMEJOB_H__
#define __KLEO_QGPGMEJOB_H__

#include <gpgmepp/interfaces/progressprovider.h>
#include <gpgmepp/interfaces/passphraseprovider.h>

#include <gpgmepp/key.h>
#include <gpgmepp/context.h>

#include <tqcstring.h>
#include <tqstring.h>

#include <vector>
#include <kdepimmacros.h>

namespace GpgME {
  class Data;
}

namespace Kleo {
  class Job;
}

namespace QGpgME {
  class QByteArrayDataProvider;
}

class TQString;
class TQStringList;

namespace Kleo {

  /** This is a hackish helper class to avoid code duplication in this
      backend's Kleo::Job subclasses. It tqcontains several workarounds
      for tqmoc/signal/slot shortcomings, most of which the author of
      this thinks are Qt bugs (lazy implementations), first and
      foremost the inability of tqmoc to handle inheritance from
      multiple QObject-derived subclasses.

      To use it, inherit from the Job-subclass, then from this class,
      add QGPGME_JOB to just after Q OBJECT and implement
      doOperationDoneEvent() by emitting your variant of the result()
      signal there. Pass "this" as the first argument this QGpgMEJOb's
      ctor. The rest is dealt with automatically.
  */
  class KDE_EXPORT QGpgMEJob : public GpgME::ProgressProvider, public GpgME::PassphraseProvider {
  public:
    QGpgMEJob( Kleo::Job * _this, GpgME::Context * context );
    ~QGpgMEJob();

  protected:
    /*! Called on operation-done events, between emitting done() and
      calling deleteLater(). You should emit your result signal here. */
    virtual void doOperationDoneEvent( const GpgME::Error & e ) = 0;
    /*! Hooks up mCtx to be managed by the event loop interactor */
    void hookupContextToEventLoopInteractor();
    /*! Fills mPatterns from the stringlist, resets chunking to the full list */
    void setPatterns( const TQStringList & sl, bool allowEmpty=false );
    /*! Returnes the number of patterns set */
    unsigned int numPatterns() const { return mNumPatterns; }
    /*! Skips to the next chunk of patterns. @return patterns() */
    const char* * nextChunk();
    /*! @return patterns, offset by the current chunk */
    const char* * patterns() const;
    /*! Set the current pattern chunksize to size and reset the chunk index to zero */
    void setChunkSize( unsigned int size );
    /*! @return current chunksize */
    unsigned int chunkSize() const { return mChunkSize; }
    /*! Creates an empty GpgME::Data/QGpgME::QByteArrayDataProvider pair */
    void createOutData();
    /*! Creates a GpgME::Data/QGpgME::QByteArrayDataProvider pair,
      filled with the contents of \a in */
    void createInData( const TQByteArray & in );
    /*! Sets the list of signing keys */
    GpgME::Error setSigningKeys( const std::vector<GpgME::Key> & signers );
    /*! Call this to implement a slotOperationDoneEvent() */
    void doSlotOperationDoneEvent( GpgME::Context * context, const GpgME::Error & e );
    /*! Call this to extract the audit log from mCtx */
    void getAuditLog();

    //
    // only boring stuff below this line...
    //

  protected:
    virtual void doEmitProgressSignal( const TQString & what, int current, int total ) = 0;
    virtual void doEmitDoneSignal() = 0;
    void doSlotCancel();
    TQString auditLogAsHtml() const { return mAuditLogAsHtml; }
    GpgME::Error auditLogError() const { return mAuditLogError; }

  private:
    /*! \reimp from GpgME::ProgressProvider */
    void showProgress( const char * what, int type, int current, int total );
    char * getPassphrase( const char * useridHint, const char * description,
			  bool previousWasBad, bool & canceled );
    void deleteAllPatterns();

  public:
    void checkInvariants() const;

  protected:
    Kleo::Job * mThis;
    GpgME::Context * mCtx;
    GpgME::Data * mInData;
    QGpgME::QByteArrayDataProvider * mInDataDataProvider;
    GpgME::Data * mOutData;
    QGpgME::QByteArrayDataProvider * mOutDataDataProvider;
  private:
    const char* * mPatterns;
    // holds the entry - if any - in mPattern that was tqreplaced with
    // NULL to create a temporary end-of-array marker for gpgme:
    const char * mReplacedPattern;
    unsigned int mNumPatterns;
    unsigned int mChunkSize;
    unsigned int mPatternStartIndex, mPatternEndIndex;
    GpgME::Error mAuditLogError;
    TQString mAuditLogAsHtml;
  };

}

#define make_slot_cancel private: void slotCancel() { QGpgMEJob::doSlotCancel(); }
#define make_progress_emitter private: void doEmitProgressSignal( const TQString & what, int cur, int tot ) { emit progress( what, cur, tot ); }
#define make_done_emitter private: void doEmitDoneSignal() { emit done(); }
#define make_auditLogAsHtml private: TQString auditLogAsHtml() const { return QGpgMEJob::auditLogAsHtml(); } GpgME::Error auditLogError() const { return QGpgMEJob::auditLogError(); }
#define QGPGME_JOB make_slot_cancel make_progress_emitter make_done_emitter make_auditLogAsHtml

#endif // __KLEO_QGPGMEJOB_H__