File indexing completed on 2022-09-27 16:29:16

0001 /**
0002  * SPDX-FileCopyrightText: (C) 2006 Petri Damsten <damu@iki.fi>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 #ifndef KGPGME_H
0007 #define KGPGME_H
0008 
0009 #include <config.h>
0010 
0011 #ifdef HAVE_LIBGPGME
0012 
0013 #include <gpgme.h>
0014 
0015 #include <QtCore/QList>
0016 #include <QtCore/QString>
0017 
0018 /**
0019     @author Petri Damsten <damu@iki.fi>
0020 */
0021 
0022 class KGpgKey
0023 {
0024 public:
0025     QString id;
0026     QString name;
0027     QString email;
0028 };
0029 
0030 typedef QList<KGpgKey> KGpgKeyList;
0031 
0032 class KGpgMe
0033 {
0034 public:
0035     KGpgMe();
0036     ~KGpgMe();
0037 
0038     QString selectKey(QString previous = QString());
0039     KGpgKeyList keys(bool privateKeys = false) const;
0040     void setText(QString text, bool saving)
0041     {
0042         m_text = text;
0043         m_saving = saving;
0044     };
0045     void setUseGnuPGAgent(bool use)
0046     {
0047         m_useGnuPGAgent = use;
0048         setPassphraseCb();
0049     };
0050     QString text() const
0051     {
0052         return m_text;
0053     };
0054     bool saving() const
0055     {
0056         return m_saving;
0057     };
0058     void clearCache();
0059 
0060     bool encrypt(const QByteArray &inBuffer, unsigned long length, QByteArray *outBuffer, QString keyid = QString());
0061     bool decrypt(const QByteArray &inBuffer, QByteArray *outBuffer);
0062 
0063     static QString checkForUtf8(QString txt);
0064     static bool isGnuPGAgentAvailable();
0065 
0066 private:
0067     gpgme_ctx_t m_ctx;
0068     QString m_text;
0069     bool m_saving;
0070     bool m_useGnuPGAgent;
0071     QString m_cache;
0072 
0073     void init(gpgme_protocol_t proto);
0074     gpgme_error_t readToBuffer(gpgme_data_t in, QByteArray *outBuffer) const;
0075     void setPassphraseCb();
0076     static gpgme_error_t passphraseCb(void *hook, const char *uid_hint, const char *passphrase_info, int last_was_bad, int fd);
0077     gpgme_error_t passphrase(const char *uid_hint, const char *passphrase_info, int last_was_bad, int fd);
0078 };
0079 #endif // HAVE_LIBGPGME
0080 #endif // KGPGME_H