File indexing completed on 2024-05-12 05:46:41
0001 /* This file is part of the KDE libraries 0002 Copyright (C) 2000 David Faure <faure@kde.org> 0003 Copyright (C) 2000 Dawit Alemayehu <adawit@kde.org> 0004 Copyright (C) 2007 Olivier Goffart <ogoffart at kde.org> 0005 0006 This library is free software; you can redistribute it and/or 0007 modify it under the terms of the GNU Library General Public 0008 License version 2 as published by the Free Software Foundation. 0009 0010 This library is distributed in the hope that it will be useful, 0011 but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0013 Library General Public License for more details. 0014 0015 You should have received a copy of the GNU Library General Public 0016 License along with this library; see the file COPYING.LIB. If 0017 not, write to the Free Software Foundation, Inc., 51 Franklin Street, 0018 Fifth Floor, Boston, MA 02110-1301, USA. 0019 */ 0020 0021 #ifndef KPASSWORDDIALOG_H 0022 #define KPASSWORDDIALOG_H 0023 0024 #include <QDialog> 0025 #include <QDialogButtonBox> 0026 0027 #include <kwidgetsaddons_export.h> 0028 0029 /** 0030 * @class KPasswordDialog kpassworddialog.h KPasswordDialog 0031 * 0032 * A dialog for requesting a password and optionaly a login from the end user. 0033 * 0034 * \section usage Usage Example 0035 * 0036 * Requesting a simple password, asynchronous 0037 * 0038 * \code 0039 * KPasswordDialog *dlg = new KPasswordDialog( parent ); 0040 * dlg->setPrompt(i18n("Enter a password")); 0041 * connect( dlg, SIGNAL( gotPassword( const QString& , bool ) ) , this, SLOT( setPassword( const QString &) ) ); 0042 * connect( dlg, SIGNAL( rejected() ) , this, SLOT( slotCancel() ) ); 0043 * dlg->show(); 0044 * \endcode 0045 * 0046 * Requesting a login and a password, synchronous 0047 * 0048 * \code 0049 * KPasswordDialog dlg(parent, KPasswordDialog::ShowUsernameLine); 0050 * dlg.setPrompt(i18n("Enter a login and a password")); 0051 * if( !dlg.exec() ) 0052 * return; //the user canceled 0053 * use( dlg.username() , dlg.password() ); 0054 * \endcode 0055 * 0056 * \image html kpassworddialog.png "KPasswordDialog" 0057 * 0058 * @short dialog for requesting login and password from the end user 0059 */ 0060 class KWIDGETSADDONS_EXPORT KPasswordDialog : public QDialog 0061 { 0062 Q_OBJECT 0063 0064 public: 0065 0066 enum KPasswordDialogFlag { 0067 NoFlags = 0x00, 0068 /** 0069 * If this flag is set, the "keep this password" checkbox will been shown, 0070 * otherwise, it will not be shown and keepPassword will have no effect 0071 */ 0072 ShowKeepPassword = 0x01, 0073 /** 0074 * If this flag is set, there will be an additional line to let the user enter his login. 0075 * otherwise, only the password line will be shown. 0076 */ 0077 ShowUsernameLine = 0x02, 0078 /** 0079 * If this flag is set, the login lineedit will be in read only mode. 0080 */ 0081 UsernameReadOnly = 0x04, 0082 /** 0083 * If this flag is set, the Anonymous Login checkbox will be displayed 0084 * @since 4.1 0085 */ 0086 ShowAnonymousLoginCheckBox = 0x08, 0087 /** 0088 * If this flag is set, there will be an additional line to let the user enter the domain. 0089 * @since 4.1 0090 */ 0091 ShowDomainLine = 0x10, 0092 /** 0093 * If this flag is set, the domain lineedit will be in read only mode. 0094 * @since 4.1 0095 */ 0096 DomainReadOnly = 0x20 0097 }; 0098 Q_DECLARE_FLAGS(KPasswordDialogFlags, KPasswordDialogFlag) 0099 0100 enum ErrorType { 0101 UnknownError = 0, 0102 0103 /** 0104 * A problem with the user name as entered 0105 **/ 0106 UsernameError, 0107 0108 /** 0109 * Incorrect password 0110 */ 0111 PasswordError, 0112 0113 /** 0114 * Error preventing further attempts, will result in disabling most of the interface 0115 */ 0116 FatalError, 0117 0118 /** 0119 * A problem with the domain as entered 0120 * @since 4.1 0121 **/ 0122 DomainError 0123 }; 0124 0125 /** 0126 * create a password dialog 0127 * 0128 * @param parent the parent widget 0129 * @param flags a set of KPasswordDialogFlag flags 0130 */ 0131 explicit KPasswordDialog(QWidget *parent = nullptr, 0132 const KPasswordDialogFlags &flags = KPasswordDialog::NoFlags); 0133 0134 /** 0135 * Destructor 0136 */ 0137 ~KPasswordDialog() override; 0138 0139 /** 0140 * Sets the prompt to show to the user. 0141 * @param prompt instructional text to be shown. 0142 */ 0143 void setPrompt(const QString &prompt); 0144 0145 /** 0146 * Returns the prompt 0147 */ 0148 QString prompt() const; 0149 0150 /** 0151 * Set the icon that appears next to the prompt. 0152 * @since 5.63 0153 */ 0154 void setIcon(const QIcon &icon); 0155 0156 /** 0157 * Returns the icon that appears next to the prompt. 0158 */ 0159 QIcon icon() const; 0160 0161 #if KWIDGETSADDONS_ENABLE_DEPRECATED_SINCE(5, 63) 0162 /** 0163 * set an image that appears next to the prompt. 0164 * @deprecated since 5.63 use setIcon() 0165 */ 0166 KWIDGETSADDONS_DEPRECATED_VERSION(5, 63, "Use KPasswordDialog::setIcon(const QIcon &)") 0167 void setPixmap(const QPixmap &); 0168 /** 0169 * @deprecated since 5.63 use icon() 0170 */ 0171 KWIDGETSADDONS_DEPRECATED_VERSION(5, 63, "Use KPasswordDialog::icon()") 0172 QPixmap pixmap() const; 0173 #endif 0174 0175 /** 0176 * Adds a comment line to the dialog. 0177 * 0178 * This function allows you to add one additional comment 0179 * line to this widget. Calling this function after a 0180 * comment has already been added will not have any effect. 0181 * 0182 * @param label label for comment (ex:"Command:") 0183 * @param comment the actual comment text. 0184 */ 0185 void addCommentLine(const QString &label, const QString &comment); 0186 0187 /** 0188 * Shows an error message in the dialog box. Prevents having to show a dialog-on-a-dialog. 0189 * 0190 * @param message the error message to show 0191 */ 0192 void showErrorMessage(const QString &message, const ErrorType type = PasswordError); 0193 0194 /** 0195 * Returns the password entered by the user. 0196 * @return the password 0197 */ 0198 QString password() const; 0199 0200 /** 0201 * set the default username. 0202 */ 0203 void setUsername(const QString &); 0204 0205 /** 0206 * Returns the username entered by the user. 0207 * @return the user name 0208 */ 0209 QString username() const; 0210 0211 /** 0212 * set the default domain. 0213 * @since 4.1 0214 */ 0215 void setDomain(const QString &); 0216 0217 /** 0218 * Returns the domain entered by the user. 0219 * @return the domain name 0220 * @since 4.1 0221 */ 0222 QString domain() const; 0223 0224 /** 0225 * set anonymous mode (all other fields will be grayed out) 0226 * @since 4.1 0227 */ 0228 void setAnonymousMode(bool anonymous); 0229 0230 /** 0231 * @return anonymous mode has been selected. 0232 * @since 4.1 0233 */ 0234 bool anonymousMode() const; 0235 0236 /** 0237 * Determines whether supplied authorization should 0238 * persist even after the application has been closed. 0239 * 0240 * this is set with the check password checkbox is the ShowKeepCheckBox flag 0241 * is set in the constructor, if it is not set, this function return false 0242 * 0243 * @return true to keep the password 0244 */ 0245 bool keepPassword() const; 0246 0247 /** 0248 * Check or uncheck the "keep password" checkbox. 0249 * This can be used to check it before showing the dialog, to tell 0250 * the user that the password is stored already (e.g. in the wallet). 0251 * enableKeep must have been set to true in the constructor. 0252 * 0253 * has only effect if ShowKeepCheckBox is set in the constructor 0254 */ 0255 void setKeepPassword(bool b); 0256 0257 /** 0258 * Sets the username field read-only and sets the 0259 * focus to the password field. 0260 * 0261 * this can also be set by passing UsernameReadOnly as flag in the constructor 0262 * 0263 * @param readOnly true to set the user field to read-only 0264 */ 0265 void setUsernameReadOnly(bool readOnly); 0266 0267 /** 0268 * Presets the password. 0269 * If the password is not empty, the ability to show the password will not be available. 0270 * @param password the password to set 0271 */ 0272 void setPassword(const QString &password); 0273 0274 /** 0275 * Presets a number of login+password pairs that the user can choose from. 0276 * The passwords can be empty if you simply want to offer usernames to choose from. 0277 * 0278 * This require the flag ShowUnernameLine to be set in the constructoe, and not the flag UsernameReadOnly 0279 * @param knownLogins map of known logins: the keys are usernames, the values are passwords. 0280 */ 0281 void setKnownLogins(const QMap<QString, QString> &knownLogins); 0282 0283 /** 0284 * @internal 0285 */ 0286 void accept() override; 0287 0288 /** 0289 * Returns the button box used in the dialog. 0290 * This can be used to add new buttons. 0291 * 0292 * @return the button box 0293 * 0294 * @since 5.0 0295 */ 0296 QDialogButtonBox *buttonBox() const; 0297 0298 Q_SIGNALS: 0299 /** 0300 * emitted when the dialog has been accepted 0301 * @param password the entered password 0302 * @param keep true if the "remember password" checkbox was checked, false otherwise. false if ShowKeepPassword was not set in the constructor 0303 */ 0304 void gotPassword(const QString &password, bool keep); 0305 0306 /** 0307 * emitted when the dialog has been accepted, and ShowUsernameLine was set on the constructor 0308 * @param username the entered username 0309 * @param password the entered password 0310 * @param keep true if the "remember password" checkbox was checked, false otherwise. false if ShowKeepPassword was not set in the constructor 0311 */ 0312 void gotUsernameAndPassword(const QString &username, const QString &password, bool keep); 0313 0314 protected: 0315 /** 0316 * Virtual function that can be overridden to provide password 0317 * checking in derived classes. It should return @p true if the 0318 * password is valid, @p false otherwise. 0319 */ 0320 virtual bool checkPassword(); 0321 0322 private: 0323 Q_PRIVATE_SLOT(d, void actuallyAccept()) 0324 Q_PRIVATE_SLOT(d, void activated(const QString &userName)) 0325 Q_PRIVATE_SLOT(d, void updateFields()) 0326 0327 private: 0328 class KPasswordDialogPrivate; 0329 friend class KPasswordDialogPrivate; 0330 KPasswordDialogPrivate *const d; 0331 0332 Q_DISABLE_COPY(KPasswordDialog) 0333 }; 0334 0335 Q_DECLARE_OPERATORS_FOR_FLAGS(KPasswordDialog::KPasswordDialogFlags) 0336 0337 #endif