Prepare for changing return types in KMime.
[kdepim.git] / libkpgp / kpgpblock.h
blobe0f50f672b364400c345772328dfbd0f57be560b
1 /*
2 kpgpblock.h
4 Copyright (C) 2001,2002 the KPGP authors
5 See file AUTHORS.kpgp for details
7 This file is part of KPGP, the KDE PGP/GnuPG support library.
9 KPGP is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 #ifndef KPGPBLOCK_H
20 #define KPGPBLOCK_H
22 #include "libkpgp_export.h"
23 #include "kpgp.h"
25 #include <QStringList>
26 #include <QString>
28 namespace Kpgp
31 #ifdef ERROR
32 # undef ERROR
33 #endif
35 enum BlockType {
36 UnknownBlock = -1, // BEGIN PGP ???
37 NoPgpBlock = 0,
38 PgpMessageBlock = 1, // BEGIN PGP MESSAGE
39 MultiPgpMessageBlock = 2, // BEGIN PGP MESSAGE, PART X[/Y]
40 SignatureBlock = 3, // BEGIN PGP SIGNATURE
41 ClearsignedBlock = 4, // BEGIN PGP SIGNED MESSAGE
42 PublicKeyBlock = 5, // BEGIN PGP PUBLIC KEY BLOCK
43 PrivateKeyBlock = 6 // BEGIN PGP PRIVATE KEY BLOCK (PGP 2.x: ...SECRET...)
46 enum MessageStatus {
47 OK = 0x0000,
48 RUN_ERR = 0x0001,
49 ERROR = 0x0001,
50 ENCRYPTED = 0x0002,
51 SIGNED = 0x0004,
52 GOODSIG = 0x0008,
53 ERR_SIGNING = 0x0010,
54 UNKNOWN_SIG = 0x0020,
55 BADPHRASE = 0x0040,
56 BADKEYS = 0x0080,
57 NO_SEC_KEY = 0x0100,
58 MISSINGKEY = 0x0200,
59 CANCEL = 0x8000
63 * BEGIN PGP MESSAGE
64 * Used for signed, encrypted, or compressed files.
66 * BEGIN PGP PUBLIC KEY BLOCK
67 * Used for armoring public keys
69 * BEGIN PGP PRIVATE KEY BLOCK (PGP 2.x: BEGIN PGP SECRET KEY BLOCK)
70 * Used for armoring private keys
72 * BEGIN PGP MESSAGE, PART X/Y
73 * Used for multi-part messages, where the armor is split amongst Y
74 * parts, and this is the Xth part out of Y.
76 * BEGIN PGP MESSAGE, PART X
77 * Used for multi-part messages, where this is the Xth part of an
78 * unspecified number of parts. Requires the MESSAGE-ID Armor
79 * Header to be used.
81 * BEGIN PGP SIGNATURE
82 * Used for detached signatures, OpenPGP/MIME signatures, and
83 * signatures following clearsigned messages. Note that PGP 2.x
84 * uses BEGIN PGP MESSAGE for detached signatures.
86 * BEGIN PGP SIGNED MESSAGE
87 * Used for cleartext signed messages.
89 class LIBKPGP_EXPORT Block
91 public:
93 explicit Block(const QByteArray &str = QByteArray());
94 ~Block();
96 QByteArray text() const;
97 void setText(const QByteArray &str);
99 void setProcessedText(const QByteArray &str);
101 int status() const;
102 void setStatus(const int status);
104 BlockType type() const;
106 /** is the message encrypted ? */
107 bool isEncrypted() const;
109 /** is the message signed by someone */
110 bool isSigned() const;
112 /** is the signature good ? */
113 bool goodSignature() const;
115 /** returns the primary user id of the signer or a null string if we
116 don't have the public key of the signer */
117 QString signatureUserId() const;
118 void setSignatureUserId(const QString &userId);
120 /** keyID of signer */
121 QByteArray signatureKeyId() const;
122 void setSignatureKeyId(const QByteArray &keyId);
124 /** date of the signature
125 WARNING: Will most likely be changed to QDateTime */
126 QByteArray signatureDate() const;
127 void setSignatureDate(const QByteArray &date);
129 /** the persons who can decrypt the message */
130 const QStringList encryptedFor() const;
132 /** shows the secret key which is needed
133 to decrypt the message */
134 QByteArray requiredKey() const;
135 void setRequiredKey(const QByteArray &keyId);
137 QString requiredUserId() const;
138 void setRequiredUserId(const QString &userId);
140 QByteArray error() const;
141 void setError(const QByteArray &str);
143 /** Resets all information about this OpenPGP block */
144 void reset();
146 /** decrypts this OpenPGP block if the passphrase is good.
147 returns false otherwise */
148 bool decrypt();
150 /** tries to verify this (clearsigned) OpenPGP block */
151 bool verify();
153 /** clearsigns this OpenPGP block with the key corresponding to the
154 given key id. The charset is needed to display the text correctly.
155 Returns
156 false if there was an unresolvable error or if signing was canceled
157 true if everything is o.k.
159 Kpgp::Result clearsign(const QByteArray &keyId,
160 const QByteArray &charset = QByteArray());
162 /** encrypts this OpenPGP block for a list of persons. if sign is true then
163 the message is signed with the key corresponding to the given key id.
164 Returns
165 false if there was an unresolvable error or if encryption was canceled
166 true if everything is o.k.
168 Kpgp::Result encrypt(const QStringList &receivers, const QByteArray &keyId,
169 const bool sign, const QByteArray &charset = QByteArray());
171 private:
172 void clear();
174 BlockType determineType() const;
176 QByteArray mText;
177 QByteArray mProcessedText;
178 QByteArray mError;
179 QString mSignatureUserId;
180 QByteArray mSignatureKeyId;
181 QByteArray mSignatureDate;
182 QByteArray mRequiredKey;
183 QString mRequiredUserId;
184 QStringList mEncryptedFor;
185 int mStatus;
186 bool mHasBeenProcessed;
187 mutable BlockType mType;
190 // -- inlined member functions ---------------------------------------------
192 inline QByteArray
193 Block::text() const
195 if (mHasBeenProcessed) {
196 return mProcessedText;
197 } else {
198 return mText;
202 inline void
203 Block::setText(const QByteArray &str)
205 clear();
206 mText = str;
209 inline void
210 Block::setProcessedText(const QByteArray &str)
212 mProcessedText = str;
213 mHasBeenProcessed = true;
216 inline QByteArray
217 Block::error() const
219 return mError;
222 inline void
223 Block::setError(const QByteArray &str)
225 mError = str;
228 inline int
229 Block::status() const
231 return mStatus;
234 inline void
235 Block::setStatus(const int status)
237 mStatus = status;
240 inline BlockType
241 Block::type() const
243 if (mType == NoPgpBlock) {
244 mType = determineType();
246 return mType;
249 inline QString
250 Block::signatureUserId() const
252 return mSignatureUserId;
255 inline void
256 Block::setSignatureUserId(const QString &userId)
258 mSignatureUserId = userId;
261 inline QByteArray
262 Block::signatureKeyId() const
264 return mSignatureKeyId;
267 inline void
268 Block::setSignatureKeyId(const QByteArray &keyId)
270 mSignatureKeyId = keyId;
273 inline QByteArray
274 Block::signatureDate() const
276 return mSignatureDate;
279 inline void
280 Block::setSignatureDate(const QByteArray &date)
282 mSignatureDate = date;
285 inline QByteArray
286 Block::requiredKey() const
288 return mRequiredKey;
291 inline void
292 Block::setRequiredKey(const QByteArray &keyId)
294 mRequiredKey = keyId;
297 inline QString
298 Block::requiredUserId() const
300 return mRequiredUserId;
303 inline void
304 Block::setRequiredUserId(const QString &userId)
306 mRequiredUserId = userId;
309 inline const QStringList
310 Block::encryptedFor() const
312 return mEncryptedFor;
315 inline bool
316 Block::isEncrypted() const
318 if (mStatus & ENCRYPTED) {
319 return true;
321 return false;
324 inline bool
325 Block::isSigned() const
327 if (mStatus & SIGNED) {
328 return true;
330 return false;
333 inline bool
334 Block::goodSignature() const
336 if (mStatus & GOODSIG) {
337 return true;
339 return false;
343 inline bool
344 Block::unknownSigner() const
346 if( mStatus & UNKNOWN_SIG )
347 return true;
348 return false;
352 // -------------------------------------------------------------------------
354 } // namespace Kpgp
356 #endif