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