File indexing completed on 2024-05-12 05:08:00

0001 /*
0002     SPDX-FileCopyrightText: 2022 Thomas Baumgart <tbaumgart@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #ifndef PASSSTORE_H
0007 #define PASSSTORE_H
0008 
0009 #include "kmm_base_widgets_export.h"
0010 
0011 // ----------------------------------------------------------------------------
0012 // QT Includes
0013 
0014 #include <QObject>
0015 #include <QString>
0016 
0017 // ----------------------------------------------------------------------------
0018 // KDE Includes
0019 
0020 // ----------------------------------------------------------------------------
0021 // Project Includes
0022 
0023 class QLineEdit;
0024 class QAction;
0025 
0026 class PassStorePrivate;
0027 /**
0028  * This class implements a mechanism to enrich a QLineEdit with an action
0029  * icon to load its content by extracting from a password store maintained
0030  * by pass (see https://www.passwordstore.org). The path to the password
0031  * file is identified by the @c applicationPrefix and @c id provided
0032  * at construction time or the @c id passed with setPasswordId().
0033  *
0034  * If GPG is not available or a password file identified by
0035  * @c applicationPrefix and @c id is not available or not
0036  * readable, the icon to load the password is not visible
0037  * inside the QLineEdit.
0038  *
0039  * The file for the password is expected to be in
0040  * ~/.password-store/<applicationPrefix>/<id>.gpg
0041  *
0042  * To use this class, the following is needed (example provided for
0043  * KMyMoney and KBanking plugin):
0044  *
0045  * @code
0046  *
0047  *    extern QString accountRef;
0048  *    auto lineedit = new QLineEdit;
0049  *    new PassStore(lineedit, QLatin1String("KMyMoney/KBanking"), accountRef);
0050  *
0051  * @endcode
0052  *
0053  * @note relies on kmm_gpgfile
0054  *
0055  * @author Thomas Baumgart
0056  */
0057 class KMM_BASE_WIDGETS_EXPORT PassStore : public QObject
0058 {
0059     Q_DECLARE_PRIVATE(PassStore);
0060     Q_OBJECT
0061 public:
0062     explicit PassStore(QLineEdit* parent, const QString& applicationPrefix, const QString& id = QString());
0063     ~PassStore();
0064 
0065     /**
0066      * Update the password id to @a id. This will check for
0067      * the password in the store and update the icon accordingly.
0068      *
0069      * @note The characters '/' and '\\' will be converted to '_'
0070      */
0071     void setPasswordId(const QString& id);
0072 
0073     /**
0074      * Return the 'converted' passwordId.
0075      *
0076      * @sa setPasswordId() for possible conversions.
0077      */
0078     QString passwordId() const;
0079 
0080     /**
0081      * Return if the action is visible or not. This can be used
0082      * by code to check if a stored password is available or not.
0083      */
0084     bool isActionVisible() const;
0085 
0086 private:
0087     PassStorePrivate* d_ptr;
0088 };
0089 
0090 #endif // PASSSTORE_H