File indexing completed on 2025-03-16 03:46:43

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org>
0004     SPDX-FileCopyrightText: 2000 Dawit Alemayehu <adawit@kde.org>
0005     SPDX-FileCopyrightText: 2007 Olivier Goffart <ogoffart at kde.org>
0006     SPDX-FileCopyrightText: 2020 Harald Sitter <sitter@kde.org>
0007 
0008     SPDX-License-Identifier: LGPL-2.0-or-later
0009 */
0010 
0011 #ifndef KPASSWORDDIALOG_H
0012 #define KPASSWORDDIALOG_H
0013 
0014 #include <KPassword>
0015 #include <QDialog>
0016 #include <QDialogButtonBox>
0017 #include <memory>
0018 
0019 #include <kwidgetsaddons_export.h>
0020 
0021 /**
0022  * @class KPasswordDialog kpassworddialog.h KPasswordDialog
0023  *
0024  * A dialog for requesting a password and optionally a login from the end user.
0025  *
0026  * \section usage Usage Example
0027  *
0028  * Requesting a simple password, asynchronous
0029  *
0030  * \code
0031  *  KPasswordDialog *dlg = new KPasswordDialog(parent);
0032  *  dlg->setPrompt(i18n("Enter a password"));
0033  *  connect(dlg, &KPasswordDialog::gotPassword,
0034  *          this, [this](const QString &password) { setPassword(password); });
0035  *  connect(dlg, &QDialog::rejected, this, [this]() { slotCancel(); });
0036  *  dlg->show();
0037  * \endcode
0038  *
0039  * Requesting a login and a password, synchronous
0040  *
0041  * \code
0042  *  KPasswordDialog dlg(parent, KPasswordDialog::ShowUsernameLine);
0043  *  dlg.setPrompt(i18n("Enter a login and a password"));
0044  *  if(!dlg.exec()) {
0045  *      return; //the user canceled
0046  *  }
0047  *  use(dlg.username(), dlg.password());
0048  * \endcode
0049  *
0050  * \image html kpassworddialog.png "KPasswordDialog"
0051  *
0052  * @short dialog for requesting login and password from the end user
0053  */
0054 class KWIDGETSADDONS_EXPORT KPasswordDialog : public QDialog
0055 {
0056     Q_OBJECT
0057 
0058 public:
0059     /**
0060      * @see KPasswordDialogFlags
0061      */
0062     enum KPasswordDialogFlag {
0063         NoFlags = 0x00,
0064         /**
0065          * If this flag is set, the "keep this password" checkbox will been shown,
0066          * otherwise, it will not be shown and keepPassword will have no effect
0067          */
0068         ShowKeepPassword = 0x01,
0069         /**
0070          * If this flag is set, there will be an additional line to let the user enter his login.
0071          * otherwise, only the password line will be shown.
0072          */
0073         ShowUsernameLine = 0x02,
0074         /**
0075          * If this flag is set, the login lineedit will be in read only mode.
0076          */
0077         UsernameReadOnly = 0x04,
0078         /**
0079          * If this flag is set, the Anonymous Login checkbox will be displayed
0080          * @since 4.1
0081          */
0082         ShowAnonymousLoginCheckBox = 0x08,
0083         /**
0084          * If this flag is set, there will be an additional line to let the user enter the domain.
0085          * @since 4.1
0086          */
0087         ShowDomainLine = 0x10,
0088         /**
0089          * If this flag is set, the domain lineedit will be in read only mode.
0090          * @since 4.1
0091          */
0092         DomainReadOnly = 0x20,
0093     };
0094     /**
0095      * Stores a combination of #KPasswordDialogFlag values.
0096      */
0097     Q_DECLARE_FLAGS(KPasswordDialogFlags, KPasswordDialogFlag)
0098 
0099     enum ErrorType {
0100         UnknownError = 0,
0101 
0102         /**
0103          * A problem with the user name as entered
0104          */
0105         UsernameError,
0106 
0107         /**
0108          * Incorrect password
0109          */
0110         PasswordError,
0111 
0112         /**
0113          * Error preventing further attempts, will result in disabling most of the interface
0114          */
0115         FatalError,
0116 
0117         /**
0118          * A problem with the domain as entered
0119          * @since 4.1
0120          */
0121         DomainError,
0122     };
0123 
0124     /**
0125      * create a password dialog
0126      *
0127      * @param parent the parent widget
0128      * @param flags a set of KPasswordDialogFlag flags
0129      */
0130     explicit KPasswordDialog(QWidget *parent = nullptr, const KPasswordDialogFlags &flags = KPasswordDialog::NoFlags);
0131 
0132     /**
0133      * Destructor
0134      */
0135     ~KPasswordDialog() override;
0136 
0137     /**
0138      * Sets the prompt to show to the user.
0139      * @param prompt        instructional text to be shown.
0140      */
0141     void setPrompt(const QString &prompt);
0142 
0143     /**
0144      * Returns the prompt
0145      */
0146     QString prompt() const;
0147 
0148     /**
0149      * Set the icon that appears next to the prompt.
0150      * @since 5.63
0151      */
0152     void setIcon(const QIcon &icon);
0153 
0154     /**
0155      * Returns the icon that appears next to the prompt.
0156      */
0157     QIcon icon() const;
0158 
0159     /**
0160      * Adds a comment line to the dialog.
0161      *
0162      * This function allows you to add one additional comment
0163      * line to this widget.  Calling this function after a
0164      * comment has already been added will not have any effect.
0165      *
0166      * @param label       label for comment (ex:"Command:")
0167      * @param comment     the actual comment text.
0168      */
0169     void addCommentLine(const QString &label, const QString &comment);
0170 
0171     /**
0172      * Shows an error message in the dialog box. Prevents having to show a dialog-on-a-dialog.
0173      *
0174      * @param message the error message to show
0175      */
0176     void showErrorMessage(const QString &message, const ErrorType type = PasswordError);
0177 
0178     /**
0179      * Returns the password entered by the user.
0180      * @return the password
0181      */
0182     QString password() const;
0183 
0184     /**
0185      * set the default username.
0186      */
0187     void setUsername(const QString &);
0188 
0189     /**
0190      * Returns the username entered by the user.
0191      * @return the user name
0192      */
0193     QString username() const;
0194 
0195     /**
0196      * set the default domain.
0197      * @since 4.1
0198      */
0199     void setDomain(const QString &);
0200 
0201     /**
0202      * Returns the domain entered by the user.
0203      * @return the domain name
0204      * @since 4.1
0205      */
0206     QString domain() const;
0207 
0208     /**
0209      * set anonymous mode (all other fields will be grayed out)
0210      * @since 4.1
0211      */
0212     void setAnonymousMode(bool anonymous);
0213 
0214     /**
0215      * @return anonymous mode has been selected.
0216      * @since 4.1
0217      */
0218     bool anonymousMode() const;
0219 
0220     /**
0221      * Determines whether supplied authorization should
0222      * persist even after the application has been closed.
0223      *
0224      * this is set with the check password checkbox is the ShowKeepCheckBox flag
0225      * is set in the constructor, if it is not set, this function return false
0226      *
0227      * @return true to keep the password
0228      */
0229     bool keepPassword() const;
0230 
0231     /**
0232      * Check or uncheck the "keep password" checkbox.
0233      * This can be used to check it before showing the dialog, to tell
0234      * the user that the password is stored already (e.g. in the wallet).
0235      * enableKeep must have been set to true in the constructor.
0236      *
0237      * has only effect if ShowKeepCheckBox is set in the constructor
0238      */
0239     void setKeepPassword(bool b);
0240 
0241     /**
0242      * Sets the username field read-only and sets the
0243      * focus to the password field.
0244      *
0245      * this can also be set by passing UsernameReadOnly as flag in the constructor
0246      *
0247      * @param readOnly true to set the user field to read-only
0248      */
0249     void setUsernameReadOnly(bool readOnly);
0250 
0251     /**
0252      * Presets the password.
0253      * If the password is not empty, the ability to show the password will not be available.
0254      * @param password the password to set
0255      */
0256     void setPassword(const QString &password);
0257 
0258     /**
0259      * Presets a number of login+password pairs that the user can choose from.
0260      * The passwords can be empty if you simply want to offer usernames to choose from.
0261      *
0262      * This require the flag ShowUsernameLine to be set in the constructoe, and not the flag UsernameReadOnly
0263      * @param knownLogins map of known logins: the keys are usernames, the values are passwords.
0264      */
0265     void setKnownLogins(const QMap<QString, QString> &knownLogins);
0266 
0267     /**
0268      * @internal
0269      */
0270     void accept() override;
0271 
0272     /**
0273      * Returns the button box used in the dialog.
0274      * This can be used to add new buttons.
0275      *
0276      * @return the button box
0277      *
0278      * @since 5.0
0279      */
0280     QDialogButtonBox *buttonBox() const;
0281 
0282     /**
0283      * Sets contextual help for the username input field. This displays a
0284      * somewhat visual hint in the UI giving very visible access to a whats-this
0285      * style input description for the user name line. This is particularly useful
0286      * when the user name may require or support special input syntax.
0287      * For example windows-like auth dialogs supports multiple different logon
0288      * name syntax.
0289      * @since 5.76
0290      */
0291     void setUsernameContextHelp(const QString &help);
0292 
0293 #if KWIDGETSADDONS_ENABLE_DEPRECATED_SINCE(6, 0)
0294     /**
0295      * Whether to show the visibility trailing action in the line edit.
0296      * Default is @c true. This can be used to honor the lineedit_reveal_password
0297      * kiosk key, for example:
0298      * \code
0299      * dlg->setRevealPasswordAvailable(KAuthorized::authorize(QStringLiteral("lineedit_reveal_password")));
0300      * \endcode
0301      * @since 5.84
0302      */
0303     [[deprecated("Use setRevealPasswordMode instead.")]] void setRevealPasswordAvailable(bool reveal);
0304 
0305     /**
0306      * Whether the visibility trailing action in the line edit is visible.
0307      * @since 5.84
0308      */
0309     [[deprecated("Use revealPasswordMode instead.")]] bool isRevealPasswordAvailable() const;
0310 #endif
0311 
0312     /**
0313      * Return when the reveal password button is visible.
0314      * @since 6.0
0315      */
0316     KPassword::RevealMode revealPasswordMode() const;
0317 
0318     /**
0319      * Set when the reveal password button will be visible.
0320      *
0321      * The default is RevealMode::OnlyNew and the reveal password button will
0322      * only be visible when entering a new password.
0323      *
0324      * This can be used to honor the lineedit_reveal_password kiosk key, for example:
0325      *
0326      * @code{.cpp}
0327      * if (KAuthorized::authorize(QStringLiteral("lineedit_reveal_password"))) {
0328      *     passwordDialog.setRevealPasswordMode(KPassword::RevealMode::OnlyNew);
0329      * } else {
0330      *     passwordDialog.setRevealPasswordMode(KPassword::RevealMode::Never);
0331      * }
0332      * @endcode
0333      * @since 6.0
0334      */
0335     void setRevealPasswordMode(KPassword::RevealMode revealPasswordMode);
0336 
0337 Q_SIGNALS:
0338     /**
0339      * emitted when the dialog has been accepted
0340      * @param password  the entered password
0341      * @param keep true if the "remember password" checkbox was checked, false otherwise.  false if ShowKeepPassword was not set in the constructor
0342      */
0343     void gotPassword(const QString &password, bool keep);
0344 
0345     /**
0346      * emitted when the dialog has been accepted, and ShowUsernameLine was set on the constructor
0347      * @param username the entered username
0348      * @param password  the entered password
0349      * @param keep true if the "remember password" checkbox was checked, false otherwise.  false if ShowKeepPassword was not set in the constructor
0350      */
0351     void gotUsernameAndPassword(const QString &username, const QString &password, bool keep);
0352 
0353 protected:
0354     /**
0355      * Virtual function that can be overridden to provide password
0356      * checking in derived classes. It should return @p true if the
0357      * password is valid, @p false otherwise.
0358      */
0359     virtual bool checkPassword();
0360 
0361 private:
0362     friend class KPasswordDialogPrivate;
0363     std::unique_ptr<class KPasswordDialogPrivate> const d;
0364 
0365     Q_DISABLE_COPY(KPasswordDialog)
0366 };
0367 
0368 Q_DECLARE_OPERATORS_FOR_FLAGS(KPasswordDialog::KPasswordDialogFlags)
0369 
0370 #endif