File indexing completed on 2024-04-28 15:32:10

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