File indexing completed on 2024-05-19 05:06:55

0001 /*
0002     SPDX-FileCopyrightText: 2004, 2005, 2009 Thomas Baumgart <ipwizard@users.sourceforge.net>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #ifndef KGPGFILE_H
0007 #define KGPGFILE_H
0008 
0009 #include <kmm_gpgfile_export.h>
0010 
0011 // ----------------------------------------------------------------------------
0012 // QT Includes
0013 
0014 #include <QFile>
0015 #include <QString>
0016 
0017 class QDateTime;
0018 // ----------------------------------------------------------------------------
0019 // KDE Includes
0020 
0021 // ----------------------------------------------------------------------------
0022 // Project Includes
0023 
0024 /**
0025  * @author Thomas Baumgart
0026  */
0027 
0028 /**
0029  * A class for reading and writing data to/from an
0030  * encrypted e.g. file.
0031  *
0032  * This class presents a QFile based object to the application
0033  * but reads/writes data from/to the file through an instance of GPG.
0034  *
0035  * @code
0036  *
0037  *  +------------------+   write  +-----------+      +---------+
0038  *  |                  |--------->|\          |----->|         |
0039  *  | Application code |   read   | QFile     |      | gpgme++ |
0040  *  |                  |<---------|/          |<-----|         |
0041  *  +------------------+          |  KGPGFile |      +---------+
0042  *                |               |           |
0043  *                |        control|           |      +-------+
0044  *                +-------------->|           |----->|       |
0045  *                                |           |      | File  |
0046  *                                |           |----->|       |
0047  *                                |           |      +-------+
0048  *                                +-----------+
0049  * @endcode
0050  *
0051  * The @p write interface contains methods as write() and putch(), the @p read
0052  * interface the methods read(), getch() and ungetch(). The @p control interface
0053  * special methods only available with KGPGFile e.g. addRecipient(), keyAvailable() and
0054  * GPGAvailable(). Other, more general methods such as open(), close() and flush() are
0055  * not shown in the above picture.
0056  */
0057 class KMM_GPGFILE_EXPORT KGPGFile : public QFile
0058 {
0059     Q_OBJECT
0060 
0061 public:
0062     explicit KGPGFile(const QString& fname = "", const QString& homedir = "~/.gnupg", const QString& options = "");
0063 
0064     ~KGPGFile();
0065 
0066     bool open(OpenMode mode) final override;
0067     void close() final override;
0068     virtual void flush();
0069 
0070     qint64 readData(char* data, qint64 maxlen) final override;
0071     qint64 writeData(const char* data, qint64 maxlen) final override;
0072 
0073     /**
0074      * Adds a recipient for whom the file should be encrypted.
0075      * At least one recipient must be specified using this
0076      * method before the file can be written to. @p recipient
0077      * must contain a valid name as defined by GPG. See the
0078      * GPG documentation for more information.
0079      *
0080      * @param recipient recipients identification (e.g. e-mail address)
0081      */
0082     void addRecipient(const QString& recipient);
0083 
0084     /**
0085      * sets the name of the file to @p fn. This method must be
0086      * called prior to open().
0087      */
0088     void setFileName(const QString& fn);
0089 
0090     /** This function returns the error from the GPG system as a user
0091      * readable string. The strinf is empty if there were no errors.
0092      */
0093     QString errorToString() const;
0094 
0095     /**
0096      * This method returns the information about the expiration date of a key.
0097      * An invalid QDateTime object is returned if @a name matches more than one
0098      * key or the key does not have an expiration date.
0099      */
0100     QDateTime keyExpires(const QString& name);
0101 
0102     /**
0103      * Checks whether GPG is available or not
0104      *
0105      * @retval true GPG can be started and returns a version number
0106      * @retval false GPG is not available
0107      */
0108     static bool GPGAvailable();
0109 
0110     /**
0111      * Checks whether a key for a given user-id @p name exists.
0112      *
0113      * @param name the user-id to be checked. @p name can be
0114      *             any reference understood by GPG (e.g. an e-mail
0115      *             address or a key-id)
0116      * @retval true key for user-id @p name was found
0117      * @retval false key for user-id @p not available
0118      */
0119     static bool keyAvailable(const QString& name);
0120 
0121     /**
0122      * This function returns a list of the secret keys contained
0123      * in the keyring. Each list item is divided into two fields
0124      * separated by a colon (':'). The first field contains the
0125      * key id, the second field the name. The list may contain
0126      * multiple entries with the same key-id and different names.
0127      *
0128      * Example of an entry in the list:
0129      *
0130      *    "9C59DB40B75DD3BA:Thomas Baumgart <ipwizard@users.sourceforge.net>"
0131      */
0132     static void secretKeyList(QStringList& list);
0133 
0134     /**
0135      * This function returns a list of the public keys contained
0136      * in the keyring. Each list item is divided into two fields
0137      * separated by a colon (':'). The first field contains the
0138      * key id, the second field the name. The list may contain
0139      * multiple entries with the same key-id and different names.
0140      *
0141      * Example of an entry in the list:
0142      *
0143      *    "9C59DB40B75DD3BA:Thomas Baumgart <ipwizard@users.sourceforge.net>"
0144      */
0145     static void publicKeyList(QStringList& list);
0146 
0147 private:
0148     void keyList(QStringList& list, bool secretKeys = false, const QString& pattern = QString());
0149 
0150 private:
0151     /// \internal d-pointer class.
0152     class Private;
0153     /// \internal d-pointer instance.
0154     Private* const d;
0155 };
0156 
0157 #endif